AI Hints
Home›Python Tutorial›Welcome to Python
Python · Lesson 3 of 49

Variables and Types

Boxes with labels, and the four basic kinds of values.

⏱ 3 min read▶ Video at 7:10🎓 Free
Watch this lesson in the Python courseWatch on YouTube ↗Full Python course

Ava sells a monstera plant for twenty four dollars. But tomorrow the price changes. She does not want to hunt for every twenty four in her program, and fix each one by hand. She wants to store the price once, with a name, and use that name everywhere. That is exactly what a variable does.

Variables and Types
From the video: Variables and Types
  • The problem: the same number repeated everywhere
  • The idea: store it once, with a name
  • The tool: a variable

Variables are labeled boxes

A variable is a name that points to a value. Think of a box with a label. The label is the variable name, and the thing inside is the value. In Python, you create one with the equals sign. The name goes on the left, and the value on the right. Here, price holds twenty four. Later, we put a new value in the box, and it simply holds the new one.

variabletypevalue
priceint26
plantstr"monstera"
price now holds 26
Variables are labeled boxes
From the video: Variables are labeled boxes

Using variables

In code, the equals sign does not mean equal, like in mathematics. It means, store this value under this name. Once stored, we can use the name anywhere. Here, we print the plant and the price. Then we change the price, and print again. The old value is gone, replaced by the new one.

shop.pypython
plant = "monstera"
price = 24
print(plant, price)
price = 26
print(plant, price)
Output
monstera 24
monstera 26

The four basic types

Every value has a type. Whole numbers are integers, called int. Numbers with a decimal point are floats. Text is called a string, or str, and is written inside quotes. And true or false values are booleans, called bool. Python works out the type by itself, so you never have to say it.

TypeWhat it holdsExampleIn Ava's shop
intwhole numbers24plants in stock
floatdecimal numbers24.99the price
strtext, in quotes"monstera"the plant name
boolTrue or FalseTrueis it on sale?

Asking Python for the type

You can ask for the type of any value with the type function. Python answers with the class. Twenty four is an int. Twenty four point nine nine is a float. Quoted text is a str. And True is a bool. A variable has the type of the value inside it, so plants, holding twelve, is an int.

type(24)
<class 'int'>
type(24.99)
<class 'float'>
type("monstera")
<class 'str'>
type(True)
<class 'bool'>
plants = 12
type(plants)
<class 'int'>

Naming rules

Names have rules. Use letters, digits, and underscores. Never start with a digit. No spaces, and no special symbols. Names are case sensitive, so price and Price are different. By convention, Python programmers write names in lowercase, with underscores between words. That style is called snake case. Choose clear names, like plant price, never just x.

Letters, digits, underscoresno spaces or symbols
Cannot start with a digit2plants is not allowed
Case sensitiveprice and Price differ
snake_case stylelowercase, words joined by _
Clear names winplant_price, not x

A tiny shop report

Let's put it together for Ava. We store the plant name, the price, how many are in stock, and whether it is on sale. Then we print a small report, and the type of the price. Notice how the names make the code readable. Anyone can understand it, even without comments.

report.pypython
plant_name = "Monstera"
price = 24.99
stock = 12
on_sale = True
print(plant_name)
print(price)
print(stock)
print(on_sale)
print(type(price))
Output
Monstera
24.99
12
True
<class 'float'>

Make your own variables

Your turn. Create variables for a plant name, its price, and how many you have. Then print each one. For a bonus, print the type of the price. Ready? Here is one solution. Three variables, then four prints. Your plant can be anything you like.

✍️ Exercise: Make your own variables

Create variables for a plant name, price and stock, print them, and print type(price).

Show hint

Three assignments, then print.

Show solution
myplants.pypython
plant = "Fern"
price = 9.5
stock = 40
print(plant)
print(price)
print(stock)
print(type(price))
Output
Fern
9.5
40
<class 'float'>

Summary

Great work. You learned that a variable is a name that points to a value. You met the four basic types, int, float, string, and bool. And you know the rules, and the style, for naming things. Next, Ava does some arithmetic, because she needs to add up her orders.

Key takeaways
  • A variable is a labeled box for a value
  • int, float, str and bool are the basic types
  • Use clear snake_case names
Next up: Numbers and Maths — add up orders, discounts and totals