HomeOur TeamContact

Data types in Python.

By Nicodemus Ngufuli
Published in Python
February 03, 2021
4 min read

Datatypes in Python.

Datatypes are mainly used to store a type of data into a computer memory or variable. In Python, datatypes can either be built in datatypes or user-defined. user-defined data types are the ones created by a Programmer.

Built in datatypes.

There are 5 major categories of built in data-types which are:

  • None datatypes(Null type)
  • Numeric datatypes
  • Sequences
  • Sets
  • Mappings

None datatypes(Null type)

None datatypes are used to represent an object that doen not have any value. It is sometimes referred as Null datatype in other programming languages such as Java. A none datatype is used as a default value of an argument in a Python function. This scenerio occurs when calling a function without passing any value and as a result the default value will be taken as none. In Python, only one none object is provided. Incase a boolean expression is used, none datatype is represented as false.

Numeric datatypes.

There are three types of numeric datatypes in Python as described below.

  • int (Integer)
  • float
  • complex

Int (Integer) datatype.

Integer datatypes represents a number without decimal point or fraction. example: 100, -100, 3300330033, -3300330033 A code snippet below describes how to store an integer value into a variable.

n = 100

n is an int variable since we stored the integer value 100 in variable n. Unlike other programming languages, Python does not have size limitation in storing int type values.

float datatype.

Float datatypes represents floating point values. A floating point value is a number that has a decimal point such as, 100.01, 3.33, 7.5 A code snippet below describes how to store a floating point value into a variable.

f = 3.33

f is a float type variable since we stored a floating point value 3.33 Scientific notation values can also be represented using floating point datatypes. To write a scientific notation value using Python’s floating point datatype, we use e or E. For example, a number 3.7 x 105 can be written as 3.7e5 or 3.7E5.

y = 3.7E5

Here the float value 3.5 X 105 will be stored into the variable y.

complex datatype

A complex datatype is used to store a complex number.

What is a complex number?

This is a number written in the form of a + bj or a + bJ, where a represents the real part of the number and b represents the imaginary part of a number. The suffix j or J represents the square root value of -1. The value in a or b can either be integer or float as seen from the examples below.

c = 2+3J
c1 = -2.5-5J 

Example Program. A Python program to display the sum of two complex numbers.

# Python program to add two complex numbers
c1 = 3.3+2.2J
c2 = 2.2+3.3J
c3 = c1 + c2
print("The sum= ", c3)

Output:

C:\python sum.py
Sum= (5.5+5.5J)

Binary, Octal and Hexadecimal numbers in Python.

  • Binary numbers are written by prefixing 0b (Zero and small b) or 0B (Zero and capital B) before a value. Examples of binary numbers are, 0b1100100 and 0B100101100.

-Octal numbers are written by prefixing 0o (Zero and small o) or 0O(Zero and capital O) before a value.

Examples of octal numbers are, 0o144 and 0O454

  • Hexadecimal numbers are written by prefixing 0x(Zero and small x) or 0X (Zero and capital X) before a value. Examples of hexadecimal numbers are, 0x64 and 0X12C.

Converting datatypes explicitly.

Python automatically detects a datatype and stores it in a variable, however it is possible to convert one datatype into another by a process called Coercion. This is done by mentioning the datatype with parenthesis.

Examples of type Coercion or type conversion

  1. int() is used to convert a number into an integer type. Converting an float number into an integer:
num = 100.23
int(num)
# It will display 100 
  1. float() is used to convert a number into a decimal type. Converting an integer number into a float type.
num = 20
float(num)
# It will display 20.0
  1. complex() is used to convert a value into complex type having a real and imaginary part. Converting an integer value into a complex value.
num = 1
complex(num)
# It will display 1+0j

Example Program. A Python program to convert Octal, Binary and Hexadecimal numbers into a decimal number system.

oct = 0o14
bin = 0b1101
hex = 0xE
n1 = int(oct)
print("Octal 0o14 = ",n1)
n2 = int(bin)
print("Binary 0b1101 = ",n2)
n3 = int(hex)
print("Hexadecimal 0xE = ",n3)

Output:

C:\>python coercion.py
Octal 0x14 = 12
Binary 0b1101 = 13
Hexadecimal 0xE = 14

Bool datatype in Python.

A bool datatype is used to represent a boolean value. Boolean values are categorised into two type and these are, true and false.

In Python true is represented by 1 and false by 0. An empty string such as "" is also represented by false. Likewise conditional statement are also evaluated to either true or false.

Example of conditional statements:

a = 4
b = 2
if(a>b):
    print("a is greater than b") # displays "a is greater than b"  

Sequences (datatypes) in Python

Sequences are used to represent a group of items or odered sets.

There are six categories of Sequences in Python.

  1. str (String)
  2. bytes
  3. bytesarray
  4. range
  5. tuple
  6. list

str (String) datatype

str represents a group of characters enclosed inside a single or double quotes.

str = "This is a string in double quotes"
str1 = 'This is another string in single quotes'

String can be created by enclosing a group of characters inside tripple quites """.

str = """Welcome to UltimaxDev the 
homepage of Computer Programming where
we learn via project tutorials"""

str1 = ''' At UltimaxDev we 
Welcome both professional and
newbies in Computer Programming'''

The tripple single quotes and tripple double quotes are also used to embed a string inside another string.

The slice operator.

The slice operator indicated by square brackets [] is used to perform various operations on strings such as retrieving some parts/pieces of a string. Examples of slice operations on strings.

myStr = "Welcome to UltimaxDev"

print(myStr[0]) # This displays the 0th character from variable myStr

print(myStr[2:6]) # This displays the 2nd to 6th character from variable myStr

bytes datatype.

A bytes datatype is used to represent a group of byte numbers similarly to arrays(depite arrays storing same values having any datatype). A bytes datatype stores only positive integers from 0 to 255.

values = [10, 20, 30, 40, 50]
n = bytes(values) # Converts the list into bytes array
print(n[0]) # This prints the 0th element which is 10

Elements inside a bytes datatype can not be modified. For instance, n[2] = 22 from the example above will result to an error.

Example Program. A Python program to create a bytes datatype variable, read then display the elements of an array.

#Creating a the list.
values = [1, 2, 3, 4, 5, 6]
#Converting the list into bytes type array.
n = bytes(values)
#Using a for loop to retrieve and display the elements in n variable
for i in n:
    print(i)

Output:

C:\>python bytestype.py
1
2
3
4
5
6

bytearray datatype

A bytearray datatype is used to represent a group of byte numbers similarly to bytes datatype but the difference between them is that, the values in bytearray datatype can be changed where as the values in bytes datatype can not be changed.

values = [10, 20, 30, 40, 50]
n = bytearray(values) # Converts the list into bytes array
print(n[0]) # This prints the 0th element which is 10

The modify the values, we simply write the code below.

n[0] = 3 # Replacing the 0th element with 3
n[1] = 6 # Replacing the 1st element with 6

Example Program. A Python program to create a bytearray datatype variable, read then display the elements of an array.

#Creating a the list.
values = [10, 20, 30, 40, 50, 60]
#Converting the list into bytes type array.
n = bytearray(values)
#modifying the first three elements of type array
n[0] = 1
n[1] = 2
n[2] = 3
#Using a for loop to retrieve and display the elements in n variable
for i in n:
    print(i)

Output:

C:\>python bytestype.py
1
2
3
40
50
60

List datatype

List datatypes are used to store a group of element. Lists are similar to arrays is programming languages such a C and Java but the only difference between them is described below.

Difference between Lists and Arrays

Lists(In Python)Arrays(In Java and C)
Stores elements of different datatypeStores elements of same datatype
Grow dynamically in memorySize is fixed and can not grow in runtime

Example of a List

list = [10,"Java", 12.2,'UltimaxDev']

Tuple datatype

Tuple datatypes are used to store a group of element. Tuples are similar to List but the only difference is that the elements inside a tuple can not be modified.

Range datatype

A Range datatype is used to represent a sequence of numbers. Elements inside a range can not be modified. Range datatype is used to repeat a for loop for a specific number of times.

Creating a range.

n = range(5)

Using a range to create a for loop that iterates from 0 to 4 using the variable n created earlier.

for i in range(n):
    print(i)

Tags

#python#datatypes#pythonint#pythoncomplex#pythonfloat
Nicodemus Ngufuli

Nicodemus Ngufuli

Software Engineer and content editor at UltimaxDev

I am a Full-Stack Software engineer, currently learning advaced java as a hobby

Expertise

NodeJS
ExpressJS
React
Laravel

Social Media

instagramtwitterwebsite

Related Posts

Introduction to python
Python
Introduction to python
January 05, 2021
5 min
Python vs other languages
© 2023, All Rights Reserved.

Quick Links

Advertise with usAbout UsContact Us

Social Media