Question 1: Variables and Data Types
Problem: Write a Python program that:
1. Accepts a string, an integer, a float, and a boolean from the user.
2. Initializes variables for each type, and prints them out.
3. Convert the string to uppercase and print it.
4. Check if the integer is even or odd and print the result.
5. Multiply the float by 2 and print the result.
Example Input: Enter a string: python Enter an integer: 25 Enter a float: 3.14 Enter a boolean
(True/False): True
Example Output: Uppercase String: PYTHON The number 25 is Odd Doubled float: 6.28
Solution 01:
# Take user input in string format
input_string = input("Enter a string: ")
# Convert input_string into upper case by using upper() function
uppercase_string = input_string.upper()
# Print the ouput
print("Uppercase String:", uppercase_string)
# Take user input in integer format
input_integer = (int(input("Enter an integer: ")))
# Check integer number is even or odd by using if-else condition and
print even or odd
if input_integer % 2 == 0:
print(f"The number {input_integer} is Even")
else:
print(f"The number {input_integer} is Odd")
# Take user input in float format
input_float = (float(input("Enter a float: ")))
# Double input float by multiplying 2
double_float = input_float * 2
# Print the double float
print("Doubled float:", double_float)
# Take user input in boolean format
input_boolean = input("Enter a boolean (True/False):
").strip().lower()
# Coverts input_boolean to True only if "true"
boolean_value = input_boolean == "true"
# Print the boolean value
print("Boolean Value:", boolean_value)
Boolean Value: False
Question 2: Operators
Problem: Write a Python program that:
1. Accepts two numbers as input from the user.
2. Performs and prints the result of all the arithmetic operations (addition, subtraction,
multiplication, division, modulus, flow division) between these two numbers.
3. Use comparison operators to check if the first number is greater than the second, and if
they are equal.
4. Use logical operators to combine two conditions (e.g., the first number is greater than
the second, and the second number is less than 10).
Example Input: Enter the first number: 10 Enter the second number: 3
Example Output: Addition: 13 Subtraction: 7 Multiplication: 30 Division: 3.3333333333333335
Modulus: 1 Flow Division: 3 First number is greater than second: True First number is equal to
second: False Both conditions are true: True
Solution 02:
# Take user input first number
first_number = int(input("Enter first number: "))
# Take user input second number
second_number = int(input("Enter second number: "))
#Add first and second number by using addition opearator
addition = first_number + second_number
#Subtract first and second number by using subration operator
subtraction = first_number - second_number
#Multiply first and second number by using multiplication operator
multiplication = first_number * second_number
#Divsion first and second number by using division operator
division = first_number / second_number
#Modulus first and second number by using modulus operator
modulus = first_number % second_number
#Floor first and second number by using floor operator
floor_division = first_number // second_number
# Check condtion first number is greater than second number
greater = first_number > second_number
# Check condtion first number is equal to second number
equal= first_number == second_number
# Using or operator one in both conditions one contion is true it
print true
both_true = greater and equal
# Display arithhmetic operation and condtions output
print("Addition:", addition)
print("Subtraction:", subtraction)
print("Multiplication:", multiplication)
print("Division:",division)
print("Modulus:", modulus)
print("Floor Division:",floor_division)
print("First number is greater than second number:", greater)
print("First number is equal to second number:", equal)
print("Both conditions are true:", both_true)
Addition: 30
Subtraction: 10
Multilication: 200
Division: 2.0
Modulus: 0
Floor: 2
First number is greater than second number: True
First number is equal to second number: False
Both conditions are true: True
Question 3: Loops
Problem: Write a Python program that:
1. Accepts a list of integers from the user.
2. Loops through the list and prints out each number.
3. If a number is greater than 10, skip it using the continue statement.
4. Stop the loop if the number is 20 using the break statement.
5. After the loop ends, print a message that the loop ended naturally.
Example Input: Enter a list of numbers separated by spaces: 5 10 12 15 20 8
Example Output: 5 10 Skipping 12 15 Breaking at 20 Loop ended naturally
Solution 03:
# Take user input list of numbers
int_list = list(map(int, input("Enter a list of numbers separated by
spaces: ").split()))
# Looping the list and print each number present in list
for number in int_list:
if number > 10 and number != 20: # check condition
print(f"Skipping 10 {number}")
continue # skip the loop when number greater than 10
elif number == 20: # check condition
print(f"Breaking at {number}")
break # Exits the loop when 20 is found
else:
print(number)
# print after loops end
print("The loops ended naturally")
5
10
Skipping 10 12
Skipping 10 15
Breaking at 20
The loops ended naturally