Friday, September 19, 2025

Python Program Collection 01

 

 Python Program Collection 01



Input/Output

 

a) Write a program to ask input as “name” from the user and greet the user as he/she provides their name.

 

name = input("Enter your name: ")

print(f"Hello, {name}! Welcome!")

 

b) Write a program to input 3 sides of triangle and print area

 

import math

a = float(input("Enter the first side of the triangle: "))

b = float(input("Enter the second side of the triangle: "))

c = float(input("Enter the third side of the triangle: "))

s = (a + b + c) / 2

area = math.sqrt(s * (s - a) * (s - b) * (s - c))

print(f"The area of the triangle is: {area:.2f}")

 

c) Write a program to input a radius and find the area and circumference of circle

 

import math

radius = float(input("Enter the radius of the circle: "))

area = math.pi * radius ** 2

circumference = 2 * math.pi * radius

print(f"The area of the circle is: {area:.2f}")

print(f"The circumference of the circle is: {circumference:.2f}")

 

d) Write a program to input 3 digits as integers and calculate their sum and average.

 

num1 = int(input("Enter the first digit: "))

num2 = int(input("Enter the second digit: "))

num3 = int(input("Enter the third digit: "))

total = num1 + num2 + num3

average = total / 3

print(f"The sum of the digits is: {total}")

print(f"The average of the digits is: {average:.2f}")

 

 

e) Write a program to input a diameter and print area of circle and circumference of circle

 

import math

diameter = float(input("Enter the diameter of the circle: "))

radius = diameter / 2

area = math.pi * radius ** 2

circumference = math.pi * diameter

print(f"The area of the circle is: {area:.2f}")

print(f"The circumference of the circle is: {circumference:.2f}")

 

 

 

 

Constant value and Operators

 

a) Write a program to input a radius and find the area and circumference of the circle.

 

import math

radius = float(input("Enter the radius of the circle: "))

area = math.pi * radius ** 2

circumference = 2 * math.pi * radius

print(f"The area of the circle is: {area:.2f}")

print(f"The circumference of the circle is: {circumference:.2f}")

 

b) Write a program to show the use of arithmetic operators. (Perform add, subtract, multiply, divide, square and modulus between two numbers)

 

num1 = float(input("Enter the first number: "))

num2 = float(input("Enter the second number: "))

addition = num1 + num2

subtraction = num1 - num2

multiplication = num1 * num2

division = num1 / num2 if num2 != 0 else "undefined (division by zero)"

square_num1 = num1 ** 2

modulus = num1 % num2

print(f"Addition of {num1} and {num2} is: {addition}")

print(f"Subtraction of {num1} and {num2} is: {subtraction}")

print(f"Multiplication of {num1} and {num2} is: {multiplication}")

print(f"Division of {num1} by {num2} is: {division}")

print(f"The square of {num1} is: {square_num1}")

print(f"The modulus of {num1} and {num2} is: {modulus}")

 

c) Write a program to show the use of a comparison operator between two variables. (Use the following operators. ==, <=, >=, !=)

 

num1 = float(input("Enter the first number: "))

num2 = float(input("Enter the second number: "))

is_equal = num1 == num2

is_less_than_equal = num1 <= num2

is_greater_than_equal = num1 >= num2

is_not_equal = num1 != num2

print(f"Is {num1} equal to {num2}? {is_equal}")

print(f"Is {num1} less than or equal to {num2}? {is_less_than_equal}")

print(f"Is {num1} greater than or equal to {num2}? {is_greater_than_equal}")

print(f"Is {num1} not equal to {num2}? {is_not_equal}")

 

If…..else Statement

 

a) Write a program using the if statement to check whether the given number is positive.

num = float(input("Enter a number: "))

if num > 0:

    print(f"{num} is a positive number.")

else:

    print(f"{num} is not a positive number.")

b) Write a program that takes people’s age as input and checks whether they are eligible to vote. (Voting age is 18 and above)

 

age = int(input("Enter your age: "))

if age >= 18:

    print("You are eligible to vote.")

else:

    print("You are not eligible to vote.")

 

c) Write a program to check whether the given number is even or odd using an if-else statement.

 

num = int(input("Enter a number: "))

if num % 2 == 0:

    print(f"{num} is even.")

else:

    print(f"{num} is odd.")

 

d) Write a program to input two numbers and find the smallest number.

 

num1 = float(input("Enter the first number: "))

num2 = float(input("Enter the second number: "))

if num1 < num2:

    print(f"{num1} is the smallest number.")

else:

    print(f"{num2} is the smallest number.")

 

e) Write a program that takes students’ marks as an input and checks whether the student passed or failed. (40 or greater is pass marks)

 

marks = float(input("Enter your marks: "))

if marks >= 40:

    print("You passed.")

else:

    print("You failed.")

 

f) Write a program that takes 3 numbers as inputs and displays the smallest number.

 

num1 = float(input("Enter the first number: "))

num2 = float(input("Enter the second number: "))

num3 = float(input("Enter the third number: "))

if num1 <= num2 and num1 <= num3:

    print(f"{num1} is the smallest number.")

elif num2 <= num1 and num2 <= num3:

    print(f"{num2} is the smallest number.")

else:

    print(f"{num3} is the smallest number.")

 

g) Make a program like a traffic light using an if-elif-else statement. Ask input from the user on providing color. (green=go, red=stop and orange=be ready, any other color=invalid color)

 

color = input("Enter the traffic light color (green, red, orange): ").lower()

if color == "green":

    print("Go!")

elif color == "red":

    print("Stop!")

elif color == "orange":

    print("Be ready!")

else:

    print("Invalid color!")

 

h) Write a program to input practical and theory marks of Computer and check pass or fail. You can also validate whether a user has entered valid marks or not. (valid marks should be checked for both theory and practical)

 

practical_marks = float(input("Enter your practical marks: "))

theory_marks = float(input("Enter your theory marks: "))

if (0 <= practical_marks <= 100) and (0 <= theory_marks <= 100):

    # Check if the student passed

    if practical_marks >= 40 and theory_marks >= 40:

        print("You passed!")

    else:

        print("You failed!")

else:

    print("Invalid marks entered. Marks should be between 0 and 100.")

 

For loop

a) Write a program to print numbers from 1-15.

 

for num in range(1, 16):

    print(num)

 

b) Write a program to print an input name 20 times.

 

name = input("Enter your name: ")

for _ in range(20):

    print(name)

c) Write a program to print the squares of numbers from 1 to 5 using a for loop.

 

for num in range(1, 6):

    print(f"The square of {num} is {num**2}")

d) Write a program to print first 20 odd numbers

 

for num in range(1, 40, 2):  # Start at 1, go up to 40 (not inclusive), step by 2

    print(num)

 

e) Write a program that takes an integer as input and prints the multiplication table for that number.

 

number = int(input("Enter a number: "))

for i in range(1, 11):

    print(f"{number} x {i} = {number * i}")

 

While loop

a) Write a program to print numbers from 1-20.

 

num = 1

while num <= 20:

    print(num)

    num += 1  # Increment the number

 

b) Write a program to print first 30 even numbers.

 

num = 2

count = 0

while count < 30:

    print(num)

    num += 2 

    count += 1 

 

c) Write a program to calculate the factorial of a given number.

number = int(input("Enter a number to find its factorial: "))

factorial = 1

while number > 0:

    factorial *= number

    number -= 1  # Decrease the number

print(f"The factorial is: {factorial}")

 

d) Write a program to print the sum of the first 20 odd numbers.

num = 1

sum_of_odds = 0

count = 0

while count < 20:

    sum_of_odds += num

    num += 2  # Go to the next odd number

    count += 1  # Increase the count

print(f"The sum of the first 20 odd numbers is: {sum_of_odds}")

e) Write a program to input multi digits numbers and display the sum of the digits.

number = int(input("Enter a multi-digit number: "))

sum_of_digits = 0

while number > 0:

    digit = number % 10  # Get the last digit

    sum_of_digits += digit  # Add the digit to the sum

    number = number // 10  # Remove the last digit

print(f"The sum of the digits is: {sum_of_digits}")

 

 

Lists

1. Write a Python program to create a list of 5 integers and print the list.

 

numbers = [10, 20, 30, 40, 50]

print("List of integers:", numbers)

 

2. Write a Python program to print the second and fourth elements of a list.

 

numbers = [10, 20, 30, 40, 50]

print("Second element:", numbers[1])

print("Fourth element:", numbers[3])

 

3. Write a Python program to append a new element to the end of a list and print the updated list.

 

numbers = [10, 20, 30, 40, 50]

numbers.append(60)

print("Updated list:", numbers)

 

4. Write a Python program to print the first three elements of a list using slicing.

 

numbers = [10, 20, 30, 40, 50]

print("First three elements:", numbers[:3])

 

5. Write a Python program to print the length of a list.

 

numbers = [10, 20, 30, 40, 50]

print("Length of the list:", len(numbers))

 

6. Write a Python program to iterate through a list and print each element.

 

numbers = [10, 20, 30, 40, 50]

for num in numbers:

    print(num)

 

 

7. Write a Python program to create a list of squares of numbers from 1 to 10 using list comprehension.

 

squares = [x**2 for x in range(1, 11)]

print("List of squares from 1 to 10:", squares)

 

8. Write a Python program to remove the third element from a list and print the updated list.

 

numbers = [10, 20, 30, 40, 50]

numbers.pop(2)

print("Updated list after removing the third element:", numbers)

 

 

Dictionaries

1. Write a Python program to create a dictionary with 3 key-value pairs and print the dictionary.

 

my_dict = {"name": "Alice", "age": 25, "city": "New York"}

print("Dictionary:", my_dict)

 

 

2. Write a Python program to print the value associated with a specific key in a dictionary.

 

my_dict = {"name": "Alice", "age": 25, "city": "New York"}

key = "age"

print(f"The value associated with '{key}' is: {my_dict[key]}")

 

3. Write a Python program to add a new key-value pair to a dictionary and print the updated dictionary.

 

my_dict = {"name": "Alice", "age": 25, "city": "New York"}

my_dict["email"] = "alice@example.com"

print("Updated Dictionary:", my_dict)

 

4. Write a Python program to remove a key-value pair from a dictionary using the del statement and print the updated dictionary.

 

my_dict = {"name": "Alice", "age": 25, "city": "New York"}

del my_dict["age"]

print("Updated Dictionary after deletion:", my_dict)

 

5. Write a Python program to iterate through a dictionary and print all the keys.

 

my_dict = {"name": "Alice", "age": 25, "city": "New York"}

print("Keys in the dictionary:")

for key in my_dict:

    print(key)

 

6. Write a Python program to create a dictionary where the keys are numbers from 1 to 5 and the values are their squares using dictionary comprehension.

 

squares_dict = {x: x**2 for x in range(1, 6)}

print("Dictionary of squares:", squares_dict)

 

7. Write a Python program to check if a specific key exists in a dictionary.

 

my_dict = {"name": "Alice", "age": 25, "city": "New York"}

key_to_check = "age"

if key_to_check in my_dict:

    print(f"Key '{key_to_check}' exists in the dictionary.")

else:

    print(f"Key '{key_to_check}' does not exist in the dictionary.")

 

8. Write a Python program to merge two dictionaries and print the resulting dictionary.

 

dict1 = {"name": "Alice", "age": 25}

dict2 = {"city": "New York", "email": "alice@example.com"}

merged_dict = {**dict1, **dict2}

print("Merged Dictionary:", merged_dict)

No comments:

Post a Comment