Python Variables :Variable Exercises

Here are several exercises related to Python variables, including basic usage, multiple assignment, global variables, and more. These exercises will help you practice and reinforce your understanding of variables.

Exercise 1: Basic Variable Assignment

Create three variables: name, age, and city. Assign your name, age, and city to these variables and print them.

# Your solution
name = "John"
age = 30
city = "New York"

print(name)
print(age)
print(city)

Expected Output:

John
30
New York

Exercise 2: Multiple Variable Assignment

Assign the values 10, 20, and 30 to three variables x, y, and z in a single line. Print the values.

# Your solution
x, y, z = 10, 20, 30
print(x, y, z)

Expected Output:

10 20 30

Exercise 3: Swapping Variables

Write code to swap the values of two variables a and b. Print their values before and after swapping.

# Your solution
a = 5
b = 10

# Print before swap
print("Before swapping:", a, b)

# Swap the values
a, b = b, a

# Print after swap
print("After swapping:", a, b)

Expected Output:

Before swapping: 5 10
After swapping: 10 5

Exercise 4: String Concatenation

Create two variables, first_name and last_name, and store your first and last names in them. Concatenate them into a full name, and print it.

# Your solution
first_name = "Alice"
last_name = "Johnson"
full_name = first_name + " " + last_name

print(full_name)

Expected Output:

Alice Johnson

Exercise 5: Variable Type Conversion

You are given two variables a = "10" and b = "20". Convert them to integers, add them together, and print the result.

# Your solution
a = "10"
b = "20"

# Convert to integers and add
result = int(a) + int(b)
print(result)

Expected Output:

30

Exercise 6: Using Global Variables

Define a global variable counter with an initial value of 0. Write a function increment_counter() that increments the global counter by 1 each time it’s called. Call the function three times and print the value of counter.

# Your solution
counter = 0

def increment_counter():
    global counter
    counter += 1

increment_counter()
increment_counter()
increment_counter()

print(counter)

Expected Output:

3

Exercise 7: Variable Unpacking

You are given a tuple coordinates = (10, 20, 30). Unpack the values into three variables x, y, and z and print them.

# Your solution
coordinates = (10, 20, 30)
x, y, z = coordinates

print(x, y, z)

Expected Output:

10 20 30

Exercise 8: Assign Same Value to Multiple Variables

Assign the value 50 to three variables a, b, and c in a single line. Print their values.

# Your solution
a = b = c = 50
print(a, b, c)

Expected Output:

50 50 50

Exercise 9: Mutable Global Variable

Create a global list variable numbers = [1, 2, 3]. Write a function add_number(num) that appends num to the global list. Call the function twice with different values, and print the list after each call.

# Your solution
numbers = [1, 2, 3]

def add_number(num):
    numbers.append(num)

add_number(4)
print(numbers)  # Output: [1, 2, 3, 4]

add_number(5)
print(numbers)  # Output: [1, 2, 3, 4, 5]

Exercise 10: F-strings Formatting

Create variables name, age, and city. Use an f-string to print a sentence such as: “My name is Alice, I’m 25 years old, and I live in New York.”

# Your solution
name = "Alice"
age = 25
city = "New York"

print(f"My name is {name}, I'm {age} years old, and I live in {city}.")

Expected Output:

My name is Alice, I'm 25 years old, and I live in New York.

Exercise 11: Dynamic Variable Values

Write a program that prompts the user to input their name and age, and then outputs a message like, “Hello [name], you are [age] years old!”

# Your solution
name = input("Enter your name: ")
age = input("Enter your age: ")

print(f"Hello {name}, you are {age} years old!")

Expected Output (depending on user input):

Hello John, you are 30 years old!

Exercise 12: Using * for Variable-Length Unpacking

You are given a list numbers = [10, 20, 30, 40, 50]. Unpack the first and last numbers into variables first and last, and assign the remaining numbers to a list middle. Print the values of first, middle, and last.

# Your solution
numbers = [10, 20, 30, 40, 50]
first, *middle, last = numbers

print(first)
print(middle)
print(last)

Expected Output:

10
[20, 30, 40]
50

These exercises will help you practice different aspects of variables in Python, including assignment, unpacking, global variables, and formatting.

Leave a Reply 0

Your email address will not be published. Required fields are marked *