Python
Practice problems
1) Why a python program to write twinkle twinkle little star poem
# Program to print “Twinkle Twinkle Little Star” poem
Print(“Twinkle, twinkle, little star,”)
Print(“ How I wonder what you are!”)
Print(“ Up above the world so high,”)
Print(“ Like a diamond in the sky.”)
Print(“Twinkle, twinkle, little star,”)
Print(“ How I wonder what you are!”)
# Program with functions to reduce repetition
Def twinkle_verse():
Print(“Twinkle, twinkle, little star,”)
Print(“ How I wonder what you are!”)
Def sky_verse():
Print(“ Up above the world so high,”)
Print(“ Like a diamond in the sky.”)
# Calling functions
Twinkle_verse()
Sky_verse()
Twinkle_verse()
2) Use REPL and print the table of 5 using it
# Printing table of 5 in REPL
For I in range(1, 11): # Iterating from 1 to 10
Print(f”5 x {i} = {5 * i}”)
3) Write a python program to print the contents of a directory using OS module. Search
online for the function which does that.
Import os
# Specify the directory path
Directory_path = ‘/path/to/directory’
Try:
# Get the list of all files and directories
Entries = os.listdir(directory_path)
Print(f”Contents of ‘{directory_path}’:”)
For entry in entries:
Print(entry)
Except FileNotFoundError:
Print(f”The directory ‘{directory_path}’ does not exist.”)
Except PermissionError:
Print(f”Permission denied to access ‘{directory_path}’.”)
Except Exception as e:
Print(f”An error occurred: {e}”)
4) Label the program wrilten in Problem with Comments.
Import os # Importing the OS module to interact with the operating
system
# Specify the directory path (replace with the desired path)
Directory_path = ‘/path/to/directory’
Try:
# Get the list of all files and directories in the specified path
Entries = os.listdir(directory_path)
# Print the directory path for context
Print(f”Contents of ‘{directory_path}’:”)
# Loop through the entries and print each one
For entry in entries:
Print(entry)
# Handle the case where the directory does not exist
Except FileNotFoundError:
Print(f”The directory ‘{directory_path}’ does not exist.”)
# Handle the case where access to the directory is denied
Except PermissionError:
Print(f”Permission denied to access ‘{directory_path}’.”)
# Handle any other unexpected errors
Except Exception as e:
Print(f”An error occurred: {e}”)
5) Write a python program to add two numbers
# Program to add two numbers
# Input: Prompt the user to enter the first number
Num1 = float(input(“Enter the first number: “))
# Input: Prompt the user to enter the second number
Num2 = float(input(“Enter the second number: “))
# Calculate the sum of the two numbers
Sum_of_numbers = num1 + num2
# Output: Display the result
Print(f”The sum of {num1} and {num2} is {sum_of_numbers}.”)
6) Write a Python program provided by 2 to find remainder when number
is
# Program to add two numbers
# Input: Prompt the user to enter the first number
Num1 = float(input(“Enter the first number: “))
# Input: Prompt the user to enter the second number
Num2 = float(input(“Enter the second number: “))
# Calculate the sum of the two numbers
Sum_of_numbers = num1 + num2
# Output: Display the result
Print(f”The sum of {num1} and {num2} is {sum_of_numbers}.”)
7) Check the type of the variable assigned using input ( ) function
# Prompt the user to input a value
User_input = input(“Enter something: “)
# Check and display the type of the variable
Print(f”The type of the variable is: {type(user_input)}”)
8) Use Comparison operators to find out whether a given variable a is
greater than b’ or not Take a= 34 and b=80
# Assign values to variables
A = 34
B = 80
# Use comparison operator to check if a is greater than b
Is_a_greater = a > b
# Output the result
Print(f”Is a ({a}) greater than b ({b})? {is_a_greater}”)
9) Write a python program to find average of two numbers entered by the
user.
# Program to calculate the average of two numbers
# Input: Prompt the user to enter the first number
Num1 = float(input(“Enter the first number: “))
# Input: Prompt the user to enter the second number
Num2 = float(input(“Enter the second number: “))
# Calculate the average
Average = (num1 + num2) / 2
# Output: Display the result
Print(f”The average of {num1} and {num2} is {average}.”)
10) Write a python program to calculate square of a number entered
by the user.
# Program to calculate the square of a number
# Input: Prompt the user to enter a number
Num = float(input(“Enter a number: “))
# Calculate the square of the number
Square = num ** 2
# Output: Display the result
Print(f”The square of {num} is {square}.”)