Application Based Programming in
Python
Lecture - 3
Operators, Expression and Data types
Today’s Outline
• 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.
• Hands on Session with Jupyter Notebook:
• We will practice on the Python basic elements in Jupyter Notebook.
2
Data Types
• Data type is the classification of the type of values that
can be assigned to variables.
• Dynamically typed languages, the interpreter itself
predicts the data type of the Python Variable based on
the type of value assigned to that variable.
>>> x = 2
x is a variable and 2 is its value
>>> type(x)
int
x can be assigned different values;
>>> x = 2.3
hence, its type changes accordingly >>> type(x)
float
3
Data Types (Numbers)
The number data type is divided into the following five
data types: >>> a = 0x19
>>> a = 2 >>> type(a)
>>> type(a) int
• Integer int >>> a = 2 + 5j
• Long Integer (removed from >>>
py3)a = 2.5 >>>type(a)
• Floating-point Numbers >>> type(a) complex
float >>> type(a)
• Complex Numbers >>> a = 0o11 float
>>> type(a) >>> a = 9999999L
int >>> type(a)
long
5
Data Types (String)
• Python string is an ordered collection of characters
which is used to represent and store the text-based
information. Strings are stored as individual characters
in a contiguous memory location. It can be accessed
from both directions: forward and backward.
>>> a = “Sharda”
>>> print(a)
Sharda
>>> a = ‘University’
>>> print(a)
University
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:
• Ordered collections of arbitrary objects >>> a = [2,3,4,5]
• Accessed by offset >>> b = [“Sharda”,
• Arrays of object references “University”]
•
>>> print(a,b)
Of variable length, heterogeneous, and arbitrarily
nestable >>> [2,3,4,5]
• Of the category, mutable sequence [‘Sharda’,
• Data types in which elements are stored in the index
’University’]
basis with starting index as 0
• Enclosed between square brackets ‘[]’
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'
>>> >>> >>>
integer float string float float integer
integer string string integer float string
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)
2. Implicit Data Conversion
• Takes place automatically during run time between ONLY numeric values
• E.g., Adding a float and an integer will automatically result in a float
value
• E.g., Adding a string and an integer (or a float) will result in an error
since string is not numeric
• Applies type promotion to avoid loss of information
• Conversion goes from integer to float (e.g., upon adding a float and
an integer) and not vice versa so as the fractional part of the float is
not lost
19
Implicit Data Type Conversion:
Examples
The result of an expression that involves >>> print(2 + 3.4)
5.4
a float number alongside (an) integer
>>> print( 2 + 3)
number(s) is a float number
5
>>> print(9/5 * 27 + 32)
80.6
>>> print(9//5 * 27 + 32)
59
>>> print(5.9 + 4.2)
10.100000000000001
>>>
20
Implicit Data Type Conversion:
Examples
The result of an expression that involves >>> print(2 + 3.4)
5.4
a float number alongside (an) integer
>>> print( 2 + 3)
number(s) is a float number
5
The result of an expression that involves >>> print(9/5 * 27 + 32)
80.6
values of the same data type will not result
>>> print(9//5 * 27 + 32)
in any conversion
59
>>> print(5.9 + 4.2)
10.100000000000001
>>>
21
Operators
• Arithmetic Operators (**, *,/,//,%,+,_)
• Comparison Operators (<, <=, >,
>=, ==)
• Python Assignment Operators (=,
+=, -=, *=, /=)
• Logical Operators (and, or, not)
• Bitwise Operators (&, |, ~, >>, <<,
^)
• Membership Operators (in, not in)
22
Operator
• Arithmetic operator
Low High
Precedence
+ - * / // % **
Plus minus multiplication Float Integer Mod power
division division (remainder)
3 3 2 2 2 2 1
Left to right Right to left
23
Comparison Operators
Boolean expressions ask a question Operator Description
== Equals to
• Produce a Yes or No result which we use to control != Not equals to
program flow <> Not equals to
Boolean expressions using comparison > Greater than
operators evaluate to: < Less Than
• True / False - Yes / No >= Greater Than Equals to
<= Less Than Equals to
Comparison operators look at variables
• But do not change the variables
== Equals to is used for comparison
= Is used for assignment 29
Example
30
Logical operators
• Logical operators are the and, or, not operators.
Operator Meaning Example
and True if both the operands are true x and y
or True if either of the operands is true x or y
not True if operand is false (complements the operand) not x
31
Logical Boolean
AND/OR True/False
AND OR
X Y Output >>>a=7>7 >>>a=7>7 or X Y Output
and 2>-1 2>-1
TRUE TRUE TRUE >>>print(a) >>>print(a) TRUE TRUE TRUE
TRUE FALSE FALSE FALSE TRUE
>>>print(7 and 0 or 5) TRUE FALSE TRUE
FALSE TRUE FALSE 5
FALSE TRUE TRUE
FALSE FALSE FALSE
FALSE FALSE FALSE
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
5&7 • Which is binary for 5. 1&0
1&1
0
1
1|0
1|1
1
1
• Binary for 4 is 0100, and that for 8 is
1000.
• After operation, output is 1100
4|8 • Which is binary for 12.
• Bitwise operators are used in encryption algorithms and
37
applications
Bitwise operators
XOR
XOR (exclusive Operator Output
OR) returns 1 0^0 0
• If one operand is 0 Otherwise, it 0^1 1
and another is 1. returns 0. 1^0 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
*, /, //, % Multiplication, Division, Floor division, Modulus
+, - Addition, Subtraction
<<, >> Bitwise shift operators
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
==, !=, >, >=, <, <=, in, not in Comparisions, Membership operators
not Logical NOT
and Logical AND
or Logical OR
44
Thank You
45