Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In Python, the int
data type is used to represent integer numbers (both positive and negative whole numbers). This tutorial will guide you through working with the int
data type in Python.
Creating integers:
You can create integers by writing a whole number without a decimal point:
# Creating integers x = 5 y = -3 print(x) # Output: 5 print(y) # Output: -3
You can also create integers from other data types using the int()
constructor:
# Creating integers from float and string x_float = 3.7 x_string = "42" x_int = int(x_float) y_int = int(x_string) print(x_int) # Output: 3 (truncates the decimal part) print(y_int) # Output: 42
Integer arithmetic:
Python supports basic arithmetic operations for integers, including addition, subtraction, multiplication, division, exponentiation, and modulo:
x = 5 y = 3 z_add = x + y z_sub = x - y z_mul = x * y z_div = x / y # Result is a float z_floor_div = x // y z_mod = x % y z_pow = x ** y print(z_add) # Output: 8 print(z_sub) # Output: 2 print(z_mul) # Output: 15 print(z_div) # Output: 1.6666666666666667 print(z_floor_div) # Output: 1 print(z_mod) # Output: 2 print(z_pow) # Output: 125
Integer comparison:
You can compare integers using comparison operators such as ==
, !=
, <
, >
, <=
, and >=
:
x = 5 y = 3 is_equal = x == y is_not_equal = x != y is_less_than = x < y is_greater_than = x > y is_less_than_or_equal_to = x <= y is_greater_than_or_equal_to = x >= y print(is_equal) # Output: False print(is_not_equal) # Output: True print(is_less_than) # Output: False print(is_greater_than) # Output: True print(is_less_than_or_equal_to) # Output: False print(is_greater_than_or_equal_to) # Output: True
Binary, octal, and hexadecimal representation:
Python supports binary, octal, and hexadecimal representations for integers using the 0b
, 0o
, and 0x
prefixes, respectively:
binary_num = 0b1011 octal_num = 0o755 hexadecimal_num = 0x1A3F print(binary_num) # Output: 11 print(octal_num) # Output: 493 print(hexadecimal_num) # Output: 6719
To convert integers to their binary, octal, or hexadecimal representation as strings, use the bin()
, oct()
, and hex()
functions, respectively.
Working with integers in Python:
my_integer = 42
Creating and initializing integer objects in Python:
my_integer = 123
Integer literals and constants in Python:
True
and False
.binary_literal = 0b1101 # Binary literal hex_literal = 0x1A # Hexadecimal literal
Operations and methods for working with integers in Python:
x = 5 y = 2 addition_result = x + y absolute_value = abs(-10)
Converting between integers and other data types in Python:
int()
, float()
, and str()
allow you to convert between integers and other data types.int_to_float = float(5) float_to_int = int(3.14)
Numeric operations and mathematical functions with integers in Python:
power_result = 2 ** 3 # Exponentiation square_root = 16 ** 0.5
Handling large integers in Python:
int
type in Python 3 has no maximum limit.large_integer = 123456789012345678901234567890
Common challenges with integer arithmetic in Python:
result = 5 / 2 # Results in a float, not an integer remainder = 10 % 3