Python Learning Guide: Week 1 & 2
Week 1 – Python Core (Basics & Control Flow)
Day 1-2: Syntax, Variables, Data Types
1. Concept Introduction
• Syntax: The grammar rules for writing Python code.
• Variables: Names that store values (like boxes holding items).
• Data Types: Types of data stored (text, numbers, boolean, etc).
2. Purpose & Importance
• Ensures Python interpreter understands and runs your code.
• Helps organize and manage data efficiently.
3. Implementation Guide
# Variable assignment
name = "Sajjad" # string
grade = 95 # integer
percentage = 95.0 # float
is_passed = True # boolean
# Checking type of data
type(name) # Output: <class 'str'>
type(grade) # Output: <class 'int'>
4. Common Mistakes & Pitfalls
• Forgetting quotes around strings
• Misspelling variable names
• Using reserved words as variable names (e.g., class = 5 )
• No spaces around assignment ( x=5 is OK but x = 5 is cleaner)
5. Best Practices Checklist
• Use descriptive variable names
• Maintain proper indentation (4 spaces)
• Use lowercase with underscores for variable names (e.g., user_name )
1
6. Practical Examples
# Example 1: Simple variable assignment
course = "Python"
print(course)
# Output: Python
# Example 2: Combining variables
first = "Muhammad"
last = "Sajjad"
print(first + " " + last)
# Output: Muhammad Sajjad
# Example 3: Numbers
x = 5
y = 3
print(x + y)
# Output: 8
# Example 4: Boolean logic
is_logged_in = True
print(is_logged_in)
# Output: True
# Example 5: Type casting
age = "25"
print(int(age) + 5)
# Output: 30
7. Interactive Elements
Data Type Syntax Example Description
int x = 10 Whole number
float pi = 3.14 Decimal number
str name = "Ali" Text
bool flag = True True or False
Day 3-4: Conditionals & Loops
1. Concept Introduction
• Conditionals: Decide actions based on conditions.
2
• Loops: Repeat blocks of code.
2. Purpose & Importance
• Make decisions in programs (if-else)
• Automate repetitive tasks (loops)
3. Implementation Guide
# if-else statement
score = 85
if score >= 90:
print("A+")
elif score >= 80:
print("A")
else:
print("B")
# For loop
for i in range(1, 6):
print(i)
# While loop
n = 1
while n <= 5:
print(n)
n += 1
4. Common Mistakes & Pitfalls
• Using = instead of ==
• Forgetting colons :
• Infinite loops from missing += in while
5. Best Practices Checklist
• Always include a base case in loops
• Indent consistently
• Use elif instead of multiple if
6. Practical Examples
# Example 1: Simple if
age = 20
if age >= 18:
3
print("Adult")
# Output: Adult
# Example 2: Looping a range
for i in range(3):
print("Hi")
# Output: Hi (3 times)
# Example 3: Even numbers
for i in range(1, 10):
if i % 2 == 0:
print(i)
# Output: 2, 4, 6, 8
# Example 4: While countdown
count = 3
while count > 0:
print(count)
count -= 1
# Output: 3, 2, 1
Day 5-6: Functions, Scope, Recursion
1. Concept Introduction
• Functions: Reusable blocks of code.
• Scope: Where variables live.
• Recursion: A function calling itself.
2. Purpose & Importance
• Reduce repetition
• Organize code
• Solve problems step-by-step
3. Implementation Guide
def greet(name):
print("Hello", name)
greet("Sajjad")
# Recursive factorial
def factorial(n):
4
if n == 1:
return 1
return n * factorial(n - 1)
print(factorial(5))
# Output: 120
4. Common Mistakes & Pitfalls
• Missing return in functions
• Forgetting base case in recursion
• Using undefined variables from other scopes
5. Best Practices Checklist
• Use def to define functions
• Always test your function with edge cases
• Keep functions small and specific
Day 7: Practice Day
• Platform: HackerRank, LeetCode
• Practice: if-else, loops, functions
• Mini Quiz: Build a simple calculator
Week 2 – Data Structures & OOP
Day 8-9: Lists, Tuples, Sets, Dictionaries
# List
fruits = ["apple", "banana"]
print(fruits[0])
# Tuple
colors = ("red", "blue")
# Set
nums = {1, 2, 2, 3} # duplicates removed
# Dictionary
student = {"name": "Sajjad", "age": 25}
print(student["name"])
5
Day 10: String Manipulation
text = " Hello Python "
print(text.strip()) # Remove spaces
print(text.upper()) # HELLO PYTHON
print("Python" in text) # True
Day 11-12: OOP - Classes, Inheritance
class Student:
def __init__(self, name):
self.name = name
def greet(self):
print("Hi", self.name)
s1 = Student("Sajjad")
s1.greet()
# Inheritance
class Animal:
def speak(self):
print("Animal sound")
class Dog(Animal):
def speak(self):
print("Bark")
d = Dog()
d.speak()
Day 13: File Handling & Modules
# File write
with open("data.txt", "w") as f:
f.write("Hello File")
# Import
6
import math
print(math.sqrt(16))
Day 14: Mini Project — Student Record System
Features: - Add student - Search student - View all students - Save data to file
Would you like this document exported as a styled PDF with syntax colors and interactive sections?