Data types in python
Integer Data Types in Python
In Python, integers are whole numbers, positive or negative,
without decimals, of unlimited length.
Declaring an Integer
To declare an integer, simply assign a whole number to a
variable.
x = 10
Integer Operations
Python supports various mathematical operations with integers,
such as addition, subtraction, multiplication, and division.
x = 10
y = 2
, print(x + y) # prints 12
print(x - y) # prints 8
print(x * y) # prints 20
print(x / y) # prints 5.0
Type Conversion
Python allows converting other data types to integers, using
the int() function.
print(int(3.7)) # prints 3
print(int('5')) # prints 5
Non-decimal Integers in Python
Python supports non-decimal integers, using the 0b prefix for
binary, 0o prefix for octal and 0x prefix for hexadecimal.
Binary Numbers
x = 0b1010 # binary number
print(x) # prints 10
Octal Numbers
x = 0o12 # octal number
print(x) # prints 10
Hexadecimal Numbers
x = 0xA # hexadecimal number
print(x) # prints 10
Converting Base n to Base 10
To convert a number from another base to base 10, use
the int() function with the base specified
Binary to Decimal
Integer Data Types in Python
In Python, integers are whole numbers, positive or negative,
without decimals, of unlimited length.
Declaring an Integer
To declare an integer, simply assign a whole number to a
variable.
x = 10
Integer Operations
Python supports various mathematical operations with integers,
such as addition, subtraction, multiplication, and division.
x = 10
y = 2
, print(x + y) # prints 12
print(x - y) # prints 8
print(x * y) # prints 20
print(x / y) # prints 5.0
Type Conversion
Python allows converting other data types to integers, using
the int() function.
print(int(3.7)) # prints 3
print(int('5')) # prints 5
Non-decimal Integers in Python
Python supports non-decimal integers, using the 0b prefix for
binary, 0o prefix for octal and 0x prefix for hexadecimal.
Binary Numbers
x = 0b1010 # binary number
print(x) # prints 10
Octal Numbers
x = 0o12 # octal number
print(x) # prints 10
Hexadecimal Numbers
x = 0xA # hexadecimal number
print(x) # prints 10
Converting Base n to Base 10
To convert a number from another base to base 10, use
the int() function with the base specified
Binary to Decimal