livepython.net
CHAPTER 05Working With Data Types
5.4  NONE & CONVERSION

Python None and Type Conversion (int, float, str)

None: No Value Yet

Sometimes you need a variable, but you don't have its value yet. Python has a special value for that: None.

PYTHONedit me
phone_number = None
print(phone_number)
print(type(phone_number))
OUTPUT
None
<class 'NoneType'>

Think of None as an empty box with a label on it. The box exists, but there's nothing inside yet.

Fill it later

PYTHONedit me
winner = None
print("Winner:", winner)
winner = "Sunny"
print("Winner:", winner)
OUTPUT
Winner: None
Winner: Sunny

Before the game ends, there's no winner, so None says exactly that.

None is not 0 and not ""

ValueMeans
0the number zero, a real amount
""an empty string, real text with no characters
Noneno value at all

A score of 0 means the player scored nothing. A score of None means we don't know the score yet. That difference matters.

How to write it

  • Capital N, no quotes: None.
  • "None" in quotes is just a four-letter word, a string.
PYTHONedit me
print(type(None))
print(type("None"))
OUTPUT
<class 'NoneType'>
<class 'str'>

Common beginner doubt: "Why does print() show None sometimes?" You'll see in the Functions chapter that a function which doesn't give back a value gives back None. Keep that in mind.

None means "no value yet". It's not 0, and it's not an empty string.

your turnPick the right idea

A player hasn't played yet. Which value best says "no score yet"?

Think about it, then pick one.

Turning Text into Numbers: int() and float()

"5" looks like a number, but it's text, so it can't do maths:

PYTHONedit me
print("5" + "2")
OUTPUT
52

+ joined the two strings! To do maths, convert the text to a number first. This is called type conversion.

int() and float()

PYTHONedit me
print(int("5") + 2)
print(float("2.5") + 1)
OUTPUT
7
3.5
  • int("5") turns the text "5" into the whole number 5.
  • float("2.5") turns the text "2.5" into the decimal number 2.5.

Think of it as translating: the value means the same, but now Python treats it as a number.

Why this matters soon

In the Input chapter, everything a user types arrives as text, even "25". You'll convert it with int() or float() before calculating. This lesson is the key to that.

When conversion fails

The text must look like a number:

PYTHONedit me
print(int("hello"))
OUTPUT
ValueError: invalid literal for int() with base 10: 'hello'

Don't be scared of this message: "invalid literal for int()" just means "this text isn't a whole number". int("2.5") fails the same way, because 2.5 isn't whole. Use float() for that.

int() and float() turn number-looking text into real numbers.

your turnFill the gap

Make it print 7

print(?("5") + 2)

It should print: 7

Which piece makes this code work?
your turnPredict the output

What will this print?

print("5" + "2")
Your turn! What will this code show on the screen?

Turning Numbers into Text: str()

Remember this error from the start of this chapter?

PYTHONedit me
age = 25
print("Sunny is " + age)
OUTPUT
TypeError: can only concatenate str (not "int") to str

+ can't join text and a number. One fix is to turn the number into text with str():

PYTHONedit me
age = 25
print("Sunny is " + str(age) + " years old")
OUTPUT
Sunny is 25 years old

str(25) gives the text "25", and text can be joined with text.

Three ways to show text and numbers

WayCode
commasprint("Sunny is", age, "years old")
str() and +print("Sunny is " + str(age) + " years old")
f-stringprint(f"Sunny is {age} years old")

All three print the same. Most Python programmers prefer f-strings: no spaces to manage, no conversion to remember. Commas are great for quick checks. str() is useful when you need the text itself, for example to count digits:

PYTHONedit me
number = 2026
print(len(str(number)))
OUTPUT
4

len() doesn't work on numbers, but str() turns the number into text first, and text has a length.

Think of str() as putting a number in a frame: you can still see it, but now it's a picture, not a number you can calculate with.

str() turns a value into text, so you can join it with other text.

your turnFix the code

Make it print: Score: 90

It should print: Score: 90

score = 90
print("Score: " + score)
This code does not work yet. Fix it, then press Run.

int() Cuts Off Decimals

int() can also turn a float into a whole number. But careful: it doesn't round, it cuts off everything after the point:

PYTHONedit me
print(int(9.7))
print(round(9.7))
OUTPUT
9
10

int(9.7) drops the .7 and gives 9. If you want the nearest whole number, use round(), which gives 10.

Think of int() as scissors: it snips off the decimal part, no matter how big it is.

Converting the other way

float() turns a whole number into a float:

PYTHONedit me
print(float(5))
print(float("7"))
OUTPUT
5.0
7.0

All the conversions in one place

FunctionGivesExampleResult
int()a whole numberint("42"), int(3.9)42, 3
float()a decimal numberfloat("2.5"), float(5)2.5, 5.0
str()textstr(25), str(True)"25", "True"

When to use which

  • Counting things (people, items, levels): int.
  • Measuring (money, height, temperature): float.
  • Showing or joining with other text: str, or simply an f-string.

Common beginner doubt: "Does int() change the original variable?" No. It gives you a new value. To keep it, store it: price = int(price).

int() cuts off decimals; round() rounds them. Pick the one you mean.

your turnType the output

Type what this prints

print(int(7.9))
Read the code line by line, then type the output exactly.
your turnOrder the lines

Put the lines in order to print 12

  1. number = int(text)
  2. print(number)
  3. number += 2
  4. text = "10"
These lines are mixed up. Put them back in order, then press Check.

Practice: None & conversion

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

  1. 42.Python Program to Add Two Numbers Stored as TextEasySolve
  2. 43.Python Program to Print a Temperature ReportHardSolve