Python Variables: A Complete Beginner Guide
Table of Contents
Variables are how we store data as our program runs. You’re probably already familiar with printing data by passing it straight into print(), but variables let us save that data so we can reuse it and change it before printing it. This guide covers everything you need to know about Python variables: creating them, naming them, and understanding the basic data types they can hold. If you’re just getting started, you might also want to know why Python is worth learning in the first place.
All the content from our Boot.dev courses are available for free here on the blog. This one is the “Variables” chapter of Learn to Code in Python. If you want to try the far more immersive version of the course, do check it out!
What Is a Variable in Python?
A “variable” is just a name that we give to a value. For example, we can make a new variable named my_height and set its value to 100:
my_height = 100
Or we can define a variable called my_name and set it to the text string "Lane":
my_name = "Lane"
Once we have a variable, we can access its value by using its name:
print(my_height) # 100
print(my_name) # Lane
Variables are called “variables” because they can hold any value and that value can change (it varies). The line acceleration = 20 reassigns the value, overwriting whatever was there before:
acceleration = 10
acceleration = 20
print(acceleration) # 20
How Do You Do Math With Python Variables?
Here are the common mathematical operators in Python:
total = a + b # addition
difference = a - b # subtraction
product = a * b # multiplication
quotient = a / b # division
Parentheses control the order of operations, just like in regular math:
average = (first + second + third) / 3
Negative numbers work the way you’d expect. Just add a minus sign:
my_negative_num = -420
What Are the Rules for Naming Variables in Python?
Variable names can not have spaces; they’re continuous strings of characters.
The creator of the Python language himself, Guido van Rossum, implores us to use snake_case for variable names. Here’s how the common casing styles compare:
| Name | Code | Language(s) that recommend it |
|---|---|---|
| Snake Case | num_new_users | Python, Ruby, Rust |
| Camel Case | numNewUsers | JavaScript, Java |
| Pascal Case | NumNewUsers | C#, C++ |
| No Casing | numnewusers | …just don’t do this |
Your Python code will still work with Camel Case or Pascal Case, but can we please just have nice things? We just want some consistency in our craft.
If you won’t use snake case for you, do it for me. I beg you.
What Are the Basic Data Types in Python?
Python has several basic data types. Every variable you create holds a value that belongs to one of these types.
Strings
In programming, snippets of text are called “strings”. They’re lists of characters strung together. We create strings by wrapping text in single quotes or double quotes. That said, double quotes are preferred.
name_with_single_quotes = 'boot.dev'
name_with_double_quotes = "boot.dev"
Integers
An integer (or “int”) is a number without a decimal part:
x = 5
y = -5
Floats
A float is a number with a decimal part:
x = 5.2
y = -5.2
Booleans
A boolean (or “bool”) is a type that can only have one of two values: True or False. As you may have heard, computers really only use 1’s and 0’s. These 1’s and 0’s are just True/False boolean values.
is_tall = True
is_short = False
How Do F-Strings Work in Python?
In Python, we can create strings that contain dynamic values with the f-string syntax:
num_bananas = 10
print(f"You have {num_bananas} bananas")
# You have 10 bananas
- Add an
fbefore the opening quotes to create an f-string - Use curly brackets
{}around a variable to interpolate its value into the string
F-strings are the preferred way to build strings with variables in modern Python. You can use + concatenation (more on that below), but f-strings are cleaner and easier to read.
What Is None Type in Python?
Not all variables have a value. We can make an “empty” variable by setting it to None. It’s a special value in Python that represents the absence of a value. It is not the same as zero, False, or an empty string.
username = None
A common use case is representing a value that hasn’t been determined yet. Maybe your program is waiting for a user to enter their name:
username = None
username = input("What's your name? ")
None is not the same as the string "None". They look identical when printed, but they’re different data types:
print(type("None")) # <class 'str'>
print(type(None)) # <class 'NoneType'>
What Is Dynamic Typing in Python?
Python is dynamically typed, which means a variable can store any type, and that type can change:
speed = 5
speed = "five"
But like, maybe don’t. In almost all circumstances, it’s a bad idea to change the type of a variable. Just create a new one:
speed = 5
speed_description = "five"
Languages that aren’t dynamically typed are statically typed, such as Go and TypeScript. In a statically typed language, if you try to assign a value of the wrong type, you’ll get a compile-time error and the program won’t run.
How Does String Concatenation Work in Python?
When working with strings, the + operator performs a “concatenation” – a fancy word for “joining two strings”. Generally speaking, it’s better to use f-strings over + concatenation.
first_name = "Lane "
last_name = "Wagner"
full_name = first_name + last_name
print(full_name) # Lane Wagner
Notice the extra space at the end of "Lane ". That extra space separates the words in the final result. With an f-string, you don’t need to worry about that:
first_name = "Lane"
last_name = "Wagner"
print(f"{first_name} {last_name}") # Lane Wagner
Can You Declare Multiple Variables on One Line in Python?
Yes. We can save space by declaring related variables on the same line:
sword_name, sword_damage, sword_length = "Excalibur", 10, 200
Which is the same as:
sword_name = "Excalibur"
sword_damage = 10
sword_length = 200
Variables declared on the same line should be related to one another so the code stays easy to understand. We call code that’s easy to understand “clean code”.
What Should You Learn After Python Variables?
Now that you understand variables, the natural next step is functions in Python. Functions let you reuse code by packaging logic into named blocks that accept variables as inputs and return new values as outputs. After that, you’ll want to tackle loops and lists.
If you want a hands-on, structured path through Python, check out the Learn to Code in Python course on Boot.dev. Variables are just the beginning.
Frequently Asked Questions
Can you change a variable's type in Python?
Yes, Python is dynamically typed so you can reassign a variable to a different type. That said, you almost never should. It makes code confusing and error-prone. Create a new variable instead.
What is the difference between an integer and a float?
An integer is a whole number like 42 or -7. A float has a decimal point like 3.14 or -0.5. Python treats them as different types, and division with / always returns a float.
Should you use snake_case or camelCase in Python?
Use snake_case. It's the official convention recommended by PEP 8, Python's style guide. Your code will still run with other casing styles, but following conventions makes your code easier for other Python developers to read.
What is the difference between None and an empty string?
None means a variable has no value at all. An empty string is still a value — it's a string that happens to contain zero characters. They behave differently in comparisons and function calls.
Related Articles
Queue Data Structure in Python: Ordering at Its Best
Oct 22, 2023 by lane
A queue is an efficient collection of ordered items. New items can be added to one side, and removed from the other side.
Understanding Stacks: Python Implementation of a Core Data Structure
Oct 06, 2023 by lane
A stack is an abstract data type that serves as a collection of elements. The name “stack” originates from the analogy of items physically stacked on top of each other. Just like a stack of plates at a buffet, plates can be added, removed, and viewed from the top. However, plates further down are not immediately accessible.
Can I Use Python for Web Development?
Aug 14, 2023 by natalie
I love giving a short answer to these: yes, 100%, Python is a great tool for web development.
The 28 Best Places to Learn Python Online
Apr 28, 2023 by natalie
For anyone who wants to learn Python online, it can be a bit overwhelming. When I Googled it, I came up with over 200 million hits.