• Home
  • 3.1 Integer Basics

3.1 Integer Basics

View Categories

3.1 Integer Basics

4 min read

An integer is a whole number without a decimal point. Integers can be positive, negative, or zero.

Python represents integers using the built-in int data type.

Integers are one of the most commonly used data types in programming. They are used to store values such as age, quantity, marks, population, and counts.

Unlike some programming languages, Python integers have unlimited precision. This means they can store very large numbers, limited only by the available memory.


Syntax #

variable_name = integer_value

Components #

Component Description
variable_name Name of the variable.
= Assignment operator.
integer_value A whole number without a decimal point.

Example 1: Positive Integer #

main.py

age = 25

print(age)
print(type(age))

Output #

25
<class 'int'>

Explanation #

  • 25 is a whole number.
  • Python automatically stores it as an integer.
  • The type() function confirms that the value belongs to the int data type.

Example 2: Negative Integer #

main.py

temperature = -15

print(temperature)
print(type(temperature))

Output #

-15
<class 'int'>

Explanation #

  • Integers can be negative.
  • The minus (-) sign indicates a negative value.
  • The value is still of type int.

Example 3: Zero #

main.py

count = 0

print(count)
print(type(count))

Output #

0
<class 'int'>

Explanation #

  • Zero is also an integer.
  • It is neither positive nor negative.
  • Python stores it as an int.

Example 4: Very Large Integer #

main.py

population = 823456789123456789123456789

print(population)
print(type(population))

Output #

823456789123456789123456789
<class 'int'>

Explanation #

  • Python supports extremely large integers.
  • Unlike many programming languages, Python does not limit integers to 32-bit or 64-bit sizes.
  • Python automatically allocates additional memory when required.

Example 5: Integer Arithmetic #

main.py

a = 25
b = 15

print(a + b)
print(a - b)
print(a * b)

Output #

40
10
375

Explanation #

  • + performs addition.
  • - performs subtraction.
  • * performs multiplication.
  • Since both operands are integers, the results are also integers.

Example 6: Integer Division #

main.py

a = 20
b = 5

print(a / b)

Output #

4.0

Explanation #

  • The / operator always performs floating-point division.
  • Even though both operands are integers, the result is a floating-point number (4.0).
  • Integer division using // will be discussed later in this chapter.

Example 7: Reading an Integer from the User #

main.py

age = int(input("Enter your age: "))

print(age)
print(type(age))

Sample Input #

25

Output #

25
<class 'int'>

Explanation #

  • input() returns a string.
  • The int() function converts the string into an integer.
  • The variable age stores an integer value.

Example 8: Integer Comparison #

main.py

a = 50
b = 30

print(a > b)

Output #

True

Explanation #

  • The > operator compares two integers.
  • Since 50 is greater than 30, the expression evaluates to True.

Example 9: Integer with Numeric Underscores #

main.py

salary = 1_500_000

print(salary)

Output #

1500000

Explanation #

  • Underscores improve readability for large numbers.
  • Python ignores underscores when storing the value.
  • Numeric underscores will be discussed in detail later in this chapter.

Example 10: Integer Identity #

main.py

x = 100
y = 100

print(type(x))
print(type(y))
print(x == y)

Output #

<class 'int'>
<class 'int'>
True

Explanation #

  • Both variables store integer values.
  • Both belong to the int data type.
  • Their values are equal, so x == y returns True.

Common Mistakes #

1. Writing a Decimal Number as an Integer #

Incorrect

number = 25.0

print(type(number))

Incorrect Expectation #

<class 'int'>

Actual Output #

<class 'float'>

Reason

A number containing a decimal point is a floating-point number.

Correct

number = 25

2. Forgetting to Convert User Input #

Incorrect

age = input("Enter age: ")

print(age + 5)

Output #

TypeError: can only concatenate str (not "int") to str

Reason

input() always returns a string.

Correct

age = int(input("Enter age: "))

print(age + 5)

3. Assuming Integers Have a Fixed Maximum Size #

Incorrect Assumption

number = 999999999999999999999999999999999999

will produce an overflow error.

Reason

Python integers automatically grow to accommodate very large values.


4. Using Quotes Around Numbers #

Incorrect

age = "25"

print(type(age))

Output #

<class 'str'>

Reason

Numbers enclosed in quotation marks become strings.

Correct

age = 25

Best Practices #

  • Use integers for whole numbers.
  • Convert user input to integers before performing arithmetic.
  • Use numeric underscores to improve readability of very large numbers.
  • Avoid storing numeric values as strings unless necessary.
  • Choose descriptive variable names that clearly represent the stored value.

Key Points to Remember #

  • Integers represent whole numbers.
  • The Python integer data type is int.
  • Integers can be positive, negative, or zero.
  • Python integers have arbitrary precision and are limited mainly by available memory.
  • Arithmetic operations such as addition, subtraction, and multiplication work directly with integers.
  • The / operator returns a floating-point result, even when dividing two integers.
  • Use int() to convert strings into integers.
  • Numeric underscores improve readability without affecting the stored value.

Powered by BetterDocs

Leave a Reply

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