SlideShare a Scribd company logo
1 of 24
An Introduction To Software
Development Using Python
Spring Semester, 2015
Class #8:
FOR Loop
Programming Challenge:
Print Each Character Of A String
• The user will enter their name
• Your program will then step through the string
that they entered and print out each character
by itself on a line listing their name vertically.
Image Credit: megmedina.com
Programming Challenge:
Print Each Character Of A String
userName = input(“Enter your name: ”)
i = 0
while i < len(userName) :
letter = userName[i]
print(letter)
i = i + 1
Image Credit: thegraphicsfairy.com
The For Statement
The for loop can be used to iterate over the contents of any container, which is an
object that contains or stores a collection of elements. Thus, a string is a container
that stores the collection of characters in the string.
Programming Challenge:
Print Each Character Of A String
userName = input(“Enter your name: ”)
for letter in userName :
print(letter)
Image Credit: thegraphicsfairy.com
What’s The Difference Between
While Loops and For Loops?
• In the for loop, the element variable is
assigned stateName[0] , stateName[1] , and so
on.
• In the while loop, the index variable i is
assigned 0, 1, and so on.
Image Credit: www.clipartpanda.com
The Range Function
• Count-controlled loops that iterate over a range of integer values are very
common.
• To simplify the creation of such loops, Python provides the range function
for generating a sequence of integers that can be used with the for loop.
• The loop code:
for i in range(1, 10) : # i = 1, 2, 3, ..., 9
print(i)
prints the sequential values from 1 to 9. The range function generates a
sequence of values based on its arguments.
• The first argument of the range function is the first value in the sequence.
• Values are included in the sequence while they are less than the second
argument
Image Credit: www.best-of-web.com
You Can Do The Same Thing With
While And For Loops
for i in range(1, 10) : # i = 1, 2, 3, ..., 9
print(i)
i = 1
while i < 10 :
print(i)
i = i + 1
Image Credit: www.rgbstock.com
Note that the ending value (the second argument to the range function) is not included
in the sequence, so the equivalent while loop stops before reaching that value, too.
Stepping Out…
• By default, the range function creates the
sequence in steps of 1. This can be changed
by including a step value as the third
argument to the function:
for i in range(1, 10, 2) : # i = 1, 3, 5, ..., 9
print(i)
Image Credit: megmedina.com
Loop Challenge #2!
• Create a program to print the sum of all odd
numbers between a and b (inclusive).
Image Credit: www.dreamstime.com
For Loop With One Argument
• You can use the range function with a single
argument.
• When you do, the range of values starts at
zero.
for i in range(10) : # i = 0, 1, 2, ..., 9
print("Hello") # Prints Hello ten times
Image Credit: www.canstockphoto.com
Review: The Many Different Forms
Of A Range
Describe The Steps Necessary For
Finding A Solution To A Problem
Problem:
You put $10,000 into a bank account that earns 5 percent interest per year.
How many years does it take for the account balance to be double the original?
Count Iterations
• Finding the correct lower and upper bounds for a loop can be
confusing.
– Should you start at 0 or at 1?
– Should you use <= b or < b as a termination condition?
• The following loops are executed b - a times:
• These asymmetric bounds are particularly
useful for traversing the characters in a string
int i = a
while i < b :
. . .
i = i + 1
for i in range(a, b) :
for i in range(0, len(str)) :
do something with i and str[i]
Image Credit: imgarcade.com
The +1 Problem
• How many times is the loop with symmetric bounds
executed?
int i = a
while i <= b :
. . .
i = i + 1
• b - a + 1 times. That “+1” is the source of many programming
errors.
• When a is 10 and b is 20, how many times does the loop
execute?
• In a Python for loop, the “+1” can be quite noticeable:
for year in range(1, numYears + 1) :
Image Credit: www.clipartbest.com
Loop Challenge #3!
• Read twelve temperature values (one for
each month), and display the number of the
month with the highest temperature. For
example the average maximum temperatures
for Death Valley are (in order by month, in
degrees Celsius):
18.2, 22.6, 26.4, 31.1, 36.6, 42.2, 45.7, 44.5, 40.2, 33.1, 24.2, 17.6
Image Credit: www.dreamstime.com
Secret Form Of print Statement
• Python provides a special form of the print function that
prevents it from starting a new line after its arguments are
displayed.
print(value1, value2, end="")
• By including end="" as the last argument to the first print
function, we indicate that an empty string is to be printed
after the last argument is printed instead of starting a new
line.
• The output of the next print function starts on the same line
where the previous one left off.
Image Credit: www.clipartpanda.com
Loop Challenge #4!
• Create a program to print the following table
that contains the first four power values for
the numbers 1-10.
Image Credit: www.rafainspirationhomedecor.com
Image Credit: www.dreamstime.com
Counting Matches In Strings
• Count the number of uppercase letters in a
string:
uppercase = 0
for char in string :
if char.isupper() :
uppercase = uppercase + 1
• Count the number of occurrences of multiple
characters within a string.
vowels = 0
for char in word :
if char.lower() in "aeiou" :
vowels = vowels + 1
Image Credit: www.clipartpanda.com
Finding All Matches In A String
• When you need to examine every character within a string,
independent of its position, you can use the for statement to
iterate over the individual characters.
• For example, suppose you are asked to print the position of
each uppercase letter in a sentence.
sentence = input("Enter a sentence: ")
for i in range(len(sentence)) :
if sentence[i].isupper() :
print(i)
Image Credit: www.clipartpanda.com
Finding the First or Last Match
In A String
• If your task is to find a match, then you can
stop as soon as the condition is fulfilled.
found = False
position = 0
while not found and position < len(string) :
if string[position].isdigit() :
found = True
else :
position = position + 1
if found :
print("First digit occurs at position", position)
else :
print("The string does not contain a digit.")
What’s In Your Python Toolbox?
print() math strings I/O IF/Else elif While For
What We Covered Today
1. For Loop
2. Range
3. Using loops to process
strings
Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/
What We’ll Be Covering Next Time
1. Lists, Part 1
Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/

More Related Content

What's hot

What's hot (20)

Numpy string functions
Numpy string functionsNumpy string functions
Numpy string functions
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator Overloading
 
Python
PythonPython
Python
 
String in python lecture (3)
String in python lecture (3)String in python lecture (3)
String in python lecture (3)
 
Pythonintro
PythonintroPythonintro
Pythonintro
 
Parts of python programming language
Parts of python programming languageParts of python programming language
Parts of python programming language
 
Python Data-Types
Python Data-TypesPython Data-Types
Python Data-Types
 
Whiteboarding Coding Challenges in Python
Whiteboarding Coding Challenges in PythonWhiteboarding Coding Challenges in Python
Whiteboarding Coding Challenges in Python
 
Text and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPText and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHP
 
07 Arrays
07 Arrays07 Arrays
07 Arrays
 
Approximation Data Structures for Streaming Applications
Approximation Data Structures for Streaming ApplicationsApproximation Data Structures for Streaming Applications
Approximation Data Structures for Streaming Applications
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3
 
Regular Expressions in Java
Regular Expressions in JavaRegular Expressions in Java
Regular Expressions in Java
 
ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1
 
Manipulation strings in c++
Manipulation strings in c++Manipulation strings in c++
Manipulation strings in c++
 
SPL 12.1 | Multi Dimensional(Two) Array Practice Problems
SPL 12.1 | Multi Dimensional(Two) Array Practice ProblemsSPL 12.1 | Multi Dimensional(Two) Array Practice Problems
SPL 12.1 | Multi Dimensional(Two) Array Practice Problems
 
Functional and Algebraic Domain Modeling
Functional and Algebraic Domain ModelingFunctional and Algebraic Domain Modeling
Functional and Algebraic Domain Modeling
 
FSTREAM,ASSERT LIBRARY & CTYPE LIBRARY.
FSTREAM,ASSERT LIBRARY & CTYPE LIBRARY.FSTREAM,ASSERT LIBRARY & CTYPE LIBRARY.
FSTREAM,ASSERT LIBRARY & CTYPE LIBRARY.
 
Monad Transformers - Part 1
Monad Transformers - Part 1Monad Transformers - Part 1
Monad Transformers - Part 1
 
Class 3: if/else
Class 3: if/elseClass 3: if/else
Class 3: if/else
 

Viewers also liked

Viewers also liked (16)

An Introduction To Python - Lists, Part 1
An Introduction To Python - Lists, Part 1An Introduction To Python - Lists, Part 1
An Introduction To Python - Lists, Part 1
 
An Introduction To Python - Tables, List Algorithms
An Introduction To Python - Tables, List AlgorithmsAn Introduction To Python - Tables, List Algorithms
An Introduction To Python - Tables, List Algorithms
 
An Introduction To Python - Dictionaries
An Introduction To Python - DictionariesAn Introduction To Python - Dictionaries
An Introduction To Python - Dictionaries
 
An Introduction To Python - WHILE Loop
An Introduction To  Python - WHILE LoopAn Introduction To  Python - WHILE Loop
An Introduction To Python - WHILE Loop
 
An Introduction To Python - Python, Print()
An Introduction To Python - Python, Print()An Introduction To Python - Python, Print()
An Introduction To Python - Python, Print()
 
An Introduction To Python - Files, Part 1
An Introduction To Python - Files, Part 1An Introduction To Python - Files, Part 1
An Introduction To Python - Files, Part 1
 
An Introduction To Python - Lists, Part 2
An Introduction To Python - Lists, Part 2An Introduction To Python - Lists, Part 2
An Introduction To Python - Lists, Part 2
 
An Introduction To Python - Variables, Math
An Introduction To Python - Variables, MathAn Introduction To Python - Variables, Math
An Introduction To Python - Variables, Math
 
An Introduction To Python - Graphics
An Introduction To Python - GraphicsAn Introduction To Python - Graphics
An Introduction To Python - Graphics
 
An Introduction To Python - Functions, Part 2
An Introduction To Python - Functions, Part 2An Introduction To Python - Functions, Part 2
An Introduction To Python - Functions, Part 2
 
An Introduction To Python - Working With Data
An Introduction To Python - Working With DataAn Introduction To Python - Working With Data
An Introduction To Python - Working With Data
 
An Introduction To Software Development - Software Support and Maintenance
An Introduction To Software Development - Software Support and MaintenanceAn Introduction To Software Development - Software Support and Maintenance
An Introduction To Software Development - Software Support and Maintenance
 
An Introduction To Python - Nested Branches, Multiple Alternatives
An Introduction To Python - Nested Branches, Multiple AlternativesAn Introduction To Python - Nested Branches, Multiple Alternatives
An Introduction To Python - Nested Branches, Multiple Alternatives
 
An Introduction To Python - Python Midterm Review
An Introduction To Python - Python Midterm ReviewAn Introduction To Python - Python Midterm Review
An Introduction To Python - Python Midterm Review
 
An Introduction To Software Development - Implementation
An Introduction To Software Development - ImplementationAn Introduction To Software Development - Implementation
An Introduction To Software Development - Implementation
 
An Introduction To Software Development - Architecture & Detailed Design
An Introduction To Software Development - Architecture & Detailed DesignAn Introduction To Software Development - Architecture & Detailed Design
An Introduction To Software Development - Architecture & Detailed Design
 

Similar to An Introduction To Python - FOR Loop

ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
priestmanmable
 

Similar to An Introduction To Python - FOR Loop (20)

An Introduction To Python - Strings & I/O
An Introduction To Python - Strings & I/OAn Introduction To Python - Strings & I/O
An Introduction To Python - Strings & I/O
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methods
 
Python basics
Python basicsPython basics
Python basics
 
gdscpython.pdf
gdscpython.pdfgdscpython.pdf
gdscpython.pdf
 
Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02
 
Acm aleppo cpc training second session
Acm aleppo cpc training second sessionAcm aleppo cpc training second session
Acm aleppo cpc training second session
 
MODULE. .pptx
MODULE.                              .pptxMODULE.                              .pptx
MODULE. .pptx
 
An Introduction To Python - Final Exam Review
An Introduction To Python - Final Exam ReviewAn Introduction To Python - Final Exam Review
An Introduction To Python - Final Exam Review
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Acm aleppo cpc training fifth session
Acm aleppo cpc training fifth sessionAcm aleppo cpc training fifth session
Acm aleppo cpc training fifth session
 
(4) cpp automatic arrays_pointers_c-strings_exercises
(4) cpp automatic arrays_pointers_c-strings_exercises(4) cpp automatic arrays_pointers_c-strings_exercises
(4) cpp automatic arrays_pointers_c-strings_exercises
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
 
A01
A01A01
A01
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 

Recently uploaded (20)

HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 

An Introduction To Python - FOR Loop

  • 1. An Introduction To Software Development Using Python Spring Semester, 2015 Class #8: FOR Loop
  • 2. Programming Challenge: Print Each Character Of A String • The user will enter their name • Your program will then step through the string that they entered and print out each character by itself on a line listing their name vertically. Image Credit: megmedina.com
  • 3. Programming Challenge: Print Each Character Of A String userName = input(“Enter your name: ”) i = 0 while i < len(userName) : letter = userName[i] print(letter) i = i + 1 Image Credit: thegraphicsfairy.com
  • 4. The For Statement The for loop can be used to iterate over the contents of any container, which is an object that contains or stores a collection of elements. Thus, a string is a container that stores the collection of characters in the string.
  • 5. Programming Challenge: Print Each Character Of A String userName = input(“Enter your name: ”) for letter in userName : print(letter) Image Credit: thegraphicsfairy.com
  • 6. What’s The Difference Between While Loops and For Loops? • In the for loop, the element variable is assigned stateName[0] , stateName[1] , and so on. • In the while loop, the index variable i is assigned 0, 1, and so on. Image Credit: www.clipartpanda.com
  • 7. The Range Function • Count-controlled loops that iterate over a range of integer values are very common. • To simplify the creation of such loops, Python provides the range function for generating a sequence of integers that can be used with the for loop. • The loop code: for i in range(1, 10) : # i = 1, 2, 3, ..., 9 print(i) prints the sequential values from 1 to 9. The range function generates a sequence of values based on its arguments. • The first argument of the range function is the first value in the sequence. • Values are included in the sequence while they are less than the second argument Image Credit: www.best-of-web.com
  • 8. You Can Do The Same Thing With While And For Loops for i in range(1, 10) : # i = 1, 2, 3, ..., 9 print(i) i = 1 while i < 10 : print(i) i = i + 1 Image Credit: www.rgbstock.com Note that the ending value (the second argument to the range function) is not included in the sequence, so the equivalent while loop stops before reaching that value, too.
  • 9. Stepping Out… • By default, the range function creates the sequence in steps of 1. This can be changed by including a step value as the third argument to the function: for i in range(1, 10, 2) : # i = 1, 3, 5, ..., 9 print(i) Image Credit: megmedina.com
  • 10. Loop Challenge #2! • Create a program to print the sum of all odd numbers between a and b (inclusive). Image Credit: www.dreamstime.com
  • 11. For Loop With One Argument • You can use the range function with a single argument. • When you do, the range of values starts at zero. for i in range(10) : # i = 0, 1, 2, ..., 9 print("Hello") # Prints Hello ten times Image Credit: www.canstockphoto.com
  • 12. Review: The Many Different Forms Of A Range
  • 13. Describe The Steps Necessary For Finding A Solution To A Problem Problem: You put $10,000 into a bank account that earns 5 percent interest per year. How many years does it take for the account balance to be double the original?
  • 14. Count Iterations • Finding the correct lower and upper bounds for a loop can be confusing. – Should you start at 0 or at 1? – Should you use <= b or < b as a termination condition? • The following loops are executed b - a times: • These asymmetric bounds are particularly useful for traversing the characters in a string int i = a while i < b : . . . i = i + 1 for i in range(a, b) : for i in range(0, len(str)) : do something with i and str[i] Image Credit: imgarcade.com
  • 15. The +1 Problem • How many times is the loop with symmetric bounds executed? int i = a while i <= b : . . . i = i + 1 • b - a + 1 times. That “+1” is the source of many programming errors. • When a is 10 and b is 20, how many times does the loop execute? • In a Python for loop, the “+1” can be quite noticeable: for year in range(1, numYears + 1) : Image Credit: www.clipartbest.com
  • 16. Loop Challenge #3! • Read twelve temperature values (one for each month), and display the number of the month with the highest temperature. For example the average maximum temperatures for Death Valley are (in order by month, in degrees Celsius): 18.2, 22.6, 26.4, 31.1, 36.6, 42.2, 45.7, 44.5, 40.2, 33.1, 24.2, 17.6 Image Credit: www.dreamstime.com
  • 17. Secret Form Of print Statement • Python provides a special form of the print function that prevents it from starting a new line after its arguments are displayed. print(value1, value2, end="") • By including end="" as the last argument to the first print function, we indicate that an empty string is to be printed after the last argument is printed instead of starting a new line. • The output of the next print function starts on the same line where the previous one left off. Image Credit: www.clipartpanda.com
  • 18. Loop Challenge #4! • Create a program to print the following table that contains the first four power values for the numbers 1-10. Image Credit: www.rafainspirationhomedecor.com Image Credit: www.dreamstime.com
  • 19. Counting Matches In Strings • Count the number of uppercase letters in a string: uppercase = 0 for char in string : if char.isupper() : uppercase = uppercase + 1 • Count the number of occurrences of multiple characters within a string. vowels = 0 for char in word : if char.lower() in "aeiou" : vowels = vowels + 1 Image Credit: www.clipartpanda.com
  • 20. Finding All Matches In A String • When you need to examine every character within a string, independent of its position, you can use the for statement to iterate over the individual characters. • For example, suppose you are asked to print the position of each uppercase letter in a sentence. sentence = input("Enter a sentence: ") for i in range(len(sentence)) : if sentence[i].isupper() : print(i) Image Credit: www.clipartpanda.com
  • 21. Finding the First or Last Match In A String • If your task is to find a match, then you can stop as soon as the condition is fulfilled. found = False position = 0 while not found and position < len(string) : if string[position].isdigit() : found = True else : position = position + 1 if found : print("First digit occurs at position", position) else : print("The string does not contain a digit.")
  • 22. What’s In Your Python Toolbox? print() math strings I/O IF/Else elif While For
  • 23. What We Covered Today 1. For Loop 2. Range 3. Using loops to process strings Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/
  • 24. What We’ll Be Covering Next Time 1. Lists, Part 1 Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/

Editor's Notes

  1. New name for the class I know what this means Technical professionals are who get hired This means much more than just having a narrow vertical knowledge of some subject area. It means that you know how to produce an outcome that I value. I’m willing to pay you to do that.