🎯 What You'll Learn
- Python syntax and indentation
- Variables and data types
- Input and output
- Operators and expressions
- Conditionals (if/elif/else)
- Loops (for, while)
📚 Core Concepts
1. Your First Python Program
Python is simple and readable - no complex setup needed!
# This is a comment - Python ignores it
print("Hello, World!") # Print to console
# Multiple lines
print("Welcome to Python!")
print("Let's learn together!")
💡 Key Difference: Python uses indentation (spaces/tabs) instead of { } brackets like C++ or JS!
2. Variables & Data Types
# No type declaration needed! Python figures it out
name = "Alice" # String
age = 18 # Integer
height = 5.6 # Float (decimal)
is_student = True # Boolean (True/False)
# You can change types!
x = 5 # x is an integer
x = "hello" # now x is a string - this is fine!
# Multiple assignment
a, b, c = 1, 2, 3
x = y = z = 0
# Check type
print(type(age)) #
print(type(height)) #
# Type conversion
num_str = "25"
num_int = int(num_str) # Convert string to int
num_float = float(num_str) # Convert to float
text = str(123) # Convert number to string
✏️ Try it: Create variables for your name, age, GPA, and whether you like coding. Print them with labels.
3. Input & Output
# Output with print()
print("Hello")
print("Score:", 95)
print("Name:", name, "Age:", age) # Multiple values
# Formatted output
name = "John"
age = 20
print(f"My name is {name} and I'm {age} years old") # f-string (modern!)
print("My name is {} and I'm {} years old".format(name, age)) # .format()
# Input from user
name = input("Enter your name: ") # Always returns a string!
age = input("Enter your age: ") # This is a STRING, not a number
# Convert input to number
age = int(input("Enter your age: ")) # Now it's an integer
score = float(input("Enter your score: ")) # Float for decimals
4. Operators
# Arithmetic operators
a, b = 10, 3
print(a + b) # 13 (addition)
print(a - b) # 7 (subtraction)
print(a * b) # 30 (multiplication)
print(a / b) # 3.333... (division - always float!)
print(a // b) # 3 (floor division - integer)
print(a % b) # 1 (remainder/modulo)
print(a ** b) # 1000 (power - a to the power of b)
# Comparison operators
10 > 5 # True
10 < 5 # False
10 >= 10 # True
10 == 10 # True (equal to)
10 != 5 # True (not equal to)
# Logical operators
True and True # True
True or False # True
not True # False
# String operators
"Hello" + " " + "World" # "Hello World" (concatenation)
"Ha" * 3 # "HaHaHa" (repetition)
5. Conditionals
# If statement - NOTICE THE INDENTATION!
age = 18
if age >= 18:
print("You are an adult")
print("You can vote") # Still inside if block
print("This always runs") # Outside if block
# If-elif-else
score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "F"
print(f"Your grade: {grade}")
# Ternary operator (one-liner)
status = "Pass" if score >= 60 else "Fail"
# Multiple conditions
age = 20
has_license = True
if age >= 18 and has_license:
print("You can drive!")
if age < 18 or not has_license:
print("Cannot drive")
💡 CRITICAL: Indentation matters! Use 4 spaces (or 1 tab). All code at same level must have same indentation.
6. Loops
# For loop - iterate over a sequence
for i in range(5): # 0, 1, 2, 3, 4
print(i)
# Range with start and end
for i in range(1, 6): # 1, 2, 3, 4, 5
print(i)
# Range with step
for i in range(0, 10, 2): # 0, 2, 4, 6, 8
print(i)
# Loop through a list
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
print(fruit)
# While loop
count = 0
while count < 5:
print(count)
count += 1 # count = count + 1
# Break and continue
for i in range(10):
if i == 5:
break # Exit loop
if i % 2 == 0:
continue # Skip to next iteration
print(i) # Only prints odd numbers: 1, 3
# Loop with else (runs if loop completes without break)
for i in range(3):
print(i)
else:
print("Loop completed!")
✏️ Try it: Write a loop that prints multiplication table for any number (ask user for the number).
7. String Basics
# String creation
text = "Hello World"
text2 = 'Single quotes work too'
long_text = """Multiple
lines of text"""
# String indexing (starts at 0!)
first_char = text[0] # "H"
last_char = text[-1] # "d" (negative index from end)
# String slicing
substring = text[0:5] # "Hello" (start:end, end not included)
substring = text[:5] # "Hello" (start from beginning)
substring = text[6:] # "World" (go to end)
# String methods
print(text.upper()) # "HELLO WORLD"
print(text.lower()) # "hello world"
print(text.replace("World", "Python")) # "Hello Python"
print(text.split()) # ["Hello", "World"] (split into list)
print(len(text)) # 11 (length)
# Check if substring exists
if "Hello" in text:
print("Found it!")
🛠️ Practice Projects
Project 1: Temperature Converter
Convert between Celsius and Fahrenheit
Features to add:
- Ask user for temperature
- Ask for conversion type (C to F or F to C)
- Perform conversion
- Display result with 2 decimal places
- Loop to do multiple conversions
# Formula hints:
# C to F: (C × 9/5) + 32
# F to C: (F - 32) × 5/9
# Format: print(f"{value:.2f}")
Project 2: Simple Quiz Game
Ask questions and keep score
Features to add:
- Ask 5 multiple choice questions
- Get user's answers
- Check if correct
- Keep score
- Display final score and percentage
Project 3: Number Guessing Game
Computer picks a number, you guess it!
Features to add:
- Generate random number (1-100)
- Get user guesses in a loop
- Give "higher" or "lower" hints
- Count attempts
- Congratulate on win
# Random number hint:
import random
number = random.randint(1, 100)