StudyLover
  • Home
  • Study Zone
  • Profiles
  • Typing Tutor
  • Contact us
  • Sign in
StudyLover Integer (int)
Download
  1. Python
  2. Pyhton MCA (Machine Learning using Python)
  3. Unit 3: Getting Started with Python: A Guide to Syntax, Data Structures, and OOP
Datatypes : float (Floating-Point Number)
Unit 3: Getting Started with Python: A Guide to Syntax, Data Structures, and OOP

An integer is a fundamental data type, so most of its functionality comes from standard operators and built-in functions rather than methods attached to the integer object itself.


Arithmetic Operations

These are the most common operations you'll perform with integers. They function just as they do in standard mathematics.

  • Standard Math: + (addition), - (subtraction), * (multiplication).

  • Division (/): This operator always results in a float (a number with a decimal), even if the division is even. For example, 10 / 2 results in 2.0.

  • Floor Division (//): This operator performs division and then rounds the result down to the nearest whole number, always resulting in an int.

  • Modulus (%): This operator returns the remainder of a division. It's very useful for determining if a number is even or odd (number % 2).

  • Exponentiation (**): This raises a number to the power of another.


Comparison Operations

These operations compare two integers and always return a Boolean value (True or False). They are the foundation of conditional logic in programs.

  • == (equal to)

  • != (not equal to)

  • > (greater than)

  • < (less than)

  • >= (greater than or equal to)

  • <= (less than or equal to)


Built-in Functions for Integers

Python has several built-in functions that are useful for working with numbers.

  • abs(): Returns the absolute (non-negative) value of a number.

  • pow(base, exp): An alternative way to perform exponentiation.

  • divmod(a, b): A handy function that returns a pair of numbers (a tuple) consisting of the quotient and the remainder from a division.


Integer Methods

Unlike more complex data types like strings or lists, integer objects themselves have very few methods. The most common one you might encounter is .bit_length().

  • .bit_length(): Returns the number of bits required to represent an integer in its binary form, excluding the sign and any leading zeros.

 

# --- 1. Arithmetic Operations ---

# These are the standard mathematical operations.

a = 10

b = 3

 

print("--- Arithmetic Operations ---")

print(f"Addition (a + b): {a + b}")

print(f"Subtraction (a - b): {a - b}")

print(f"Multiplication (a * b): {a * b}")

print(f"Standard Division (a / b): {a / b}") # Note: This results in a float

print(f"Floor Division (a // b): {a // b}") # This results in an integer

print(f"Modulus (a % b): {a % b}") # This gives the remainder

print(f"Exponentiation (a ** b): {a ** b}") # This is a to the power of b

 

# --- 2. Comparison Operations ---

# These operations compare two integers and return True or False.

print("\n--- Comparison Operations ---")

print(f"Is a equal to b? (a == b): {a == b}")

print(f"Is a not equal to b? (a != b): {a != b}")

print(f"Is a greater than b? (a > b): {a > b}")

print(f"Is a less than b? (a < b): {a < b}")

print(f"Is a greater than or equal to b? (a >= b): {a >= b}")

print(f"Is a less than or equal to b? (a <= b): {a <= b}")

 

# --- 3. Built-in Functions ---

# Python provides several useful functions that work with integers.

negative_number = -250

 

print("\n--- Built-in Functions ---")

print(f"Absolute value of {negative_number}: {abs(negative_number)}")

print(f"pow(10, 3) is an alternative to **: {pow(10, 3)}")

 

# divmod returns both the quotient and the remainder in a tuple

quotient, remainder = divmod(10, 3)

print(f"divmod(10, 3) -> Quotient: {quotient}, Remainder: {remainder}")

 

# --- 4. Integer Methods ---

# Integers have very few methods. .bit_length() is a common one.

num = 10 # The binary representation of 10 is 1010, which requires 4 bits.

print("\n--- Integer Methods ---")

print(f"The number of bits to represent {num} is: {num.bit_length()}")

 

num_large = 1000

print(f"The number of bits to represent {num_large} is: {num_large.bit_length()}")

 


 

Datatypes float (Floating-Point Number)
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website