Pylerno
English

Types and conversion

Distinguish strings, integers, floats, booleans, and None, then convert deliberately.

On this page

The concept

Types determine what operations mean. 10 + 2 adds integers, while "10" + "2" joins strings. The visible digits are similar, but the values have different behavior. The main scalar types introduced here are int, float, str, and bool. None represents the absence of a value; it is not the string "None".

type(value) reveals a value's type. Conversion functions create a value of another type when the input is valid. int("10") parses integer text. float("3.5") parses decimal text. str(42) creates text. int(3.9) discards the fractional part toward zero; it does not round to the nearest integer.

Worked example

python
count = 10
price = 12.5
name = "Sara"
is_active = True
missing = None
print(type(count), type(price), type(name), type(is_active), type(missing))
raw_count = "10"
print(raw_count + "2")
print(int(raw_count) + 2)
print(float("3.5"), str(42), int(3.9))

Trace the result

  1. The first group binds one example of each type, then prints their types together.
  2. The raw_count variable contains text. Joining it to "2" produces "102"; converting first and adding the integer 2 produces 12.
  3. The last group demonstrates three conversions. Pay attention to the result of int(3.9), because truncation is different from rounding.
text
<class 'int'> <class 'float'> <class 'str'> <class 'bool'> <class 'NoneType'>
102
12
3.5 42 3
Line-by-line walkthrough

Line 1: count = 10

Evaluate 10, then bind or update count with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.

Line 2: price = 12.5

Evaluate 12.5, then bind or update price with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.

Line 3: name = "Sara"

Evaluate 'Sara', then bind or update name with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.

Line 4: is_active = True

Evaluate True, then bind or update is_active with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.

Line 5: missing = None

Evaluate None, then bind or update missing with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.

Line 6: print(type(count), type(price), type(name), type(is_active), type(missing))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 7: raw_count = "10"

Evaluate '10', then bind or update raw_count with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.

Line 8: print(raw_count + "2")

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 9: print(int(raw_count) + 2)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 10: print(float("3.5"), str(42), int(3.9))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Common mistake

bool("False") is True because the string is not empty. Parsing a yes/no setting requires checking the text, not merely calling bool.

Check your understanding

What does this program print?

python
print("4" + "2")
Choose one answer

Search documentation

Share this page