Sei sulla pagina 1di 35

Datatypes in Python

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

• Comments are non-executable statements.


• Python compiler nor PVM will execute them.
Comments in Python
• When we want to mark several lines as comment, then we can write the previous
block of code inside ””” (triple double quotes) or ’’’ (triple single quotes) in the
beginning and ending of the block as.
”””
Write a program to find the total marks scored by a
student in the subjects
”””

’’’
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 “Python Programming"


Python Programming

>>> print "Hi, I am Learning Python Programming."


Hi, I am Learning Python Programming.
Print Statement
>>> print 25
25

>>> print 2*2


4

>>> print 2 + 5 + 9
16
Print Statement
>>> print "Hello,","I","am",“Python"
Hello, I am Python

>>> print “Python","is",27,"years","old"


Python is 27 years old

>>> print 1,2,3,4,5,6,7,8,9,0


1234567890
Print Statement
>>> print "Hello Python, " + "How are you?"
Hello Python, How are you?

>>> 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.

str1 = ‘‘‘This is ‘Core Python’ book.’’’


print(str1) # This is ‘Core Python’ book.

s = ‘Welcome to Core Python’


print(s[0]) #display 0th character from s
W

print(s[3:7]) #display from 3rd to 6th character


come

print(s[11:]) #display from 11th characters onwards till end.


Core Python

print(s[-1]) #display first character from the end


n
str datatype
• The repetition operator is denoted by ‘*’ symbol and useful to
repeat the string for several times. For example s * n repeats
the string for n times.

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

• We can modify or edit any element in the bytes


type array.
list Datatype
• Lists in Python are similar to arrays in C or Java.
• The main difference between a list an array is that a list can
store different types of elements, but array can store only one
type of elements.
• List can grow dynamically in memory.
• But the size of arrays is fixed and they cannot grow at
runtime.

list = [10, -20, 15.5, ‘vijay’, “Marry”]


list Datatype
• The slicing operation like [0:3] represents elements from 0th to
2nd positions. i.e 10,20,15.5
>>>list = [10, -20, 15.5, ‘vijay’, “Marry”]
>>>print(list)
10, -20, 15.5, ‘vijay’, “Marry”
>>> print(list[0])
10
>>>print(list[1:3])
[-20,15.5]
>>>print(list[-2])
vijay
>>>print(list*2)
[ 10, -20, 15.5, ‘vijay’, “Marry” 10, -20, 15.5, ‘vijay’, “Marry”]
tuple datatype
• A tuple contains a group of elements which
can be of different types.
• The elements in the tuple are separated by
commas and enclosed in parentheses().
• It is not possible to modify the tuple elements.

tpl = (10, -20, 15.5, ‘Vijay’ , “Marry”)


tuple datatype
>>> tpl = (10,-20,14.5, "Prakash", 'Khaire')
>>> print(tpl)
(10, -20, 14.5, 'Prakash', 'Khaire')
>>> print(tpl[1:3])
(-20, 14.5)
>>> print(tpl[0])
10
>>> print(tpl[-1])
Khaire
>>> print(tpl*2)
(10, -20, 14.5, 'Prakash', 'Khaire', 10, -20, 14.5, 'Prakash', 'Khaire')
>>> tpl[0] = 1
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
tpl[0] = 1
TypeError: 'tuple' object does not support item assignment
range Datatype
• The range datatype represents a sequence of
numbers.
• The numbers is the range are not modifiable.
• Range is used for repeating a for loop for a
specific number of times.
range Datatype
>>> r = range(10)
>>> for i in r : print(i)

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

>>> lst = list(range(10))


>>>
>>> print(lst)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Sets
• A set is an unordered collection of elements muck like
a set in Mathematics.
• The order of elements is not maintained in the sets.
• It means the elements may not appear in the same
order as they are entered into the set.
• A set does not accept duplicate elements.
• There are two sub types in sets
– Set datatype
– Frozenset datatype
Sets
• To create a set, we should enter the elements separated by
commas inside curly braces {}.
s = {10, 20, 30, 20, 50}
>>> print(s)
{10, 20, 50, 30}

ch = set("Python")
>>> print(ch)
{'n', 'h', 't', 'o', 'P', 'y'}

>>> lst = [1,2,4,3,5]


>>> s = set(lst)
>>> print(s)
{1, 2, 3, 4, 5}
Sets
• To create a set, we should enter the elements separated by
commas inside curly braces {}.
s = {10, 20, 30, 20, 50}
>>> print(s)
{10, 20, 50, 30}

ch = set("Python")
>>> print(ch)
{'n', 'h', 't', 'o', 'P', 'y'}

>>> lst = [1,2,4,3,5]


>>> s = set(lst)
>>> print(s)
{1, 2, 3, 4, 5}
Sets
>>> print(s[0])
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
print(s[0])
TypeError: 'set' object does not support indexing

>>> 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

Potrebbero piacerti anche