Python’s integer (int) type
Python uses int
to represent integers (or whole numbers) such as 10
, 100
, or -30
.
Create an integer
We can create an int
variable through an assignment with an integer literal, for example
x = 5
print(x)5type(x)int
We can also create an int
variable through an assignment with an expression that produces an integer. Here are some examples of expressions producing integers.
# Addition of integers
x = 5 + 10
print(x)
print(type(x))15
<class 'int'># Multiplication of integers
x = 5 * 10
print(x)
print(type(x))50
<class 'int'>
Integers in Python can be arbitrarily long (there is no overflow behavior as in some other languages).
# A very long int
x = 3565622255488771132323355767676856586585965999884384
print(x)
print(type(x))3565622255488771132323355767676856586585965999884384
<class 'int'>
When use int?
We use int
for values that should be integers such as age, years, number of children, etc.
Typecasting
Similar to what we have learned with bool
, we use int()
function to convert values from other types to int
. However, not all values can be converted to int
(only integer-like values can).
Example 1: values that can be converted to int
From bool
# True becomes 1
int(True)1# False becomes 0
int(False)0
From float
# Decimal part is truncated
int(12.34)12
From str
# Strings that look like an integer
x = int("10")
print(x)
print(type(x))10
<class 'int'># White spaces at two ends are ignored
x = int(" -10 ")
print(x)
print(type(x))-10
<class 'int'># Leading zeros are also ignored
x = int(" -00010 ")
print(x)
print(type(x))-10
<class 'int'>
Operations on int
Arithmetic operations
Addition, subtraction, multiplication
print(2 + 3)
print(2 - 3)
print(2 * 3)5
-1
6
Real division (return real numbers, or float
)
2 / 30.6666666666666666# Even 4 is divisible by 2
# We still get a float back
4 / 22.0
Integer division (get the quotient)
2 // 30
Modulus (get the remainder)
2 % 32
Exponential
2**38
Negation
-10-10x = 10
print(-x)-10
Note that a bool
is actually an integer under the hood with True
and False
equivalent to 1
and 0
.
Let’s confirm this.
# Same value
print(True == 1)
print(False == 0)True
True# Use isinstance
print(isinstance(True, int))
print(isinstance(False, int))True
True
Thus, we can perform arithmetic on bool
and int
, and the result is promoted to type int
(more general)
# Bool plus bool
x = True + True
print(x)
print(type(x))2
<class 'int'># Int plus bool
x = 5 + True
print(x)
print(type(x))6
<class 'int'>
Comparision operations
Comparisons on numbers (int
or float
) always return a bool
. (Already covered in the previous article)
However, strings like "1,000,000"
(contains ,
) or "12.34"
are not valid. Try them yourself to see errors.
Practice
Ex 1
Do the following
- Create a variable
x
with value100
- Show its value and type
Ex 2
Do the following
- Create a variable
x
with value10.5
- Show the value and type of
x
- Create a variable
y
by applyingint()
onx
- Show the value and type of
y
Ex 3
Gives three examples that produce an integer
Navigation
Previous: Python’s truth values (bool) type
Next: Python’s real number (float) type