Back to Course

Introduction to Python

0% Complete
0/0 Steps
  1. Python: Basics
    14Topics
    |
    15 Quizzes
  2. Python: Control Flow
    12Topics
    |
    12 Quizzes
  3. Python: Errors
    5Topics
    |
    5 Quizzes
Lesson 1, Topic 4
In Progress

Python: Variables

Yasin Cakal 3 Oct 2021
Lesson Progress
0% Complete

Programming languages provide a mechanism for reusing data. If we want to present a greeting, reuse a date, or remember a user ID, we can create a variable that can store a value. In Python, variables are assigned using the = equals sign.

output_message = "Welcome to Python Programming!"
# Prints "Welcome to Python Programming!"
print(output_message)

The message "Welcome to Python Programming!" is stored in a variable called output_message in the preceding example. Variable names cannot contain spaces or symbols other than an _ underscore. They cannot begin with a number but may include one following the first letter, for example, example_variable_7 is acceptable.

Coincidentally variables can vary. When the context of a program changes, we can update a variable while maintaining the same logical operation.

# Let's create a new variable
car_brand = "Mercedes"
print(car_brand)

# Let's change the brand name, but use the same variable
car_brand = "BMW"
print(car_brand)

Output:

Mercedes
BMW

As you can see, variables can change throughout the program, taking on new values. If a person can change which car they drive, it only makes sense that the variable holding that value can adapt as well.

%d bloggers like this: