Python – Variable Exercises

Exercise 1: Create a Variable and Print It

Question

Create a variable called name and store your name.
Print the value.

Solution

name = "Amit"
print(name)

Exercise 2: Store a Number and Print It

Question

Create a variable age and store your age.
Print it.

Solution

age = 20
print(age)

Exercise 3: Print Text with Variable

Question

Store your city in a variable and print a sentence.

Solution

city = "Delhi"
print("I live in", city)

Exercise 4: Add Two Numbers Using Variables

Question

Create two variables and print their sum.

Solution

a = 10
b = 15
print("Sum:", a + b)

Exercise 5: Change Variable Value

Question

Create a variable and change its value.

Solution

x = 5
x = 10
print(x)

Exercise 6: Assign Multiple Values

Question

Assign values to three variables in one line and print them.

Solution

a, b, c = 1, 2, 3
print(a, b, c)

Exercise 7: Same Value to Multiple Variables

Question

Assign the same value to three variables.

Solution

x = y = z = 100
print(x, y, z)

Exercise 8: Take Input and Store in Variable

Question

Ask user for name and print it.

Solution

name = input("Enter your name: ")
print("Hello", name)

Exercise 9: Take Number Input and Add 1

Question

Ask user for age and print next year age.

Solution

age = int(input("Enter your age: "))
print("Next year:", age + 1)

Exercise 10: Swap Two Variables

Question

Swap two numbers using variables.

Solution

a = 5
b = 10
a, b = b, a
print(a, b)

Exercise 11: Print Variable Type

Question

Create a variable and print its type.

Solution

x = 10
print(type(x))

Exercise 12: Simple Real-Life Example

Question

Store product name and price, then print them.

Solution

product = "Book"
price = 250
print(“Product:”, product)
print(“Price:”, price)

Exercise 13: Fix the Error

Question

Fix the error in the code.

❌ Wrong code:

age = "20"
print(age + 5)

Correct Code

age = int("20")
print(age + 5)

Exercise 14: Global Variable Example

Question

Create a global variable and print it inside a function.

Solution

x = 50

def show():
print(x)

show()

Exercise 15: Update Variable Using Itself

Question

Increase a number by 1.

Solution

count = 0
count = count + 1
print(count)

✅ Summary

✔ Variables store data
✔ Values can change
✔ Input can be stored
✔ Multiple assignment is possible
✔ Practice makes perfect

📘 Perfect for Beginner eBook

This exercise chapter is perfect for:

  • Python beginner books

  • School & college students

  • Self-learners

If you want next, I can write:

  • Data Type Exercises

  • Input & Output Exercises

  • if–else Practice

  • Loop Exercises

  • MCQs with answers

Just tell me 😊

You may also like...