Python Beginner Notes With Exercises and Solutions
Python Beginner Notes With Exercises and Solutions
1. Introduction to Python
- Python ek high-level, interpreted language hai.
- Asaan syntax ki wajah se beginners ke liye best hai.
- Python ka use: web development, AI, automation, data science, games, etc.
Example:
print("Hello, World!")
2. Python Installation
- Download from: https://www.python.org/
- Install karte waqt "Add Python to PATH" option check karna na bhoolen.
- Terminal ya CMD me check karne ke liye:
python --version
5. Operators
# Arithmetic
a=5+3 #8
b=5*2 # 10
c = 10 / 2 # 5.0
# Comparison
5>3 # True
5 == 5 # True
6. Conditional Statements
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
7. Loops
# for loop
for i in range(5):
print(i)
# while loop
i=0
while i < 5:
print(i)
i += 1
8. Functions
def greet(name):
print("Hello,", name)
greet("Ali")
9. Lists
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # apple
fruits.append("mango") # add item
10. Dictionaries
person = {
"name": "Ayaan",
"age": 15
}
print(person["name"])
Practice Exercises
1. Print Statement
Write a program to print your name and your favorite color.
2. Variables
Create variables to store your name, age, and city. Print them.
4. Arithmetic Operations
Write a program to input two numbers and print their sum, difference, product, and
division.
5. Conditional Statements
Write a program that asks the user for their age and prints whether they are a child,
teenager, or adult.
6. Loops
Use a for loop to print numbers from 1 to 10.
Use a while loop to print even numbers between 1 and 20.
7. Functions
Write a function that takes a number and prints whether it is even or odd.
8. Lists
Create a list of your 5 favorite foods. Add one more item to the list and print all items.
9. Dictionaries
Create a dictionary with keys: name, age, and country. Fill it with your info and print the
dictionary.
10. Bonus Challenge
Make a small program that asks for the user's name and age, and then tells them how old
they will be in 5 years.
Solutions to Exercises
1. Print Statement
print("My name is Ayaan")
print("My favorite color is Blue")
2. Variables
name = "Ayaan"
age = 15
city = "Lahore"
print(name, age, city)
4. Arithmetic Operations
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum:", a + b)
print("Difference:", a - b)
print("Product:", a * b)
print("Division:", a / b)
5. Conditional Statements
age = int(input("Enter your age: "))
if age < 13:
print("You are a child.")
elif age < 20:
print("You are a teenager.")
else:
print("You are an adult.")
6. Loops
# For loop
for i in range(1, 11):
print(i)
# While loop
i=2
while i <= 20:
print(i)
i += 2
7. Functions
def check_even_odd(num):
if num % 2 == 0:
print("Even")
else:
print("Odd")
check_even_odd(5)
8. Lists
foods = ["biryani", "pizza", "burger", "pasta", "ice cream"]
foods.append("samosa")
print(foods)
9. Dictionaries
info = {
"name": "Ayaan",
"age": 15,
"country": "Pakistan"
}
print(info)