x = 10Variables
Variable assignment stores data in a named container. In Python, use = to assign values: x = 5. Variables are case-sensitive and can store numbers, strings, lists, and other datatypes. Variables in Python are mutable meaning the value can be modified.
The cells below set a variable x, print it, update it, and print it again.
print(x)10
x = 20print(x)20
Naming Variables
Variables names should be short and meaningful. Long names take time to type and can make code dense. Uninformative names can get confusing or accidentally overwritten.
Python rules for variable names include:
- Letters, numbers, and underscores (_) only
- Must start with a letter or underscore
- Snake case is preferred (e.g., my_var)
- No spaces, dashes, or dots
- Can not be reserved Python word (e.g., if, return)
See the table below for more examples.
| Example | Valid? | Reason |
|---|---|---|
age |
✅ Yes | Starts with a letter, letters only |
user_name |
✅ Yes | Letters + underscore are allowed |
temperature2 |
✅ Yes | Digits allowed, but not at start |
_hidden |
✅ Yes | Leading underscore allowed (often used for “private” values) |
MAX_VALUE |
✅ Yes | Uppercase is allowed, often used for constants |
2nd_value |
❌ No | Cannot start with a digit |
user-name |
❌ No | Dash (-) not allowed |
file.path |
❌ No | Dot (.) means “attribute,” not part of a name |
my var |
❌ No | Spaces not allowed |
return |
❌ No | return is a reserved Python keyword |
Valid Variables
age = 10
print(age)10
user_name = "John Smith"
print(user_name)John Smith
temperature2 = 87
print(temperature2)87
_hidden = "secret value"
print(temperature2)87
MAX_VALUE = 1000000
print(MAX_VALUE)1000000
Non-Valid Variables
2nd_value = 2
print(2nd_value)Cell In[10], line 1 2nd_value = 2 ^ SyntaxError: invalid decimal literal
user-name = "John Smith"
print(user-name)Cell In[11], line 1 user-name = "John Smith" ^ SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?
file.path = "Users/dbugz/script.py"
print(file.path)--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[12], line 1 ----> 1 file.path = "Users/dbugz/script.py" 2 print(file.path) NameError: name 'file' is not defined
my-var = 10
print(my-var)Cell In[13], line 1 my-var = 10 ^ SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?
return = "Reserved word"
print(return)Cell In[14], line 1 return = "Reserved word" ^ SyntaxError: invalid syntax
Functions
Functions create reusable code which can simplify tasks, allow iterative runs, and can make your code much more powerful. Python comes with some builtin functions which will help demonstrate.
my_list = [1, 2, 27, 0]print(my_list)[1, 2, 27, 0]
type(my_list)list
len(my_list)4
min(my_list)0
max(my_list)27
str(my_list)'[1, 2, 27, 0]'
int(my_list)--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[22], line 1 ----> 1 int(my_list) TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'
lucky_number = "7020"print(lucky_number)
type(lucky_number)7020
str
lucky_int = int(lucky_number)print(lucky_int)
type(lucky_int)7020
int
lucky_float = float(lucky_number)print(lucky_float)
type(lucky_float)7020.0
float