T
Tech Town
Log InSign Up Free
← Back to Blog

June 19, 2025 · admin

Variable Exercises


๐Ÿงช Python Variables โ€“ Practice Exercises with Solutions | TechTown.in

Learning Python is not just about reading โ€” itโ€™s about practicing. And when it comes to variables, thereโ€™s no better way to understand how they work than by solving real code challenges.

In this guide, we bring you Python variable exercises for beginners, complete with solutions and explanations. Test yourself, spot your mistakes, and master variable assignment, output, and scope with confidence!


โœ… Exercise 1: Create a Variable and Print It

๐Ÿ”น Question:

Create a variable named carname and assign the value "Volvo" to it. Then print it.

โœ… Solution:

carname = "Volvo"
print(carname)

โœ… Exercise 2: Assign Multiple Variables

๐Ÿ”น Question:

Create variables x, y, and z, and assign them the values "Orange", "Banana", and "Cherry" respectively in one line.

โœ… Solution:

x, y, z = "Orange", "Banana", "Cherry"

โœ… Exercise 3: One Value to Multiple Variables

๐Ÿ”น Question:

Create three variables x, y, and z, and assign them the same value "Python".

โœ… Solution:

x = y = z = "Python"

โœ… Exercise 4: Output Variables

๐Ÿ”น Question:

Print the word โ€œawesomeโ€ stored in a variable called txt.

โœ… Solution:

txt = "awesome"
print(txt)

โœ… Exercise 5: Combine Text and Variables

๐Ÿ”น Question:

Use the print() function to output the following using two variables:

x = "Python"
y = "is awesome"

Expected Output:

Python is awesome

โœ… Solution:

x = "Python"
y = "is awesome"
print(x, y)

โœ… Exercise 6: Add Two Numbers

๐Ÿ”น Question:

Create two variables x and y and assign them the values 5 and 10. Print the sum of x and y.

โœ… Solution:

x = 5
y = 10
print(x + y)

โœ… Exercise 7: Output With Text

๐Ÿ”น Question:

Add a variable z = x + y, then print “The sum is” followed by the result.

โœ… Solution:

x = 5
y = 10
z = x + y
print("The sum is", z)

โœ… Exercise 8: Variable Type Casting

๐Ÿ”น Question:

Convert the integer 5 into a string and assign it to the variable x.

โœ… Solution:

x = str(5)

โœ… Exercise 9: Variable Name Case Sensitivity

๐Ÿ”น Question:

Fix the error in the code below:

a = 4
A = "Sally"
print(a, A)

โœ… Solution:

โœ… This is already valid! It demonstrates that Python is case-sensitive, so a and A are two different variables.


๐Ÿง  Quick Practice Tips

  • Always try solving the problem before checking the solution
  • Use meaningful variable names in real-world projects
  • Mix different data types (strings, integers) for realistic practice
  • Experiment with f-strings, str(), and input() for added challenge

๐Ÿ Final Words

Practicing these Python variable exercises helps build a solid foundation. Variables are the building blocks of every Python script youโ€™ll ever write โ€” get comfortable with them now, and everything else becomes easier.


๐Ÿš€ Keep Practicing on TechTown.in