Course Content
1.
Python Variables
0 min
3 min
9
2.
The Print Function
4 min
4 min
0
3.
Numbers and Math in Python
0 min
1 min
0
4.
What is Machine Learning?
0 min
6 min
0
5.
Strings in Python
0 min
11 min
0
6.
Comments in Python
0 min
4 min
0
7.
Functions in Python
0 min
26 min
0
8.
String Formatting with F-Strings
0 min
3 min
0
9.
Conditionals, Booleans, and If Statements
0 min
12 min
0
10.
Intro to Python Lists
0 min
6 min
0
11.
Intro to Python Lists - Exercises
0 min
2 min
6
12.
Lists as a Sequence of Values
0 min
6 min
0
13.
Coming Soon...
0 min
1 min
0
- Save
- Run All Cells
- Clear All Output
- Runtime
- Download
- Difficulty Rating
Loading Runtime
How to declare a new variable in Python
Here's how to make a new variable in Python:
Try it in a code cell:
Displaying a variable's stored value
If you write a variable all by itself in a code cell or as the last line of code in a code cell, its value will be displayed in the output section below the cell.
30
You can also use the print()
function to display the value stored within a variable.
30
Variable naming rules
Variable names can only consist of:
- letters (uppercase or lowercase)
A->Z
ora->z
- underscores:
_
- numbers:
0-9
HOWEVER, a variable name cannot start with a number.
For example, the following variable name: 49ers
is not a valid variable name because its first characer is a number (digit).
So the following code will throw an error due to the invalid variable name:
File "<exec>", line 1
49ers = 'An American football team.'
^
SyntaxError: invalid decimal literal
Variable naming conventions (you should use "snake case")
It's optional (but a best practice) to use something called "snake case" when naming your variables in Python.
A variable that is in "snake case" uses:
- All lowercase letters
- Words are separated by underscores
For example the variable snake_case_variable
conforms to the snake case convention.
Is variable name that uses "snake case"
Variable names are case sensitive
Capitalization matters when it comes to variables. If two variable names don't have the same capitalization then they will be viewed by Python as being distinct variables.
A clever and handsome instructor.
A tiresome and longwinded talker.
Python's "reserved keywords" cannot be used as variable names
Python has 33 reserved keywords. These are words that have been assigned functionality within the Python language. In order to avoid conflicting usage, it is not allowed to use any of the reserved keywords as variable names.
If you unluckily tried to use one of these words as a variable name on accident, your coding tool's syntax-highlighting would likely try to warn you by coloring the reserved keyword blue or purple (usually one of those two colors) after you type it. This is the case for most notebook editors.
Reserved Keywords
False | None | True | and |
as | assert | break | class |
continue | def | del | elif |
else | except | finally | for |
from | global | if | import |
in | is | lambda | nonlocal |
not | or | pass | raise |
return | try | while | with |
yield |
Notice in the image below how the acceptable variable name is black but the reserved keywords are either blue or purple in color.
Exercise 1 - Create an age
variable
Create a variable called age
and assign it the value of your age in years as an integer.
You don't have to put your actual age if you don't want to, but the value you store to this variable should be:
- An integer
- Greater than 0
- Less than 120
Exercise 2 - Create a height_cm
variable
Create a variable called height_cm
and assign it the value of your height in centimeters as an integer.
You don't have to put your actual height if you don't want to, but the value you store to this variable should be:
- An integer
- Greater than 0
- Less than 228
Exercise 3 - Create a weight_kg
variable
Create a variable called weight_kg
and assign it the value of your weight rounded to the nearest whole kilogram.
You don't have to put your actual weight if you don't want to, but the value you store to this variable should be:
- An integer
- Greater than 0
- Less than 651
Exercise 4 - Create a number_of_pets
variable
Create a variable called number_of_pets
and assign it the the number of pets owned in your household.
The value you store to this variable should be:
- An integer
- 0 or greater.
- Less than 5000. (Sorry if you actually own 5000 or more pets).
Exercise 5 - Create a number_of_cars
variable
Create a variable called number_of_cars
and assign it the value of however many cars are owned by your household.
The value you store to this variable should be:
- An integer
- 0 or greater.
- Less than 5000. (Sorry if you actually own more than 5000 cars)
Exercise 6 - create a power_level
variable
Declare a variable called power_level
and assign it the value of 9001
.
Exercise 7 - create a hardy_ramanujan
variable
Declare a variable called hardy_ramanujan
and assign it the value of 1729
.
When you're done writing your code make sure you hit the "play button" on the left-hand side to execute the code. Once the code has been executed, you can test your solution to see if it's correct by hitting the "submit" button on the right-hand side.
Exercise 8 - create an annual_salary
variable
Declare a variable called annual_salary
and assign it the value of 90000
.
Exercise 9 - reassign the annual_salary
variable a new value
Congrats! You got a raise! Re-assign the value of annual_salary
a new value of 95000
.
You can explicitly re-assign the value of a variable at any time in Python. Doing this will overwrite the previous value that was stored to the variable.