DevAcademia
C++C#CPythonJava
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz

Loading Python tutorial…

Loading content
Python FundamentalsTopic 10 of 77
←PreviousPrevNextNext→

Variable Exercises

Beginner Exercises

Practice basic variable operations:

  • Create variables for your **name** (string), **age** (integer), **height** (float in meters), and **student status** (boolean). Print them with descriptive labels.
  • Perform variable swapping: given `a = 10` and `b = 20`, swap their values and print before and after.
Example
# Exercise 1: Create variables
name = "Alice"
age = 25
height = 1.68
is_student = True

print("Name:", name)
print("Age:", age)
print("Height (m):", height)
print("Student:", is_student)

# Exercise 2: Variable swapping
a, b = 10, 20
print(f"Before: a={a}, b={b}")
a, b = b, a
print(f"After: a={a}, b={b}")
Output
Name: Alice
Age: 25
Height (m): 1.68
Student: True
Before: a=10, b=20
After: a=20, b=10
ℹ️ Note: Use clear, descriptive variable names for readability.

Intermediate Challenges

More complex variable manipulation tasks:

  • Convert between types: given `num_str = "123"`, convert to integer, float, and back to string. Print each with its type.
  • Unpack tuple `("Python", 3.9, "Guido van Rossum")` into variables and print a descriptive sentence.
⚠️ Warning: Always ensure type compatibility before performing operations.
Example
# Exercise 3: Type conversion
num_str = "123"
num_int = int(num_str)
num_float = float(num_str)
num_back_to_str = str(num_int)

print(num_str, type(num_str))
print(num_int, type(num_int))
print(num_float, type(num_float))
print(num_back_to_str, type(num_back_to_str))

# Exercise 4: Tuple unpacking
data = ("Python", 3.9, "Guido van Rossum")
language, version, creator = data
print(f"{language} {version} was created by {creator}")
Output
123 <class 'str'>
123 <class 'int'>
123.0 <class 'float'>
123 <class 'str'>
Python 3.9 was created by Guido van Rossum

Advanced Problems

Real-world variable management challenges:

  • Implement a **global counter** with functions `increment_counter()` and `reset_counter()`. Ensure they modify the global variable correctly.
  • Create a **configuration manager** using a dictionary. Write a function that safely updates settings, handling invalid keys gracefully.
Example
# Exercise 5: Global counter
counter = 0

def increment_counter():
    global counter
    counter += 1

def reset_counter():
    global counter
    counter = 0

increment_counter()
print(counter)  # 1
reset_counter()
print(counter)  # 0

# Exercise 6: Configuration manager
config = {"theme": "light", "volume": 50}

def update_config(key, value):
    if key in config:
        config[key] = value
    else:
        print(f"Invalid key: {key}")

update_config("volume", 75)
print(config)
update_config("language", "EN")
Output
1
0
{'theme': 'light', 'volume': 75}
Invalid key: language
ℹ️ Note: For safe dictionary lookups, use `.get()` with a default value.

Solutions

Example solutions (try solving before checking):

Example
# Exercise 2 Solution
a, b = 10, 20
print(f"Before: a={a}, b={b}")
a, b = b, a
print(f"After: a={a}, b={b}")

# Exercise 4 Solution
data = ("Python", 3.9, "Guido van Rossum")
language, version, creator = data
print(f"{language} {version} was created by {creator}")
Output
Before: a=10, b=20
After: a=20, b=10
Python 3.9 was created by Guido van Rossum
Test your knowledge: Variable Exercises
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Python FundamentalsTopic 10 of 77
←PreviousPrevNextNext→