Understanding Variables & Assignment in Python

Welcome to your first step in Python! Variables are a fundamental concept in programming. They act like labeled containers or boxes where you can store information (data) that your program can use and change.

What is a Variable?

Think of a variable as a name that refers to a value. Once you assign a value to a variable, you can use the variable's name to access that value throughout your code. This makes your code more readable and manageable.

The Assignment Operator: =

In Python, you create or update a variable using the assignment operator, which is the equals sign (=). The structure is:
variable_name = value

Assigning a number:

# The variable `count` now stores the number 10
count = 10
print(count)
10

Assigning text (a string):

# The variable `message` now stores the text "Hello, Python!"
greeting = "Hello, Python!"
print(greeting)
Hello, Python!

The value on the right side of = is stored in the variable name on the left. Variable names are case-sensitive (e.g., myVariable is different from myvariable).

Data Types and Reassigning Variables

Variables in Python can hold different types of data, such as numbers (integers, floats), text (strings), and more complex structures like lists. Python automatically determines the type of data a variable holds.

You can also change (reassign) the value stored in a variable at any time:

Reassigning a variable:

# Initial assignment
score = 100
print(f"Initial score: {score}")

# Update the score
score = 150
print(f"Updated score: {score}")

# Variables can even change type (though be careful!)
score = "No score yet"
print(f"Final score status: {score}")
Initial score: 100
Updated score: 150
Final score status: No score yet

Variables in Action: A Practical Example

Let's look at a slightly more complex example that involves getting input from a user, which is a common task where variables are essential.

# Ask the user for their name
user_name = input("Enter your name: ")

# Store a personalized greeting message
message = "Hello, " + user_name + "! Welcome."
print(message)

# Example with numbers and type conversion
value_as_string = input("Enter your age: ")
age = int(value_as_string) # Convert string to an integer
print(f"You entered {age}. In two years, you will be {age + 2} years old.")
Enter your name: [User types Alice]
Hello, Alice! Welcome.
Enter your age: [User types 30]
You entered 30. In two years, you will be 32 years old.
In this example:
  • input() gets text from the user (always a string).
  • user_name and message store strings.
  • value_as_string stores the age as text first.
  • int() converts the text "30" into the number 30 so we can do math with it.
  • f"" (f-string) is a handy way to embed variable values directly into strings for printing.
Don't worry if not all parts of this example are clear yet; we'll cover functions like input() and int(), and f-strings in more detail in later lessons! The key takeaway is how variables (user_name, message, age) store and manage data.

Your Turn: Practice Time!

The starter code has a variable x initialized to 0. Your task is to:

  1. Modify the line x = 0 in the editor.
  2. Change the value assigned to x so that it becomes 42.

When you run the code, it should print 42. Then, press Check to verify your solution.

Section 1/5What is a Variable?

What is a Variable?

Think of a variable as a name that refers to a value. Once you assign a value to a variable, you can use the variable's name to access that value throughout your code. This makes your code more readable and manageable.

Understanding Variables & Assignment in Python

Welcome to your first step in Python! Variables are a fundamental concept in programming. They act like labeled containers or boxes where you can store information (data) that your program can use and change.

What is a Variable?

Think of a variable as a name that refers to a value. Once you assign a value to a variable, you can use the variable's name to access that value throughout your code. This makes your code more readable and manageable.

Output:

Click "Check" to run your code.