Variables, Data Types & Input/Output — Practice Questions
50 questions. Try each one yourself before checking the answer.
Write a program that prints the exact text Hello, World! to the console.
Write a program that prints Python, is, and fun on the same line separated by a - instead of the default space, using the sep argument of print().
Create a variable age and assign it the value 25. Print the variable.
Create two variables, first_name and last_name, and print a greeting like Hello, John Doe! by joining them with +.
Create four variables — one each of type int, float, str, and bool — and print the type of each using type().
The following variable names break Python's naming rules or conventions: 2cool, my-variable, class. Rewrite each as a valid, PEP8-style (snake_case) variable name, assign each a value, and print them.
Write a short program that calculates the area of a square with side 5. Add a single-line comment explaining the formula, and a multi-line comment (using consecutive # lines) describing what the whole program does.
Write a program that asks the user to enter their name using input() and prints Welcome, <name>!.
Write a program that reads a value using input() and prints its type. If the user types 25, what type does Python report, and why?
Create a variable city with your city's name and use an f-string to print I live in <city>.
Create variables name, age, and country. Print a single sentence containing all three using one f-string.
Write a program that asks the user to enter a number, converts it to an int using int(), adds 10 to it, and prints the result.
Write a program that asks the user to enter a price as a decimal number, converts it to float, and prints the price multiplied by 2.
Print the result of bool() applied to each of these values: 0, 1, -5, "", "hello", 0.0. What pattern do you notice about which values come out False?
Create an int variable and a float variable, add them together, and print both the result and its type(). What type does Python choose when mixing int and float?
Create an int variable score, convert it to a string with str(), and use + to build the message "Your score: 90".
Using a single line of code, assign the values 1, 2, and 3 to three variables a, b, and c respectively. Print all three.
Create two variables x = 5 and y = 10. Swap their values without using a temporary/third variable, then print both.
Create a variable pi = 3.14159265. Print it formatted to exactly 2 decimal places using an f-string.
Print the number 42 right-aligned inside a field of width 10, using an f-string format specifier.
Write a simple interest calculator. Ask the user for principal, rate (in %), and time (in years) as floats, compute interest = principal * rate * time / 100, and print it formatted to 2 decimal places.
Write a program that converts a temperature from Celsius to Fahrenheit using F = C * 9/5 + 32. Take Celsius as input and print the Fahrenheit value formatted to 1 decimal place.
Write a program that takes the length and width of a rectangle as input and prints both its area and perimeter, each on its own line, formatted to 2 decimal places.
Write a program that asks for the price of one item and the quantity purchased, then prints the total bill formatted as currency (e.g. $49.50).
Ask the user for three exam scores (as floats) and print their average, formatted to 1 decimal place.
The following code crashes with a TypeError:
age = 20
message = "I am " + age + " years old"
print(message)Fix it so it runs correctly.
The following code is supposed to print My name is Sam, but instead prints My name is {name} literally. Find and fix the bug.
name = "Sam"
print("My name is {name}")The following code raises a NameError. Find and fix the typo.
username = "coder123"
print(User_name)The following code crashes with a TypeError when adding 10 to user input. Find and fix the bug.
number = input("Enter a number: ")
result = number + 10
print(result)Write a program that prints a two-column "receipt" line for one item: the item name left-aligned in a field of width 15, and its price right-aligned in a field of width 10 with 2 decimal places. Example output: Notebook 45.50.
Write a program that takes an integer and prints its binary, octal, and hexadecimal representations using f-string format specifiers (b, o, x) — without using bin(), oct(), or hex().
Write a program that takes a decimal fraction like 0.4567 and prints it as a percentage with 1 decimal place (e.g. 45.7%) using an f-string format specifier — without manually multiplying by 100.
Write a program that prints the number 2500000 with comma thousands-separators (e.g. 2,500,000) using an f-string format specifier.
Write a program that prints a profit value with an explicit + sign when positive and - when negative (e.g. +250.00 or -120.50), formatted to 2 decimal places, using an f-string format specifier.
int("3.9") raises a ValueError because int() can't parse a decimal point directly. Demonstrate the fix: convert the string "3.9" to an int by first converting it to a float, then to an int, and print the result.
Print type(True) and isinstance(True, int). Explain in a comment why the result of isinstance(True, int) might be surprising.
Given a = 17 and b = 5, print the results of a / b, a // b, and a % b, each right-aligned in a field of width 10, with a label for each.
Print round(2.5) and round(3.5). Both look like they should round up, but one doesn't — print both and add a comment explaining Python's "round half to even" (banker's rounding) behavior.
The following snippet has three separate bugs (a mismatched quote, a missing type conversion, and a typo'd variable name). Find and fix all three.
Price = input("Enter price: )
total = Price * 3
print("Total: " + total)Write a program that asks the user for a single word and prints it centered inside a field of width 20, padded with * characters, along with its length (using len()) and type on separate lines.
Build a "Personal Profile Card". Ask the user for their name, age, city, and email, then print a bordered card like:
==============================
Name : Asha
Age : 22
City : Pune
Email: asha@example.com
==============================
Build a tip calculator. Ask for the bill_amount and tip_percent, compute the tip and the final total, and print both formatted as currency to 2 decimal places.
Build a length unit converter. Ask the user for a length in meters and print its equivalent in centimeters, feet, and inches, each formatted to 2 decimal places. (1 m = 100 cm = 3.28084 ft = 39.3701 in)
Build a "Type Conversion Report". Ask the user for one value with input(), then print a table showing that value converted to int (via float first, to support decimals), float, bool, and str, along with the type() of each converted result.
Build a simple invoice generator for one item. Ask for item_name, quantity, and unit_price, then print a formatted invoice with aligned columns and the total cost:
Item Qty Unit Price Total
Notebook 3 45.50 136.50
Python is "dynamically typed" — a variable's type can change at runtime. Demonstrate this: assign an int to a variable, print its value and type, then reassign the same variable to a str, then to a float, printing the value and type each time.
Given a = 5 (an int) and b = "5" (a str), explain and demonstrate why a == b is False but a == int(b) is True.
Given value = 8342.567, print it formatted four different ways using only f-string format specifiers: as currency with commas ($8,342.57), as a percentage, in scientific notation, and rounded to the nearest whole number.
Simulate an ATM withdrawal receipt. Ask for the account holder's name, current balance, and withdrawal amount. Compute the new balance and print a formatted receipt showing the account holder, amount withdrawn, and new balance, all currency values aligned and shown to 2 decimal places.
Build a "Variable Inspector". Ask the user for one value with input(). Print a formatted report showing: the raw string value, its length (len()), the value converted to float, the value converted to bool, and the original type versus the float's type — all clearly labeled and aligned. This should be the single most complete demonstration of everything in this module: variables, I/O, all four basic types, type conversion, and formatted output.
Still stuck on something?
Book a free 1-on-1 session and we'll work through it together.
Book a Free Session