SlideShare a Scribd company logo
Online Workshop on ‘How to develop
Pythonic coding rather than Python
coding – Logic Perspective’
21.7.20 Day 1 session 2
Dr. S.Mohideen Badhusha
Professor/ CSE department
Alva’s Institute Engineering and
Technology
Mijar, Moodbidri, Mangalore
1
2
Iteration & String
3
To acquire knowledge of iteration and string in Python
To comprehend the concept of looping statements in
Python
To practice the simple problems in iteration and string
Objectives of the Day 1 session 2
4
Iteration
for loop
for var in <collection>:
<statements>
where collection is iterable obj like list, tuple,dictionary,string
and range
a = [’i’, ’say’, ’hello’] # a list of string elements
for i in range(len(a)):
print (i,end=’ ’)
print (a[i])
Output
0 I
1 say
2 hello
5
Iteration
while loop
while condition :
<Statements>
i=0
while (i < 10):
print( i)
i += 1
o/p
0
1
2
.
9
6
Another Example for loop
for someChar in “Hello” :
print(someChar)
Output
H
e
l
l
o
7
d=[(1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e')]
for (x, y) in d :
print (x,y)
o/p
1 a
2 b
3 c
4 d
5 e
8
for loop using range()
for x in range(5):
print x
o/p
0
1
2
3
4
9
Python program but not Pythonic
def traverse(string):
index = 0
while index < len(string):
letter = string[index]
print(letter)
index += 1
traverse('Monty Python')
Output ?
10
def traverse(string):
for letter in string:
print (letter)
traverse('Monty Python’)
Pythonic program
11
in operator
in is a boolean operator that checks
membership within a sequence
It is also called membership operator.
'a' in 'banana'
True
'o' in 'banana'
False
12
String Operations 12
13
String handling functions
s=’HELLO’
s.lower() # hello
s1=’hello’
s1.upper() # HELLO
s.strip() -- returns a string with whitespace
removed from the start and end
s=’ I am happy ‘
s.strip() # I am happy ( no front and back spaces)
s.isalpha()# True
s.isdigit() # False
s.isspace() #False
14
split() and join() functions
s.split('delim') - returns a string into a list of
words separated by delim
Ex:
'aaa,bbb,ccc'.split(',') -> ['aaa', 'bbb', 'ccc'].
Defalut delim is space
s.join(list) -- opposite of split(), joins the
elements in the given list together using the
string as the delimiter.
Ex:
'---'.join(['aaa', 'bbb', 'ccc']) -> ‘aaa---bbb---ccc’
14
15
String Formatting Operator: %
The operator % allows strings to be built out of many
data items in a “fill in the blanks” fashion.
x = “Ram”
y = 34
print(“%s’s age is %d” % (x, y))
o/p
Ram’s age is 34
The tuple following the % operator is used to fill in
the blanks in the original string marked with %s %g
%d to represent string, float and integer values
16
Converting anything to a String
The built-in str() function can convert an
instance of any data type into a string
Ex:
print(“Hello ” + str(2))
o/p
Hello 2
17
Slicing strings
A segment of a string is called a slice.
Selecting a slice is similar to selecting a
character:
Ex:
s = 'Monty Python'
print(s[0:5])
Monty
print(s[6:12])
Python
18
String Indexing
print(s[:3])
o/p
Hel
print(s[3:])
o/p
lo
18
19
String Indexing
print(s[-1])
o/p
'o' #last char (1st from the end)
print(s[-4])
o/p
'e' # 4th from the end
print(s[:-3])
o/p
'He' – from starting, going up to but not
including the last 3 chars.
s[-3:] is 'llo' -- starting with the 3rd char from
the end and extending to the end of the string.
20
s[1:4] is 'ell' -- chars starting at index 1 and
extending up to but not including index 4
s[1:] is 'ello' -- omitting either index defaults
to the start or end of the string
s[:] is 'Hello' -- omitting both always gives
us a copy of the whole thing
s[1:100] is 'ello' -- an index that is too big is
truncated down to the string length
21
Concluding Tips
for loop in Python is entirely different from
other programming languages[ for var in
collection]
while loop in Python is similar to the
programming constructs in other languages
There is no do… while loop existing in Python
To be Pythonic, we should use data
structures and PEP 8 rules in Python
In slicing of string, st[i:n], we have to consider
from i th index to n-1 .
st[:n] from 0 th index to n-1.
st[i:] from i th index to end of the string

More Related Content

PPTX
Improve Your Edge on Machine Learning - Day 1.pptx
PDF
CP Handout#8
PDF
Presentation pythonpppppppppppppppppppppppppppppppppyyyyyyyyyyyyyyyyyyytttttt...
PDF
CS4200 2019 | Lecture 4 | Syntactic Services
DOCX
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docx
PPT
Introduction To Python
PPTX
Python Workshop - Learn Python the Hard Way
PDF
14-Strings-In-Python strings with oops .pdf
Improve Your Edge on Machine Learning - Day 1.pptx
CP Handout#8
Presentation pythonpppppppppppppppppppppppppppppppppyyyyyyyyyyyyyyyyyyytttttt...
CS4200 2019 | Lecture 4 | Syntactic Services
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docx
Introduction To Python
Python Workshop - Learn Python the Hard Way
14-Strings-In-Python strings with oops .pdf

Similar to ‘How to develop Pythonic coding rather than Python coding – Logic Perspective’ (20)

PDF
23UCACC11 Python Programming (MTNC) (BCA)
PPTX
Ruby Basics
PPTX
BINARY files CSV files JSON files with example.pptx
PPTX
Week6_P_String.pptx
PDF
Python.pdf
PPTX
Programming in C by SONU KUMAR.pptx
PDF
Python - Lecture 12
PPTX
funadamentals of python programming language (right from scratch)
PPTX
lecture 2.pptx
PPT
python fundamental for beginner course .ppt
PPTX
Lecture 15_Strings and Dynamic Memory Allocation.pptx
PDF
Python- strings
PPTX
Keep it Stupidly Simple Introduce Python
PDF
Functional Smalltalk
PPT
PE1 Module 4.ppt
PPTX
lecture-5 string.pptx
PDF
Data Analysis with R (combined slides)
PPT
ShellProgramming and Script in operating system
PPTX
Introduction to learn and Python Interpreter
23UCACC11 Python Programming (MTNC) (BCA)
Ruby Basics
BINARY files CSV files JSON files with example.pptx
Week6_P_String.pptx
Python.pdf
Programming in C by SONU KUMAR.pptx
Python - Lecture 12
funadamentals of python programming language (right from scratch)
lecture 2.pptx
python fundamental for beginner course .ppt
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Python- strings
Keep it Stupidly Simple Introduce Python
Functional Smalltalk
PE1 Module 4.ppt
lecture-5 string.pptx
Data Analysis with R (combined slides)
ShellProgramming and Script in operating system
Introduction to learn and Python Interpreter
Ad

More from S.Mohideen Badhusha (7)

PDF
Introduction to Python Data Analytics.pdf
PDF
Simple intro to HTML and its applications
PDF
PHP Programming and its Applications workshop
PDF
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
PDF
Object oriented Programming using C++ and Java
PDF
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
PDF
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
Introduction to Python Data Analytics.pdf
Simple intro to HTML and its applications
PHP Programming and its Applications workshop
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
Object oriented Programming using C++ and Java
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
Ad

Recently uploaded (20)

PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Current and future trends in Computer Vision.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
UNIT 4 Total Quality Management .pptx
PPT
introduction to datamining and warehousing
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
PPT on Performance Review to get promotions
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPTX
Sustainable Sites - Green Building Construction
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
DOCX
573137875-Attendance-Management-System-original
PPT
Mechanical Engineering MATERIALS Selection
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Current and future trends in Computer Vision.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
CH1 Production IntroductoryConcepts.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
UNIT 4 Total Quality Management .pptx
introduction to datamining and warehousing
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPT on Performance Review to get promotions
Operating System & Kernel Study Guide-1 - converted.pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Sustainable Sites - Green Building Construction
UNIT-1 - COAL BASED THERMAL POWER PLANTS
573137875-Attendance-Management-System-original
Mechanical Engineering MATERIALS Selection

‘How to develop Pythonic coding rather than Python coding – Logic Perspective’

  • 1. Online Workshop on ‘How to develop Pythonic coding rather than Python coding – Logic Perspective’ 21.7.20 Day 1 session 2 Dr. S.Mohideen Badhusha Professor/ CSE department Alva’s Institute Engineering and Technology Mijar, Moodbidri, Mangalore 1
  • 3. 3 To acquire knowledge of iteration and string in Python To comprehend the concept of looping statements in Python To practice the simple problems in iteration and string Objectives of the Day 1 session 2
  • 4. 4 Iteration for loop for var in <collection>: <statements> where collection is iterable obj like list, tuple,dictionary,string and range a = [’i’, ’say’, ’hello’] # a list of string elements for i in range(len(a)): print (i,end=’ ’) print (a[i]) Output 0 I 1 say 2 hello
  • 5. 5 Iteration while loop while condition : <Statements> i=0 while (i < 10): print( i) i += 1 o/p 0 1 2 . 9
  • 6. 6 Another Example for loop for someChar in “Hello” : print(someChar) Output H e l l o
  • 7. 7 d=[(1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e')] for (x, y) in d : print (x,y) o/p 1 a 2 b 3 c 4 d 5 e
  • 8. 8 for loop using range() for x in range(5): print x o/p 0 1 2 3 4
  • 9. 9 Python program but not Pythonic def traverse(string): index = 0 while index < len(string): letter = string[index] print(letter) index += 1 traverse('Monty Python') Output ?
  • 10. 10 def traverse(string): for letter in string: print (letter) traverse('Monty Python’) Pythonic program
  • 11. 11 in operator in is a boolean operator that checks membership within a sequence It is also called membership operator. 'a' in 'banana' True 'o' in 'banana' False
  • 13. 13 String handling functions s=’HELLO’ s.lower() # hello s1=’hello’ s1.upper() # HELLO s.strip() -- returns a string with whitespace removed from the start and end s=’ I am happy ‘ s.strip() # I am happy ( no front and back spaces) s.isalpha()# True s.isdigit() # False s.isspace() #False
  • 14. 14 split() and join() functions s.split('delim') - returns a string into a list of words separated by delim Ex: 'aaa,bbb,ccc'.split(',') -> ['aaa', 'bbb', 'ccc']. Defalut delim is space s.join(list) -- opposite of split(), joins the elements in the given list together using the string as the delimiter. Ex: '---'.join(['aaa', 'bbb', 'ccc']) -> ‘aaa---bbb---ccc’ 14
  • 15. 15 String Formatting Operator: % The operator % allows strings to be built out of many data items in a “fill in the blanks” fashion. x = “Ram” y = 34 print(“%s’s age is %d” % (x, y)) o/p Ram’s age is 34 The tuple following the % operator is used to fill in the blanks in the original string marked with %s %g %d to represent string, float and integer values
  • 16. 16 Converting anything to a String The built-in str() function can convert an instance of any data type into a string Ex: print(“Hello ” + str(2)) o/p Hello 2
  • 17. 17 Slicing strings A segment of a string is called a slice. Selecting a slice is similar to selecting a character: Ex: s = 'Monty Python' print(s[0:5]) Monty print(s[6:12]) Python
  • 19. 19 String Indexing print(s[-1]) o/p 'o' #last char (1st from the end) print(s[-4]) o/p 'e' # 4th from the end print(s[:-3]) o/p 'He' – from starting, going up to but not including the last 3 chars. s[-3:] is 'llo' -- starting with the 3rd char from the end and extending to the end of the string.
  • 20. 20 s[1:4] is 'ell' -- chars starting at index 1 and extending up to but not including index 4 s[1:] is 'ello' -- omitting either index defaults to the start or end of the string s[:] is 'Hello' -- omitting both always gives us a copy of the whole thing s[1:100] is 'ello' -- an index that is too big is truncated down to the string length
  • 21. 21 Concluding Tips for loop in Python is entirely different from other programming languages[ for var in collection] while loop in Python is similar to the programming constructs in other languages There is no do… while loop existing in Python To be Pythonic, we should use data structures and PEP 8 rules in Python In slicing of string, st[i:n], we have to consider from i th index to n-1 . st[:n] from 0 th index to n-1. st[i:] from i th index to end of the string