Skip to content
Laviyx Learn

Try

Python Operators and Input Practice

Practise arithmetic operators, operator precedence, type conversion and reading user input with input().

Beginner8 problemsUpdated

Review first: Operators, Type Conversion, User Input, Data Types

  1. 1.Split the bill

    Beginner

    A dinner costs 1850 rupees and 4 friends split it equally. Print each person's share with exactly two decimal places, like Each person pays 462.50.

    Show hint

    / always gives a float. Format it with an f-string: f"{value:.2f}".

    Show solution
    Python
    total = 1850
    people = 4
    
    share = total / people
    
    print(f"Each person pays {share:.2f}")
    Output
    Each person pays 462.50
  2. 2.Minutes to hours and minutes

    Beginner

    A film lasts 135 minutes. Print it as 2 h 15 min.

    Show hint

    // gives the whole hours and % gives the minutes left over.

    Show solution
    Python
    minutes = 135
    
    hours = minutes // 60
    remaining = minutes % 60
    
    print(f"{hours} h {remaining} min")
    Output
    2 h 15 min

    divmod(135, 60) returns both values at once: hours, remaining = divmod(minutes, 60).

  3. 3.Predict the results

    Beginner

    Without running it, work out what this prints. Then check your answer.

    Python
    print(2 + 3 * 4)
    print((2 + 3) * 4)
    print(2 ** 3 ** 2)
    print(-3 ** 2)
    print(7 // 2, -7 // 2)
    Show hint

    * runs before +. ** runs right to left and before a leading minus sign. // rounds down, towards minus infinity.

    Show solution
    Python
    print(2 + 3 * 4)
    print((2 + 3) * 4)
    print(2 ** 3 ** 2)
    print(-3 ** 2)
    print(7 // 2, -7 // 2)
    Output
    14
    20
    512
    -9
    3 -4
    • 2 ** 3 ** 2 is 2 ** (3 ** 2), which is 2 ** 9 = 512, not 8 ** 2.
    • -3 ** 2 is -(3 ** 2) = -9. Write (-3) ** 2 to get 9.
    • -7 // 2 is -4, because -3.5 rounded down is -4.
  4. 4.Strings that look like numbers

    Beginner

    Given a = "42" and b = "8", print what a + b gives, then convert both to integers and print their real sum.

    Show hint

    + joins strings. int() converts a string of digits to a number.

    Show solution
    Python
    a = "42"
    b = "8"
    
    print(a + b)
    print(int(a) + int(b))
    Output
    428
    50

    Values read with input() or from a file are strings, so this mistake is very common. Convert them before doing maths.

  5. 5.Age next year

    Beginner

    Ask the user for their name and age, then print a message like Hi Asha, next year you will be 22. In the example run, the user types Asha and 21.

    Show hint

    input() always returns a string. Convert the age with int() before adding 1.

    Show solution
    Python
    name = input("Your name: ")
    age = int(input("Your age: "))
    
    print(f"Hi {name}, next year you will be {age + 1}")
    Output
    Your name: Asha
    Your age: 21
    Hi Asha, next year you will be 22
  6. 6.Area of a rectangle

    Beginner

    Ask for the length and width of a room in metres (they may have decimals), then print the area rounded to 2 decimal places. In the example run, the user types 4.5 and 3.2.

    Show hint

    Use float() rather than int(), because int("4.5") raises a ValueError.

    Show solution
    Python
    length = float(input("Length (m): "))
    width = float(input("Width (m): "))
    
    area = length * width
    
    print(f"Area: {area:.2f} square metres")
    Output
    Length (m): 4.5
    Width (m): 3.2
    Area: 14.40 square metres
  7. 7.Price with tax

    Beginner

    An item costs 499 before tax, and the tax rate is 18%. Print the tax and the final price, both with two decimals.

    Show hint

    The tax is price * 18 / 100, and the final price is price + tax.

    Show solution
    Python
    price = 499
    rate = 18
    
    tax = price * rate / 100
    final = price + tax
    
    print(f"Tax: {tax:.2f}")
    print(f"Final price: {final:.2f}")
    Output
    Tax: 89.82
    Final price: 588.82
  8. 8.What type is it?

    Beginner

    Print the type of each value 10, 10.0, "10", True and None, then print int(True) and bool("").

    Show hint

    type(value) returns the type. True behaves like 1, and an empty string counts as False.

    Show solution
    Python
    for value in [10, 10.0, "10", True, None]:
        print(repr(value), type(value))
    
    print(int(True))
    print(bool(""))
    Output
    10 <class 'int'>
    10.0 <class 'float'>
    '10' <class 'str'>
    True <class 'bool'>
    None <class 'NoneType'>
    1
    False

    repr() shows strings with quotes, so you can see that "10" is text. Empty values ("", 0, [], None) are all falsy: they count as False in an if.