Basics of Python Integer Type (int): Usage and Notes on Numeric Literals

The “Integer type” (int) in Python is one of the most frequently used data types in programming. It refers to numbers without decimal points: positive integers, negative integers, and zero (0).

This article explains the basic usage of the int type, arithmetic operations, and important points to remember when writing numbers.


目次

Basic Usage of Integer Type (int)

Simply assigning an integer to a variable makes it automatically treated as an int type.

# Positive integer
current_year = 2025
# Negative integer
temperature_delta = -15
# Zero
initial_count = 0

# Check data type with type() function
print(f"Type of 'current_year': {type(current_year)}")

Output:

Type of 'current_year': <class 'int'>

Theoretically, Python’s int type can handle numbers as large (or small) as your computer’s memory allows.


Arithmetic Operations with Integers

int data can be used for calculations using basic arithmetic operators (+, -, *, /, //, %).

base_price = 500
discount = 50

# Addition (+) and Subtraction (-)
final_price = base_price - discount
print(f"Final Price: {final_price}")

# Multiplication (*)
total_cost = final_price * 3
print(f"Total for 3 items: {total_cost}")

# Division (/)
# Note: Standard division (/) returns a float (floating-point number)
# even if the result is a whole number.
average = total_cost / 3
print(f"Average (float): {average} (Type: {type(average)})")

# Integer Division (//) and Modulo (%)
# '//' returns the quotient (integer part)
items_per_box = 10
total_items = 125

box_count = total_items // items_per_box
remaining_items = total_items % items_per_box

print(f"Box count: {box_count} (Type: {type(box_count)})")
print(f"Remainder: {remaining_items}")

Output:

Final Price: 450
Total for 3 items: 1350
Average (float): 450.0 (Type: <class 'float'>)
Box count: 12 (Type: <class 'int'>)
Remainder: 5

Notes on Numeric Literals

There are a few things to watch out for when writing numbers directly in your code (numeric literals).

1. Numbers Starting with Zero (0)

In Python 3 and later, writing a non-zero number starting with 0 (e.g., 015) causes a SyntaxError.

# Adding a leading 0 to a non-zero number causes an error
# item_code = 015
# SyntaxError: leading zeros in decimal integer literals are not permitted

This is because in older versions of Python (and languages like C), a leading 0 indicated an “octal” number. Python 3 forbids this to avoid ambiguity. If you explicitly want to use octal, use 0o (zero-o), and for hexadecimal, use 0x (zero-x).

# Octal (0o)
octal_value = 0o15  # Decimal 13 (8*1 + 5*1)
# Hexadecimal (0x)
hex_value = 0x15    # Decimal 21 (16*1 + 5*1)

2. Digit Separator for Large Numbers (_)

When writing very large numbers, they can be hard to read if written directly. 120000000

In such cases, you can use the underscore _ as a digit separator. Python ignores the underscore, so it does not affect the calculation.

# Using underscores makes it easier to read
population = 120_000_000
budget = 5_500_000_000

print(f"Population: {population}")
print(f"Budget: {budget}")

total = population + budget
print(f"Total: {total}")

Output:

Population: 120000000
Budget: 5500000000
Total: 5620000000

Summary

  • Python’s int type handles positive integers, negative integers, and zero.
  • / (division) returns a float, while // (integer division) and % (modulo) return an int.
  • Writing a number like 015 (leading zero) causes a SyntaxError.
  • Using _ (e.g., 120_000_000) helps improve the readability of large numbers.
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次