Checking types
print(type(10)) print(type(3.5)) print(type("Kolkata")) print(type(True))
Write Python. Understand every error.
Python basics · topic 4 of 25
Every value in Python has a type, which decides what you can do with it. The four basic types are:
int: whole numbers such as 7, -3, 2026float: numbers with a decimal point such as 3.14 or 7.0str: text in quotes such as "hello" or "42"bool: the two truth values True and FalseThe function type() tells you the type of any value. Notice that "42" is a string, not a number, because it is inside quotes.
print(type(42)) print(type("42"))
You can convert between types with int(), float(), str() and bool(). Converting a float to an int cuts off the decimal part without rounding, so int(7.9) is 7.
The type matters: 5 + 5 is 10, but "5" + "5" is "55", because + joins strings together.
int (whole numbers), float (decimals), str (text) and bool (True or False)."42" in quotes is text, not a number.int(), float() and str(). int(7.9) gives 7: it cuts off the decimal part and does not round.type(x) tells you what kind of value you have."5" + 3. Read the TypeError guide.int("12a"). Read the ValueError guide.int(3.9) to round to 4. It gives 3.
print(type(10)) print(type(3.5)) print(type("Kolkata")) print(type(True))
print(int("25") + 5) print(float("2.5") * 2) print(str(100) + " rupees") print(int(7.9))
print("5" + "5") print(5 + 5) print("ha" * 3)
5 quick questions to check your understanding. Choose an answer for each, then press Check answers.
print(type(7.0))
A number with a decimal point is a float, even if the fraction is zero.
print("5" + "3")
Both are strings, so + joins them.
print(int("12") + 3)
int("12") turns the text into the number 12.
print(int(7.9))
int() removes the decimal part; it does not round.
"5" + 3 give?Python will not add text and a number. Convert first: int("5") + 3 or "5" + str(3).