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
PPTX
PART 1 - Python Tutorial | Variables and Data Types in Python
PDF
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
PPTX
Iterarators and generators in python
PDF
Function arguments In Python
PPTX
Python ppt
PPTX
Values and Data types in python
PDF
Strings in Python
PART 1 - Python Tutorial | Variables and Data Types in Python
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Iterarators and generators in python
Function arguments In Python
Python ppt
Values and Data types in python
Strings in Python

What's hot (20)

PPTX
Counters &amp; time delay
PPT
1.python interpreter and interactive mode
PPTX
Python programming
PPTX
Python basics
PDF
Strings in python
PPTX
The Phases of a Compiler
PPT
BINARY TREE REPRESENTATION.ppt
PPSX
Modules and packages in python
PDF
Applications of stack
PPT
Branch prediction
PPTX
Unit 4 sp macro
PPTX
Linked list
PPTX
Presentation on Segmentation
PPT
Parallel processing
PPTX
Object oriented programming in python
PPT
PPT
programming with python ppt
PPTX
Single accumulator based CPU.pptx
PPT
Scheduling algorithms
Counters &amp; time delay
1.python interpreter and interactive mode
Python programming
Python basics
Strings in python
The Phases of a Compiler
BINARY TREE REPRESENTATION.ppt
Modules and packages in python
Applications of stack
Branch prediction
Unit 4 sp macro
Linked list
Presentation on Segmentation
Parallel processing
Object oriented programming in python
programming with python ppt
Single accumulator based CPU.pptx
Scheduling algorithms
Ad

Similar to Parts of python programming language (20)

PPTX
funadamentals of python programming language (right from scratch)
PDF
Basics of Python and Numpy_583aab2b47e30ad9647bc4aaf13b884d.pdf
PPTX
Basics of Python and Numpy jfhfkfff.pptx
PPTX
Python Conditional and Looping statements.pptx
PDF
Data Handling_XI- All details for cbse board grade 11
PPTX
Python PPT2
PPTX
Python For Data Science.pptx
PPTX
unit1 python.pptx
PPTX
Introduction to python programming ( part-1)
PPTX
Python 3.pptx
PPTX
1664611760basics-of-python-for begainer1 (3).pptx
PPTX
PPT
PE1 Module 2.ppt
PPTX
Chapter1 python introduction syntax general
PDF
Python Basics
PPTX
introduction to python programming concepts
PPTX
PPTX
Python-The programming Language
ODP
Introduction to programming with python
PPTX
Welcome to python workshop
funadamentals of python programming language (right from scratch)
Basics of Python and Numpy_583aab2b47e30ad9647bc4aaf13b884d.pdf
Basics of Python and Numpy jfhfkfff.pptx
Python Conditional and Looping statements.pptx
Data Handling_XI- All details for cbse board grade 11
Python PPT2
Python For Data Science.pptx
unit1 python.pptx
Introduction to python programming ( part-1)
Python 3.pptx
1664611760basics-of-python-for begainer1 (3).pptx
PE1 Module 2.ppt
Chapter1 python introduction syntax general
Python Basics
introduction to python programming concepts
Python-The programming Language
Introduction to programming with python
Welcome to python workshop
Ad

More from Megha V (20)

PPTX
Soft Computing Techniques_Part 1.pptx
PPTX
JavaScript- Functions and arrays.pptx
PPTX
Introduction to JavaScript
PPTX
Python Exception Handling
PPTX
Python- Regular expression
PPTX
File handling in Python
PPTX
Python programming -Tuple and Set Data type
PPTX
Python programming –part 7
PPTX
Python programming Part -6
PPTX
Python programming: Anonymous functions, String operations
PPTX
Python programming- Part IV(Functions)
PPTX
Python programming –part 3
PPTX
Python programming
PPTX
Strassen's matrix multiplication
PPTX
Solving recurrences
PPTX
Algorithm Analysis
PPTX
Algorithm analysis and design
PPTX
Genetic algorithm
PPTX
UGC NET Paper 1 ICT Memory and data
PPTX
Seminar presentation on OpenGL
Soft Computing Techniques_Part 1.pptx
JavaScript- Functions and arrays.pptx
Introduction to JavaScript
Python Exception Handling
Python- Regular expression
File handling in Python
Python programming -Tuple and Set Data type
Python programming –part 7
Python programming Part -6
Python programming: Anonymous functions, String operations
Python programming- Part IV(Functions)
Python programming –part 3
Python programming
Strassen's matrix multiplication
Solving recurrences
Algorithm Analysis
Algorithm analysis and design
Genetic algorithm
UGC NET Paper 1 ICT Memory and data
Seminar presentation on OpenGL

Recently uploaded (20)

PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
medical staffing services at VALiNTRY
PPT
Introduction Database Management System for Course Database
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
L1 - Introduction to python Backend.pptx
PDF
top salesforce developer skills in 2025.pdf
PDF
System and Network Administration Chapter 2
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Digital Strategies for Manufacturing Companies
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
Wondershare Filmora 15 Crack With Activation Key [2025
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
medical staffing services at VALiNTRY
Introduction Database Management System for Course Database
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
L1 - Introduction to python Backend.pptx
top salesforce developer skills in 2025.pdf
System and Network Administration Chapter 2
Which alternative to Crystal Reports is best for small or large businesses.pdf
Digital Strategies for Manufacturing Companies
PTS Company Brochure 2025 (1).pdf.......
ISO 45001 Occupational Health and Safety Management System
2025 Textile ERP Trends: SAP, Odoo & Oracle
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Upgrade and Innovation Strategies for SAP ERP Customers
Understanding Forklifts - TECH EHS Solution
Odoo POS Development Services by CandidRoot Solutions
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
ManageIQ - Sprint 268 Review - Slide Deck

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