Python – Variable Exercises

Welcome to The Coding College, where we turn learning Python into a hands-on experience! In this post, we’ll guide you through a series of variable-related exercises to solidify your understanding of Python variables. Whether you’re a beginner or looking to brush up on your skills, these exercises will help you master variables and their applications.

Why Practice Python Variables?

Variables are the foundation of programming. They store data, track changes, and help manage complex logic in your code. Practicing with variables ensures that you understand:

  • How to create and assign values.
  • The difference between global and local variables.
  • Proper naming conventions.

Python Variable Exercises

1. Basic Variable Assignment

Create a Python program that:

  1. Declares three variables: name, age, and city.
  2. Assigns appropriate values to each variable.
  3. Prints a sentence using the variables.

Example Output:

Hello, my name is Alice. I am 25 years old and I live in New York.

Starter Code:

# Declare variables  
name = "Your Name"  
age = 0  # Replace with your age  
city = "Your City"  

# Print the sentence  
print(f"Hello, my name is {name}. I am {age} years old and I live in {city}.")  

2. Swapping Variables

Write a program that swaps the values of two variables without using a third variable.

Example:

Input:

a = 5  
b = 10  

Output:

a = 10  
b = 5  

Starter Code:

# Initial values  
a = 5  
b = 10  

# Swap values  
a, b = b, a  

# Print results  
print(f"a = {a}, b = {b}")  

3. Sum of Two Variables

Create a program that:

  1. Takes two numbers as input.
  2. Stores them in variables.
  3. Calculates and displays their sum.

Example Output:

Enter first number: 10  
Enter second number: 20  
The sum is: 30  

Starter Code:

# Input numbers  
num1 = int(input("Enter first number: "))  
num2 = int(input("Enter second number: "))  

# Calculate sum  
total = num1 + num2  

# Print result  
print(f"The sum is: {total}")  

4. Check Data Type of a Variable

Write a program that checks and displays the data type of a variable.

Example Output:

The variable '25' is of type <class 'int'>.  

Starter Code:

# Assign a value  
value = 25  

# Check and print data type  
print(f"The variable '{value}' is of type {type(value)}.")  

5. Global and Local Variables

Write a program to demonstrate the difference between global and local variables.

Starter Code:

# Global variable  
x = 10  

def my_function():  
    # Local variable  
    x = 5  
    print(f"Inside the function, x = {x}")  

# Call function  
my_function()  

# Print global variable  
print(f"Outside the function, x = {x}")  

Expected Output:

Inside the function, x = 5  
Outside the function, x = 10  

6. Assign Multiple Values

Create a program that assigns multiple values to multiple variables in a single line.

Example:

x, y, z = "Python", 3.9, True  
print(x, y, z)  

Your Task:

Add code to print the type of each variable.

7. User-Defined Variable Name

Write a program that:

  1. Prompts the user to enter a variable name and its value.
  2. Stores the value in a dictionary using the given variable name as the key.
  3. Displays the dictionary.

Starter Code:

# Input variable name and value  
var_name = input("Enter variable name: ")  
var_value = input("Enter variable value: ")  

# Store in dictionary  
variables = {var_name: var_value}  

# Print dictionary  
print(variables)  

8. Length of a String Variable

Write a program that calculates and displays the length of a string stored in a variable.

Example:

text = "Hello, Python!"  
length = len(text)  
print(f"The length of the string is: {length}")  

9. Modify a Global Variable

Write a program to modify a global variable inside a function using the global keyword.

Starter Code:

# Global variable  
counter = 0  

def increment_counter():  
    global counter  
    counter += 1  

# Call function and print result  
increment_counter()  
print(f"Counter: {counter}")  

Test Your Knowledge

Once you’ve completed these exercises, test your knowledge by building a small program that combines multiple concepts. For example:

  • Create a calculator that takes user input, stores values in variables, and performs basic operations like addition, subtraction, multiplication, and division.

Learn Python at The Coding College

At The Coding College, we emphasize learning through practice. Our step-by-step tutorials and exercises are designed to strengthen your programming skills. Explore our website for more:

  • Beginner-Friendly Python Guides
  • Hands-On Coding Projects
  • Expert Tips for Writing Clean Code

Conclusion

Practicing variables is a foundational step in learning Python. The exercises provided here will help you build confidence and apply your knowledge to real-world coding tasks.

Leave a Comment