In this article, we will discuss how to handle integer numbers in Python. We will discuss about positive integers and negative integers, even numbers and odd numbers, prime numbers and composite numbers. Let’s begin our journey of integer numbers.

What are Integers?

Numbers which do not have a fractional part are classified as integers. Integers are also called as whole numbers in Mathematics. Integers may be positive or negative. (In case you need more information about integers, read this article on Integers at Wikipedia)

Positive and Negative Integers

Integer numbers which have a value greater than zero (0) are classified as positive integers. Integer numbers which have a value less then zero (0) are classified as negative integers. Negative integers are prefixed with a minus sign (-).

Python code to check if integer is positive or not

Let’s write a small function in Python to check if a given integer value is positive or not. The function returns True for positive integers.

# function to check if given number is positive
def isPositive(n):
    if n>=0:
        return True
    else:
        return False

# code to test the above function
n = int(input("Enter a integer number"))
if isPositive(n):
    print("Number is positive")
else:
    print("Number is not positive, it is negative")

Python code to check if integer is negative or not

Similarly, let’s write a small function in Python to check if a given integer value is negative or not. The function returns True for negative integers.

# function to check if given number is negative
def isNegative(n):
    if n<0:
        return True
    else:
        return False

# code to test the above function
n = int(input("Enter a integer number"))
if isNegative(n):
    print("Number is negative")
else:
    print("Number is not negative, it is positive")

Even and Odd Integers

Integer values which are completely divisible by 2 are called as Even numbers. In other words, when a even number is divided by 2 the remainder is 0. Similarly, values which are not divisible by 2 are called as Odd numbers. An odd number when divided by 2 returns the remainder value as 1.

Python code to check if integer value is even or not

Let’s write a small function in Python to check if a given integer value is even or not. The function returns True for even numbers. The remainder obtained when the number is divided by 2 is compared with 0 to check if it is even or not.

# function to check if given number is even
def isEven(n):
    if n%2==0:
        return True
    else:
        return False

# code to test the above function
n = int(input("Enter a integer number"))
if isEven(n):
    print("Number is even")
else:
    print("Number is not even, it is odd")

Python code to check if integer value is odd or not

Similarly, let’s write a small function in Python to check if a given integer value is odd or not. The function returns True for odd numbers. The remainder obtained when the number is divided by 2 is compared with 1 to check if it is odd or not.

# function to check if given number is odd
def isOdd(n):
    if n%2==1:
        return True
    else:
        return False

# code to test the above function
n = int(input("Enter a integer number"))
if isOdd(n):
    print("Number is odd")
else:
    print("Number is not odd, it is even")

Prime Numbers

A number which is not divisible by any number except 1 and itself is called as a prime number. A prime number has only two factors, 1 and itself.

Python code to check if integer number is prime or not

Let’s write a small function in Python to check if a given integer number is prime or not. The function returns True for prime numbers. The given number is checked for divisibility by all numbers except 1 and itself. If it is not divisible by all the tested divisors, the function returns True.

# function to check if given number is prime 
def isPrime(n):
    i=2
    while i<n:
        if n%i == 0:
            return False    #number is not a prime number
            break
        i = i + 1
    else:
        return True         #number is a prime number

# code to test the above function
n = int(input("Enter a integer number"))
if isPrime(n):
    print("Number is prime")
else:
    print("Number is not prime, it is composite")

Type Conversion

The built-in int() function can be used to convert values from float/string to int

Type Conversion from float to int

Floating point numbers (float) can be converted to integer numbers (int), by using the built-in int() function. The fractional part of the number is lost during the conversion.

#converting float to int
a=2.7
b=int(a)
print(b)    #result = 2

Type Conversion from string (integer) to int

Numerical strings containing an integer value can be converted to integer numbers (int) by using the built-in int() function.

#converting integer value string to int
a="3"
b=int(a)
print(b)    #result = 3

Type Conversion from string (float) to int

Numerical strings containing floating point number cannot be converted to integer numbers (int) directly. The string needs to be converted to float first and then convert to integer.

#converting float value string to int
a="3.5"
b=int(float(a))        #convert to float, then to int
print(b)               #result = 3

More Concepts in Data Types

Last modified: March 29, 2023

Author