livepython.net
CHAPTER 04Variables
4.4  TYPES

Python Variable Types: str, int, float and bool

Every Value Has a Type

Every value in Python has a type: the kind of value it is. Text is one type, whole numbers are another, decimal numbers another.

Think of a kitchen: water goes in a bottle, rice in a jar, eggs in a box. Each kind of thing has its own container, and you handle it in its own way. Python treats each type of value in its own way too.

Python figures out the type for you

In some languages, like C or Java, you must say the type when you create a variable. In Python, you don't:

PYTHONedit me
city = "London"
score = 100
print(city, score)
OUTPUT
London 100

Python looks at the value and knows: "London" is text, 100 is a whole number. This is called dynamic typing.

Why types matter

The type decides what you can do with a value. You can multiply numbers, but not names:

PYTHONedit me
print(5 * 2)
print("5" * 2)
OUTPUT
10
55

The same * 2 does two different things. With the number 5, it multiplies. With the text "5", it repeats it. Same symbols, different types, different results. That's why knowing a value's type helps you predict what your code will do.

The four basic types

TypeName in PythonExample
Textstr (string)"Sunny"
Whole numberint (integer)25
Decimal numberfloat19.99
True or Falsebool (boolean)True

You'll meet each one in the next lessons.

Every value has a type, and Python works it out from the value.

your turnPick the right idea

Do you have to tell Python a variable's type?

Think about it, then pick one.

Strings, Integers and Floats

The three types you've used most are text and two kinds of numbers.

str: text

A string (str) is text inside quotes: names, messages, even a phone number.

PYTHONedit me
city = "Delhi"
phone = "9876543210"
print(city, phone)
OUTPUT
Delhi 9876543210

The phone number is a string because you never do maths with it.

int: whole numbers

An integer (int) is a whole number, with no decimal point: counts, ages, scores. It can be negative too.

PYTHONedit me
age = 25
lives = 3
temperature = -5
print(age, lives, temperature)
OUTPUT
25 3 -5

float: decimal numbers

A float is a number with a decimal point: prices, heights, averages.

PYTHONedit me
price = 19.99
height = 1.75
print(price, height)
OUTPUT
19.99 1.75

Careful: 10.0 is a float, even though it's a whole amount. The decimal point decides the type.

"25" and 25 again

You saw in chapter 2 that "25" and 25 look the same when printed. Now you know why they're different: "25" is a str, and 25 is an int. Only the int can do maths.

Text in quotes is str, whole numbers are int, numbers with a decimal point are float.

your turnPick the right idea

What type is 19.99?

Think about it, then pick one.
your turnPredict the output

What will this print?

print("Price:", 5 * 2)
Your turn! What will this code show on the screen?

Booleans: True and False

A boolean (bool) has only two possible values: True or False.

PYTHONedit me
is_logged_in = True
has_paid = False
print(is_logged_in, has_paid)
OUTPUT
True False

Think of a light switch: it's either on or off. Nothing in between.

How to write them

  • Always with a capital first letter: True and False.
  • No quotes. In quotes, "True" is just text.
PYTHONedit me
is_open = true
OUTPUT
NameError: name 'true' is not defined

With a small t, Python looks for a variable called true, and doesn't find one.

What they're for

Booleans answer yes-or-no questions:

VariableQuestion it answers
is_open = TrueIs the shop open?
has_ticket = FalseDoes the user have a ticket?
game_over = FalseIs the game over?

A good boolean name reads like a question: is_..., has_..., can_....

Booleans become really powerful in chapter 8, with if. Remember the diamond in a flowchart? Its yes-or-no answer is a boolean.

A boolean is True or False: capital letter, no quotes.

your turnFill the gap

Make is_open a real boolean

is_open = ? print(type(is_open))

It should print: <class 'bool'>

Which piece makes this code work?

Check a Type with type()

Not sure what type a value is? Ask Python with the built-in type() function:

PYTHONedit me
score = 50
print(type(score))
OUTPUT
<class 'int'>

Python answers <class 'int'>. For now, read class as "type": score holds an int.

All four types

PYTHONedit me
print(type("Sunny"))
print(type(25))
print(type(19.99))
print(type(True))
OUTPUT
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>

When it helps

  • When output surprises you. If "5" * 2 gives 55, type() shows you that "5" is a str.
  • When you're learning. Try type() on any value you're unsure about. It's a quick way to check your understanding.
  • When values come from users. In the Input chapter, you'll see that typed values are always strings at first, and type() proves it.

type() works on values and on variables. With a variable, it tells you the type of the value inside.

Common beginner doubt: "Why does it print class?" In Python, every type is a "class", a blueprint for values. You'll learn about classes in the last chapter. For now, class 'int' simply means "type int".

type() tells you the type of any value or variable.

your turnType the output

Type what this prints

print(type(3.0))
Read the code line by line, then type the output exactly.
your turnPredict the output

What will this print?

x = "100" print(type(x))
Your turn! What will this code show on the screen?

Practice: Types

Solve these to lock in what you learned in this topic. Each one runs in your browser.

  1. 17.Python Program to Create and Print a Boolean VariableEasySolve
  2. 26.Python Program to Check the Type of a VariableEasySolve
  3. 34.Python Program to Print a Shopping Bill with VariablesHardSolve