๐งช 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(), andinput()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