Input(), Print(), and Operators
Understanding how to take input, display output, and use basic operators is essential for programming. These are the building blocks to create dynamic and interactive programs. Here are some beginner-friendly exercises to get you started.
Practice Problems
Problems Index
- Input marks in 3 subjects and find the total and percentage.
- Input name, hobby, and age, then print them as a paragraph.
- Take a string and repeat it
n
times (using*
). - Input two numbers and print them separated by a custom separator.
- Print a message on one line and another message on the same line using the
end
argument. - Input marks in two subjects and calculate their average.
- Input the length and breadth of a rectangle and calculate its area.
- Input the cost of an item and the quantity purchased, then calculate the total cost.
- Input two numbers and swap their values using a temporary variable.
- Input the base and height of a triangle and calculate its area.
Try It Yourself
Now that you’ve reviewed the problems, practice them below using the code runner!
Example 1: Calculate Total and Percentage
Pyground
Input marks in 3 subjects and find the total and percentage.
Output:
Example 2: Input Name, Hobby, and Age
Pyground
Input name, hobby, and age, then print them as a paragraph.
Output:
Example 3: Repeat String n
Times
Pyground
Take a string and repeat it `n` times.
Output:
Example 4: Swap Two Numbers
Pyground
Input two numbers and swap their values using a temporary variable.
Output:
Solutions
Solutions
-
Input marks in 3 subjects and find the total and percentage.
Show Code
marks1 = int(input("Enter marks for subject 1: ")) marks2 = int(input("Enter marks for subject 2: ")) marks3 = int(input("Enter marks for subject 3: ")) total = marks1 + marks2 + marks3 percentage = (total / 300) * 100 print("Total Marks:", total) print("Percentage:", percentage)
-
Input name, hobby, and age, then print them as a paragraph.
Show Code
name = input("Enter your name: ") hobby = input("Enter your hobby: ") age = input("Enter your age: ") print("Hi, my name is", name + ".", "I am", age, "years old and I love", hobby + ".")
-
Take a string and repeat it
n
times (using*
).Show Code
text = input("Enter a string: ") n = int(input("How many times to repeat? ")) print(text * n)
-
Input two numbers and swap their values using a temporary variable.
Show Code
num1 = input("Enter the first number: ") num2 = input("Enter the second number: ") temp = num1 num1 = num2 num2 = temp print("After swapping: First number =", num1, "Second number =", num2)