Skip to content
beginner

Practice Exercises: Python Basics

Reading about variables and input() is useful. Running them is where the skill actually lands. These python basics exercises are built around one idea:…

Published 2026-05-11Updated 2026-09-1511 min read
Close-up of hands typing on a white keyboard at a wooden desk in an office setting with accessories.
Close-up of hands typing on a white keyboard at a wooden desk in an office setting with accessories. Photo by Cedric Fauntleroy on Pexels.

Reading about variables and input() is useful. Running them is where the skill actually lands. These python basics exercises are built around one idea: write the code, run it, inspect the output, then make a small change to see what breaks. That loop is how you learn Python by doing.

Before you start, make sure you are comfortable with the building blocks. If any exercise feels unfamiliar, review variables and data types, working with numbers and strings, and user input first.

How to Work Through These Exercises

Each exercise follows the same shape: a goal, starter code, expected behavior, a hint, a solution, and an explanation. Here is how to get the most out of them:

  • Attempt the exercise before reading the hint. The struggle is part of the learning. If you peek at the solution immediately, you will recognize the pattern instead of owning it.
  • Run every example. Do not just read the code and nod. Type it, run it, and compare the output to what you expected.
  • Break things on purpose. Change a value, remove a conversion, rename a variable. The error messages you get are evidence about how Python actually works.

You can run the code in any Python environment you already have, whether that is IDLE, a terminal, or an online editor. The exercises are short enough to paste into a single file and run with python filename.py.

The One Boundary That Matters: Text vs. Numbers

A flowchart shows a user typing 7 into input(), producing the string "7"; one branch shows string multiplication producing "77", while another passes through int() to produce the number 7 and multiplication producing 14.
The same typed characters behave differently until you convert input text into a number.

Here is the thread that runs through every exercise in this article: Python treats text and numbers as different kinds of values, and the boundary between them is where beginners trip most often.

input() always hands you text, even when the user types digits. "7" is not the number 7; it is a string made of one character. Arithmetic needs numbers. Concatenation needs strings. The moment you mix them up, Python stops and tells you exactly where you went wrong.

Keep one question in your pocket for every exercise below: what type is this value right now? If you can answer that, you can predict most beginner errors before they happen.

Knowledge check

Check your understanding

Answer this question before you continue.

A user types 7 in response to input(). What type of value does the program receive?
Misconception Check

Focus: Recognize that input() returns a string and must be converted before arithmetic.

Exercise 1: Variables and Naming Rules

Goal: Create variables, reassign one of them, and inspect what type each value holds.

Starter code:

name = "Alice"
age = 25
print(name)
print(age)

Expected behavior: The program prints Alice and 25, each on its own line.

Tasks:

  1. Add a line that prints the type of age using type(age).
  2. Reassign age to a new value, like your age next year, and run the program again.
  3. Try creating a variable named 2cool and run the program. Read the error carefully.

Hint: Variable names must start with a letter or an underscore, and they cannot contain spaces or dashes. Reserved words like print are off-limits too. type() is a built-in that tells you what kind of value a variable holds.

Solution:

name = "Alice"
age = 25
print(name)
print(age)
print(type(age))

age = 26
print(age)
print(type(age))

Expected output:

Alice
25
<class 'int'>
26
<class 'int'>

Explanation: This exercise is really about seeing the type, not just storing a value. type(age) reports <class 'int'>, which tells you age holds a number. Now reassign age to "26" with quotes and run it again: the type flips to <class 'str'>, and the value stops behaving like a number. That single observation is the foundation for every later exercise.

Extension: Try naming a variable user-name and see what error you get. The dash is a subtraction operator, so Python reads it as user minus name and complains that neither variable exists.

Exercise 2: Basic Math and Division

Goal: Practice arithmetic and predict how each operator treats its inputs.

Starter code:

a = 10
b = 4
print(a + b)
print(a - b)
print(a * b)
print(a / b)

Expected behavior: The program prints 14, 6, 40, and 2.5.

Tasks:

  1. Before you run anything, write down what you expect a / b to print. Then run it and compare.
  2. Add a line that computes the remainder of a divided by b using the % operator.
  3. Change a and b to decimal numbers like 7.5 and 2.0, and run it again.

Hint: In Python 3, the / operator always returns a float, even when both numbers are integers. If you want whole-number division, use //.

Solution:

a = 10
b = 4
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a % b)

Expected output:

14
6
40
2.5
2

Explanation: The / operator gives 2.5, not 2, because Python 3 returns a float for division. The % operator gives the remainder, which is 2 because 10 divided by 4 leaves a remainder of 2. This distinction matters the moment you start writing code that checks whether a number is even or odd.

Extension: Predict what 10 // 4 returns before you run it. Then run it and check whether your mental model was right.

Knowledge check

Check your understanding

Answer this question before you continue.

What does this code print, in order? ```python a = 10 b = 4 print(a / b) print(a % b) ```
Output Prediction

Focus: Predict the result of arithmetic operators, including Python 3 division and remainder.

Exercise 3: Combining Strings

Goal: Join strings together and repair the mixed-type error that trips up beginners.

Starter code:

greeting = "Hello"
name = "Alice"

Expected behavior: The program prints a single greeting that includes both values.

Tasks:

  1. Combine greeting and name into a variable called message so it reads Hello Alice.
  2. Print message.
  3. Now try this line and run it: print(greeting + " " + name + " " + 5). Read the error, then fix it so the message ends with the number 5.

Hint: The + operator joins strings, but it will not add a space for you, and it refuses to join a string to a number. You need to include the space yourself and convert the number with str().

Solution:

greeting = "Hello"
name = "Alice"
message = greeting + " " + name + "!"
print(message)
print(greeting + " " + name + " " + str(5))

Expected output:

Hello Alice!
Hello Alice 5

Explanation: String concatenation is literal: Python joins exactly what you give it, nothing more. Forgetting the space is the most common mistake here, and the output makes it obvious. The second line is where the text-versus-number boundary bites: greeting + " " + name + " " + 5 fails because you cannot add a string and an integer. Wrapping the number in str(5) converts it to text, and the join works.

Extension: Try an f-string instead: message = f"{greeting} {name}!". It is cleaner when you are combining many values, and it converts numbers for you automatically. You will use f-strings as the main style in the final exercise.

Knowledge check

Check your understanding

Answer this question before you continue.

Which replacement makes this line print `Hello Alice 5` without a mixed-type error? ```python greeting = "Hello" name = "Alice" print(greeting + " " + name + " " + ???) ```
Debugging

Focus: Repair a string-and-number concatenation error by converting the number to text.

Exercise 4: Getting User Input

Goal: Read input from the user and convert it to a number when you need math.

Starter code:

color = input("What is your favorite color? ")
print("Your favorite color is " + color)

Expected behavior: The program asks a question, waits for the user to type an answer, then repeats it back.

Tasks:

  1. Ask the user for a number and store it in a variable.
  2. Print double that number.

Hint: input() always returns a string, even if the user types digits. You must convert it with int() or float() before doing math.

Solution:

color = input("What is your favorite color? ")
print("Your favorite color is " + color)

number = int(input("Enter a number: "))
print("Double that is " + str(number * 2))

Expected behavior (example run):

What is your favorite color? blue
Your favorite color is blue
Enter a number: 7
Double that is 14

Explanation: The second input() is wrapped in int() so the user's text becomes a number you can multiply. Notice the solution also wraps the result in str() before printing, because you cannot concatenate a number to a string directly.

Here is the subtle part that trips up beginners. If you skip the int() conversion and write number * 2, Python does not crash. It repeats the string: "7" * 2 gives "77", not 14. The output looks almost right, which makes it worse than an error. The multiplication works because strings support repetition, but it is not the math you wanted.

Run this to see the difference for yourself:

text = input("Enter a number: ")
print(text * 2)
Enter a number: 7
77

That is the text-versus-number boundary in action. "7" is a string, so * repeats it. 7 is a number, so * doubles it. The same symbol, two completely different behaviors, decided entirely by the type of the value.

Extension: Ask for a decimal number and use float() instead. Then print the result with two decimal places.

Knowledge check

Check your understanding

Answer this question before you continue.

If the user enters `7`, what does this program print? ```python text = input("Enter a number: ") print(text * 2) ```
Output Prediction

Focus: Distinguish string repetition from numeric multiplication when using input().

Exercise 5: Putting It All Together

Goal: Combine variables, input, math, and string formatting in one small program.

Task: Write a program that asks for the user's name and age, then prints a message telling them what year they will turn 100.

Starter code:

name = input("What is your name? ")
age = int(input("How old are you? "))

Expected behavior: For a user named Alice who is 25, the program prints Alice will turn 100 in the year 2101.

Hint: You need a starting year to do the math. Use a fixed value like 2026 so the output is reproducible, and compute the target year as starting_year + (100 - age). This is a practice simplification, not a general date-handling solution.

Solution:

name = input("What is your name? ")
age = int(input("How old are you? "))

starting_year = 2026
year_100 = starting_year + (100 - age)

print(f"{name} will turn 100 in the year {year_100}")

Expected behavior (example run):

What is your name? Alice
How old are you? 25
Alice will turn 100 in the year 2101

Explanation: This exercise pulls together everything from the earlier ones. input() reads the name as a string and the age as an integer. The math starting_year + (100 - age) computes the target year. This time the message uses an f-string, so the number is formatted into the text for you—no manual str() call needed. The name stays a string, the age becomes a number, and the f-string handles the display.

Test your own version: Before you compare with the solution, run your program with these two cases and check that the output matches:

  • Input Alice and 25 should print Alice will turn 100 in the year 2101.
  • Input Bob and 50 should print Bob will turn 100 in the year 2076.

If your output matches both, your conversions and math are correct. Then change the age input to "twenty-five" and watch the program crash with a ValueError—that is the boundary refusing to guess what you meant.

Extension: Ask for a decimal age and use float() instead of int(). The math still works, and you will see the result come out as a float. (Wrapping input in try/except to handle bad input is a useful idea, but it belongs to a later lesson on exceptions, not this one.)

Common Beginner Mistakes

These are the errors beginners hit again and again. Each one is a small mental model fix, not a deep problem:

  • Forgetting quotes around strings. name = Alice makes Python look for a variable called Alice, which does not exist. Always quote text.
  • Doing math on input() without converting. input() returns a string. "7" * 2 gives "77", not 14. Convert with int() or float() first.
  • Concatenating a number to a string. "Age: " + age fails because age is an integer. Wrap it in str(age) or use an f-string.
  • Expecting / to return a whole number. In Python 3, 10 / 4 is 2.5. Use // if you want 2.

Next Steps

You have now practiced the core loop: write, run, inspect, adjust. That loop is the real skill, and it transfers to every topic that comes next.

Here is your next concrete task: build a small program that asks the user for a temperature in Celsius and prints the Fahrenheit equivalent. It uses everything from this article—input, conversion, math, and string formatting—with no new concepts required.

Make it testable the same way Exercise 5 was. Run it with 0 and confirm it prints 32.0. Run it with 100 and confirm it prints 212.0. Those two boundary cases tell you whether your formula and conversions are right before you trust any other input.

When you finish, change the input and run it again. Then change it to a decimal and watch how the output shifts. Every run is a small experiment, and each one sharpens the same question: what type is this value right now?

When you are ready to move beyond the basics, the same practice-first approach applies to bigger ideas. The next natural step is conditionals, where the text-versus-number boundary starts deciding which branch your program takes. Master the loop here, and you will carry it everywhere.

Knowledge check

Final check

Finish the article by checking the ideas you just learned.

Using the article's fixed starting year of 2026, what target year should the program calculate for a user whose age is 50?
Question 1 of 2Single Choice

Focus: Apply integer conversion and the article's target-year formula to combine input, variables, and math.

The program uses `starting_year + (100 - age)` after converting the age input with `int()`.
Which statement correctly describes how to print text together with an integer age?
Question 2 of 2Misconception Check

Focus: Choose an appropriate way to include a numeric value in text output.

References

  1. Python Exercises - W3Schoolswww.w3schools.com
Practical resource

Want a more structured Python path?

Use the Python Starter Pack to turn scattered tutorials into a focused practice path.

View the bundle
Coming soon

Python Starter Pack

A compact LearnPyFast PDF pack covering what Python is, installation, your first program, running Python code, and Python versions.

$9
PDF BundleTopic PackPythonBeginner
  • 5 curated chapters
  • Enhanced PDF edition with bundle-only learning guidance
  • Offline-friendly format for focused review
  • Source article links for future online updates

Coming soon

Free Python bundle

Get the LearnPyFast Python for Artificial Intelligence Starter Bundle

Build a Python foundation you can actually use. The Python for Artificial Intelligence Starter Pack brings together a guided path through setup, core programming concepts, data structures, files, JSON, APIs, debugging, and practical projects—so you can move quickly from running your first program to understanding and building useful software.

You’ll receive the bundle by email. You can unsubscribe anytime.

No spam. You can unsubscribe anytime. See our Privacy policy.

Related sites

Continue beyond Python

Explore related Worldmonger sites when you want to move from Python basics into JavaScript or LLM application building.

JavaScript tutorialstutorial

LearnJSFast

Beginner-friendly JavaScript tutorials for practical web development and self-taught developers.

JavaScriptFrontendWeb development
Visit LearnJSFast
LLM tutorialstutorial

LearnLLMFast

Practical LLM tutorials for builders who want to understand prompting, workflows, agents, and AI applications.

LLMAIBuilders
Visit LearnLLMFast

Keep learning

Related tutorials

Continue with nearby Python topics and beginner-friendly explanations.

Close-up view of HTML and CSS code displayed on a computer screen, ideal for programming and technology themes.
beginner
11 min read

Basic Math in Python

Python does not make you memorize a calculator manual. It hands you a small set of operators and lets you run the calculation and read the answer…

Read tutorial
Close-up of a large pot filled with black dye used in traditional incense stick production indoors.
beginner
9 min read

Python Comments and Code Style

Comments are not for Python. They are for the next human who reads your code—and that human is often you, six weeks later. The interpreter skips every line…

Read tutorial