DEV Community

Cover image for A friendly beginners Python 3 Guide: Where to start
Leandro Nuñez for Digital Pollution

Posted on • Updated on

A friendly beginners Python 3 Guide: Where to start

TL;DR:

Welcome to A friendly beginners Python 3 Guide: Where to start. I'm thrilled to be your guide on this learning journey. From setting up your Python environment to diving deep into data types, control flow, functions, OOP, and more, this guide has got you covered.

Let's begin!

Introduction:

Hello there!

I'm Leandro, a passionate software engineer, and I'm thrilled to embark on this Python 3 adventure with you.

Python is an elegant and versatile language that has won the hearts of developers worldwide.

Whether you're a newcomer or an experienced programmer, this guide will equip you with all the essential knowledge you need to excel in Python 3.

So, let's dive in and start coding!

Table of Contents:

  1. Setting up the Environment for Python 3
  2. Python 3 Basics: Data Types, Variables, and Operators
  3. Control Flow: Conditional Statements and Loops
  4. Functions and Modules
  5. Error Handling
  6. Object-Oriented Programming (OOP)
  7. Exception Handling
  8. Modules and Packages
  9. File Handling
  10. Regular Expressions

Content:

1. Setting up the Environment for Python 3:

To start our Python journey, let's install Python 3 and verify the version:

python3 --version
Enter fullscreen mode Exit fullscreen mode

Output:

Python 3.x.x
Enter fullscreen mode Exit fullscreen mode

We'll also set up a virtual environment for managing project dependencies:

python3 -m venv myenv
source myenv/bin/activate  # On Windows, use: myenv\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

Output:
(Virtual environment activated)

2. Python 3 Basics: Data Types, Variables, and Operators:


i. Data Types:
Python supports various data types.
Let's explore some of them:

age = 25
name = "Leandro"
height = 1.75
is_student = True
favorite_fruits = ["apple", "banana", "orange"]
Enter fullscreen mode Exit fullscreen mode


ii. Variables:
Variables store values, and their data type can change dynamically:

x = 10
x = "hello"
Enter fullscreen mode Exit fullscreen mode


iii. Operators:
Python offers a variety of operators for different operations:

# Arithmetic Operators
x = 10 + 5
y = 8 - 3
z = 3 * 4
w = 15 / 3

# Comparison Operators
is_equal = x == y
is_greater = x > y
is_not_equal = x != y

# Logical Operators
is_both_true = is_student and x > y
is_either_true = is_student or x > y
is_not_true = not is_student
Enter fullscreen mode Exit fullscreen mode

Output:

x = 15
y = 5
z = 12
w = 5.0

is_equal = False
is_greater = True
is_not_equal = True

is_both_true = True
is_either_true = True
is_not_true = False
Enter fullscreen mode Exit fullscreen mode

3. Control Flow: Conditional Statements and Loops:


i. Conditional Statements:
Control the flow of your program using if, elif, and else:

if x > y:
   print("x is greater than y")
elif x == y:
   print("x and y are equal")
else:
   print("x is smaller than y")
Enter fullscreen mode Exit fullscreen mode

Output:

x is greater than y
Enter fullscreen mode Exit fullscreen mode


ii. Loops:
Use for and while loops for repetitive tasks:

for fruit in favorite_fruits:
   print(f"Enjoy eating {fruit}")

count = 0
while count < 5:
   print(f"Count is {count}")
   count += 1
Enter fullscreen mode Exit fullscreen mode

Output:

Enjoy eating apple
Enjoy eating banana
Enjoy eating orange

Count is 0
Count is 1
Count is 2
Count is 3
Count is 4
Enter fullscreen mode Exit fullscreen mode

4. Functions and Modules:

Create reusable functions with parameters and return values:

def add_numbers(a, b):
   return a + b

sum_result = add_numbers(10, 5)
print(f"The sum is: {sum_result}")
Enter fullscreen mode Exit fullscreen mode

Output:

The sum is: 15
Enter fullscreen mode Exit fullscreen mode

5. Error Handling:

Handle errors gracefully using try-except blocks:

try:
   result = 10 / 0
except ZeroDivisionError:
   print("Cannot divide by zero!")
Enter fullscreen mode Exit fullscreen mode

Output:

Cannot divide by zero!
Enter fullscreen mode Exit fullscreen mode

6. Object-Oriented Programming (OOP):

Implement classes and objects for object-oriented programming:

class Dog:
   def __init__(self, name):
       self.name = name

   def bark(self):
       return "Woof!"

my_dog = Dog("Buddy")
print(my_dog.name)
print(my_dog.bark())
Enter fullscreen mode Exit fullscreen mode

Output:

Buddy
Woof!
Enter fullscreen mode Exit fullscreen mode

7. Exception Handling:

Customize and raise exceptions for specific cases:

class MyCustomException(Exception):
   pass

try:
   raise MyCustomException("This is a custom exception")
except MyCustomException as e:
   print(f"Caught custom exception: {e}")
Enter fullscreen mode Exit fullscreen mode

Output:

Caught custom exception: This is a custom exception
Enter fullscreen mode Exit fullscreen mode

8. Modules and Packages:

Organize code into modules and packages for better project structure:

# Create a module named my_module.py
def say_hello():
   print("Hello from my_module!")

# In another file, import and use the module
import my_module

my_module.say_hello()
Enter fullscreen mode Exit fullscreen mode

Output:

Hello from my_module!
Enter fullscreen mode Exit fullscreen mode

9. File Handling:

Read and write files using Python:

# Write to a file
with open("output.txt", "w") as file:
   file.write("Hello, this is written to a file.")

# Read from a file
with open("output.txt", "r") as file:
   content = file.read()
   print(content)


Enter fullscreen mode Exit fullscreen mode

Output:

Hello, this is written to a file.
Enter fullscreen mode Exit fullscreen mode

10. Regular Expressions:

Use regular expressions for powerful pattern matching:

import re

pattern = r"\b\w{3}\b"
text = "The cat and dog are running."

matches = re.findall(pattern, text)
print(matches)
Enter fullscreen mode Exit fullscreen mode

Output:

['The', 'cat', 'and', 'dog']
Enter fullscreen mode Exit fullscreen mode

Hands-on Experience:

Throughout this guide, you've gained hands-on experience with Python 3, mastering its core concepts and features.

Now, you're equipped to build exciting applications and solve real-world problems with confidence.

Conclusion:

Congratulations on completing The Ultimate Python 3 Guide!

You've acquired a solid foundation in Python 3, empowering you to build exciting applications and solve real-world problems.

Keep honing your skills and exploring Python's vast ecosystem.

Happy coding, and may the force -of Python 3- be with you!

Top comments (0)