Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25
Python Programs
• Simple Input and Output:
• # Program 1 • name = input("Enter your name: ") • print("Hello, " + name + "!") • # Program 2 • num1 = int(input("Enter the first number: ")) • num2 = int(input("Enter the second number: ")) • result = num1 + num2 • print("The sum is:", result) • # Program 3 • num1 = float(input("Enter the first number: ")) • num2 = float(input("Enter the second number: ")) • result = num1 * num2 • print("The product is:", result) • # Program 4 • text = input("Enter a text: ") • repeat_count = int(input("Enter the number of times to repeat: ")) • output = text * repeat_count • print("Output:", output) • # Program 5 • number = int(input("Enter a number: ")) • if number % 2 == 0: • print(number, "is even.") • else: • print(number, "is odd.") Arithmetic operator Program • # Arithmetic Operators Program
• age = int(input("Enter your age: ")) • has_license = input("Do you have a driving license? (yes/no): ".lower() == "yes" • # Check if the person is eligible to drive • eligible_to_drive = age >= 18 and has_license
• # Display the result
• if eligible_to_drive: • print("You are eligible to drive.") • else: • print("You are not eligible to drive.") • # Logical AND Example
• # Get input from the user
• age = int(input("Enter your age: ")) • is_student = input("Are you a student? (yes/no): ").lower() == "yes"
• # Check if the person is both under 18 and a student
• eligible_for_discount = age < 18 and is_student • # Display the result • if eligible_for_discount: • print("You are eligible for a discount!") • else: • print("You are not eligible for a discount.") • # Logical OR Example
• # Get input from the user
• temperature = float(input("Enter the temperature in Celsius: "))
• # Check if it's a hot day (above 30 degrees) or a cold day (below 0
degrees) • is_hot_day = temperature > 30 • is_cold_day = temperature < 0 • # Use logical OR to check if it's either a hot or a cold day • extreme_weather = is_hot_day or is_cold_day • # Display the result • if extreme_weather: • print("Extreme weather alert!") • else: • print("The weather is moderate.") • # Logical NOT Example
• # Get input from the user
• age = int(input("Enter your age: "))
• # Check if the person is not a teenager
• is_not_teenager = not (13 <= age <= 19) • # Display the result • if is_not_teenager: • print("You are not a teenager.") • else: • print("You are a teenager.") • # Logical OR Example
• # Get input from the user
• age = int(input("Enter your age: ")) • is_student = input("Are you a student? (yes/no): ").lower() == "yes"
• # Check if the person is either a senior (60 or older) or a student
• eligible_for_discount = age >= 60 or is_student • # Display the result • if eligible_for_discount: • print("You are eligible for a discount!") • else: • print("You are not eligible for a discount.") • Question 4: Check if a person is eligible to vote. • # Question 4 Solution
• # Taking input from the user
• age = int(input("Enter your age: "))
• # Checking eligibility to vote
• if age >= 18: • print("You are eligible to vote.") • else: • print("You are not eligible to vote yet.") • Question 1: • Write a Python program to print numbers from 1 to 10 using a while loop. • num = 1 • while num <= 10: • print(num) • num += 1 • Write a Python program to calculate the factorial of a given number using a while loop. • num = int(input("Enter a number: ")) • factorial = 1 • while num > 0: • factorial *= num • num -= 1 • print(f"The factorial is: {factorial}") • Write a Python program to find the sum of natural numbers up to a given number using a while loop. • n = int(input("Enter a number: ")) • sum_result = 0 • while n > 0: • sum_result += n • n -= 1 • print(f"The sum of natural numbers is: {sum_result}") • Question 4: • Write a Python program to check if a given number is a prime number using a while loop. • num = int(input("Enter a number: ")) • is_prime = True • i=2 • while i <= num // 2: • if num % i == 0: • is_prime = False • break • i += 1 • if is_prime: • print(f"{num} is a prime number.") • else: • print(f"{num} is not a prime number.") • Write a Python program to reverse a number using a while loop. • num = int(input("Enter a number: ")) • reversed_num = 0 • while num > 0: • digit = num % 10 • reversed_num = reversed_num * 10 + digit • num = num // 10 • print(f"The reversed number is: {reversed_num}") # Nested if example
• # Taking input from the user
• num = int(input("Enter a number: "))
• # Checking if the number is positive, negative, or zero
• if num >= 0: • if num == 0: • print("The entered number is zero.") • else: • print("The entered number is positive.") • else: • print("The entered number is negative.")