Master the fundamentals of Python with clear explanations and hands-on examples
0 / 8 topics completed
01
Introduction to Python
Python is a high-level, interpreted programming language known for its simple,
readable syntax. It was created by Guido van Rossum and first
released in 1991. Today it powers everything from web apps to AI research.
Why Learn Python?
Easy to learn – clean, English-like syntax
Versatile – web dev, data science, automation, AI/ML
Massive community – tons of libraries and resources
In-demand – one of the most sought-after languages in jobs
Your very first Python program is the classic "Hello, World!" — just one line:
Python
print('Hello, World!')
Output
Hello, World!
💡
Python uses indentation (whitespace) to define code blocks instead of curly braces {}. This enforces clean, readable code.
02
Installation & Setup
Follow the steps below to install Python on your machine. Most systems come with
Python pre-installed, but it's best to verify you have a recent version (3.9+).
macOS
Install via Homebrew (recommended):
Bash
brew install python3
Windows
Download the installer from python.org/downloads and check "Add Python to PATH" during installation.
Linux
Most distributions include Python. Install or update via your package manager:
Bash
sudo apt update && sudo apt install python3
✅
Verify your installation by running python3 --version in your terminal.
Every interactive program needs to read input and display output.
Python provides input() and print() for this.
print() — Displaying Output
The print() function outputs text to the console. You can separate values with sep and end with end.
Python
name = "Alice"
age = 25# f-strings (formatted string literals)print(f"My name is {name} and I am {age} years old.")
# Multiple valuesprint("Name:", name, "| Age:", age)
Output
My name is Alice and I am 25 years old.
Name: Alice | Age: 25
input() — Reading User Input
The input() function pauses the program and waits for the user to type something. It always returns a string.
Python
name = input("What is your name? ")
print(f"Hello, {name}! Welcome!")
# Convert input to integer
age = int(input("How old are you? "))
print(f"In 10 years you will be {age + 10}.")
⚠️
input() always returns a str. Wrap it with int() or float() to use it as a number.
Practice: Input & Output
Build a simple greeting program:
Ask the user for their name using input()
Ask for their favorite color
Print a message: "Hello {name}, your favorite color is {color}!"
Python
name = input("Enter your name: ")
color = input("Enter your favorite color: ")
print(f"Hello {name}, your favorite color is {color}!")
05
Conditional Statements
Conditionals let your program make decisions. Python uses if,
elif, and else to branch execution based on conditions.
age = 20
has_id = Trueif age >= 18and has_id:
print("Entry allowed")
else:
print("Entry denied")
Practice: Conditionals
Write a program that:
Reads a number from the user
Prints "Positive" if it's greater than 0
Prints "Negative" if it's less than 0
Prints "Zero" if it's exactly 0
Python
num = float(input("Enter a number: "))
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")
06
Loops
Loops let you repeat a block of code multiple times. Python has two main loop
types: for and while.
for Loop — Iterating Over a Sequence
Use for when you know how many times to loop or are iterating over a collection.
Python
# Iterate over a rangefor i inrange(5):
print(f"Count: {i}")
# Iterate over a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Be careful with while loops — always make sure the condition eventually becomes False to avoid infinite loops.
Python
# Break and Continuefor i inrange(10):
if i == 3:
continue# skip 3if i == 7:
break# stop at 7print(i)
Output
0
1
2
4
5
6
Practice: Loops
Write two programs:
Use a for loop to print all even numbers from 1 to 20
Use a while loop to find the sum of numbers from 1 to 100
Python
# Even numbers 1-20for i inrange(1, 21):
if i % 2 == 0:
print(i)
# Sum of 1 to 100
total = 0
num = 1while num <= 100:
total += num
num += 1print(f"Sum: {total}")
07
Functions
Functions are reusable blocks of code that perform a specific task. They help
you avoid repetition and keep your code organized.
Python
defgreet(name):
"""Greet a user by name."""returnf"Hello, {name}!"# Call the function
message = greet("Alice")
print(message)
Output
Hello, Alice!
Default Parameters & Multiple Returns
You can set default values for parameters and return multiple values using tuples.
Python
defcalculate(a, b, operation="add"):
if operation == "add":
return a + b
elif operation == "subtract":
return a - b
elif operation == "multiply":
return a * b
print(calculate(10, 5)) # 15 (default: add)print(calculate(10, 5, "multiply")) # 50defmin_max(numbers):
returnmin(numbers), max(numbers)
lo, hi = min_max([3, 1, 7, 2, 9])
print(f"Min: {lo}, Max: {hi}")
Output
15
50
Min: 1, Max: 9
Practice: Functions
Write a function is_even(n) that returns True if n is even, False otherwise
Write a function factorial(n) that returns the factorial of n
Write a function reverse_string(s) that returns the reversed string
Python
defis_even(n):
return n % 2 == 0deffactorial(n):
if n <= 1:
return1return n * factorial(n - 1)
defreverse_string(s):
return s[::-1]
print(is_even(4)) # Trueprint(factorial(5)) # 120print(reverse_string("hello")) # olleh
08
Lists & Dictionaries
Lists and dictionaries are Python's most commonly used data structures.
Lists hold ordered sequences; dictionaries store key-value pairs.
Lists — Ordered & Mutable
Lists can hold any data type and can be modified after creation.