Variable Exercises
3 min read ·
These exercises are designed to strengthen your understanding of Python variables.
They focus on variable creation, assignment, reassignment, type handling, and real-world usage.
Exercise 1: Create and Print Variables
Create variables to store your name, age, and city, then print them.
Exercise 2: Assign Multiple Values
Assign values to three variables in one line and print them.
Exercise 3: One Value to Multiple Variables
Assign the same value to three variables and print them.
Exercise 4: Change Variable Type
Assign an integer to a variable, then change it to a string and print both.
Exercise 5: Get the Type of Variables
Use the
type() function to check the data type of variables.Exercise 6: Use Casting
Convert a float into an integer and print the result.
Exercise 7: Case-Sensitive Variables
Create two variables with the same name but different cases and print them.
Exercise 8: Valid vs Invalid Variable Names
Identify which variable names are valid.
Exercise 9: Swap Two Variables
Swap values of two variables without using a temporary variable.
Exercise 10: Global and Local Variable
Create a global variable and access it inside a function.
Exercise 11: Modify Global Variable
Use the
global keyword to change a global variable inside a function.Exercise 12: Variable Naming Practice
Create variables using:
- Snake case
- Camel case
- Pascal case
Exercise 13: Store and Calculate
Store two numbers in variables and print their sum, difference, and product.
Exercise 14: Constant Variable
Create a constant variable and use it in a calculation.
Exercise 15: Variable Reassignment
Reassign a variable multiple times and print its final value.