11. Int type#

Author: Tue Nguyen

11.1. Outline#

  • Why int type?

  • How to get an int?

  • Conversion to int

  • When 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: int

    • Possible 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 ints

  • A call to a function that returns an int

  • Typecasting 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 to int

  • Conversion to int is 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

    • bool to int: True -> 1 and False -> 0

    • float to int: the decimal part will be truncated

    • str to int: the string must look like an integer

      • Whitespaces at two ends will be ignored

      • Leading zeros will be ignored

    • Conversion to int is NOT ALWAYS possible

How to get an int?

  • From int literals: x = 1000

  • From an expression that produces an int such as

    • Math operations between ints: 500 + 200

    • Call 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 x with value 100

  • Show its value and type

11.7.2. Exercise 2#

Do the following

  • Create a variable x with value 10.5

  • Show the value and type of x

  • Create a variable y by applying int() on x

  • Show the value and type of y

11.7.3. Exercise 3#

Gives three examples that produce an integer