Datatypes in Python: The Mandvi Education Society
Datatypes in Python: The Mandvi Education Society
Prakash G Khaire
The Mandvi Education Society
Institute of Computer Studies
Comments in Python
• Single Line Comments
#To find the sum of two numbers
a = 10 # store 10 into variable a
’’’
Write a program to find the total marks scored by a
student in the subjects
’’’
Comments in Python
• Python supports only single line comments.
• Multiline comments are not available in Python.
• Triple double quotes or triple single quotes
are actually not multiline comments but they are regular
strings with the exception that they can span
multiple lines.
• Use of ’’’ or ””” are not recommended for comments as
they internally occupy memory.
Variables
• In many languages, the concept of variable is
connected to memory location.
• In python, a variable is seen as a tag that is tied to
some value.
• Variable names in Python can be any length.
• It can consist of uppercase and lowercase letters (A-Z,
a-z), digits (0-9), and the underscore character (_).
• A restriction is that, a variable name can contain digits,
the first character of a variable name cannot be a
digit.
Datatypes in Python
• A datatype represents the type of data stored
into a variable or memory.
• The datatypes which are already avaiable in
Python language are called Built-in datatypes.
• The datatypes which are created by the
programmers are called User-Defined
datatypes.
Built-in datatypes
• None Type
• Numeric Types
• Sequences
• Sets
• Mappings
None Type
• ‘None’ datatype represents an object that does not contain
any value.
• In Java, it is called ‘Null’ Object’, but in Python it is called
‘None’ object.
• In Python program, maximum of only one ‘None’ object is
provided.
• ‘None’ type is used inside a function as a default value of the
arguments.
• When calling the function, if no value is passed, then the
default value will be taken as ‘None’.
• In Boolean expressions, ‘None’ datatype is represents ‘False’
Numeric Types
• int
• float
• complex
int Datatype
• The int datatype represents an integer number.
• An integer number is a number without any
decimal point or fraction part.
a = 10
b = -15
•
int datatype supports both negative and positive
integer numbers.
• It can store very large integer numbers
conveniently.
Float datatype
• The float datatype represents floating point numbers.
• A floating point number is a number that contains a decimal
point.
#For example : 0.5, -3.4567, 290.08,0.001
num = 123.45
x = 22.55e3
#here the float value is 22.55 x 103
bool Datatype
• The bool datatype in Python represents
boolean values.
• There are only two boolean values True or
False that can be represented by this
datatype.
• Python internally represents True as 1 and
False as 0.
Print Statement
>>>print “Python"
Python
>>> print 2 + 5 + 9
16
Print Statement
>>> print "Hello,","I","am",“Python"
Hello, I am Python
>>> print "I am 20 Years old " + "and" + " My friend is 21 years old.“
I am 20 Years old and My friend is 21 years old.
Sequences in Python
• A sequence represents a group of elements or items.
• There are six types of sequences in Python
– str
– bytes
– bytearray
– list
– tuple
– range
str datatype
• A string is represented by a group of characters. Strings are
enclosed in single quotes or double quotes.
str = “Welcome to TMES”
str = ‘Welcome to TMES’
• We can also write string inside “““ (triple double quotes) or ‘‘‘
(single double quotes)
str1 = “““ This is book on python which discusses all the topics of Core python
in a very lucid manner.”””
str1 = ‘‘‘This is book on python which discusses all the topics of Core python in
a very lucid manner.’’’
str datatype
str1 = “““ This is ‘Core Python’ book.”””
print(str1) # This is ‘Core Python’ book.
print(s*2)
Welcome to Core PythonWelcome to Core Python
bytes Datatype
• A byte number is any positive integer from 0 to
255.
• Bytes array can store numbers in the range from
0 to 255 and it cannot even storage.
elements = [10, 20, 0, 40, 15] #this is a list of byte numbers
x = bytes(elements) #convert the list into byte array
print(x[10]) #display 0th element, i.e 10
0
1
2
3
4
5
6
7
8
9
range Datatype
>>> r = range(30,40,2)
>>> for i in r:print(i)
30
32
34
36
38
ch = set("Python")
>>> print(ch)
{'n', 'h', 't', 'o', 'P', 'y'}
ch = set("Python")
>>> print(ch)
{'n', 'h', 't', 'o', 'P', 'y'}
>>> s.update([110,200])
>>> print(s)
{1, 2, 3, 4, 5, 200, 110}
>>> s.remove(4)
>>> print(s)
{1, 2, 3, 5, 200, 110}
frozenset Datatype
• The frozenset datatype is same as the set datatype.
• The main difference is that the elements in the set datatype
can be modified.
• The elements of the frozenset cannot be modified.
>>> s = {20,40,60,10,30,50}
>>> print(s)
{40, 10, 50, 20, 60, 30}
>>> fs = frozenset(s)
>>> print(s)
{40, 10, 50, 20, 60, 30}
frozenset Datatype
• The frozenset datatype is same as the set datatype.
• The main difference is that the elements in the set datatype can be
modified.
• The elements of the frozenset cannot be modified.
>>> s = {20,40,60,10,30,50}
>>> print(s)
{40, 10, 50, 20, 60, 30}
>>> fs = frozenset(s)
>>> print(s)
{40, 10, 50, 20, 60, 30}
>>> fs = frozenset("Python")
>>> print(fs)
frozenset({'n', 'h', 't', 'o', 'P', 'y'})
What is ?
• Mapping Types
• Literals
• type function