1.
Python Basics
Python is easy to read because it uses plain English words for commands (like print or input).
It uses indentation (spaces) to structure the code instead of symbols like {} or ;.
2. Variables
A variable is like a box where you store data (like numbers, text, or lists).
Example:
name = "John" # Stores the text "John" in the variable 'name'
age = 25 # Stores the number 25 in the variable 'age'
3. Printing
You use the print() function to display something on the screen.
Example:
print("Hello, World!") # Prints: Hello, World!
4. Input
The input() function asks the user to type something.
Example:
name = input("What is your name? ") # Asks the user for their name
print("Hello, " + name + "!") # Greets the user
5. Data Types
Numbers: int (whole numbers) and float (decimal numbers)
age = 30 # int
height = 5.9 # float
Text (Strings): Stored in quotes ("Hello" or 'Hello')
greeting = "Hi!" # string
Boolean: True or False
is_adult = True # boolean
6. If-Else (Decisions)
Python can make decisions based on conditions using if, elif, and else.
Example:
age = 18
if age >= 18:
print("You are an adult!")
else:
print("You are not an adult.")
7. Loops
Loops let you repeat tasks.
For Loop: Repeats for a specific range.
for i in range(5): # Loops 5 times
print(i) # Prints 0, 1, 2, 3, 4
While Loop: Repeats as long as a condition is true.
count = 0
while count < 3:
print(count)
count += 1 # Prints 0, 1, 2
8. Functions
Functions are reusable blocks of code.
Example:
def greet(name):
print(f"Hello, {name}!")
greet("Alice") # Prints: Hello, Alice!
9. Example Program
Let’s combine what we’ve learned in a simple program:
# Ask for the user's name
name = input("Enter your name: ")
# Ask for the user's age
age = int(input("Enter your age: "))
# Greet the user and check if they are an adult
if age >= 18:
print(f"Hello, {name}! You are an adult.")
else:
print(f"Hello, {name}! You are not an adult yet.")
Problem 1: Basic Input and Output
Question:
Write a program that asks for the user's name and prints a greeting message using their name.
Example:
Enter your name: John
Hello, John!
Problem 2: Simple Arithmetic
Question:
Write a program that takes two numbers as input, adds them, and prints the result.
Example:
Enter first number: 5
Enter second number: 3
The sum is: 8
Problem 3: Area of a Rectangle
Question:
Write a program that asks the user for the length and width of a rectangle, then calculates and prints the area.
Example:
Enter length: 4
Enter width: 3
The area of the rectangle is: 12
Problem 4: Age Calculation
Question:
Write a program that asks for the user's birth year and calculates their age, assuming the current year is
2025.
Example:
Enter your birth year: 2000
You are 25 years old.
Problem 5: Temperature Conversion
Question:
Write a program that takes a temperature in Celsius from the user and converts it to Fahrenheit.
Formula:
F = (C × 9/5) + 32
Example:
Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77.0
Problem 6: Multiply Two Numbers
Question:
Write a program that takes two numbers as input, multiplies them, and prints the product.
Example:
Enter first number: 6
Enter second number: 4
The product is: 24
Problem 7: Simple Interest Calculation
Question:
Write a program to calculate the simple interest. Ask the user for the principal, rate of interest, and time,
then calculate and print the interest.
Formula:
Interest = (Principal × Rate × Time) / 100
Example:
Enter principal: 1000
Enter rate of interest: 5
Enter time in years: 2
The simple interest is: 100.0
Problem 8: Convert Minutes to Hours
Question:
Write a program that takes a number of minutes as input and converts it into hours and remaining minutes.
Example:
Enter minutes: 130
130 minutes is 2 hours and 10 minutes.
Problem 9: Even or Odd
Question:
Write a program that takes an integer as input and determines whether the number is even or odd.
Example:
Enter a number: 7
7 is odd.
Problem 10: Discount Calculation
Question:
Write a program that takes the original price of an item and a discount percentage, then calculates and prints
the final price after applying the discount.
Formula:
Final Price = Original Price - (Original Price × Discount / 100)
Example:
Enter original price: 200
Enter discount percentage: 20
The final price after discount is: 160.0
SOLUTIONS
Problem 1: Basic Input and Output
name = input("Enter your name: ")
print("Hello, " + name + "!")
Problem 2: Simple Arithmetic
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
sum_result = num1 + num2
print("The sum is:", sum_result)
Problem 3: Area of a Rectangle
length = float(input("Enter length: "))
width = float(input("Enter width: "))
area = length * width
print("The area of the rectangle is:", area)
Problem 4: Age Calculation
birth_year = int(input("Enter your birth year: "))
current_year = 2025
age = current_year - birth_year
print(f"You are {age} years old.")
Problem 5: Temperature Conversion
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print("Temperature in Fahrenheit:", fahrenheit)
Problem 6: Multiply Two Numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
product = num1 * num2
print("The product is:", product)
Problem 7: Simple Interest Calculation
principal = float(input("Enter principal: "))
rate = float(input("Enter rate of interest: "))
time = float(input("Enter time in years: "))
interest = (principal * rate * time) / 100
print("The simple interest is:", interest)
Problem 8: Convert Minutes to Hours
minutes = int(input("Enter minutes: "))
hours = minutes // 60
remaining_minutes = minutes % 60
print(f"{minutes} minutes is {hours} hours and {remaining_minutes} minutes.")
Problem 9: Even or Odd
number = int(input("Enter a number: "))
if number % 2 == 0:
print(f"{number} is even.")
else:
print(f"{number} is odd.")
Problem 10: Discount Calculation
original_price = float(input("Enter original price: "))
discount_percentage = float(input("Enter discount percentage: "))
discount = (original_price * discount_percentage) / 100
final_price = original_price - discount
print(f"The final price after discount is: {final_price}")