Python 3.3 - Datatypes in Python
Datatypes in Python
As we have already discussed that data type refers to the
type of data. So datatype implies that what type of data we can store in our
Python program. There are two categories of datatypes available in Python – Built-in
data types and User defined data types
Built-in data types
The data types which are pre-defined in Python are called
built-in data types. These data types have already been developed by Python
developer team which we can use directly. So we do not need to do anything in
it. Following are built-in data types –
·
None
·
Numbers
·
Boolean
·
Sequences
·
Sets
·
Dictionary
These datatypes are further classified in some more
datatypes. Observe the image below –
1.
The None Type
The None Type represents no value. When we have no value to store in a variable or object then we can store None into that variable. There might be a case when we will need a value in a variable later in our program but not at that time, so we can store None into that variable. None is mostly used in functions (Functions will be covered in the book ‘Python’ volume 2). While calling a function, if no value is passed as arguments then ‘None’ will be taken as the default value. And if some value is passed to the function then that value will be used by the function. Keep in mind that a None is only None and not the zero i.e. a None is not equal to a Zero. For example –
‘x = 0’ and ‘x = None’ are two different values. In first statement, value 0 is stored into variable ‘x’ whereas in second statement, we have no value so we stored None into ‘x’. To store None into any variable –
x = None # Remember that N is capital in None
2.
Numbers
We all know what numbers are. This is the most common data
type which is used everywhere. Generally, we know about two types of numbers
that are ‘without any point’ and ‘with a point’. For example, 5, 67, -65, 8.6,
54.76 etc. But there are 3 types of numbers in Python –
int Datatype:
The int datatype represents an integer number. An integer number is a number without any decimal point or fraction part. It may be a positive or negative number. For example - 5, 35, -78, -56565, 4565 etc. are treated as integers. If we store an integer number in a variable, then that variable will be of type int. We can store very large integer numbers in Python. There is no limit of it. Some examples of integers are –
X = 25
Y = -30
Z = 674637645334
So far we have discussed many things in Python so let’s write a
simple Python program to add two integer numbers –
Program –
# Python program to add two integers
x = 10 # first we need a value so we stored 10 into a variable
x
y = 20 # then we stored 2nd value 20 into variable y
z = x+y # then we added x and y, and stored the result into
variable z
print("addition of x and y =",z) # finally we
printed the result on the screen
Output –
addition of x and y = 30
int is an immutable object which means we cannot change or modify
the value of an integer variable. You must be thinking, what?
Now you would say that I can write ‘x= 10’ and then I can write ‘x
=20’ to modify the value of ‘x’. Yes of course you can do it but you did not actually
modify the value of variable ‘x’. So when we wrote ‘x=10’ then Python stored 10
in some memory location and named that location ‘x’. And when we tried to
modify the value of ‘x’ by writing ‘x=20’ then Python did not place 20 at the
same location rather it stored 20 at some other memory location and named it ‘x’
and then Python released the old memory location occupied by ‘x’. The thing is
that Python did not remove 10 to store 20 rather it stored 20 somewhere else
which proves that ‘int’ is immutable. To understand this thing clearly we can
check the address of variable ‘x’ using id() function. So it should print
different address for variable ‘x’ when we modified its content. Now observe
the code below-
x = 10
print("address of x =",id(x)) # 10 is stored at this
location
x = 20 # we tried to modify the integer x with value 20
print("address of x =",id(x))
Output –
address of x = 2699190364752
address of x = 2699190365072
As you can see that 20 is stored at different location when we
tried to modify the previous value 10. Remember, the previous memory location 4752
was released/deleted when we wrote ‘x=20’ and it is no more accessible. So
integers are immutable.
float Datatype:
Float Datatype represents float numbers. A float number is a
number with decimal point. A float number may be positive or negative. For example
– 5.5, 35.9, -65.78, 52432.566 etc. are treated as floats. If we store a float
number in a variable, then that variable will be of type float. Some examples
are –
X = 5.5
Y = -56.78
Z = 43423.4324
We can also use scientific notation to store float numbers.
Scientific notation is very helpful to store large float numbers. Since large
numbers occupy more memory, we can use scientific notation that occupies very
less memory. Numbers in this notation are stored in power of 10 where power 10
is represented by ‘e’ or ‘E’. For example, if we want to store a float number
753468.234*108. Can you imagine how big the number is? So we can
store this number as 753468.234e8 which will occupy less memory as compared to
the original form. Here ‘e’ represents power of 10.
45.6*107 = 45.6e7
234.89*10-5 = 234.89e-5
float is an immutable object just like ‘int’ which means we cannot
change or modify the value of a float variable.
Complex Datatype:
Complex data type represents a complex number. A complex
number in Python is written in the form of a+bj or a+bJ. here ‘a’ represents
the real part and ‘b’ represents the imaginary part of the number. ‘j’ or ‘J’
represents the square root value of -1. ‘a’ and ‘b’ can be integer or float.
For example, 5+7j, 4.5+8j, -2+3.5j, -5-9j are all complex numbers. Complex
numbers are used in many applications of mathematics and physics.
Type conversion –
As we know that we need not declare anything in Python.
Python automatically understands the data type at run time. For example, if we
have written x = 10, then Python will understand that ‘x’ is an integer
variable and 10 is an integer literal. But sometimes it may be required to
change the type of data in our program explicitly. For example, we may need to
change the datatype from ‘int’ to ‘float’ or from ‘float’ to ‘int’ etc. in a
program. So converting one data type into another data type is called type
conversion. We can convert one type into another type by mentioning the
required datatype with parentheses. For example, to convert a float number into
integer, we can write ‘int(float number)’.
Where float number is the number that we want to convert and
‘int’ is the data type which will convert the number into ‘int’.
Let’s take some example –
x = 25.5 # we have a float value here
print(x) # will display 25.5
x = int(x) # we converted float 25.5 into integer 25
print(x) # will display 25
float(integer) is used to convert an integer number into
float type
x = 25 # we have an integer value here
print(x) # will display 25
x = float(x) # we converted integer 25 into float 25.0
print(x) # will display 25.0
complex(number) is used to convert the number into complex
type
x = 25 # we have an integer value here
print(x) # will display 25
x = complex(x) # we converted integer 25 into a complex type
print(x) # will display 25+0j
by using these methods, we can convert any valid data type
to any other valid data type.
3.
Boolean datatype
Boolean data type has only two values – True or False. The value
of this data type can be either True or False. Python internally treats True as
1 and False as 0. Conditions will be evaluated internally to either True or
False. So you need to remember that, the result of a condition can be either
True or False. If I ask you, is 10 greater than 5? what will you say? Your
answer will be either ‘Yes’ or ‘No’ or either ‘True’ or ‘False’. This thing
exactly works in Python. When we need to compare two values then the result
will be either ‘True’ or ‘False’ which is a Boolean datatype. For example –
a = 10
b = 5
print(a>b)# the result of the condition a>b will be True
print(b>a)# the result of the condition b>a will be
False
# as you can see conditions internally evaluate to either True
or False
Output –
True
False
4.
Sequences
A sequence datatype represents collection of data or values.
We can use a sequence type when we want to store a large number of data at
once. For example, if we want to store the marks of a class of 40 students then
we can use a sequence rather than using 40 variables like ‘Marcus = 90’, ‘Jack
= 80’, ‘Robin = 78’ and all the other 37 variables. It is not a good way to
store large data so we should use a sequence datatype in this situation. There
are six types of sequences in Python –
·
Str
·
list
·
range
·
tuple
·
bytes
·
bytearray
str Datatype:
In Python ‘str’ data type represents a string. A
string is a collection or stream of characters. A string is always written within single
quotes (’ ‘) or double quotes (” “) or triple quotes (“”” or ‘’’) in Python.
For example, “India”, ‘Russia’, “japan”, ‘Israel’, “12345”, ‘&%hg89+’ etc.
all are strings in Python. Whatever we write within these quotes becomes a
string. If you remember we have stored integers, floats etc. into variables.
Now let’s store some strings into variables.
name = “Pradeep”
profession = ‘Teacher’
country = “India”
Here name, profession, country are variables and “Pradeep”,
‘Teacher’, “India” are strings. Please remember that anything you put within
quotes become a string, no matter if you store numbers or special characters.
They also become strings. For example, “546” is a string and not an integer
data type since written within quotes. “456.56” is also a string and not a
float data type since written within quotes.
We can also use triple single quotes or triple double quotes
to write strings, the difference is that it allows us to span multiple line.
For example, if we want to store a message of 4 to 5 lines then we can use “””
or ‘’’.
message = “”” always use triple double quotes
or triple single quotes to store strings that will
allow us to span multiple lines “””
string is an immutable object which means we cannot change or
modify the value of a string variable
we will study string in detail in a separate chapter.
What about character Datatype?
Python does not have any character data type. There
is a character data type available in programming languages like C, C++ and
Java where we can store a single character in a variable but Python does not
have any. If you want to store a character, then you can use string method to
store it. A character can be stored inside single quotes ( ‘ ) or double quotes
( “ ). But this will be treated as a string. So you have no any other option. To
store a single character, we can use –
chr = ‘m’ # but type of variable chr is ‘str’ and not a
character
list Datatype:
A list is a collection of heterogeneous (different types
of) elements or data. Heterogeneous elements refer to elements of different
types such as integers, floats, strings etc. are different types of data or
elements. list data type is used when we want to store a large collection of
dissimilar data.
For example, we have to write a program in which we want to
store information about a student. This information contains student’s name,
percentage, section, dob, subject and roll number. So one solution is that we
use 6 different variables to store this information but this approach is not a
good idea because these 6 variables will be stored in 6 different memory
locations. So it will be time consuming for interpreter to go to each different
location and bring the data back when needed. And it will also be difficult for
us to remember all the variable names. What will happen when we want our data
back? We have to remember all the variable names. And think if there were
hundreds of variables. Will it be possible to remember the name of every
variable? of course not. So in this situation we should use a list.
A list is created using square brackets [ ] in Python.
Elements are written in [ ] and separated by commas. For example, let’s
store some information about a student –
student = [‘Marcus’, 86.5, ‘mca’, ‘A’, ‘05/09/91’, ‘computer
science’, 3285]
Here ‘student’ is a list. As you can see we have stored
different types of data in square bracket [ ] separated by commas (strings,
floats and integers in our list).
list is a mutable object which means we can change or modify the
elements of list.
we will study list in detail in a separate chapter.
tuple Datatype:
A tuple is similar to a list. A tuple is used to store
elements of different types. The elements in a tuple are separated by commas
and enclosed in parentheses ( ). The difference between a list and a tuple is
that we can modify elements of a list but we cannot modify elements of a tuple.
For example, we could change the section from ‘A’ to ‘B’ in above list ‘student’
but if ‘student’ was a tuple then we could not change it. Let’s create a tuple
now –
my_student = (‘Marcus’, 86.5, ‘mca’, ‘A’, ‘05/09/91’, ‘computer
science’, 3285)
Here my_student is a tuple since we stored the elements in
parentheses ( ). We cannot change elements of tuple ‘my_student’. For example,
if a student transferred from section ‘A’ to ‘B’ then we will want to change
this element but because ‘my_student’ is a tuple, we cannot change it.
tuple is an immutable object which means we cannot change or
modify the elements of a tuple.
we will study tuple in detail in a separate chapter.
bytes Datatype:
The bytes datatype represents a group of byte numbers. We can
only store integer numbers from 0 to 255 in a bytes type variable. We cannot
modify the elements of bytes variable. No other numbers are allowed other than
0 to 255. If you try to store any other number, then it will give an error. There
is no direct way to create a bytes data type but we can use bytes() function to
create bytes type from other sequences like list and tuple. For example –
lst=[0,1,5,40,255] # first we created a list named lst
byt=bytes(lst) # then we converted the list into bytes datatype
bytearray Datatype:
A bytearray is same as bytes data type but the only
difference is that we cannot modify the elements of bytes whereas we can modify
the elements of bytearray. bytearray() function is used to create a bytearray
data type. For example –
lst=[0,1,5,40,255] # first we created a list named lst
byt=bytearray(lst) # then we converted list lst to bytearray
Data type
range Datatype:
range data type represents a collection of integer numbers.
We cannot modify these numbers. As the name suggests, range type is used to
create a range of numbers. range datatype is mostly (95%) used in for loops.
It is very important and mostly used datatype. We can create a range or a sequence
of numbers using range() function. The syntax of this function is as follows –
range(start, stop, step)
start – the starting value of range from where we
want to start our numbers
stop – the ending value of range where we want to
stop our numbers
step – it represents how many steps we want to
increase every time from start
start and step are optional, stop is mandatory.
Note: The range generates numbers from start to stop-1. If
start is not mentioned, then 0 will be taken as the default value of start and
if step is not mentioned, then 1 will be taken as the default value.
If we specify only one value in range, for example range(10)
then 10 will be ‘stop’ value, if we specify two values, for example range(1,10)
then 1 will be ‘start’ and 10 will be ‘stop’ value, and if we specify all the
three values, for example range(1,10,2) then 1 is ‘start’, 10 is ‘stop’ and 2
is ‘step’ value. So a range data type can have following combinations –
range(stop)
range(start, stop)
range(start, stop, step)
To create a range we can simply write –
r = range(10)
It will create a range from 0 to 9 i.e. 0,1,2,3,4,5,6,7,8,9 (since
only one value is specified so 10 is stop value. No start value is specified so
0 is default value for start). The range will stop at 9 because range generates
numbers from start to stop-1 so in this case, 0 to 10-1.
r = range(0,15)
It will create a range from 0 to 14 i.e. 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14
(start = 0, stop = 15) remember range goes from start to stop-1. So it will
create a range from 0 to 15-1=14.
r = range(0,10,2)
It will create a range i.e. 0,2,4,6,8 (since 2 is step value
so we will start skipping from 0. First 0 is taken, then increase by 2 so we
get 2, then increase by 2 so we get 4 and so on. When we get 8 then again
increase by 2 so we get 10 but the range is going from 0 to 9 so 10 will not be
taken and range will stop on 8)
let’s take an example – what if I ask you to write a program
to print numbers from 1 to 100? Will you take hundred variables to store
numbers from 1 to 100 and then print those variables 100 times again? or will
you print by typing 1 to 100 numbers? Both the solutions are extremely
worthless. In first solution, your code will contain many unnecessary variables
and in second solution, your code may contain unnecessary large statements and
it is not a good way, actually it is very bad way to solve such problem. So
good solution is that we can use range data type to print numbers from 1 to
100. Right? this is what we learnt that a range is used to store collection of numbers.
And we have large numbers to store so we will use range data type here.
Now let’s solve this problem using range type. We will solve
this problem with only three lines. We are going to use a ‘for’ loop with range
function. So don’t worry about it. We will discuss loops later –
numbers = range(1,101) # generate numbers from 1 to 100 and
store into numbers
for i in numbers:
print(i) # we are
printing values of i through ‘for’ loop
Output –
1
2
3
4
5
6
7
.
.
.
100
5.
Sets
A set is an unordered collection of elements. The order of
elements in the set is not fixed and may change when we print its elements. A
set does not allow duplicate values. Although we can store duplicate values
in a set such as 2, 2, 2 but set would take only one value i.e. 2 and not all
the three values which means set will ignore the other two values. There are
two types of a set –
·
set datatype
·
frozenset datatype
set Datatype:
We can create a set by using curly braces { } and store
elements separated by commas. We can store different types of data in a set. For
example –
St = {10,20,40,’a’,’b’,’god’}
Print(St) # may display {‘b’, 40, 10, 20, ‘a’, ‘god’}
Please observe that the order of elements is not the same.
We stored elements in St in different order {10,20,40,’a’,’b’,’god’} and when
we printed the result we got the different order {‘b’, 40, 10, 20, ‘a’, ‘god’}
of elements.
Since the order of elements is not fixed, we cannot access
the elements of a set by indexing and slicing. Indexing and slicing are two
ways to access the elements of a sequence. We will understand ‘indexing’ and
‘slicing’ very soon.
frozenset Datatype:
There is no direct method to create a frozenset. But we can
convert a sequence such as string, list, tuple or a set into a frozenset by
using frozenset() function. We can pass one of the sequences or a set as
parameter and the frozenset() function will convert that sequence or set into a
frozenset. For example –
st = {1,2,3,4,5} # st is a set
frozen = frozenset(st) # frozenset() function will convert
‘st’ into frozenset
print(frozen) # may print frozenset({3,2,1,4,5})
The only difference between a set and frozenset is that we
can modify the elements of a set but we cannot modify the elements of a
frozenset which means a set is a mutable object and a frozenset is an
immutable object.
6.
Dictionary
A dictionary is also called a mapping datatype. In Python
‘dict’ represents a dictionary datatype. A dictionary is a collection of
elements in the form of key-value pairs so we can store elements in a
dictionary in a specific form called key-value form. The value is associated
with its corresponding key, so we can retrieve the associated value when the
key is given. The dictionary contains pairs of elements such that the first
element represents the key and the next one becomes its value. The key and its
value should be separated by a colon ( : ) and every key-value pair should be
separated by a comma. All the elements should be enclosed inside curly brackets
{ }.
For example, you can create a dictionary when you want to
store every user’s username and password combination for your company. In this
situation you need such a datatype that stores a large collection of data in
pairs (username and password). You can also use other sequences like list, tuple
etc. to store this type of data but list will be a good choice for ordered
collection of data and tuple is immutable so you cannot modify the data once
stored. But we may have to change the user name. So in this case, dictionary is
the perfect choice. Let’s create a dictionary of Users –
Users = {“user1” : “password1”, “user2” : “password2”, “user3”
: “password3”}
Here ‘Users’ is a dictionary that contains elements in the
key-value (user-password) pair. So ‘user1’ is the key and ‘password1’ is its
value, ‘user2’ is key and ‘password2’ is its value and ‘user3’ is the key and
‘password3’ is its value.
We can also create an empty dictionary as –
dct = { }
We will study dictionary in detail in a separate chapter.
User defined data types
The data types which are created by user or programmer, are called user defined data types. For example, class, functions, modules etc. are called user defined data types. We will cover these types later.
Comments
Post a Comment