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 data types are the ones created by a Programmer.
There are 5 major categories of built in data-types which are:
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
In Python, only one
none object is provided.
Incase a boolean expression is used,
none datatype is represented as
There are three types of numeric datatypes in Python as described below.
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 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
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. 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
A complex datatype is used to store 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.
J represents the square root value of -1.
The value in
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)
0b(Zero and small b) or
0B(Zero and capital B) before a value. Examples of binary numbers are,
-Octal numbers are written by prefixing
0o (Zero and small o) or
0O(Zero and capital O) before a value.
0x(Zero and small x) or
0X(Zero and capital X) before a value. Examples of hexadecimal numbers are,
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.
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
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
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
Hexadecimalnumbers 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 is used to represent a boolean value. Boolean values are categorised into two type and these are,
true is represented by
0. An empty string such as "" is also represented by
false. Likewise conditional statement are also evaluated to either
Example of conditional statements:
a = 4 b = 2 if(a>b): print("a is greater than b") # displays "a is greater than b"
Sequences are used to represent a group of items or odered sets.
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
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) # This displays the 0th character from variable myStr print(myStr[2:6]) # This displays the 2nd to 6th character from variable myStr
bytes datatype is used to represent a group of byte
numbers similarly to arrays(depite arrays storing same values having any datatype).
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) # This prints the 0th element which is 10
Elements inside a bytes datatype can not be modified. For instance,
n = 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 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) # This prints the 0th element which is 10
The modify the values, we simply write the code below.
n = 3 # Replacing the 0th element with 3 n = 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 = 1 n = 2 n = 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 datatypes are used to store a group of element.
Lists are similar to
arrays is programming languages such a
Java but the only difference between them is described below.
|Lists(In Python)||Arrays(In Java and C)|
|Stores elements of different datatype||Stores elements of same datatype|
|Grow dynamically in memory||Size is fixed and can not grow in runtime|
Example of a List
list = [10,"Java", 12.2,'UltimaxDev']
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 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)