Lecture 3 Operators Expression and Data Types
Lecture 3 Operators Expression and Data Types
Python
Lecture - 3
• Previous Session:
• Introduction to Token.
• Statements and Expressions.
• Today’s Session:
• Data Types, indexing and Operators
• Mutable and Immutable data types.
• Data type conversion.
• Operators.
2
Data Types
6
Data Types (String)
• Characters of string can be individually accessed using
a method called indexing.
• Characters can be accessed from both directions:
forward and backward.
• Forward indexing starts form 0, 1, 2…. Whereas,
backward indexing starts form −1, −2, −3…
>>> a = “Sharda University”
>>> print(a[5])
t
>>> print(a[-1])
y
>>> print(a[-5])
r
7
• Mutable Data Types: Data types in python where the
value assigned to a variable can be changed
• Immutable Data Types: Data types in python where the
value assigned to a variable cannot be changed
Lists
Tuples
Data Types (Tuples)
• Tuple data type in Python is a collection of various
immutable Python objects separated by commas.
• Tuples are generally used for different Python Data
Types.
• A Python tuple is created using parentheses around the
elements in the tuple. Although using parentheses is
only optional, it is considered a good practice to use
>>> a = (1,2,3,4)
them. >>> print(a)
(1,2,3,4)
>>> a = (‘ABC’,’DEF’,’XYZ’)
>>>print(a)
(ABC,DEF,XYZ)
11
Data Types (Tuple)
• To access an element of a tuple, we simply use the
index of that element. We use square brackets.
• Reverse Indexing by using indexes as −1, −2, −3, and
so on, where −1 represents the last element.
• Slicing that is, extract some elements from the tuple.
>>> a = (1,2,3,4) >>> a = (1,2,3,4)
>>> print(a[1]) >>> print(a[-1])
2 4
>>> a = (‘ABC’,’DEF’,’XYZ’) >>> a = (‘ABC’,’DEF’,’XYZ’)
>>>print(a[2]) >>>print(a[1:])
XYZ (DEF, XYZ)
12
Data Types (List)
Unlike strings, lists can contain any sort of objects:
numbers, strings, and even other lists. Python lists are:
13
Data Types (List)
• Much similar to strings, we can use the index number to
access items in lists as shown below.
• Accessing a List Using Reverse Indexing
• To access a list in reverse order, we have to use indexing from
−1, −2…. Here, −1 represents the last item in the list.
>>> a = [“Sharda”, “University”, “Computer”]
>>> print(a[0])
Sharda
>>> print(a[-1])
Computer
>>> print(a[1])
University
14
Data Types (Set)
• It is an unordered collection of elements which means
that a set is a collection that stores elements of
different Python Data Types.
• In Python sets, elements don’t have a specific order.
• Sets in Python can’t have duplicates. Each item is
unique.
• The elements of a =set
>>> myset in Python
{“sharda”, are “science”}
“computer”, immutable. They
can’t accept changes once added.
>>>print(myset)
{‘sharda', 'computer', 'science’}
>>>myset = set((“sharda”, “computer”, “science”))
15
Data Types ( Dictionary)
• Yet another unordered collection of elements.
• Difference between dictionary and other unordered
Python data types such as sets lies in the fact that
unlike sets, a dictionary contains keys and values rather
than just elements.
• Unlike lists the values in dictionaries are accessed using
keys and not by their positions
>>>dict1 ={“Branch”:”computer”,”College”:”Sharda”,”year”:2025}
>>>print (dict1)
{‘Branch’:’computer’,’College’:’Sharda’,’year’:2025}
>>>di = dict({1:’abc’,2:’xyz’})
16
Data Types ( Dictionary)
• The keys are separated from their respective values by a
colon (:) between them, and each key–value pair is
separated using commas (,).
• All items are enclosed in curly braces.
• While the values in dictionaries may repeat, the keys are
always unique.
• The value can be of any data type, but the keys should be
of immutable data type, that is
• Using the key inside square brackets like we used to use
>>>dict1 ={“Branch”:”computer”,”College”:”Bennett”,”year”:2020}
the index inside
>>>print square brackets.
(dict1[year])
2020
17
Datatype Conversion
• As a matter of fact, we can do various kinds of
conversions between strings, integers and floats using
the built-in int, float, and str functions
>>> x = 10 >>> y = "20" >>> z = 30.0
>>> float(x) >>> float(y) >>> int(z)
10.0 20.0 30
>>> str(x) >>> int(y) >>> str(z)
'10' 20 '30.0'
>>> >>> >>>
18
Explicit and Implicit Data Type
Conversion
• Data conversion can happen in two ways in Python
1. Explicit Data Conversion (we saw this earlier with the int, float, and str
built-in functions)
22
Operator
• Arithmetic operator
Low High
Precedence
+ - * / // % **
Plus minus multiplication Float Integer Mod power
division division (remainder)
3 3 2 2 2 2 1
23
Comparison Operators
30
Logical operators
• Logical operators are the and, or, not operators.
31
Logical Boolean
AND/OR True/False
AND OR
32
Logical operators
Just the reverse of
NOT
what is there.
• not(true) false
33
Bitwise operators
Operator Description
& Binary AND Operator copies a bit to the result if it
Bitwise operators act on operands as exists in both operands
if they were string of binary digits. | Binary OR It copies a bit if it exists in either
operand.
^ Binary XOR It copies the bit if it is set in one
operand but not both.
It operates bit by bit.
~ Binary Ones Complement It is unary and has the effect of 'flipping'
bits.
• 2 is 10 in binary and 7 is 111.
<< Binary Left Shift The left operands value is moved left by
In the table below: the number of bits specified by the right
operand.
• Let x = 10 (0000 1010 in binary) and y = 4 >> Binary Right Shift The left operands value is moved right
by the number of bits specified by the
(0000 0100 in binary) right operand.
35
Bitwise operators
AND
Operation Output Operation Output
• Same as 101 & 111. 0&0 0 0|0 0
• This results in 101 0&1 0 0|1 1
1^1 0
38
Shift operator
A shift operator performs bit manipulation on data by shifting the bits of its first operand right or left
Shift operator
Left Shift (<<) Let’s Solve 15^13
12 & 10
Right Shift (>>)
11|00
5<<2
5>>2
40
Bitwise operators
Operators Let’s Solve
()
** 10*4>>2 and 3
+x, -x, ~x
*, /, //, %
+, -
<<, >> 10%(15<10 and
& 20<30)
^
|
==, !=, >, >=, <, <=,
in, not in
not 10/(5-5)
and
Or
=,*=,/=,//=,%=
2.5%0.15
42
Membership Operators
Operator Precedence
Operators Meaning
() Parentheses
** Exponent
+x, -x, ~x Unary plus, Unary minus, Bitwise NOT
+, - Addition, Subtraction
<<, >> Bitwise shift operators
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
==, !=, >, >=, <, <=, in, not in Comparisions, Membership operators
44
Thank You
45