✅Python Tutorial for Beginners

 # 


Here's a comprehensive Python tutorial covering the basics with code examples:


## 1. Python Setup

First, install Python from [python.org](https://www.python.org/) or use an online interpreter like [replit.com](https://replit.com/).


## 2. Basic Syntax


```python

# This is a comment

print("Hello, World!")  # Simple output


# Variables

x = 5

y = "Python"

print(type(x))  # <class 'int'>

print(type(y))  # <class 'str'>

```


## 3. Data Types


```python

# Numeric types

integer = 10

float_num = 10.5

complex_num = 3 + 4j


# Sequence types

string = "Hello"

list_example = [1, 2, 3]

tuple_example = (1, 2, 3)


# Boolean

is_true = True

is_false = False


# Dictionary

person = {"name": "Alice", "age": 25}

```


## 4. Operators


```python

# Arithmetic

print(5 + 3)   # 8

print(5 - 3)   # 2

print(5 * 3)   # 15

print(5 / 3)   # 1.666...

print(5 // 3)  # 1 (floor division)

print(5 % 3)   # 2 (modulus)

print(5 ** 3)  # 125 (exponent)


# Comparison

print(5 == 5)  # True

print(5 != 3)  # True

print(5 > 3)   # True

print(5 < 3)   # False


# Logical

print(True and False)  # False

print(True or False)   # True

print(not True)        # False

```


## 5. Control Flow


```python

# If-elif-else

age = 18

if age < 13:

    print("Child")

elif age < 18:

    print("Teenager")

else:

    print("Adult")


# For loop

for i in range(5):  # 0 to 4

    print(i)


# While loop

count = 0

while count < 5:

    print(count)

    count += 1

```


## 6. Functions


```python

# Basic function

def greet(name):

    return f"Hello, {name}!"


print(greet("Alice"))  # Hello, Alice!


# Function with default parameter

def power(base, exponent=2):

    return base ** exponent


print(power(3))    # 9

print(power(3, 3)) # 27


# Lambda function

square = lambda x: x * x

print(square(5))  # 25

```


## 7. Working with Lists


```python

fruits = ["apple", "banana", "cherry"]


# Accessing elements

print(fruits[0])   # apple

print(fruits[-1])  # cherry (last element)


# Slicing

print(fruits[1:3])  # ['banana', 'cherry']


# List methods

fruits.append("orange")

fruits.remove("banana")

print(len(fruits))  # 3


# List comprehension

squares = [x**2 for x in range(5)]

print(squares)  # [0, 1, 4, 9, 16]

```


## 8. File Handling


```python

# Writing to a file

with open("example.txt", "w") as file:

    file.write("Hello, Python!")


# Reading from a file

with open("example.txt", "r") as file:

    content = file.read()

    print(content)  # Hello, Python!

```


## 9. Object-Oriented Programming


```python

class Dog:

    # Class attribute

    species = "Canis familiaris"

    

    # Initializer

    def __init__(self, name, age):

        self.name = name

        self.age = age

    

    # Instance method

    def description(self):

        return f"{self.name} is {self.age} years old"

    

    # Another instance method

    def speak(self, sound):

        return f"{self.name} says {sound}"


# Create an instance

my_dog = Dog("Buddy", 5)

print(my_dog.description())  # Buddy is 5 years old

print(my_dog.speak("Woof"))  # Buddy says Woof

```


## 10. Error Handling


```python

try:

    result = 10 / 0

except ZeroDivisionError:

    print("Cannot divide by zero!")

else:

    print("Division successful")

finally:

    print("This always executes")


# Raising exceptions

def check_age(age):

    if age < 0:

        raise ValueError("Age cannot be negative")

    return f"Age is {age}"


try:

    print(check_age(-5))

except ValueError as e:

    print(e)  # Age cannot be negative

```


## Next Steps


1. Practice with small projects

2. Explore Python modules (math, random, datetime, etc.)

3. Learn about virtual environments

4. Try web frameworks like Flask or Django

5. Explore data science libraries (pandas, numpy, matplotlib)


Would you like me to focus on any specific area in more detail?

Comentários