Skip to content

Python Journey

My Coding Blog

Menu
  • Links
Menu

Python>>Data Type:Integer

Posted on August 23, 2022August 23, 2022 by coden00b

Integers or Int are whole numbers without the decimal point or fractional part. It can be positive or negative or zero.It can be binary, octal or hexadecimal values.

Let’s do mathematical calculation:

Arithmetic operators:

 

addition = +
subtraction = –
multiplication = *
division = /
modulus = %
exponent = **
floor division = //
-modulus will output the remainder of the division
-exponent will output the exponent
-floor division will  output quotient in which the digits after the decimal point are removed.
Examples:

 

>>> 4+2
6
>>> 4-2
2
>>> 4*2
8
>>> 4/2
2.0
>>> 9/4
2.25
>>> 9%4
1
>>> 9//4
2
So is like 9 divided by 4 = 2 remainder 1.So the quotient without the remainder is the floor division.
So from what I learned, if you use division , the quotient of two whole numbers (int) becomes float because it has decimal point.
Example:
>>> 20/2
10.0
>>> div = 20/2
>>> print (div)
10.0
>>> print (type(div))
<class ‘float’>

 

>>> x = 8+8
>>> print (x)
16
>>> print (type(x))
<class ‘int’>
Some key things to take note:
1. Leading zeroes are not allowed to represent integeer or whole numbers
e.g.
>>> 012345
  File “<stdin>”, line 1
    012345
         ^
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers
2. It does not allow comma to use as delimeter, use the symbol underscore (_)
e.g.
>>> 1,2,3,4
(1, 2, 3, 4)
>>> 1_2_3_4
1234
3. Do not use decimal point, else it will become a float data type
e.g.
>>> x = 100
>>> print (type(x))
<class ‘int’>
>>> x = 100.0
>>> print (type(x))
<class ‘float’>
4. As mentioned above, integers  can be binary, octal or hexadecimal values, but need to use these characters in front of it
binary = 0b
hexadecimal = 0x (or 0X)
octal = 0o (or 0O)
e.g.
decimal= 100
if convert becomes:
binary = 1100100
hexadecimal = 64
octal = 144
>>> 100
100
>>> 0b1100100
100
>>> 0x64
100
>>> 0o144
100
5. there’s a function called int() to convert strings to int
>>> x = 12.34
>>> int(x)
12
>>> x = 777.888
>>> int (x)
777
>>> int(‘100’)
100
If you directly put it inside the int function, you will get something like this error:
>>> int (‘777.888’)
Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
ValueError: invalid literal for int() with base 10: ‘777.888’
Since it is a float, need to do something like this:
>>> int(float(‘777.888’))
777

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • The Zen of Python
  • Python>>String Interpolation and Formatting
  • Python>>String Methods
  • Python>>String Concatenation
  • Python>>Strings: Indexing and Slicing

Recent Comments

No comments to show.

Archives

  • January 2023
  • August 2022

Categories

  • Blog
  • Data Types