3.1 Variables and Data Types
Variables
In Python, like most other programming languages, variables allow you to store information or values that can be used in future computations, displayed to a user, or influence conditional logic. The variable's name helps you find and use that information again.
Think of a variable like a labeled box where you store a piece of information you'll use later in your code.
- Declaring = putting a label on the box and putting something inside.
- Using = opening the box to see what's inside.
- Updating = replacing what's inside the box with something new.
Example: Creating and Using Variables
>>> greeting = "Hello, world!"
>>>
hour = 6greetingis a variable that stores the text"Hello, world!".houris a variable that stores the number6.
Now, anytime you use greeting in your code, Python will look inside the box and pull out the value:
>>>
print(greeting)
Hello, world! # OutputSometimes, you'll encounter the phrase "declaring a variable". Declaring a variable simply means creating a variable and giving it a value for the first time, like in the examples above.
We'll get to the exciting things you can do with variables soon, but for now we're going to focus on the fundamentals.
Reserved Keywords in Python
Some words in Python are "reserved keywords". These are special words that Python itself uses to understand and run your code. Reserved keywords have a predefined meaning in Python. You cannot use them as variable names. They tell Python how to behave.
How to check the list of keywords:
Python makes it easy to see all reserved keywords. You can run the following commands:
# Check list of reserved keywords in
Python
>>>
import keyword
>>>
print(keyword.kwlist)List of Reserved Keywords in Python (as of Python 3.15) (click to expand)
- _*
- False
- None
- True
- and
- as
- assert
- async
- await
- break
- case*
- class
- continue
- def
- del
- elif
- else
- except
- finally
- for
- from
- global
- if
- import
- in
- is
- lambda
- lazy*
- match*
- nonlocal
- not
- or
- pass
- raise
- return
- try
- type*
- while
- with
- yield
(* = "soft" keywords that are reserved only in specific contexts)
Updating a Variable (Replacing Values)
One important thing to know is that you can change the value of a variable at any time. But be careful! If you reuse a variable name by accident, you might override a value you still need. For example:
>>> message = "Good Morning!"
>>>
message = 100 # Accidentally replaced text with a number- Now
messageno longer stores"Good Morning", it stores 100. Python doesn't keep both values, it only remembers the latest one.
Key Things to Keep in Mind about Variables in Python:
- Variables names should be descriptive, use meaningful names (e,g,.
average_temp,patient_id). - Avoid single letters (except in short loops, like
for i in range(5)). - Never use words that sound like keywords (e.g.,
classss,ifff), they'll confuse readers even if Python allows them. - Python variable names are case-sensitive (Name and name are different).
- You can update variables, but be mindful not to override values you still need.
- Python is dynamically typed, meaning a variable can first store a number, then later store text. This flexibility is powerful but requires careful tracking of what your variables hold.