• 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 or a->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:

  1. All lowercase letters
  2. 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

FalseNoneTrueand
asassertbreakclass
continuedefdelelif
elseexceptfinallyfor
fromglobalifimport
inislambdanonlocal
notorpassraise
returntrywhilewith
yield

Notice in the image below how the acceptable variable name is black but the reserved keywords are either blue or purple in color.

Python Reserved Keywords with Syntax Highlighting

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
Fun Fact:The person with the oldest verifiable age was Jeanne Calment who lived to be 122 years and 164 days old.
Reset Code

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
Reset Code

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
Fun Fact:Jon Brower Minnoch was the heaviest person ever recorded reaching a weight of 650 kilograms.
Reset Code

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).
Reset Code

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)
Fun Fact:The largest private car collection is owned by Sultan of Brunei –Hassanal Bolkiah. His collection boasts over 5000 cars and has an estimated value of more than five billion dollars.
Reset Code

Exercise 6 - create a power_level variable

Declare a variable called power_level and assign it the value of 9001.

What did you say his power level is?? –It's over 9000!
Reset Code

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.

Reset Code

Exercise 8 - create an annual_salary variable

Declare a variable called annual_salary and assign it the value of 90000.

Fun Fact:According to Indeed.com the average salary for a Data Scientist in the United States is $124,300
Reset Code

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.

Take Note: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.
Reset Code