0% found this document useful (0 votes)
20 views71 pages

Chap 5

Chapter 5 part 2

Uploaded by

8g.aditya.m.5570
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views71 pages

Chap 5

Chapter 5 part 2

Uploaded by

8g.aditya.m.5570
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 71

CH - Getting started

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)

Data types are used to identifies the type of data


a variable can hold and the operations that can
be performed on that data.
Operators: symbols that are used to
perform operations on operands.

Operand: An operand is a data item on


which operator acts.
OPERATORS (Based on
operands)

OPERATORS

UNARY BINARY TERNARY

 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

Arithmetic Relationa Logical Assignment Identity Membershi


l p

+,-,*,/,%,//,** and, or, is , is


not not

<,>,<=,>=,= =, in, not


=,!= +=,-=,*=,/=,%=,//=,**= in
ARITHMETIC OPERATORS

Arithmetic Operators are used to


perform arithmetic operations like addition,
multiplication, division etc.
+ ,
-
%,//,**
* / % // **
Arithmetic Operators Description Example

+ perform addition of two number a+b => 3+2=5


Relational
- perform subtraction of two number a-b => 3-2=1

Logical / perform division of two number a/b => 3/2=1.5

* perform multiplication of two a*b => 3*2=6


Assignment number

% Modulus = returns remainder a%b => 3%2=1

Identity // Floor Division = remove digits


after the decimal point
a//b => 3//2=1

** Exponent = perform raise to power a**b => 3**2=9


Membership
+ ,
-
%,//,**
* / % // **
Arithmetic
What will be the output of
Relational
following code
Logical >>> x=-8
>>> print(x//3)
Assignment
>>> print(8/-4)
Identity >>> print(8//-3)

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

Relational Operators are used to


compare the values.
> ,
<
%,//,**
>= <= == !=
Arithmetic Operators Description Example

== Equal to, return true if a equals to b a == b =>3==2 = False

Relational != Not equal, return true if a is not a != b =>3!=2 = True


equals to b
-3
Greater than, return true if a is
Logical >
greater than
a>b =>3>2 = True
b

Greater than or equal to , return


Assignment >=
true if a is greater than b or a is
a >= b =>3>=2 = True
-
equals to b 3

< Less than, return true if a is less a<b =>3<2 = False


Identity than b
-3
Less than or equal to , return-
<= a <= b =>3<=2 = False
true if a is less than b or a is4
equals to b
512
Membership
LOGICAL OPERATORS

Logical Operators are used to


perform logical operations on the given two
variables or values .
, and or
%,//,**

Arithmetic
Operators Description Example

Relational
and return true if both condition are true x and y
-3
Þ 3 and 35
Logical Þ 35

or return true if either or both condition x or y


are true
Assignment =>3 or 35
-
=>3 3

not Reverse the condition not(5)


Identity -3
=>False
-
4
512
Membership
ASSIGNMENT OPERATORS

Assignment Operators are used to


assign values to the variables.
-=
= +=
,
%,//,**
*= /= %= //= **=
Operator Description Eg.
Arithmetic s (b=3)

= Assigns values from right side operands to left side a=b


operand
=>3
Relational += Add 2 numbers and assigns the result to left operand. a+=b
(a=a+b)

/= Divides 2 numbers and assigns the result to left -3 a/=b


operand. (a=a/b)
Logical *= Multiply 2 numbers and assigns the result to left a*=b
operand. (a=a*b)

Assignme -= Subtracts 2 numbers and assigns the result to left


operand. (a=a-b) -
a-=b

3
nt %= modulus 2 numbers and assigns the result to left a%=b
operand. (a=a%b)

Identity //= Perform floor division on 2 numbers and assigns the


-3 a//=b
result to left operand. (a=a//b)
-
4
**= calculate power on operators and assigns the result a**=b
512
Membership to left operand. (a=a**b)
IDENTITY OPERATORS

Identity operators in Python


compare the memory locations of two
objects.
,
%,//,** is is not
Arithmetic
Operators Description Example

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

The membership operators


in Python are used to validate whether a
value is found within a sequence such as
such as strings, lists, or tuples.
,
Arithmetic %,//,** in not in
Operators Description Example
Relational
in return true if value exists in the sequence, a in list
else false.
Logical -3

not in return true if value does not exists in the a not in


sequence, else false. list
Assignment
-
3

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

 Comparison of equality & Identity operators


OPERATOR
Operators PRECEDENCE
Description PRECEDANCE
() Parenthesis HIGHEST
** Exponent
~x Bitwise compliment
+x, -x Positive or negative
*, /, //, % Arithmetic operator
+, - Add, Sub
& Bitwise &
^ Bitwise XOR
| Bitwise OR
<,<=,>,>=,<>,!=,==, is, is not Comparison & Identity
not Boolean Not
and Boolean AND
or Boolean OR LOWEST
Expression Evaluation

>>>(2 and 3 or 7) > (4*2**3/3)+9

>>>False

>>>(2 and 3 or 7) + (4*2**3/2)+9

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

 Classification of data items

 Identifies the type of data a variable can hold and the


operations that can be performed on that data.

 Every value will have a datatype.


DATA TYPES

Core Data
types

Numbe Non Sequenc Mappin


rs e es gs
Floatin
Intege Compl Strin Tupl Lis Dictiona
g
rs point ex g e t ry

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.

 3 types of Sequence datatypes are

 Strings

 Lists

 Tuples
string Tuple List
Numbers

Sequences 1. String is a collection of any valid


characters in a quotation marks ( 'or “ )
Mapping 2.Strings are used to store information like
name, address, descriptions. Etc
Sets For example:
“zion”, ‘Alwin’, “sales2020”
None
string
In Python string is a sequence of characters and each character can
be individually access using index. From beginning the first character in String
is at index 0 and last will be at len-1. From backward direction last character
will be at index -1 and first character will be at –len.

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

• Standard mapping type in python is Dictionary


Dictionary
Numbers Dictionary is another feature of Python. It is
an unordered set of comma separated key:value
Sequences
pairs.
Dictionary Items are defined in Curly Brackets
{}
Mapping
• No two keys can be same.

Sets >>> student={'Roll':1,'Name':"Jagga",'Per':91.5}


>>>print(student)
>>> print(student['Per'])
None 91.5
Dictionary
Numbers
>>> val={1:100,2:300,4:900}
>>> print(val[1])
Sequences 100

Mapping Dictionary is mutable. i.e. We can modify dictionary


elements.
>>>val[2]=1000
Sets >>>print(val) # {1: 100, 2: 1000, 4: 900}

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

 Tuples – for month

 Sets – admno

 Dictionary – student’s phone number

You might also like