Chap 5
Chap 5
Core Data
types
with Python
Non
e
Floatin
Intege Compl Tupl Lis
g
rs point ex e t
Boolea
n
Operators in Python
Data (input) + processing =Information (output)
OPERATORS
require only one are those operators that are those operators
operand to operate like require two operands to that require three
unary + ,–, ~(bitwise operate. operands to operate.
complement) and not Operand operator operand Operand if operand else
Operator operand >>>a=30 operand
>>>b=20
>>> a=30
>>>a+b >>>a,b=10,20
>>> print(-a)
>>>50 >>> a if (a>b) else b
-30
20
BINARY
OPERATORS
OPERATORS
Membership
+ ,
-
%,//,**
* / % // **
Arithmetic
What will be the output
Relational of following code
>>> x=-8
Logical
>>> print(x//3)
-3
Assignment >>> print(8/-4)
>>> print(8//-3)
Identity
Membership
+ ,
-
%,//,**
* / % // **
Arithmetic
What will be the output
Relational of following code
>>> x=-8
Logical
>>> print(x//3)
-3
Assignment >>> print(8/-4) -
-2.03
>>> print(8//-3)
Identity
Membership
+ ,
-
%,//,**
* / % // **
Arithmetic
What will be the output
Relational of following code
>>> x=-8
Logical
>>> print(x//3)
-3
Assignment >>> print(8/-4) -
-2.03
>>> print(8//-3)
Identity
-3
Membership
+ ,
-
%,//,**
* / % // **
Arithmetic
What will be the output
Relational of following code
>>> -11 // 5 -3
Logical
>>> -11 % 5
Assignment >>> 11 % - 5 -
>>> 11 // -5 3
Identity
>>>2**3**2 -3
Membership
+ ,
-
%,//,**
* / % // **
Arithmetic
What will be the output
Relational of following code
>>> -11 // 5 -3
Logical
>>> -11 % 5
Assignment >>> 11 % - 5 -
>>> 11 // -5 3
Identity
>>>2**3**2 -3
Membership
+ ,
-
%,//,**
* / % // **
Arithmetic
What will be the output
Relational of following code
>>> -11 // 5 -3
Logical
>>> -11 % 5
4
Assignment >>> 11 % - 5 -
>>> 11 // -5 3
Identity
>>>2**3**2 -3
Membership
+ ,
-
%,//,**
* / % // **
Arithmetic
What will be the output
Relational of following code
>>> -11 // 5 -3
Logical
>>> -11 % 5
4
Assignment >>> 11 % - 5 -
>>> 11 // -5 - 3
4
Identity
>>>2**3**2 -3
Membership
+ ,
-
%,//,**
* / % // **
Arithmetic
What will be the output
Relational of following code
>>> -11 // 5 -3
Logical
>>> -11 % 5
4
Assignment >>> 11 % - 5 -
>>> 11 // -5 - 3
4
Identity
>>>2**3**2 -4-3
-
4
Membership
+ ,
-
%,//,**
* / % // **
Arithmetic
What will be the output
Relational of following code
>>> -11 // 5 -3
Logical
>>> -11 % 5
4
Assignment >>> 11 % - 5 -
>>> 11 // -5 - 3
4
Identity
>>>2**3**2 -4-3
-
4
Membership 512
RELATIONAL OPERATORS
Arithmetic
Operators Description Example
Relational
and return true if both condition are true x and y
-3
Þ 3 and 35
Logical Þ 35
3
nt %= modulus 2 numbers and assigns the result to left a%=b
operand. (a=a%b)
Relational is returns true if two variables point the same object, else a is b
false
is not returns true if two variables point the different object, a is not b
else false -3
Logical
-
Assignment 3
Identity -3
-
4
512
Membership
,
%,//,** is is not
Arithmetic
e.g.
>>>a=34
Relational >>> b=34
>>> a is b
True
-3
>>> a is not b
Logical False
>>> a=20 -
Assignment >>> b=34 3
>>> a is b
False
Identity >>> a is not b -3
True -
4
512
Membership
Note: In python each value in memory is assigned a
memory address. So each time a new variable is
pointing to that value they will be assigned the same
address and no new memory allocation
valu
e
10 2 21 40
addres
15
5520 2
5600 0
568 5696 55
6000 6240
s 0
>>> a=10
a = 10
>>> b=10
b = 10
>>> c=15
c = 15
>>>
print(id(a))
a b c
>>>17084355
20
>>>print(id(b))
>>>17084355
20
>>>print(id(c))
MEMBERSHIP OPERATORS
Identity
-3
-
4
Membersh 512
ip
,
Arithmetic %,//,** in not in
E.g.
Relational a = 22
list = [22,99,27,31]
Logical In_Ans = a in list -3
NotIn_Ans = a not in list
print(In_Ans)
Assignment
print(NotIn_Ans) -
3
Identity
Output :- -
-3
True 4
Membersh 512
False
ip
OTHER OPERATORS
Bitwise operator
Operator Purpose Action
& Bitwise AND Return 1 if both
inputs are 1
^ Bitwise XOR Return 1, if the
number of 1 in
input is in odd
| Bitwise OR Return 1 if any
input is 1
>>> 20 & 6
Bitwise operator works on
the binary value of number
Eg: 20 - 00010100
6 - 00000110
---------------
00000100 4
OUTPUT:
>>>4
Operators Purpose
<< Shift left (*)
>> Shift right (/)
Eg.
Eg.
20 << 2
20 >> 2
00010100 ->20
00010100 ->20
01010000
00000101
Output:
Output:
80 (Dec. no. of
5 (Dec. no. of 00101)
1010000)
Comparison of Assignment & Relational operators
>>>False
>>>28.0
Applications of Python OPERATORS
Expression Evaluation
Punctuators/
Delimiters
Used to implement the grammatical and structure of
a Syntax.Following are the python punctuators.
Everything is an
Python
objecttreats every value or data item as an
object
Every object is assigned a unique identity.
The Function id() returns the identity of an object.
Core Data
types
DATA
Non
TYPES
e
Floatin
Intege
rs
MUTABLE
g
Compl
ex
point
ANDe IMMUTABLE
Tupl Lis
t
Boolea TYPES
n
DATA TYPES
Core Data
types
Boolea
n
NUMBERS
The Number data types are
Numbers
used to store numeric values.
Numbers in Python can be of
Sequences
following types:
i) Integers
Mapping
a) Integers(signed)
b) Boolean
Sets
ii) Floating point numbers
iii) Complex Numbers
None
int float complex
Numbers Integers allows to store whole numbers only and there
is no fraction parts.
Integers can be positive and negative e.g. 100,
Sequences 250, -12, +50
There are two integers in Python:
1) Integers(signed):it is normal integer
Mapping representation of whole numbers. Integers in
python can be on any length, it is only limited by
memory available. In Python 3.x int data type can
Sets be used to store big or small integer value
whether it is +ve or –ve.
2) Booleans: it allows to store only two values True
None and False. The internal value of boolean value
True and False is 1 and 0 resp. We can get
boolean value from 0 and 1 using bool() function.
int float complex
Numbers It allows to store numbers with decimal
points.
Sequences 1. Fractional Form : 200.50, 0.78, -12.787
2. Exponent Form : it is represented with
mantissa and exponent. For e.g
Mapping
>>>x = 1.5E2 # means 1.5 x 102 which is 150
>>>print(x) # 150.0
Sets
>>>y=12.78654E04
>>>print(y) # 127865.4
None
int float complex
Numbers Floating point numbers have two advantage
over integers:
Sequences they can represent values between the
integers
they can represent a much greater
Mapping range of values
But floating point numbers suffers from one
disadvantage also:
Sets
Floating point operations are usually
slower than integer operations.
None
int float complex
Numbers
Python represent
complex
Sequences
numbers in the form A+Bj.
Mapping If the complex number is a then we can
write a.real or a.imag
Sets Example
>>>a=1+3.54j
# 1.0
None >>>print(a.real)
# 3.54
>>>print(a.imag)
SEQUENCES
SEQUENCES
Ordered collection of items where each item is
indexed by an integer.
Strings
Lists
Tuples
string Tuple List
Numbers
Forward
indexing
0 1 2 3 4 5 6
messa
W E L C O M E
ge
-7 -6 -5 -4 -3 -2 -1
Backward
indexing
string List Tuple
Numbers
We cannot change the individual letters of string
by assignment because string in python is
Sequences immutable and hence if we try to do this,
Python will raise an error “object does not
support Item assignment”
Mapping
>>>name=“Ronaldo”
>>>name[1]="I” # error
Sets
However we can assign string to another string.
For e.g
None
>>>name=“Ronaldo”
>>>name=“Bekham” # no error
string List Tuple
Numbers
A list in python represents a list
of comma-separated values of any
Sequences
data type between square brackets.
Mapping
[10,20,30,40,50]
Sets [“a‟,‟e‟,‟o‟,‟i‟,‟u‟]
None [“alwin”,208004,97.5]
string List Tuple
Numbers
Examples:
>>> family=["Mom","Dad","Sis","Bro"]
Sequences
>>> print(family)
['Mom', 'Dad', 'Sis', 'Bro']
Mapping
>>>
Employee=["E001","Naman",50000,10.5]
Sets
>>> Employee[2]=100
>>> print(Employee)
None ['E001', 'Naman’, 100, 10.5]
string List Tuple
Numbers
Tuple values are also internally numbered from 0
and so on enclosed within ( ).
Sequences
>>> print(favorites[1])
Cricket
Mapping
>>> print(student[2])
97.5
Sets >>>Employee=("E001","N
aman",50000,10.5)
>>> Employee[2]=100
None
# Error, tuple does not
support assignment
i.e. immutable
MAPPING
MAPPING
• Unordered Datatype in Python
None
SET
Set
Numbers
• Unordered collection of items separated by
commas
Sequences and enclosed in curly brackets { }.
• It cannot have duplicate entries.
Mapping Eg.:
set1={1,2,”Alwin”}
Set print(set1)
None
None
None
Numbers
Special datatype with a single value
represents the absence of value
Sequences
Eg.:
Mapping
v= None
print(v)
Set None
None
Datatypes
Immutable Mutable
Datatype Datatype
Numbers List
String Dictionary
Tuple Sets
MUTABLE AND IMMUTABLE
TYPES
Python data object can be broadly categorized into two types – mutable and immutable
types. In simple words changeable/modifiable and non-modifiable types.
1. Immutable types: are those that can never change their value in place. In python
following types are immutable: integers, float, Boolean, strings, tuples
Sample Code:
a = 10
b=a
c = 15 # will give output 10,10,30
a = 20 From this code, you can say the value of integer a, b,c
b = 40 could be changed effortlessly, but this is not the case. Let
us understand what was done behind the scene
c=b
IMMUTABLE
TYPES
From the previous code it is clear that variable names are stored references to a
value-object. Each time we change the value the variable‟s reference memory
address changes. So it will not store new value in same memory location that‟s
why Integer, float, Booleans, strings and tuples are immutable.
Variables (of certain type) are NOT LIKE storage containers i.e. with fixed
memory address where value changes every time. Hence they are
immutable
MUTABLE
TYPE
Mutable means in same memory address, new value can be stored as and when it
is required. Python provides following mutable types:
1. Lists
2. Dictionaries
3. Sets
Examples: (using List)
>>> employee=["E001","Rama","Sales",67000] See, even if
we change
>>> print(id(employee))
the value, its
71593896 reference
>>> employee[3]=75000 memory
>>> print(id(employee)) address has
71593896 remained
same
>>>
Applications of Python Datatypes
List - Names of students of a class
Sets – admno