Variables, Data Types & Input/Output: Practice Questions
100 questions. Try each one yourself before checking the answer.
Short on time? Filter by Must Do for the 25 questions that cover this topic on their own.
Print the exact text Hello, World! to the screen.
Print your own name, then on the next line print your favourite colour. Use two separate print() statements.
Print the three words Python, is, and fun using a single print() with commas between them. Notice that Python puts a space between each one for you.
Print a blank line between the words Top and Bottom, using an empty print().
Print Python, is, and fun joined by a dash instead of a space, like Python-is-fun. Use the sep setting of print().
Normally each print() starts a new line. Print Hello and World from two separate print() statements but make them appear on the same line, using the end setting.
Print Line one and Line two on two separate lines using one print() and the newline escape sequence \n.
Print Name and Age separated by a tab, using the escape sequence \t.
Print the sentence She said "hello" to me. — including the double quotes around hello. Do it two ways: once with single quotes around the whole string, and once using \".
Print a Windows-style file path exactly as C:\Users\Asha. A single backslash has a special meaning, so you need \\ for each one.
Print a three-line poem using one print() and a triple-quoted string ("""). The line breaks you type inside the quotes are kept exactly as written.
Print a line of exactly 20 equals signs (====================) without typing them all out. Multiply the string by a number.
Print Good morning! by joining the two pieces "Good " and "morning!" with the + sign.
Create a variable called age, store the number 25 in it, and print it.
Create a variable score holding 10. Print it. Then store 20 in the same variable and print it again — a variable keeps only its most recent value.
Create a variable city holding the text "Mumbai" and print it. Text values must be wrapped in quotes; numbers must not.
Create first_name and last_name, then print a greeting like Hello, John Doe! by joining the pieces with +. Remember to include the space between the names.
Write a program that prints 2 + 3 = 5, with a comment above the line explaining what it does. Comments start with # and Python ignores them completely.
Store 5 in a variable side and print it, with an inline comment at the end of the assignment line saying what side means.
Write a three-line block comment at the top of a program describing what it does, then print Ready. Each line of a block comment needs its own #.
Store the whole number 7 in a variable and print both the value and its type using type(). Whole numbers are called int.
Store the decimal number 19.99 in a variable and print its type. Numbers with a decimal point are called float.
Store the text "Alice" in a variable and print its type. Text is called str, short for string.
Store True in a variable and print its type. True and False are the only two bool values, and both start with a capital letter.
Store None in a variable called result and print both the value and its type. None is Python's way of saying "no value yet".
These three names are all illegal in Python: 2cool, my-variable, class. Rewrite each as a valid snake_case name, give it a value, and print all three.
Create a variable total_marks holding 450. Then try printing Total_Marks and explain in a comment why Python complains.
Python has no real constants, but by convention a value that should never change is named in ALL_CAPS. Create PI = 3.14159 and MAX_USERS = 100 and print both.
Assign 1, 2 and 3 to the variables a, b and c on a single line, then print all three.
Set three variables x, y and z all to 0 in one line, using chained assignment, and print them.
Create x = 5 and y = 10, then swap their values without using a third variable, and print both.
Start with score = 10. Add 5 to it using the shortcut += instead of writing score = score + 5, then print the result.
Start with balance = 100. Subtract 30 with -=, then double it with *=, then halve it with /=, printing after each step.
Ask the user for their name and print Welcome, <name>!.
Ask the user to type a number, then print the type of what you received. Explain the result in a comment.
Ask the user for a whole number, convert it with int(), add 10, and print the answer.
Ask the user for a price as a decimal, convert it with float(), and print double the price.
Ask the user for two numbers on two separate lines, then print their sum.
Given score = 90 (an int), build the message "Your score: 90" using +. You will need str() to make this work.
Convert the float 9.87 to an int and print it. Note in a comment that Python chops the decimal off rather than rounding.
Convert the integer 7 to a float and print both the value and its type.
Print bool() applied to each of 0, 1, -5, "", "hello", 0.0. Then describe the pattern in a comment.
Add an int and a float together. Print the result and its type, then explain in a comment which type Python picked and why.
Create city = "Pune" and print I live in Pune. using an f-string. An f-string is a normal string with f in front, letting you drop variables straight into {}.
Create name, age and country, then print one sentence containing all three using a single f-string.
Given a = 7 and b = 3, print 7 + 3 = 10 using one f-string. You can put a calculation directly inside the {}.
Print pi = 3.14159265 rounded to exactly 2 decimal places using an f-string.
Print the number 42 right-aligned in a column 10 characters wide, so it sits at the far right.
Print the word Hi three times in a 10-wide column: once left-aligned (<), once centred (^), once right-aligned (>). Add a | after each so you can see the column edge.
Print the word SALE centred in a 20-wide field, padded with * on both sides instead of spaces.
Ask for the length and width of a rectangle and print its area to 2 decimal places.
Extend the last idea: ask for length and width, then print both the area and the perimeter on separate lines, each to 2 decimal places.
Ask for the radius of a circle and print its area using PI = 3.14159, to 2 decimal places.
Write a simple-interest calculator. Ask for principal, rate (%) and time (years), compute principal * rate * time / 100, and print the interest to 2 decimal places.
Convert a temperature from Celsius to Fahrenheit using F = C * 9 / 5 + 32. Take Celsius as input and print the result to 1 decimal place.
Now go the other way: convert Fahrenheit to Celsius using C = (F - 32) * 5 / 9, printing to 1 decimal place.
Ask for the price of one item and how many were bought, then print the total bill as currency, e.g. Total: $49.50.
Ask for three exam scores and print their average to 1 decimal place.
Ask for marks obtained and total marks, then print the percentage to 2 decimal places.
Write a tip calculator. Ask for the bill amount and tip percent, then print the tip and the final total, both as currency.
Ask for an item's original price and a discount percent, then print the amount saved and the final price.
Ask for a distance in kilometres and print it in miles, to 2 decimal places (1 km = 0.621371 miles).
Ask for a weight in kilograms and print it in pounds and grams, each to 2 decimal places (1 kg = 2.20462 lb = 1000 g).
Write a BMI calculator. Ask for weight in kg and height in metres, compute weight / (height * height), and print the BMI to 1 decimal place.
Ask for a distance in km and a time in hours, then print the average speed in km/h to 2 decimal places.
Print the number 2500000 with comma thousands-separators, so it reads 2,500,000.
Given fraction = 0.4567, print it as a percentage with 1 decimal place (45.7%) using a format specifier — do not multiply by 100 yourself.
Print one receipt line: the item name left-aligned in 15 characters, and the price right-aligned in 10 characters with 2 decimals. Aim for Notebook 45.50.
Ask for a single word and print it centred in a 20-wide banner made of - characters, with the word's length underneath.
Ask for a bill total and the number of people sharing it, then print the amount each person owes as currency.
int("3.9") crashes with a ValueError. Show why, then convert the string "3.9" to an int correctly and print the result.
Print round(2.5) and round(3.5). One of them does not do what you expect. Print both and explain the behaviour in a comment.
Print the result of 0.1 + 0.2 and then compare it to 0.3. Explain the surprising output in a comment.
Print type(True) and isinstance(True, int), then print True + True. Explain why a bool behaves like a number.
Given value = 10, show the difference between type(value) == int and isinstance(value, int), and note in a comment which one is generally preferred.
Print the number 255 in binary, octal and hexadecimal using only f-string format specifiers (b, o, x) — without bin(), oct() or hex().
Print 93500000.0 in scientific notation with 2 decimal places, using a format specifier.
Print a profit of 250.0 and a loss of -120.5 so that the sign is always shown (+250.00 and -120.50), to 2 decimal places.
Print 1234567.891 as currency with both thousands-separators and 2 decimals, right-aligned in a 20-wide field, like $1,234,567.89.
Given total = 17.5, print total=17.5 using the f-string = specifier, which prints the variable's name and value together. Explain why this is useful when debugging.
This code crashes with a TypeError. Explain the cause and fix it.
age = 20
message = "I am " + age + " years old"
print(message)This is meant to print My name is Sam but prints My name is {name} instead. Find and fix the bug.
name = "Sam"
print("My name is {name}")This raises a NameError. Find and fix it.
username = "coder123"
print(User_name)This crashes when it tries to add 10. Explain why and fix it.
number = input("Enter a number: ")
result = number + 10
print(result)This snippet has three separate bugs: an unterminated string, a missing type conversion, and a name used with the wrong capitalisation. Find and fix all three.
Price = input("Enter price: )
total = price * 3
print("Total: " + total)Build a Personal Profile Card. Ask for name, age, city and email, then print a bordered card:
==============================
Name : Asha
Age : 22
City : Pune
Email: asha@example.com
==============================
Build a Length Unit Converter. Ask for a length in metres and print the equivalent in centimetres, feet and inches, each to 2 decimal places, in aligned columns. (1 m = 100 cm = 3.28084 ft = 39.3701 in)
Build an Invoice Generator for one item. Ask for item name, quantity and unit price, then print aligned columns with the total:
Item Qty Unit Price Total
Notebook 3 45.50 136.50
Build a Student Report Card. Ask for the student's name and marks in three subjects, then print a bordered card showing each subject's mark, the total, and the percentage to 2 decimal places.
Build a Shopping Bill for one item. Ask for the item name, unit price, quantity, discount percent and tax percent. Print the subtotal, discount, taxed amount and final payable, all aligned as currency.
Build a Salary Slip. Ask for the employee's name and basic salary. Compute HRA as 20% of basic, DA as 10% of basic, tax as 15% of basic, and net pay as basic + HRA + DA - tax. Print an aligned slip.
Build a Time Breakdown display. Ask for a duration in total hours as a decimal (e.g. 2.75) and print it as hours and minutes (2 hours 45 minutes). Use int() to separate the whole hours from the fraction.
Build a Circle Fact Sheet. Ask for a radius and print the diameter, circumference and area, each to 3 decimal places and right-aligned in a column.
Build a Type Conversion Report. Ask for one value, then print a table showing it converted to float, int (via float first), bool and str, alongside the type() of each result.
Build an Escape Sequence Showcase: a single program that demonstrates \n, \t, \", \\ and a triple-quoted block, each with a printed label saying which one it is.
Python is dynamically typed — a variable's type can change while the program runs. Demonstrate it: point one variable at an int, then a str, then a float, printing the value and type each time.
Given a = 5 (an int) and b = "5" (a str), show why a == b is False while a == int(b) is True.
Given value = 8342.567, print it four ways using only f-string format specifiers: as currency with commas, as a percentage, in scientific notation, and rounded to a whole number.
Simulate an ATM Withdrawal Receipt. Ask for the account holder's name, current balance and withdrawal amount. Print a receipt showing the holder, the amount withdrawn and the new balance, with all currency aligned to 2 decimal places.
Build a Variable Inspector — the single most complete demonstration of this whole topic. Ask for one value, then print a labelled, aligned report showing: the raw text and its type, its length, the value as a float, as an int, as a bool, and the float shown to 2 decimal places, in scientific notation, and as a percentage.
Still stuck on something?
Book a free 1-on-1 session and we'll work through it together.
Book a Free Session