Python Programming
Python Programming
What is Python? Use input() to get user input and print() to display output.
Python is a popular programming language used to name = input("What is your name? ")
create software, games, websites, and much more. print("Hello, " + name)
Known for its simplicity and readability, Python is a Indentation: Python relies on indentation (spacing) to
define blocks of code. Make sure to use the same number
great language for beginners.
of spaces to avoid errors.
Python is used in many fields: web development, data Basic Operators
science, game development, and more. Arithmetic Operators:
It’s powerful but simple, which makes it a favourite for + (addition), - (subtraction), * (multiplication), / (division)
beginners and experts. Example:
x=5
Basic Concepts in Python y=2
Variables print(x + y) # 7
A variable is like a container for storing data. You can print(x - y) # 3
give it a name and assign it a value. print(x * y) # 10
For example: print(x / y) # 2.5
age = 12 Comparison Operators:
name = "Aarav" == (equal), != (not equal), < (less than), > (greater than), <=
Data Types (less than or equal to), >= (greater than or equal to)
Numbers: ____________________________________________________
Integers (e.g., 5, -3, 100)
Floats (decimals, e.g., 3.14, 0.5) Conditional Statements: Conditional statements allow the
Strings: Text inside quotes is called a string ("Hello",) program to make decisions based on conditions.
name = "Python" Example: age = 14
print (name) if age >= 13:
Booleans: Represents True or False values. print("You are a teenager!")
is_sunny = True else:
is_raining = False print("You are a child.")
Loops: Loops allow you to repeat a block of code multiple 4. Area of a Rectangle: A program to calculate the area of a
times. rectangle using its length and width.
For Loop: A for loop is used when we know how many length = float(input("Enter the length of the rectangle: "))
times we want to repeat a task. width = float(input("Enter the width of the rectangle: "))
for i in range(1, 6): area = length * width
print(i) print("The area of the rectangle is:", area)
While Loop: A while loop keeps going as long as a
certain condition is true. 2. Perimeter of a Square: A program to find the perimeter of a
count = 1 square given the length of one side.
while count <= 5: side = float(input("Enter the length of a side of the square: "))
print(count) perimeter = 4 * side
count += 1 print("The perimeter of the square is:", perimeter)
When to Use For Loops and While Loops
Use a for loop when you know exactly how many times 3. Convert Celsius to Fahrenheit: A program to convert
you need to repeat something. temperature from Celsius to Fahrenheit.
Use a while loop when you want to repeat something until celsius = float(input("Enter temperature in Celsius: "))
a condition is met. fahrenheit = (celsius * 9/5) + 32
print("Temperature in Fahrenheit is:", fahrenheit)
Examples:
1. Hello, World! Program 4. Calculate the Average of Three Numbers: A program to
print("Hello, World!") find the average of three numbers entered by the user.
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
2. Simple Addition Program
num1 = int(input("Enter first number: ")) num3 = float(input("Enter the third number: "))
num2 = int(input("Enter second number: ")) average = (num1 + num2 + num3) / 3
sum = num1 + num2 print("The average of the numbers is:", average)
print("The sum is:", sum)
3. Square of a Number
num = int(input("Enter a number: "))
square = num * num
print("The square of the number is:", square)
5. Calculate the Volume of a Cuboid: A program to calculate 9. Swap Two Numbers Using Python’s Multiple Assignment
the volume of a cuboid given its length, width, and height. A program to swap the values of two numbers entered
length = float(input("Enter the length of the cuboid: ")) a = float(input("Enter the first number: "))
width = float(input("Enter the width of the cuboid: ")) b = float(input("Enter the second number: "))
height = float(input("Enter the height of the cuboid: ")) print("Before swapping:")
volume = length * width * height print("First number:", a)
print("The volume of the cuboid is:", volume) print("Second number:", b)
a, b = b, a
print("After swapping:")
6. Calculate the Simple Interest: A program to calculate simple print("First number:", a)
interest using principal, rate, and time. print("Second number:", b)
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the rate of interest: ")) 10. Combine First and Last Name: A program to take a first
time = float(input("Enter the time in years: ")) name and last name as input and print the full name.
si = (principal * rate * time) / 100 first_name = input("Enter your first name: ")
print("The simple interest is:", si) last_name = input("Enter your last name: ")
full_name = first_name + " " + last_name
print("Your full name is:", full_name)
7. Calculate the Area of a Circle: A program to calculate the
area of a circle given its radius. 11. Check Even or Odd: A program to check if a number
radius = float(input("Enter the radius of the circle: ")) entered by the user is even or odd.
area = 3.14* radius * radius num = int(input("Enter a number: "))
print("The area of the circle is:", area) if num % 2 == 0:
print("The number is even.")
else:
8. Convert Kilometers to Miles: A program to convert distance print("The number is odd.")
from kilometers to miles.
kilometers = float(input("Enter distance in kilometers: ")) 12. Print all lowercase alphabets
miles = kilometers * 0.621371 import string
print("Distance in miles is:", miles) alphabets = string.ascii_lowercase
for letter in alphabets:
print(letter)
13. Find the Largest Number: A program to find the largest of 15. Check if Number is Positive, Negative, or Zero
three numbers entered by the user. A program to check if a number is positive, negative, or zero.
num1 = int(input("Enter first number: ")) num = int(input("Enter a number: "))
num2 = int(input("Enter second number: ")) if num > 0:
num3 = int(input("Enter third number: ")) print("The number is positive.")
if num1 >= num2 and num1 >= num3: elif num < 0:
print("The largest number is:", num1) print("The number is negative.")
elif num2 >= num1 and num2 >= num3: else:
print("The largest number is:", num2) print("The number is zero.")
else:
print("The largest number is:", num3)
16. Multiplication Table Program: A program to print the
14. Simple Calculator: A program to perform addition, subtraction, multiplication table of a given number.
multiplication, or division based on user choice. number = int(input("Enter a number: "))
print("Choose operation:") for i in range(1, 11):
print("1. Add") print(number, "x", i, "=", number * i)
print("2. Subtract")
print("3. Multiply") 17. Count Down Timer: A countdown timer that prints numbers
print("4. Divide") from 10 to 1.
choice = int(input("Enter choice (1/2/3/4): ")) for i in range(10, 0, -1):
num1 = int(input("Enter first number: ")) print(i)
num2 = int(input("Enter second number: ")) print("Time's up!")
if choice == 1:
print("Result:", num1 + num2) 18. Simple Star Square: Print a square of stars (*), where each
elif choice == 2: side has the same length.
print("Result:", num1 - num2) for i in range(4):
elif choice == 3: print("* " * 4)
print("Result:", num1 * num2) Output:
elif choice == 4: *****
print("Result:", num1 / num2) *****
else: *****
print("Invalid choice") *****
19. Right-Angled Triangle: Print a right-angled triangle using 22. Diamond Pattern: A diamond pattern using stars, achieved
stars. by combining an upward and downward triangle.
for i in range(1, 6): rows = 5
print("* " * i) # Top half of the diamond
Output: for i in range(1, rows + 1):
* print(" " * (rows - i) + "* " * i)
** # Bottom half of the diamond
*** for i in range(rows - 1, 0, -1):
**** print(" " * (rows - i) + "* " * i)
***** Output:
*
20. Inverted Right-Angled Triangle: Print an inverted right- **
angled triangle using stars. ***
for i in range(5, 0, -1): ****
print("* " * i) *****
Output: ****
***** ***
**** **
*** *
** 23. Number Triangle: Print a triangle with numbers in
* Increasing order.
21. Pyramid Pattern: Print a pyramid shape using stars. This for i in range(1, 6):
pattern uses spaces to center each row of stars. for j in range(1, i + 1):
rows = 5 print(j, end=" ")
for i in range(1, rows + 1): print()
print(" " * (rows - i) + "* " * i) Output:
Output: Copy code
* 1
** 12
*** 123
**** 1234
***** 12345
24. Repeating Number Triangle: A triangle where each row Output:
contains the same number, which increases with each row. *****
for i in range(1, 6): * *
print((str(i) + " ") * i) * *
Output: * *
1 *****
22
333 27. Checkerboard Pattern: Create a checkerboard pattern using
4444 * and spaces.
55555 size = 5
for i in range(size):
25. Alphabet Triangle: Print a triangle with letters in print((" * " if i % 2 == 0 else " * ") * size)
alphabetical order. Output:
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" *****
for i in range(5): *****
print(" ".join(alphabet[:i + 1])) *****
*****
Output: *****
A
AB
ABC
ABCD
ABCDE
26. Hollow Square: Print a hollow square with stars, where only
the borders are filled.
size = 5
for i in range(size):
if i == 0 or i == size - 1:
print("* " * size)
else:
print("* " + " " * (size - 2) + "*")