print(type(52)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type('hello')) # <class 'str'><class 'int'>
<class 'float'>
<class 'str'>
type() to identify the type of a value or variable.int(), float(), and str().The Basics lesson introduced the three fundamental types — integers (int), floats (float), and strings (str). Use the built-in type() function at any time to check what type a value or variable has:
<class 'int'>
<class 'float'>
<class 'str'>
The variable is just a label — it is the value that carries the type. A value’s type determines which operations make sense. For exmaple, arithmetic subtraction works on numbers but not on strings:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[3], line 1 ----> 1 print('hello' - 'h') TypeError: unsupported operand type(s) for -: 'str' and 'str'
However, + and * do work on strings — they just mean something different. Adding two strings concatenates them, and multiplying a string by an integer repeats it:
Strings have a length, countable with len(). Numbers do not — calling len() on an integer raises a TypeError:
You cannot mix numbers and strings in arithmetic directly. Python refuses 1 + '2' because the intent is ambiguous — should the result be the number 3 or the string '12'? Use int(), float(), or str() to convert explicitly first:
float() will convert a numeric string like "3.4" to a float. int() will truncate a float to its integer part, discarding the decimal. Conversions that do not make sense raise a ValueError:
Note that int("3.4") also raises a ValueError — Python does not chain conversions automatically. You must convert in steps: int(float("3.4")).
When an expression contains both integers and floats, Python automatically promotes the integer to a float so no precision is lost. The / operator always returns a float in Python 3, even when both operands are integers:
half is 0.5
three squared is 9.0
integer division: 3.5
To get a whole-number result from division, use // (floor division). The % operator gives the remainder:
Unlike a spreadsheet cell, a Python variable does not update automatically when the values it depended on change. Assignment copies the current value — the connection to any previous expression is severed immediately:
first is 2 and second is 5
When Python evaluated 5 * first, it computed 5, stored that number in second, and moved on. Changing first later has no effect on second.
What type of value is 3.4? How can you find out?
What type of value is 3.25 + 4?
What type of value (integer, float, or string) would you use to represent each of the following? Try to think of more than one defensible answer for each.
Given the three division operators in Python 3:
5 // 3: 1
5 / 3: 1.6666666666666667
5 % 3: 2
If num_subjects is the number of subjects in a study and num_per_survey is the number who can participate in one survey, write an expression that calculates the minimum number of surveys needed to reach everyone at least once.
float() converts a numeric string to a float, and int() truncates a float to an integer:
string to float: 3.4
float to int: 3
What do you expect the following to do? What does it actually do, and why?
Which of the following expressions produce 2.0? There may be more than one correct answer.
first + float(second)float(second) + float(third)first + int(third)first + int(float(third))int(first) + int(float(third))2.0 * secondAnswers 1 and 4 produce 2.0.
Python supports complex numbers written as 1.0+2.0j. The real and imaginary parts are accessible as .real and .imag.
j rather than i for the imaginary unit?1+2j + 3 produce?4j? What about 4 j or 4 + j?i is already used for current. Python adopted the engineering notation early on and it has never changed — see the Stack Overflow discussion for more history.4+2j — the integer 3 is promoted to complex.4j is the imaginary number \(4i\). 4 j is a syntax error (space not allowed). 4 + j is a NameError unless j is a defined variable.int, float, or str are the most common.type() to find out what type a value has.+ concatenates strings; * repeats them. Other arithmetic operators do not work on strings.int(), float(), and str() — conversions must be explicit./ always returns a float.