[go: up one dir, main page]

0% found this document useful (0 votes)
42 views9 pages

Class 8-1

Uploaded by

raistar46383
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views9 pages

Class 8-1

Uploaded by

raistar46383
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Introduction to Python Input and Output

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.")

27. Find the Absolute Value of a Number


num = float(input("Enter a number: "))
if num < 0:
num = -num
print("The absolute value is:", num)
Looping Statements 34. Reverse a Number
31. Prime Numbers: Prime numbers are numbers that have only # Reverse a number
2 factors: 1 and themselves. num = int(input("Enter a number: "))
# Prime numbers in a range reversed_num = 0
start = int(input("Enter start of range: "))
end = int(input("Enter end of range: ")) while num > 0:
print(f"Prime numbers between {start} and {end}:") digit = num % 10
for num in range(start, end + 1): reversed_num = reversed_num * 10 + digit
if num > 1: # Numbers less than 2 are not prime num //= 10
for i in range(2, int(num**0.5) + 1):
if num % i == 0: print(f"Reversed number: {reversed_num}")
break
else: 35. Factorial of a Number: The factorial of a number is the
print(num, end=" ") product of that number and every positive integer less than it, up
to 1
32. Fibonacci Sequence: The Fibonacci sequence is a sequence # Factorial of a number
in which each number is the sum of the two preceding ones num = int(input("Enter a number: "))
# Fibonacci sequence generator factorial = 1
n = int(input("Enter the number of terms: ")) for i in range(1, num + 1):
a, b = 0, 1 factorial *= i
print("Fibonacci Sequence:") print(f"Factorial of {num}: {factorial}")
for _ in range(n):
print(a, end=" ") 36. Armstrong Numbers: An Armstrong number is a special
a, b = b, a + b kind of number in math. It's a number that equals the sum of its
digits, each raised to a power.
33. Sum of Digits # Armstrong numbers in a range
# Sum of digits of a number start = int(input("Enter start of range: "))
num = int(input("Enter a number: ")) end = int(input("Enter end of range: "))
sum_digits = 0 print(f"Armstrong numbers between {start} and {end}:")
while num > 0: for num in range(start, end + 1):
sum_digits += num % 10 sum_of_powers = sum(int(digit)**len(str(num)) for digit in
num //= 10 str(num))
print(f"Sum of the digits: {sum_digits}") if num == sum_of_powers:
print(num, end=" ")
37. Check if a Number is Palindrome: A palindrome number is 40. Decimal to Binary Conversion
a number that reads the same forward and backward # Convert a decimal number to binary
# Check if a number is palindrome num = int(input("Enter a decimal number: "))
num = int(input("Enter a number: ")) binary = ""
original_num = num while num > 0:
reversed_num = 0 binary = str(num % 2) + binary
while num > 0: num //= 2
digit = num % 10 print(f"Binary equivalent: {binary}")
reversed_num = reversed_num * 10 + digit
num //= 10 41. Check if a Number is Perfect: A perfect number is a
if original_num == reversed_num: positive integer that is equal to the sum of its positive divisors,
print(f"{original_num} is a palindrome.") excluding the number itself
else: # Check if a number is perfect
print(f"{original_num} is not a palindrome.") num = int(input("Enter a number: "))
sum_of_divisors = 0
38. Count Digits in a Number for i in range(1, num):
# Count digits in a number if num % i == 0:
num = int(input("Enter a number: ")) sum_of_divisors += i
count = 0 if sum_of_divisors == num:
while num > 0: print(f"{num} is a perfect number.")
count += 1 else:
num //= 10 print(f"{num} is not a perfect number.")
print(f"Number of digits: {count}")
42. Sum of First N Natural Numbers
39. Find the Highest Common Factor (HCF) # Sum of first N natural numbers
# Find HCF of two numbers n = int(input("Enter a number: "))
a = int(input("Enter first number: ")) sum_n = 0
b = int(input("Enter second number: ")) for i in range(1, n + 1):
while b: sum_n += i
a, b = b, a % b print(f"Sum of first {n} natural numbers: {sum_n}")
print(f"HCF is: {a}")
43. Sum of Squares of Digits 47. Inverted Right-Angled Triangle:
# Sum of squares of digits for i in range(5, 0, -1):
num = int(input("Enter a number: ")) print("* " * i)
sum_of_squares = 0 Output:
while num > 0: *****
digit = num % 10 ****
sum_of_squares += digit**2 ***
num //= 10 **
print(f"Sum of squares of digits: {sum_of_squares}") *
48. Pyramid Pattern:
44. Multiplication Table Program: rows = 5
number = int(input("Enter a number: ")) for i in range(1, rows + 1):
for i in range(1, 11): print(" " * (rows - i) + "* " * i)
print(number, "x", i, "=", number * i) Output:
*
45. Simple Star Square: **
for i in range(4): ***
print("* " * 4) ****
Output: *****
*****
***** 49. Diamond Pattern:
***** rows = 5
***** # Top half of the diamond
for i in range(1, rows + 1):
46. Right-Angled Triangle: print(" " * (rows - i) + "* " * i)
for i in range(1, 6): # Bottom half of the diamond
print("* " * i) for i in range(rows - 1, 0, -1):
print(" " * (rows - i) + "* " * i)
Output:
*
**
***
****
*****
Output: 52. Hollow Square:
* size = 5
** for i in range(size):
*** if i == 0 or i == size - 1:
**** print("* " * size)
***** else:
**** print("* " + " " * (size - 2) + "*")
*** Output:
** *****
* * *
50. Repeating Number Triangle: * *
for i in range(1, 6): * *
print((str(i) + " ") * i) *****
Output:
1 53. Checkerboard Pattern:
22 size = 5
333 for i in range(size):
4444 print((" * " if i % 2 == 0 else " * ") * size)
55555 Output:
*****
51. Alphabet Triangle: *****
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" *****
for i in range(5): *****
print(" ".join(alphabet[:i + 1]))
Output:
A
AB
ABC
ABCD $$$$$$$$$$$$$
ABCDE

You might also like