25:00
Focus
Lesson 2

Storing Data with Basic Variables

~6 min50 XP

Introduction

Welcome to the fundamental building blocks of programming! In this lesson, you will discover how to harness the power of variables to store information in Python, allowing your programs to "remember" data for later use.

The Concept of Containers

Think of a variable as a labeled box stored in your computer's memory. When you assign a value to a variable, you are effectively placing an objectโ€”like a number or a piece of textโ€”inside that box. In Python, you don't need to specify the type of data beforehand; the language is dynamically typed, meaning it figures out what is inside the box automatically.

To create a variable, you use the assignment operator (=). It is vital to understand that this is not a mathematical equality, but an instruction to "store the value on the right into the name on the left."

# Creating variables
user_age = 25
user_name = "Alice"

A common pitfall for beginners is confusing the variable name with the value itself. Always think of the variable name as a pointer to the data. If you reassign the variable, the old data is discarded by Pythonโ€™s garbage collector.

Exercise 1Multiple Choice
In Python, what is the correct way to store the value 42 in a variable named 'score'?

Data Types: Numbers and Strings

Python categorizes data into different data types. Two of the most essential types are integers (whole numbers) and strings (text). An integer is any whole number, such as 55, โˆ’10-10, or 40004000. A string, on the other hand, is a sequence of characters wrapped in quotes, such as "Hello World".

Because Python is smart about types, you can add two integers together and get a new integer, but if you try to add an integer to a string, Python will raise a TypeError. Knowing the "identity" of your data is the first step toward writing bug-free code.

Exercise 2True or False
In Python, the variable 'x = 10' and 'x = "10"' store the same data type.

Mathematical Operations

Variables become powerful when you use them in calculations. Python supports all standard arithmetic operators: addition (+), subtraction (-), multiplication (*), and division (/). You can perform these operations on variables just as you would with literal numbers.

For instance, if you have a variable representing the price of an item and another for tax, you can calculate the total as:

total=price+tax\text{total} = \text{price} + \text{tax}

Python also follows the standard order of operations (PEMDAS/BODMAS). When writing complex expressions, it is best practice to use parentheses to ensure the calculation happens in the order you intend.

Note: When you use the / operator, Python always produces a float (a number with a decimal point), even if the numbers divide evenly.

Naming Conventions and Best Practices

When naming your variables, readability is everything. While a = 15 works, item_price = 15 tells anyone reading your code (including your future self) exactly what that number represents. This is known as using descriptive variables.

In Python, there are a few strict rules:

  1. Variable names must start with a letter or an underscore.
  2. Variable names cannot start with a number.
  3. Variable names can only contain letters, numbers, and underscores.
  4. Variable names are case-sensitive (Age and age are different).

Follow the PEP 8 style guide by using snake_case for your variables (e.g., user_email_address instead of userEmailAddress or useremailaddress). This makes your code professional and consistent with the wider Python community.

Exercise 3Fill in the Blank
___ is the recommended naming convention in Python for variables, using underscores to separate words.

Key Takeaways

  • Variables are containers used to store data, created using the assignment operator =.
  • Python is dynamically typed, so you do not need to explicitly declare if a variable is an integer or a string.
  • Respecting data types (like strings vs. integers) is crucial to avoid runtime errors.
  • Always use descriptive names and follow the snake_case convention to keep your code clean and maintainable.
Finding tutorial videos...
Go deeper
  • What happens if I change a variable's data type later?๐Ÿ”’
  • Can I use numbers at the start of a variable name?๐Ÿ”’
  • Are there specific rules for naming variables in Python?๐Ÿ”’
  • What is the benefit of Python being dynamically typed?๐Ÿ”’
  • How does the garbage collector know when to delete data?๐Ÿ”’