2.1 What is a variable

A variable in programming is a name that holds a value, like a box that you can store something in and refer to later. In Python, a variable is a named reference to a memory location where a value is stored.

 

What is a variable

 

We may use variables for:

  • Readability and Maintenance - Variables with meaningful names make the code easier to read and understand. Refer to section 2.2 Variable naming for more info.

Example 2.1.1

total_price = 150

 

  • Reusability - Storing data in variables allows for reuse of the data without hardcoding values multiple times.

Example 2.1.2

tax_rate = 0.08
total_tax = total_price * tax_rate

 

  • Dynamic Data Handling - Variables allow programs to handle dynamic data input, making them flexible. User input is addressed in 2.5 Input statement topic.

Example 2.1.3

user_input = input("Enter your name: ")
print(f"Hello, {user_input}")

 

In summary, variables in programming are essential tools for storing, retrieving, and manipulating data, which enhances code readability, reusability, and overall efficiency.