Python Fundamentals: A Beginner's Guide
Chapter 1: Introduction to Python
- What is Python?
- Installing Python and setting up your environment
- Using an IDE or text editor
- Running your first Python program: 'Hello, World!'
Example
print("Hello, World!")
Chapter 2: Variables and Data Types
- Introduction to variables and how to declare them.
- Primitive Data Types: Integers, Floats, Strings, Booleans.
- Using type functions to check data types.
Example
age = 30
name = "John"
is_student = True
height = 5.9
# Check data types
print(type(age)) # Output: <class 'int'>
Chapter 3: Operators
- Arithmetic Operators: +, -, *, /, //, %, **.
- Comparison Operators: ==, !=, >, <, >=, <=.
- Logical Operators: and, or, not.
- Assignment Operators: =, +=, -=, *=, /=.
Example
a = 10
b=3
# Arithmetic operations
sum_result = a + b # 13
division_result = a / b # 3.33
# Comparison
is_equal = (a == b) # False
# Logical operations
if a > 5 and b < 5:
print("Both conditions are true")
Chapter 4: Control Structures
- Conditional Statements: if, elif, else.
- Loops: for loop and while loop.
Example
# If-Else Example
age = 18
if age >= 18:
print("Adult")
else:
print("Minor")
# For loop
for i in range(5):
print(i)
# While loop
count = 0
while count < 3:
print("Count:", count)
count += 1
Chapter 5: Functions
- Defining and calling functions.
- Understanding function parameters and return values.
- Default arguments and keyword arguments.
Example
def greet(name="User"):
return f"Hello, {name}"
# Call the function
print(greet("Alice")) # Output: Hello, Alice
print(greet()) # Output: Hello, User
Chapter 6: Lists and List Manipulation
- Creating lists.
- Accessing and modifying elements.
- Common list methods: append(), remove(), pop(), sort(), reverse().
- List slicing.
Example
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
fruits.append("orange") # Adding an item
fruits.remove("banana") # Removing an item
# List slicing
print(fruits[1:3]) # Output: ['cherry', 'orange']
Chapter 7: Dictionaries and Sets
- Understanding dictionaries (key-value pairs).
- Accessing, adding, and removing key-value pairs.
- Dictionary methods: get(), items(), keys().
- Introduction to sets and common operations.
Example
person = {"name": "Alice", "age": 25}
# Access value
print(person["name"]) # Output: Alice
# Add key-value pair
person["city"] = "New York"
# Using get() to avoid KeyError
print(person.get("email", "Not Found")) # Output: Not Found
Chapter 8: Tuples
- Introduction to tuples: immutable sequences.
- Tuple operations.
- Unpacking tuples.
Example
coordinates = (10, 20)
# Accessing tuple elements
x, y = coordinates
print(x, y) # Output: 10 20
Chapter 9: Input and Output
- Using input() to take user input.
- Formatting strings.
- Basic output with print().
Example
name = input("Enter your name: ")
print(f"Hello, {name}")
Chapter 10: File Handling
- Opening, reading, and writing files.
- Using with for file management.
Example
# Reading from a file
with open("example.txt", "r") as file:
content = file.read()
print(content)
# Writing to a file
with open("output.txt", "w") as file:
file.write("Hello, World!")
Chapter 11: Error Handling
- Understanding exceptions and how to handle them.
- Using try, except, finally blocks.
Example
try:
num = int(input("Enter a number: "))
result = 10 / num
except ZeroDivisionError:
print("You cannot divide by zero!")
except ValueError:
print("Invalid input! Please enter a number.")
finally:
print("Execution completed.")
Chapter 12: Basic Object-Oriented Programming (OOP)
- Introduction to classes and objects.
- Defining classes and creating instances.
- Using constructors (__init__).
- Basic inheritance.
Example
class Car:
def __init__(self, model, year):
self.model = model
self.year = year
def display_info(self):
print(f"Model: {self.model}, Year: {self.year}")
# Creating an object
my_car = Car("Toyota", 2020)
my_car.display_info() # Output: Model: Toyota, Year: 2020
Chapter 13: Conclusion: Next Steps in Python
- Recap of the basics.
- Resources for deeper learning: websites, books, and courses.
- Moving on to intermediate topics like modules, decorators, and working with libraries.