Ultimate Python Guide
Learn Python from scratch with this complete beginner‑friendly guide. No prior coding experience needed.
📖 Table of Contents
1.1 Your First Python Program
Learn how to write your very first Python program using print() and variables.
print("Hello, World!")
print()– a built‑in function that outputs text to the screen."Hello, World!"– a string (text) that will be printed.- Python executes code line by line, from top to bottom.
1.2 Variables & Data Types
Variables store data. Python has several built‑in data types.
name = "Alice"
age = 30
height = 5.9
is_student = True
str– string (text).int– integer (whole numbers).float– numbers with a decimal point.bool– True or False.
1.3 Lists & Tuples
Lists are mutable collections; tuples are immutable.
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
fruits.remove("banana")
coordinates = (10, 20)
list– mutable, created with[].tuple– immutable, created with().- Common list methods:
.append(),.remove(),.pop().
1.4 Conditional Statements
Use if, elif, and else to make decisions.
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
else:
print("Grade: C")
if– starts a conditional block.elif– else if (multiple checks).else– runs if all conditions are false.- Indentation is crucial – the code block must be indented.
1.5 Loops (for/while)
Loops repeat code.
# for loop
for i in range(5):
print(i)
# while loop
count = 0
while count < 5:
print(count)
count += 1
for– iterates over a sequence (e.g.,range(), list).while– repeats while a condition is true.range(start, stop, step)– generates a sequence of numbers.
1.6 Functions
Functions are reusable blocks of code.
def greet(name):
return f"Hello, {name}!"
result = greet("Alice")
print(result)
def– defines a function.- Parameters – values passed to the function.
return– sends a value back to the caller.
1.7 String Methods & Formatting
Strings have many built‑in methods and can be formatted with f‑strings.
text = " Hello World! "
print(text.upper()) # " HELLO WORLD! "
print(text.lower()) # " hello world! "
print(text.strip()) # "Hello World!"
name = "Alice"
age = 30
print(f"My name is {name} and I'm {age} years old.")
.upper(),.lower(),.strip(),.replace(),.split(),.join().- f‑strings –
f"Hello {name}".
1.8 Dictionaries
Dictionaries store key‑value pairs.
person = {
"name": "Alice",
"age": 30,
"city": "Lahore"
}
print(person["name"])
person["age"] = 31
person["country"] = "Pakistan"
- Created with
{}. - Access via key:
dict["key"]or.get(). - Keys must be immutable (string, number, tuple).
1.9 Error Handling
Handle exceptions gracefully with try/except.
try:
num = int(input("Enter a number: "))
print(f"You entered: {num}")
except ValueError:
print("That's not a valid number!")
try– block that might raise an error.except– runs if an error occurs.finally– always runs (cleanup).
1.10 Project: Calculator
Apply everything you've learned to build a functional calculator.
- Functions – add, subtract, multiply, divide.
- User menu – display options and loop until exit.
- Error handling – catch division by zero and invalid input.
- Input validation – ensure valid menu choices.
🚀 Want to go further?
Explore the full Python course with interactive demos, code examples, and challenges.
Full Python Course →