input_output_variable_problems
input_output_variable_problems
Python Basics
Python is easy to read because it uses plain English words for commands (like print or input).
It uses indentation (spaces) to structure the code instead of symbols like {} or ;.
2. Variables
A variable is like a box where you store data (like numbers, text, or lists).
Example:
3. Printing
4. Input
name = input("What is your name? ") # Asks the user for their name
print("Hello, " + name + "!") # Greets the user
5. Data Types
age = 30 # int
height = 5.9 # float
6. If-Else (Decisions)
Python can make decisions based on conditions using if, elif, and else.
Example:
age = 18
if age >= 18:
print("You are an adult!")
else:
print("You are not an adult.")
7. Loops
count = 0
while count < 3:
print(count)
count += 1 # Prints 0, 1, 2
8. Functions
def greet(name):
print(f"Hello, {name}!")
greet("Alice") # Prints: Hello, Alice!
9. Example Program
Question:
Write a program that asks for the user's name and prints a greeting message using their name.
Example:
Question:
Write a program that takes two numbers as input, adds them, and prints the result.
Example:
Question:
Write a program that asks the user for the length and width of a rectangle, then calculates and prints the area.
Example:
Enter length: 4
Enter width: 3
The area of the rectangle is: 12
Question:
Write a program that asks for the user's birth year and calculates their age, assuming the current year is
2025.
Example:
Question:
Write a program that takes a temperature in Celsius from the user and converts it to Fahrenheit.
Formula:
F = (C × 9/5) + 32
Example:
Question:
Write a program that takes two numbers as input, multiplies them, and prints the product.
Example:
Question:
Write a program to calculate the simple interest. Ask the user for the principal, rate of interest, and time,
then calculate and print the interest.
Formula:
Interest = (Principal × Rate × Time) / 100
Example:
Question:
Write a program that takes a number of minutes as input and converts it into hours and remaining minutes.
Example:
Question:
Write a program that takes an integer as input and determines whether the number is even or odd.
Example:
Enter a number: 7
7 is odd.
Problem 10: Discount Calculation
Question:
Write a program that takes the original price of an item and a discount percentage, then calculates and prints
the final price after applying the discount.
Formula:
Final Price = Original Price - (Original Price × Discount / 100)
Example:
SOLUTIONS