SlideShare a Scribd company logo
Parts of Python
Programming language
Megha V
Research Scholar
Kannur University
31-10-2021 meghav@kannuruniv.ac.in 1
Parts of Python Programming language
• Keywords
• statements and expressions
• Variables
• Operators
• Precedence and Associativity
• Data Types
• Indentation
• Comments
• Reading Input
• Print Output
31-10-2021 meghav@kannuruniv.ac.in 2
Keywords
Keywords are the reserved words that are already defined by the Python for
specific uses
• False await else import pass
• None break except in raise
• True class finally is return
• and continue for as assert
• async def del elif from
• global if lambda try not
• or while with yield
31-10-2021 meghav@kannuruniv.ac.in 3
Variables
• Reserved memory locations to store values.
• It means that when you create a variable, you reserve some space in
the memory.
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = "John" # A string
print (counter)
print (miles)
print (name)
31-10-2021 meghav@kannuruniv.ac.in 4
Multiple Assignment
• Python allows you to assign a single value to several variables
simultaneously.
a = b = c = 1
• You can also assign multiple objects to multiple variables
a, b, c = 1, 2, "john”
31-10-2021 meghav@kannuruniv.ac.in 5
Operators
Python language supports the following types of operators-
• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
31-10-2021 meghav@kannuruniv.ac.in 6
Arithmetic Operators
• Assume a=10 ,b =21, then
Operator Description Example
+ Addition Adds values on either side of the operator. a + b = 31
- Subtraction Subtracts right hand operand from left hand
operand.
a – b = -11
* Multiplication Multiplies values on either side of the operator a * b = 210
/ Division Divides hand operand by right hand operand left b / a = 2.1
% Modulus Divides left hand operand by right hand operand
and returns remainder
b % a = 1
** Exponent Performs exponential (power) calculation on
operators
a**b =10 to the
power 21
// Floor Division 9 - The division of operands where the result is the
quotient in which the digits after the decimal point
are removed.
//2 = 4 and 9.0//2.0
= 4.0
31-10-2021 meghav@kannuruniv.ac.in 7
Comparison Operators/ Relational operators
• These operators compare the values on either side of them and decide the relation among them.
• Assume a =10 , b=20, then
Operator< Description Example
== If the values of two operands are equal, then the condition become true (a==b) is not
true
!= If values of two operands are not equal, then condition become true (a!=b) is true
> If the value of the left operand is greater than the value of the right operand,
then condition become true
(a>b) is not
true
< If the value of the left operand is less than the value of the right operand, then
condition become true
(a<b)is true
>= If the value of the left operand is greater than or equal to the value of the right
operand, then condition become true
(a>=b)is not
true
<= If the value of the left operand is less than or equal to the value of the right
operand, then condition become true
(a<=b)is true
31-10-2021 meghav@kannuruniv.ac.in 8
Logical operators
Precedence of logical operator
• not –unary operator
• and Decreasing order
• or
Example
>>> (10<5) and ((5/0)<10)
False
>>> (10>5) and ((5/0)<10)
ZeroDivisionError
31-10-2021 meghav@kannuruniv.ac.in 9
Bitwise operators
• Bitwise AND – x & y
• Bitwise OR – x|y
• Bitwise Complement - ~ x
• Bitwise Exclusive OR -x˄y
• Left Shift – x<<y
• Right Shift –x>>y
31-10-2021 meghav@kannuruniv.ac.in 10
Precedence and Associativity of operators
Operator
Decreasing
order
()
**
+x, -x, ~x
/, //, *, %
+, -
<<, >>
&
˄
|
==,<, >, <=, >= , !=
not
and
or
31-10-2021 meghav@kannuruniv.ac.in 11
Data Types
Python has five standard data types-
• Numbers
• String
• List
• Tuple
• Dictionary
31-10-2021 meghav@kannuruniv.ac.in 12
Numbers
• Number data types store numeric values.
• Number objects are created when you assign a value to them.
• For example
var1 = 1 var2 = 10
• Python supports three different numerical types
- int (signed integers)
- float (floating point real values)
- complex (complex numbers)
31-10-2021 meghav@kannuruniv.ac.in 13
Strings
• Continuous set of characters represented in the quotation
marks.
• Python allows either pair of single or double quotes.
• Subsets of strings can be taken using the slice operator ([ ] and
[:] ) with indexes starting at 0 in the beginning of the string and
working their way from -1 to the end.
• The plus (+) sign is the string concatenation operator and the
asterisk (*) is the repetition operator.
31-10-2021 meghav@kannuruniv.ac.in 14
Strings
For example-
str = 'Hello World!’
print (str) # Prints complete string – Hello World!
print (str[0]) # Prints first character of the string - H
print (str[2:5]) # Prints characters starting from 3rd to 5th- llo
print (str[2:]) # Prints string starting from 3rd character –llo World!
print (str * 2) # Prints string two times –Hello World! Hello World!
print (str + "TEST") # Prints concatenated string- Hello World!Test
31-10-2021 meghav@kannuruniv.ac.in 15
Lists
• Lists are the most versatile of Python's compound data types.
• A list contains items separated by commas and enclosed within square
brackets ([]).
• To some extent, lists are similar to arrays in C.
• One of the differences between them is that all the items belonging to a list
can be of different data type.
• The values stored in a list can be accessed using the slice operator ([ ] and
[:]) with indexes starting at 0 in the beginning of the list and working their
way to end -1.
• The plus (+) sign is the list concatenation operator, and the asterisk (*) is
the repetition operator.
31-10-2021 meghav@kannuruniv.ac.in 16
Lists
31-10-2021 meghav@kannuruniv.ac.in 17
For example
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john’] Output
print (list) # Prints complete list
print (list[0]) # Prints first element of the list
print (list[1:3]) #Prints elements starting from 2nd till 3rd
print (list[2:]) # Prints elements starting from 3rd element
print (tinylist * 2) # Prints list two times
print (list + tinylist) # Prints concatenated lists
Tuples
• Similar to the list.
• Consists of a number of values separated by commas.
• Enclosed within parenthesis.
• The main difference between lists and tuples is-
• Lists are enclosed in brackets ( [ ] ) and their elements and size can be
changed, while tuples are enclosed in parentheses ( ( ) ) and cannot
be updated.
• Tuples can be thought of as read-only lists.
31-10-2021 meghav@kannuruniv.ac.in 18
Tuples
For example-
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john’) Output
print (tuple) # Prints complete tuple
print (tuple[0]) # Prints first element of the tuple
print (tuple[1:3]) # Prints elements starting from 2nd till 3rd
print (tuple[2:]) # Prints elements starting from 3rd element
print (tinytuple * 2) # Prints tuple two times
print (tuple + tinytuple) # Prints concatenated tuple
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tuple[2] = 1000 # Invalid syntax with tuple
list[2] = 1000 # Valid syntax with list
31-10-2021 meghav@kannuruniv.ac.in 19
Dictionary
• Python's dictionaries are kind of hash-table type.
• They work like associative arrays or hashes found in Perl and consist
of key-value pairs.
• A dictionary key can be almost any Python type, but are usually
numbers or strings. Values, on the other hand, can be any arbitrary
Python object.
31-10-2021 meghav@kannuruniv.ac.in 20
Dictionary
• Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed
using square braces ([]).
For example-
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
tinydict = {'name': 'john','code':6734, 'dept': 'sales’} Output
print (dict['one']) # Prints value for 'one' key
print (dict[2]) # Prints value for 2 key
print (tinydict) # Prints complete dictionary
print (tinydict.keys()) # Prints all the keys
print (tinydict.values()) # Prints all the values
31-10-2021 meghav@kannuruniv.ac.in 21
Indentation
• Adding white space before a statement
• To indicate a block of code.
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!") #syntax error
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!") # syntax error
31-10-2021 meghav@kannuruniv.ac.in 22
Comments
• A hash sign (#) that is not inside a string literal is the beginning of a
comment.
• All characters after the #, up to the end of the physical line, are part
of the comment and the Python interpreter ignores them.
• # First comment
• print ("Hello, Python!") # second comment
31-10-2021 meghav@kannuruniv.ac.in 23
Reading Input
• Python provides some built-in functions to perform both input and output
operations.
Python provides us with two inbuilt functions to read the input from the
keyboard.
raw_input()
input()
raw_input(): This function reads only one line from the standard input and returns
it as a String.
input(): The input() function first takes the input from the user and then evaluates
the expression, which means python automatically identifies whether we entered a
string or a number or list.
• But in Python 3 the raw_input() function was removed and renamed to input().
31-10-2021 meghav@kannuruniv.ac.in 24
Print Output
31-10-2021 meghav@kannuruniv.ac.in 25
In order to print the output, python provides us with a built-in function called
print().
Example:
Print(“Hello Python”)
Output:
Hello Python
Type conversion
• Python defines type conversion functions to directly convert one data
type to
1. int(a,base) # any data type to integer. ‘Base’ specifies the base in which string
is if data type is string.
2. float() #any data type to a floating point number
3. ord() #character to integer.
4. hex() #integer to hexadecimal string.
5. oct() #integer to octal string.
6. tuple() #convert to a tuple.
7. set() #returns the type after converting to set.
8. list() # any data type to a list type
10. str() # integer into a string.
11. complex(real,imag) # real numbers to complex(real,imag) number.
12. chr(number) #number to its corresponding ASCII character.
31-10-2021 meghav@kannuruniv.ac.in 26
is operator
• The Equality operator (==) compares the values of both the operands and checks for
value equality.
• Whereas the ‘is’ operator checks whether both the operands refer to the same object or
not (present in the same memory location).
31-10-2021 meghav@kannuruniv.ac.in 27
Dynamic and Strongly typed language
• Python is a dynamically typed language.
• What is dynamic?
• We don't have to declare the type of a variable or manage the
memory while assigning a value to a variable in Python.
31-10-2021 meghav@kannuruniv.ac.in 28
Mutable and immutable objects in Python
• There are two types of objects in python i.e.
• Mutable and Immutable objects.
• Whenever an object is instantiated, it is assigned a unique
object id.
• The type of the object is defined at the runtime and it can’t be
changed afterwards.
• However, it’s state can be changed if it is a mutable object.
• Immutable Objects : These are of in-built types like int, float,
bool, string, unicode, tuple. In simple words, an immutable
object can’t be changed after it is created.
• Mutable Objects : These are of type list, dict, set . Custom
classes are generally mutable.
31-10-2021 meghav@kannuruniv.ac.in 29

More Related Content

PPTX
Python programming
Megha V
 
PPTX
Operators in Python
Anusuya123
 
PPTX
Seminar on Advanced Driver Assistance Systems (ADAS).pptx
Mohit Nayal
 
PDF
BCS515B Module3 vtu notes : Artificial Intelligence Module 3.pdf
Swetha A
 
PPTX
File handling in Python
Megha V
 
PPTX
Python
Shivam Gupta
 
PPTX
Introduction to cyber security
Self-employed
 
PPT
Medicinal plants
Dr. Shalini Pandey
 
Python programming
Megha V
 
Operators in Python
Anusuya123
 
Seminar on Advanced Driver Assistance Systems (ADAS).pptx
Mohit Nayal
 
BCS515B Module3 vtu notes : Artificial Intelligence Module 3.pdf
Swetha A
 
File handling in Python
Megha V
 
Python
Shivam Gupta
 
Introduction to cyber security
Self-employed
 
Medicinal plants
Dr. Shalini Pandey
 

What's hot (20)

PDF
Python exception handling
Mohammed Sikander
 
PPTX
Constructor in java
Pavith Gunasekara
 
PPTX
Strings in C
Kamal Acharya
 
PPTX
Python Seminar PPT
Shivam Gupta
 
PPTX
C++ Overview PPT
Thooyavan Venkatachalam
 
PPTX
Code Optimization
Akhil Kaushik
 
PDF
Strings in python
Prabhakaran V M
 
PPTX
Preprocessor directives in c language
tanmaymodi4
 
PPTX
Basics of Object Oriented Programming in Python
Sujith Kumar
 
PDF
Immutable vs mutable data types in python
Learnbay Datascience
 
PPTX
What is identifier c programming
Rumman Ansari
 
PPTX
Tokens in C++
Mahender Boda
 
PPTX
Infix to postfix conversion
Then Murugeshwari
 
PPTX
Logical and shift micro operations
Sanjeev Patel
 
PPTX
Data types in python
RaginiJain21
 
PPTX
Command line arguments
Ashok Raj
 
PPTX
Floating point arithmetic operations (1)
cs19club
 
PDF
Python Flow Control
Mohammed Sikander
 
PPTX
Structure in C
Kamal Acharya
 
PPTX
Function C programming
Appili Vamsi Krishna
 
Python exception handling
Mohammed Sikander
 
Constructor in java
Pavith Gunasekara
 
Strings in C
Kamal Acharya
 
Python Seminar PPT
Shivam Gupta
 
C++ Overview PPT
Thooyavan Venkatachalam
 
Code Optimization
Akhil Kaushik
 
Strings in python
Prabhakaran V M
 
Preprocessor directives in c language
tanmaymodi4
 
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Immutable vs mutable data types in python
Learnbay Datascience
 
What is identifier c programming
Rumman Ansari
 
Tokens in C++
Mahender Boda
 
Infix to postfix conversion
Then Murugeshwari
 
Logical and shift micro operations
Sanjeev Patel
 
Data types in python
RaginiJain21
 
Command line arguments
Ashok Raj
 
Floating point arithmetic operations (1)
cs19club
 
Python Flow Control
Mohammed Sikander
 
Structure in C
Kamal Acharya
 
Function C programming
Appili Vamsi Krishna
 
Ad

Similar to Parts of python programming language (20)

PDF
Raspberry Pi - Lecture 5 Python for Raspberry Pi
Mohamed Abdallah
 
PPTX
“Python” or “CPython” is written in C/C+
Mukeshpanigrahy1
 
PPTX
DATA TYPES IN PYTHON jesjdjdjkdkkdk.pptx
rajvishnuf9
 
PPTX
IOT notes,................................
taetaebts431
 
PDF
Python Programminng…………………………………………………..
somyaranjan27
 
PDF
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
S.Mohideen Badhusha
 
PPTX
Keep it Stupidly Simple Introduce Python
SushJalai
 
PPSX
Programming with Python
Rasan Samarasinghe
 
PPTX
Python programming –part 3
Megha V
 
PPTX
Python programming: Anonymous functions, String operations
Megha V
 
PPTX
Python programming
Ashwin Kumar Ramasamy
 
PPT
Python lab basics
Abi_Kasi
 
PPTX
Python-CH01L04-Presentation.pptx
ElijahSantos4
 
ODP
Python slide.1
Aswin Krishnamoorthy
 
PPT
Data types usually used in python for coding
PriyankaRajaboina
 
PPT
02python.ppt
ssuser492e7f
 
PPT
02python.ppt
rehanafarheenece
 
PPTX
unit1 python.pptx
TKSanthoshRao
 
PPTX
scripting in Python
Team-VLSI-ITMU
 
PPTX
Python
Sangita Panchal
 
Raspberry Pi - Lecture 5 Python for Raspberry Pi
Mohamed Abdallah
 
“Python” or “CPython” is written in C/C+
Mukeshpanigrahy1
 
DATA TYPES IN PYTHON jesjdjdjkdkkdk.pptx
rajvishnuf9
 
IOT notes,................................
taetaebts431
 
Python Programminng…………………………………………………..
somyaranjan27
 
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
S.Mohideen Badhusha
 
Keep it Stupidly Simple Introduce Python
SushJalai
 
Programming with Python
Rasan Samarasinghe
 
Python programming –part 3
Megha V
 
Python programming: Anonymous functions, String operations
Megha V
 
Python programming
Ashwin Kumar Ramasamy
 
Python lab basics
Abi_Kasi
 
Python-CH01L04-Presentation.pptx
ElijahSantos4
 
Python slide.1
Aswin Krishnamoorthy
 
Data types usually used in python for coding
PriyankaRajaboina
 
02python.ppt
ssuser492e7f
 
02python.ppt
rehanafarheenece
 
unit1 python.pptx
TKSanthoshRao
 
scripting in Python
Team-VLSI-ITMU
 
Ad

More from Megha V (20)

PPTX
Soft Computing Techniques_Part 1.pptx
Megha V
 
PPTX
JavaScript- Functions and arrays.pptx
Megha V
 
PPTX
Introduction to JavaScript
Megha V
 
PPTX
Python Exception Handling
Megha V
 
PPTX
Python- Regular expression
Megha V
 
PPTX
Python programming -Tuple and Set Data type
Megha V
 
PPTX
Python programming –part 7
Megha V
 
PPTX
Python programming Part -6
Megha V
 
PPTX
Python programming- Part IV(Functions)
Megha V
 
PPTX
Strassen's matrix multiplication
Megha V
 
PPTX
Solving recurrences
Megha V
 
PPTX
Algorithm Analysis
Megha V
 
PPTX
Algorithm analysis and design
Megha V
 
PPTX
Genetic algorithm
Megha V
 
PPTX
UGC NET Paper 1 ICT Memory and data
Megha V
 
PPTX
Seminar presentation on OpenGL
Megha V
 
PPT
Msc project_CDS Automation
Megha V
 
PPTX
Gi fi technology
Megha V
 
PPTX
Digital initiatives in higher education
Megha V
 
PPTX
Information and Communication Technology (ICT) abbreviation
Megha V
 
Soft Computing Techniques_Part 1.pptx
Megha V
 
JavaScript- Functions and arrays.pptx
Megha V
 
Introduction to JavaScript
Megha V
 
Python Exception Handling
Megha V
 
Python- Regular expression
Megha V
 
Python programming -Tuple and Set Data type
Megha V
 
Python programming –part 7
Megha V
 
Python programming Part -6
Megha V
 
Python programming- Part IV(Functions)
Megha V
 
Strassen's matrix multiplication
Megha V
 
Solving recurrences
Megha V
 
Algorithm Analysis
Megha V
 
Algorithm analysis and design
Megha V
 
Genetic algorithm
Megha V
 
UGC NET Paper 1 ICT Memory and data
Megha V
 
Seminar presentation on OpenGL
Megha V
 
Msc project_CDS Automation
Megha V
 
Gi fi technology
Megha V
 
Digital initiatives in higher education
Megha V
 
Information and Communication Technology (ICT) abbreviation
Megha V
 

Recently uploaded (20)

PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PDF
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PPTX
oapresentation.pptx
mehatdhavalrajubhai
 
PDF
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
PDF
Become an Agentblazer Champion Challenge
Dele Amefo
 
PDF
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PDF
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PDF
Become an Agentblazer Champion Challenge Kickoff
Dele Amefo
 
PPTX
Why Use Open Source Reporting Tools for Business Intelligence.pptx
Varsha Nayak
 
PPTX
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PDF
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
PPTX
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
Presentation about variables and constant.pptx
kr2589474
 
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
oapresentation.pptx
mehatdhavalrajubhai
 
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
Activate_Methodology_Summary presentatio
annapureddyn
 
Become an Agentblazer Champion Challenge
Dele Amefo
 
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
Become an Agentblazer Champion Challenge Kickoff
Dele Amefo
 
Why Use Open Source Reporting Tools for Business Intelligence.pptx
Varsha Nayak
 
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
Explanation about Structures in C language.pptx
Veeral Rathod
 

Parts of python programming language

  • 1. Parts of Python Programming language Megha V Research Scholar Kannur University 31-10-2021 [email protected] 1
  • 2. Parts of Python Programming language • Keywords • statements and expressions • Variables • Operators • Precedence and Associativity • Data Types • Indentation • Comments • Reading Input • Print Output 31-10-2021 [email protected] 2
  • 3. Keywords Keywords are the reserved words that are already defined by the Python for specific uses • False await else import pass • None break except in raise • True class finally is return • and continue for as assert • async def del elif from • global if lambda try not • or while with yield 31-10-2021 [email protected] 3
  • 4. Variables • Reserved memory locations to store values. • It means that when you create a variable, you reserve some space in the memory. counter = 100 # An integer assignment miles = 1000.0 # A floating point name = "John" # A string print (counter) print (miles) print (name) 31-10-2021 [email protected] 4
  • 5. Multiple Assignment • Python allows you to assign a single value to several variables simultaneously. a = b = c = 1 • You can also assign multiple objects to multiple variables a, b, c = 1, 2, "john” 31-10-2021 [email protected] 5
  • 6. Operators Python language supports the following types of operators- • Arithmetic Operators • Comparison (Relational) Operators • Assignment Operators • Logical Operators • Bitwise Operators • Membership Operators • Identity Operators 31-10-2021 [email protected] 6
  • 7. Arithmetic Operators • Assume a=10 ,b =21, then Operator Description Example + Addition Adds values on either side of the operator. a + b = 31 - Subtraction Subtracts right hand operand from left hand operand. a – b = -11 * Multiplication Multiplies values on either side of the operator a * b = 210 / Division Divides hand operand by right hand operand left b / a = 2.1 % Modulus Divides left hand operand by right hand operand and returns remainder b % a = 1 ** Exponent Performs exponential (power) calculation on operators a**b =10 to the power 21 // Floor Division 9 - The division of operands where the result is the quotient in which the digits after the decimal point are removed. //2 = 4 and 9.0//2.0 = 4.0 31-10-2021 [email protected] 7
  • 8. Comparison Operators/ Relational operators • These operators compare the values on either side of them and decide the relation among them. • Assume a =10 , b=20, then Operator< Description Example == If the values of two operands are equal, then the condition become true (a==b) is not true != If values of two operands are not equal, then condition become true (a!=b) is true > If the value of the left operand is greater than the value of the right operand, then condition become true (a>b) is not true < If the value of the left operand is less than the value of the right operand, then condition become true (a<b)is true >= If the value of the left operand is greater than or equal to the value of the right operand, then condition become true (a>=b)is not true <= If the value of the left operand is less than or equal to the value of the right operand, then condition become true (a<=b)is true 31-10-2021 [email protected] 8
  • 9. Logical operators Precedence of logical operator • not –unary operator • and Decreasing order • or Example >>> (10<5) and ((5/0)<10) False >>> (10>5) and ((5/0)<10) ZeroDivisionError 31-10-2021 [email protected] 9
  • 10. Bitwise operators • Bitwise AND – x & y • Bitwise OR – x|y • Bitwise Complement - ~ x • Bitwise Exclusive OR -x˄y • Left Shift – x<<y • Right Shift –x>>y 31-10-2021 [email protected] 10
  • 11. Precedence and Associativity of operators Operator Decreasing order () ** +x, -x, ~x /, //, *, % +, - <<, >> & ˄ | ==,<, >, <=, >= , != not and or 31-10-2021 [email protected] 11
  • 12. Data Types Python has five standard data types- • Numbers • String • List • Tuple • Dictionary 31-10-2021 [email protected] 12
  • 13. Numbers • Number data types store numeric values. • Number objects are created when you assign a value to them. • For example var1 = 1 var2 = 10 • Python supports three different numerical types - int (signed integers) - float (floating point real values) - complex (complex numbers) 31-10-2021 [email protected] 13
  • 14. Strings • Continuous set of characters represented in the quotation marks. • Python allows either pair of single or double quotes. • Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string and working their way from -1 to the end. • The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator. 31-10-2021 [email protected] 14
  • 15. Strings For example- str = 'Hello World!’ print (str) # Prints complete string – Hello World! print (str[0]) # Prints first character of the string - H print (str[2:5]) # Prints characters starting from 3rd to 5th- llo print (str[2:]) # Prints string starting from 3rd character –llo World! print (str * 2) # Prints string two times –Hello World! Hello World! print (str + "TEST") # Prints concatenated string- Hello World!Test 31-10-2021 [email protected] 15
  • 16. Lists • Lists are the most versatile of Python's compound data types. • A list contains items separated by commas and enclosed within square brackets ([]). • To some extent, lists are similar to arrays in C. • One of the differences between them is that all the items belonging to a list can be of different data type. • The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the list and working their way to end -1. • The plus (+) sign is the list concatenation operator, and the asterisk (*) is the repetition operator. 31-10-2021 [email protected] 16
  • 17. Lists 31-10-2021 [email protected] 17 For example list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] tinylist = [123, 'john’] Output print (list) # Prints complete list print (list[0]) # Prints first element of the list print (list[1:3]) #Prints elements starting from 2nd till 3rd print (list[2:]) # Prints elements starting from 3rd element print (tinylist * 2) # Prints list two times print (list + tinylist) # Prints concatenated lists
  • 18. Tuples • Similar to the list. • Consists of a number of values separated by commas. • Enclosed within parenthesis. • The main difference between lists and tuples is- • Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. • Tuples can be thought of as read-only lists. 31-10-2021 [email protected] 18
  • 19. Tuples For example- tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) tinytuple = (123, 'john’) Output print (tuple) # Prints complete tuple print (tuple[0]) # Prints first element of the tuple print (tuple[1:3]) # Prints elements starting from 2nd till 3rd print (tuple[2:]) # Prints elements starting from 3rd element print (tinytuple * 2) # Prints tuple two times print (tuple + tinytuple) # Prints concatenated tuple tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] tuple[2] = 1000 # Invalid syntax with tuple list[2] = 1000 # Valid syntax with list 31-10-2021 [email protected] 19
  • 20. Dictionary • Python's dictionaries are kind of hash-table type. • They work like associative arrays or hashes found in Perl and consist of key-value pairs. • A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object. 31-10-2021 [email protected] 20
  • 21. Dictionary • Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]). For example- dict = {} dict['one'] = "This is one" dict[2] = "This is two" tinydict = {'name': 'john','code':6734, 'dept': 'sales’} Output print (dict['one']) # Prints value for 'one' key print (dict[2]) # Prints value for 2 key print (tinydict) # Prints complete dictionary print (tinydict.keys()) # Prints all the keys print (tinydict.values()) # Prints all the values 31-10-2021 [email protected] 21
  • 22. Indentation • Adding white space before a statement • To indicate a block of code. if 5 > 2: print("Five is greater than two!") if 5 > 2: print("Five is greater than two!") #syntax error if 5 > 2: print("Five is greater than two!") print("Five is greater than two!") # syntax error 31-10-2021 [email protected] 22
  • 23. Comments • A hash sign (#) that is not inside a string literal is the beginning of a comment. • All characters after the #, up to the end of the physical line, are part of the comment and the Python interpreter ignores them. • # First comment • print ("Hello, Python!") # second comment 31-10-2021 [email protected] 23
  • 24. Reading Input • Python provides some built-in functions to perform both input and output operations. Python provides us with two inbuilt functions to read the input from the keyboard. raw_input() input() raw_input(): This function reads only one line from the standard input and returns it as a String. input(): The input() function first takes the input from the user and then evaluates the expression, which means python automatically identifies whether we entered a string or a number or list. • But in Python 3 the raw_input() function was removed and renamed to input(). 31-10-2021 [email protected] 24
  • 25. Print Output 31-10-2021 [email protected] 25 In order to print the output, python provides us with a built-in function called print(). Example: Print(“Hello Python”) Output: Hello Python
  • 26. Type conversion • Python defines type conversion functions to directly convert one data type to 1. int(a,base) # any data type to integer. ‘Base’ specifies the base in which string is if data type is string. 2. float() #any data type to a floating point number 3. ord() #character to integer. 4. hex() #integer to hexadecimal string. 5. oct() #integer to octal string. 6. tuple() #convert to a tuple. 7. set() #returns the type after converting to set. 8. list() # any data type to a list type 10. str() # integer into a string. 11. complex(real,imag) # real numbers to complex(real,imag) number. 12. chr(number) #number to its corresponding ASCII character. 31-10-2021 [email protected] 26
  • 27. is operator • The Equality operator (==) compares the values of both the operands and checks for value equality. • Whereas the ‘is’ operator checks whether both the operands refer to the same object or not (present in the same memory location). 31-10-2021 [email protected] 27
  • 28. Dynamic and Strongly typed language • Python is a dynamically typed language. • What is dynamic? • We don't have to declare the type of a variable or manage the memory while assigning a value to a variable in Python. 31-10-2021 [email protected] 28
  • 29. Mutable and immutable objects in Python • There are two types of objects in python i.e. • Mutable and Immutable objects. • Whenever an object is instantiated, it is assigned a unique object id. • The type of the object is defined at the runtime and it can’t be changed afterwards. • However, it’s state can be changed if it is a mutable object. • Immutable Objects : These are of in-built types like int, float, bool, string, unicode, tuple. In simple words, an immutable object can’t be changed after it is created. • Mutable Objects : These are of type list, dict, set . Custom classes are generally mutable. 31-10-2021 [email protected] 29