Class 8-1
Class 8-1
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
science, game development, and more. Basic Operators
It’s powerful but simple, which makes it a favourite for Arithmetic Operators:
beginners and experts. + (addition), - (subtraction), * (multiplication), / (division)
Example:
Basic Concepts in Python x=5
Variables y=2
A variable is like a container for storing data. You can print(x + y) # 7
give it a name and assign it a value. print(x - y) # 3
For example: print(x * y) # 10
age = 12 print(x / y) # 2.5
name = "Aarav"
Data Types Comparison Operators:
Numbers: == (equal), != (not equal), < (less than), > (greater than), <=
Integers (e.g., 5, -3, 100) (less than or equal to), >= (greater than or equal to)
Floats (decimals, e.g., 3.14, 0.5) ____________________________________________________
Strings: Text inside quotes is called a string ("Hello",)
name = "Python" Conditional Statements: Conditional statements allow the
print (name) program to make decisions based on conditions.
Booleans: Represents True or False values. Example: age = 14
is_sunny = True if age >= 13:
is_raining = False print("You are a teenager!")
else:
print("You are a child.")
3. Perimeter of a Square:
Loops: Loops allow you to repeat a block of code multiple side = float(input("Enter the length of a side of the square: "))
times. perimeter = 4 * side
For Loop: A for loop is used when we know how many print("The perimeter of the square is:", perimeter)
times we want to repeat a task.
for i in range(1, 6): 4. Convert Celsius to Fahrenheit:
print(i) celsius = float(input("Enter temperature in Celsius: "))
While Loop: A while loop keeps going as long as a fahrenheit = (celsius * 9/5) + 32
certain condition is true. print("Temperature in Fahrenheit is:", fahrenheit)
count = 1
while count <= 5: 5. Calculate the Volume of a Cuboid:
print(count) length = float(input("Enter the length of the cuboid: "))
count += 1 width = float(input("Enter the width of the cuboid: "))
height = float(input("Enter the height of the cuboid: "))volume =
When to Use For Loops and While Loops length * width * heightprint("The volume of the cuboid is:",
Use a for loop when you know exactly how many times volume)
you need to repeat something.
Use a while loop when you want to repeat something until 6. Calculate the Simple Interest:
a condition is met. principal = float(input("Enter the principal amount: "))
Examples: rate = float(input("Enter the rate of interest: "))
time = float(input("Enter the time in years: "))
si = (principal * rate * time) / 100
1. Square of a Number
num = int(input("Enter a number: ")) print("The simple interest is:", si)
square = num * num
print("The square of the number is:", square) 7. Calculate the Area of a Circle:
radius = float(input("Enter the radius of the circle: "))
area = 3.14* radius * radius
2. Area of a Rectangle:
length = float(input("Enter the length of the rectangle: ")) print("The area of the circle is:", area)
width = float(input("Enter the width of the rectangle: "))
area = length * width 8. Convert Kilometers to Miles:
print("The area of the rectangle is:", area) kilometers = float(input("Enter distance in kilometers: "))
miles = kilometers * 0.621371
print("Distance in miles is:", miles)
9. Convert Hours to Seconds 13. Combine First and Last Name:
hours = float(input("Enter time in hours: ")) first_name = input("Enter your first name: ")
seconds = hours * 3600 last_name = input("Enter your last name: ")
print("Time in seconds:", seconds) full_name = first_name + " " + last_name
print("Your full name is:", full_name)
10. Sum of the Digits of a Two-Digit Number
number = int(input("Enter a two-digit number: ")) Conditional Statements
tens = number // 10 14. Check if Number is Positive, Negative, or Zero
units = number % 10 num = int(input("Enter a number: "))
digit_sum = tens + units if num > 0:
print("The sum of the digits is:", digit_sum) print("The number is positive.")
elif num < 0:
11. Product of the Digits of a Three-Digit Number print("The number is negative.")
number = int(input("Enter a three-digit number: ")) else:
hundreds = number // 100 print("The number is zero.")
tens = (number // 10) % 10
units = number % 10 15. Check if a Year is a Leap Year
digit_product = hundreds * tens * units year = int(input("Enter a year: "))
print("The product of the digits is:", digit_product) if year % 4 == 0 and (year % 100 != 0 or year % 400 ==
0):
12. Swap Two Numbers Using Python’s Multiple Assignment print("It's a leap year.")
a = float(input("Enter the first number: ")) else:
b = float(input("Enter the second number: ")) print("It's not a leap year.")
print("Before swapping:")
print("First number:", a) 16. Check if a Character is a Vowel or Consonant
print("Second number:", b) char = input("Enter a single character: ").lower()
a, b = b, a if char in "aeiou":
print("After swapping:") print("The character is a vowel.")
print("First number:", a) else:
print("Second number:", b) print("The character is a consonant.")
17. Check Eligibility to Vote 21. Grade Based on Marks
age = int(input("Enter your age: ")) marks = int(input("Enter your marks: "))
if age >= 18: if marks >= 90:
print("You are eligible to vote.") print("Grade: A")
else: elif marks >= 75:
print("You are not eligible to vote.") print("Grade: B")
elif marks >= 50:
18. Check if a Number is Divisible by Both 5 and 11 print("Grade: C")
num = int(input("Enter a number: ")) else:
if num % 5 == 0 and num % 11 == 0: print("Grade: F")
print("The number is divisible by both 5 and 11.")
else: 22. Check if a Character is Uppercase, Lowercase, or Digit
print("The number is not divisible by both 5 and 11.") char = input("Enter a single character: ")
if char.isupper():
19. Find the Smallest of Three Numbers print("It's an uppercase letter.")
a = int(input("Enter first number: ")) elif char.islower():
b = int(input("Enter second number: ")) print("It's a lowercase letter.")
c = int(input("Enter third number: ")) elif char.isdigit():
if a < b and a < c: print("It's a digit.")
print("The smallest number is:", a) else:
elif b < c: print("It's a special character.")
print("The smallest number is:", b)
else: 23. Electricity Bill Calculation
print("The smallest number is:", c) units = int(input("Enter electricity units consumed: "))
if units <= 100:
20. Check if a Number is a Multiple of Another Number bill = units * 1.5
a = int(input("Enter the first number: ")) elif units <= 300:
b = int(input("Enter the second number: ")) bill = 100 * 1.5 + (units - 100) * 2.5
if a % b == 0: else:
print(f"{a} is a multiple of {b}.") bill = 100 * 1.5 + 200 * 2.5 + (units - 300) * 3.5
else: print("Electricity bill:", bill)
print(f"{a} is not a multiple of {b}.")
24. Find the Type of a Triangle 28. Categorize a Person Based on Age
a = int(input("Enter first side: ")) age = int(input("Enter your age: "))
b = int(input("Enter second side: ")) if age <= 12:
c = int(input("Enter third side: ")) print("Child")
if a == b == c: elif age <= 19:
print("Equilateral triangle.") print("Teenager")
elif a == b or b == c or c == a: elif age <= 60:
print("Isosceles triangle.") print("Adult")
else: else:
print("Scalene triangle.") print("Senior Citizen")
25. Check if a Number is Between Two Given Numbers 29. Check if Two Numbers Are Equal or Unequal
num = int(input("Enter a number: ")) a = int(input("Enter the first number: "))
low = int(input("Enter the lower bound: ")) b = int(input("Enter the second number: "))
high = int(input("Enter the upper bound: ")) if a == b:
if low <= num <= high: print("The numbers are equal.")
print("The number is within the range.") else:
else: print("The numbers are not equal.")
print("The number is outside the range.")
30. Check if a Number is a Three-Digit Number
26. Check if a Day is a Weekend or Weekday num = int(input("Enter a number: "))
day = input("Enter a day of the week: ").lower() if 100 <= num <= 999:
if day in ["saturday", "sunday"]: print("It's a three-digit number.")
print("It's a weekend.") else:
else: print("It's not a three-digit number.")
print("It's a weekday.")