SlideShare a Scribd company logo
1 of 18
Programming in Python
Week-3 Content
Data types
Strings in Python
● Strings can be surrounded by single or double quotations.
● Can be multiline with 3 double/single quotation marks.
● Are stored in the memory as arrays
● Python doesn’t have character data type, the characters are
also internally stored as strings of length 1.
● You can access any characters of the string using indices.
● str_name[index] will give you the character at that index in
the string.
● str_name[start_index: end_index] will give substring
● Strings are immutable
Strings & Accessing characters
alpha = ‘c’ c
alpha
0
alpha = ‘coffee’ c
alpha
0
o f f e e
1 2 3 4 5
index
alpha[4]
e
4
f f e
2 3 4
alpha[2:5]
P Y T H O N
C L A S S E S
str1 =
str2 =
0 1 2 3 4 5
0 1 2 3 4 5 6
+
=
P Y T H O N
0 1 2 3 4 5
C L A S S E S
6 7 8 9 10 11 12
add =
N
5
str1[5]
S
9
add[9]
P Y T H O Nstr1 =
0 1 2 3 4 5
* 3
multiply =
P Y T H O N
0 1 2 3 4 5
Y T H O N
6 7 8 9 10 11
P Y T H O N
12 13 14 15 16 17
P
Now:
print(“Addition of %s and %s is %s”, %(str1, str2, add))
Initially:
print(“Addition of ” + str1 + “ and “ + str2 + “ is: “ + add )
Now: can be done for integers too
print(“Addition of %i and %i is %i”, %(num1, num2, add))
Python Lists
● Store multiple values in a single variable
● A single list may contain items of any data type
● len() can be used to get the length of the list
● append() can be used to add an element to an existing list
● remove() can be used to remove the first occurence of the
specified item in the list
● Each element of the list has a definite position - index starting from
0
● Lists are mutable
List Slicing
1 2 3 1 1 1
0 1 2 3 4 5
-6 -5 -4 -3 -2 -1
3 1 1 1
2 3 4 5
-4 -3 -2 -1
list[2:6]
3 1 1
2 3 4
-4 -3 -2
list[-4:-1]
3 1 1 1
2 3 4 5
-4 -3 -2 -1
list[2:]
1 2
0 1
-6 -5
list[:-4]
Tuples
● Identical to lists but created using ()
● Tuples are immutable
● Indexing and slicing works similar to strings and lists
● Use tuples instead of list when you don’t want data to be modified
● When the amount of data is too huge, tuples work faster than lists
Operator Precedence and Associativity in Python
● Combination of values, variables, operators and function calls makes a valid
expression.
● When there are more than 1 operators in an expression, rules of precedence
are used.
● If more than 1 operator is in the same group and hence has same precedence,
associativity is used to evaluate the expression.
● ** has right-to-left associativity, rest all operators have left-to-right associativity
Homework Questions

More Related Content

What's hot (16)

List and iterator
List and iteratorList and iterator
List and iterator
 
Binary Search
Binary SearchBinary Search
Binary Search
 
The List Data Model
The List Data ModelThe List Data Model
The List Data Model
 
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
 
Hashing
HashingHashing
Hashing
 
Hashing data
Hashing dataHashing data
Hashing data
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithm
 
Hashing
HashingHashing
Hashing
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms Hashing
 
Linked list
Linked listLinked list
Linked list
 
08 Hash Tables
08 Hash Tables08 Hash Tables
08 Hash Tables
 
lists
listslists
lists
 
Hash table
Hash tableHash table
Hash table
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithms
 
Hashing algorithms and its uses
Hashing algorithms and its usesHashing algorithms and its uses
Hashing algorithms and its uses
 

Similar to Programming in python - Week 3

Similar to Programming in python - Week 3 (20)

Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
 
Python data type
Python data typePython data type
Python data type
 
Python
PythonPython
Python
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
 
02 Python Data Structure.pptx
02 Python Data Structure.pptx02 Python Data Structure.pptx
02 Python Data Structure.pptx
 
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdfCOMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
 
tupple.pptx
tupple.pptxtupple.pptx
tupple.pptx
 
PYTHON.pptx
PYTHON.pptxPYTHON.pptx
PYTHON.pptx
 
Datatypes.pptx
Datatypes.pptxDatatypes.pptx
Datatypes.pptx
 
2. Values and Data types in Python.pptx
2. Values and Data types in Python.pptx2. Values and Data types in Python.pptx
2. Values and Data types in Python.pptx
 
Python Collections
Python CollectionsPython Collections
Python Collections
 
Python for Beginners(v3)
Python for Beginners(v3)Python for Beginners(v3)
Python for Beginners(v3)
 
Unit v
Unit vUnit v
Unit v
 
Data structure
 Data structure Data structure
Data structure
 
‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
 
Python PPT.pptx
Python PPT.pptxPython PPT.pptx
Python PPT.pptx
 
Chapter - 2.pptx
Chapter - 2.pptxChapter - 2.pptx
Chapter - 2.pptx
 
Python Session - 3
Python Session - 3Python Session - 3
Python Session - 3
 
Slicing in Python - What is It?
Slicing in Python - What is It?Slicing in Python - What is It?
Slicing in Python - What is It?
 

More from Priya Nayak

Programming in python - Week 7,8
Programming in python - Week 7,8Programming in python - Week 7,8
Programming in python - Week 7,8Priya Nayak
 
Programming in python w6
Programming in python w6Programming in python w6
Programming in python w6Priya Nayak
 
Programming in python - Week 5
Programming in python - Week 5Programming in python - Week 5
Programming in python - Week 5Priya Nayak
 
Programming in python - Week 4
Programming in python  - Week 4Programming in python  - Week 4
Programming in python - Week 4Priya Nayak
 
Programming in python - Week 2
Programming in python - Week 2Programming in python - Week 2
Programming in python - Week 2Priya Nayak
 
Programming in python
Programming in python Programming in python
Programming in python Priya Nayak
 

More from Priya Nayak (7)

Programming in python - Week 7,8
Programming in python - Week 7,8Programming in python - Week 7,8
Programming in python - Week 7,8
 
Programming in python w6
Programming in python w6Programming in python w6
Programming in python w6
 
Programming in python - Week 5
Programming in python - Week 5Programming in python - Week 5
Programming in python - Week 5
 
Programming in python - Week 4
Programming in python  - Week 4Programming in python  - Week 4
Programming in python - Week 4
 
Programming in python - Week 2
Programming in python - Week 2Programming in python - Week 2
Programming in python - Week 2
 
Programming in python
Programming in python Programming in python
Programming in python
 
Git and GitHub
Git and GitHubGit and GitHub
Git and GitHub
 

Recently uploaded

fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Recently uploaded (20)

Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

Programming in python - Week 3

  • 3. Strings in Python ● Strings can be surrounded by single or double quotations. ● Can be multiline with 3 double/single quotation marks. ● Are stored in the memory as arrays ● Python doesn’t have character data type, the characters are also internally stored as strings of length 1. ● You can access any characters of the string using indices. ● str_name[index] will give you the character at that index in the string. ● str_name[start_index: end_index] will give substring ● Strings are immutable
  • 4.
  • 5. Strings & Accessing characters
  • 6. alpha = ‘c’ c alpha 0 alpha = ‘coffee’ c alpha 0 o f f e e 1 2 3 4 5 index alpha[4] e 4 f f e 2 3 4 alpha[2:5]
  • 7. P Y T H O N C L A S S E S str1 = str2 = 0 1 2 3 4 5 0 1 2 3 4 5 6 + = P Y T H O N 0 1 2 3 4 5 C L A S S E S 6 7 8 9 10 11 12 add = N 5 str1[5] S 9 add[9]
  • 8. P Y T H O Nstr1 = 0 1 2 3 4 5 * 3 multiply = P Y T H O N 0 1 2 3 4 5 Y T H O N 6 7 8 9 10 11 P Y T H O N 12 13 14 15 16 17 P
  • 9. Now: print(“Addition of %s and %s is %s”, %(str1, str2, add)) Initially: print(“Addition of ” + str1 + “ and “ + str2 + “ is: “ + add ) Now: can be done for integers too print(“Addition of %i and %i is %i”, %(num1, num2, add))
  • 10. Python Lists ● Store multiple values in a single variable ● A single list may contain items of any data type ● len() can be used to get the length of the list ● append() can be used to add an element to an existing list ● remove() can be used to remove the first occurence of the specified item in the list ● Each element of the list has a definite position - index starting from 0 ● Lists are mutable
  • 11. List Slicing 1 2 3 1 1 1 0 1 2 3 4 5 -6 -5 -4 -3 -2 -1 3 1 1 1 2 3 4 5 -4 -3 -2 -1 list[2:6] 3 1 1 2 3 4 -4 -3 -2 list[-4:-1] 3 1 1 1 2 3 4 5 -4 -3 -2 -1 list[2:] 1 2 0 1 -6 -5 list[:-4]
  • 12. Tuples ● Identical to lists but created using () ● Tuples are immutable ● Indexing and slicing works similar to strings and lists ● Use tuples instead of list when you don’t want data to be modified ● When the amount of data is too huge, tuples work faster than lists
  • 13. Operator Precedence and Associativity in Python ● Combination of values, variables, operators and function calls makes a valid expression. ● When there are more than 1 operators in an expression, rules of precedence are used. ● If more than 1 operator is in the same group and hence has same precedence, associativity is used to evaluate the expression. ● ** has right-to-left associativity, rest all operators have left-to-right associativity
  • 14.
  • 15.
  • 16.
  • 17.