Int type
Contents
11. Int type#
Author: Tue Nguyen
11.1. Outline#
Why
inttype?How to get an
int?Conversion to
intWhen uses
int?Operations on
int
11.2. Why int type?#
Motivation: used to represent integers such as count values
Real-life data: number of children, years of educations, number of items purchased
Python implementation:
Type:
intPossible values: any integers
11.3. How to get an int?#
a) Ex 1: from int literals
# Init an int variable
x = 10
print(x)
print(type(x))
10
<class 'int'>
# Integers in Python can be arbitrarily long
# there is no overflow behavior as in some other languages
x = 3565622255488771132323355767676856586585965999884384
print(x)
print(type(x))
3565622255488771132323355767676856586585965999884384
<class 'int'>
b) Ex 2: from an expression that produces a int
Here are some examples of expressions producing ints
Math operations between
intsA call to a function that returns an
intTypecasting to
int
b1) Math operations between ints
# Init two variable
x = 4
y = 2
# Addition
x + y
6
# Subtraction
x - y
2
# Multiplication
x * y
8
# Exponential
x**y
16
# Integer division
x // 2
2
# Modulus
x % 2
0
b2) A call to a function producing an int
# Sum of a list of integers
x = [1, 2, -2, 0, 7]
sum(x)
8
# Count the number of elements of a list
len(x)
5
b3) Typecasting to int
Use
int()to convert a value tointConversion to
intis NOT always
# From None to int
# Not possible
# From a bool to int
# True -> 1
# False -> 0
print(int(True))
print(int(False))
1
0
# From float to int
# The decimal part will be truncated
int(2.5)
2
# From str to int
# Only strings that look like integers can be converted
print(int("50"))
print(int("-50"))
50
-50
# White space at two ends are ignored
print(int(" 50 "))
print(int(" -50 "))
50
-50
# Leading zeros are also ignored
int("050")
50
# However, "50,000" or "50.0" can not be converted to int
# Try them to see the errors
11.4. When uses int?#
Any time we need an integer
11.5. Operations on int#
Math operations:
+, -, *, /, //, %, **Comparison operations:
==, !=, >=, <=, >, <, is, is not
11.6. Summary#
Why int type?
Used to represent integers such as count values
Conversion to int
Use
int()Rules
booltoint:True->1andFalse->0floattoint: the decimal part will be truncatedstrtoint: the string must look like an integerWhitespaces at two ends will be ignored
Leading zeros will be ignored
Conversion to
intis NOT ALWAYS possible
How to get an int?
From
intliterals:x = 1000From an expression that produces an
intsuch asMath operations between
ints:500 + 200Call a function that returns an
int:len(x)Typecasting:
int(x)
When uses int?
Any time we need an integer
Operations on int
Math operations
Comparison operations
11.7. Practice#
11.7.1. Exercise 1#
Do the following
Create a variable
xwith value100Show its value and type
11.7.2. Exercise 2#
Do the following
Create a variable
xwith value10.5Show the value and type of
xCreate a variable
yby applyingint()onxShow the value and type of
y
11.7.3. Exercise 3#
Gives three examples that produce an integer