Input(), Print(), and Operators
Understanding how to take input, details 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!
Pyground
Play with Python!
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 print them separated by a custom separator.
Show Code
num1 = input("Enter the first number: ") num2 = input("Enter the second number: ") separator = input("Enter a custom separator: ") print(num1, num2, sep=separator)
-
Print a message on one line and another message on the same line using the
end
argument.Show Code
print("This is the first message.", end=" ") print("This is the second message.")
-
Input marks in two subjects and calculate their average.
Show Code
marks1 = int(input("Enter marks for subject 1: ")) marks2 = int(input("Enter marks for subject 2: ")) average = (marks1 + marks2) / 2 print("Average Marks:", average)
-
Input the length and breadth of a rectangle and calculate its area.
Show Code
length = float(input("Enter the length of the rectangle: ")) breadth = float(input("Enter the breadth of the rectangle: ")) area = length * breadth print("Area of the rectangle:", area)
-
Input the cost of an item and the quantity purchased, then calculate the total cost.
Show Code
cost = float(input("Enter the cost of one item: ")) quantity = int(input("Enter the quantity purchased: ")) total_cost = cost * quantity print("Total Cost:", total_cost)
-
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)
-
Input the base and height of a triangle and calculate its area.
Show Code
base = float(input("Enter the base of the triangle: ")) height = float(input("Enter the height of the triangle: ")) area = 0.5 * base * height print("Area of the triangle:", area)