Every piece of data is a value
Every program, from a calculator to a chat app, is instructions working on data. Python sorts data into types because each type supports different operations: you can multiply two numbers, but multiplying two names makes no sense. Knowing a value's type tells you, and Python, exactly what you are allowed to do with it, and most beginner crashes are type mix-ups, so this pays off from day one.
A value is a single piece of data a program works with: the number 42, the text "hello", the answer True. Every value in Python has a type. The four types you will use constantly:
| Type | Name | Examples |
|---|---|---|
int | whole number | 42, -7, 0 |
float | decimal number | 3.5, -0.25 |
str | text (a string of characters) | "hello", "42" |
bool | truth value | True, False |
Text always sits inside quotes. That is how Python tells the text "42" apart from the number 42.
Your first two functions
print() and type() are functions: named instructions built into Python. You run a function by writing its name followed by parentheses, and the parentheses hold the value you want the instruction to work on. That placement is the actual rule of the language: Python reads print(42) as "run the instruction named print on the value 42". print() shows a value on the screen, and type() reports a value's type.
Printing values and inspecting their types
print shows a value on the screen, one line per call. The last two lines use type to report what kind of value it was given.
print("Hello, Python") print(42) print(3.5) print(True) print(type(42)) print(type("Hello"))
Output
Hello, Python 42 3.5 True <class 'int'> <class 'str'>
The first four lines print the values themselves, and the printed text looks much like what was written in the code. The last two print a description instead, because type hands back the value's type and print then displays that. Python spells those descriptions as <class 'int'> and <class 'str'>, where the word after class is the type name.
In type(42), the parentheses hold the input that the function should work on. A function is a named instruction, and the value it operates on goes inside the parentheses immediately after the name.
That is how Python knows type() should examine the value 42 rather than anything else on the line. The parentheses are also what makes the function actually run: written on its own, type merely names the function without invoking it.
Of 42, "42", True, and 3.14, the value with type str is "42", because quotes are what make a string.
"42" is text built from the two characters 4 and 2, so Python classifies it as str. Strip the quotes and 42 becomes an int, while True is a bool and 3.14 is a float. Those quotes are the entire difference, and the consequence is practical: arithmetic works on 42 but not on "42".
Each line of output comes from exactly one print(...) call, and the three calls below show a literal sentence followed by two type inspections.
print("Python has 4 basic types") print(type(2.5)) print(type(False))
Output
Python has 4 basic types <class 'float'> <class 'bool'>
The sentence needs quotes around it, since it is text rather than a number. The other two calls nest one function inside another: type(2.5) produces the type, and the surrounding print(...) displays it, which is why 2.5 reports float and False reports bool.