0% found this document useful (0 votes)
4 views16 pages

Chapter 4 Part 3 Pfe

The document provides an overview of loops in Python, focusing on for loops, while loops, and the range function for iterating over sequences. It explains how to use nested loops for tasks such as printing tables and patterns, as well as special print function usage to control output formatting. Additionally, it touches on counting specific characters in strings and validating string formats, such as telephone numbers.

Uploaded by

thetechfluxyt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views16 pages

Chapter 4 Part 3 Pfe

The document provides an overview of loops in Python, focusing on for loops, while loops, and the range function for iterating over sequences. It explains how to use nested loops for tasks such as printing tables and patterns, as well as special print function usage to control output formatting. Additionally, it touches on counting specific characters in strings and validating string formats, such as telephone numbers.

Uploaded by

thetechfluxyt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Loops

1
The for loop

• Often, you will need to visit each character in a string.

• The for loop makes this process particularly easy to program. For
example, suppose we want to print a string, with one character
per line. We cannot simply print the string using the print
function. Instead, we need to iterate over the characters in the
string and print each character individually. Hereis how you use
the for loop to accomplish this task:

stateName = "Virginia"
for letter in stateName :
print(letter)

2
Equivalent while loop

This loop is equivalent to the following while loop that uses an


explicit index variable:

i=0
while i < len(stateName) :
letter = stateName[i]
print(letter)
i=i+1

3
Range in python

• As you have seen in prior sections, 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

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.

4
Range in python

• 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)

• We can also have the for loop count down instead of up:

for i in range(10, 0, -1) : # i = 10, 9, 8, ..., 1


print(i)

5
Range in python

• Finally, 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

• With a single argument, the sequence is the values from 0 to one


less than the argument, in steps of 1.

6
Nested loops

• Now we will see how to print a table. For


simplicity, we will print the powers of x, x^n, as
in the table below.

• How do you print a table row? You need to print a


value for each exponent.
• This requires a second loop.
For n from 1 to 4
Print x^n.
• This loop must be placed inside the preceding
loop. We say that the inner loop is nested inside
the outer loop.
• There are 10 rows in the outer loop. For each x,
the program prints four columns in the inner loop.
Thus, a total of 10 × 4 = 40 values are printed.
7
Table using nested for loop

• In this program, we
want to show the
results of multiple print
statements on the
same line. This is
achieved by adding the
argument end="" to
the print function.

8
Nested for loops

9
Practice questions

• How would you change the program to display all powers from
x^0 to x^5?

• Write nested loops that make the following pattern of brackets:


[][][][]
[][][][]
[][][][]

• What do the following nested loops display?


for i in range(3) :
for j in range(1, 4) :
print(i + j, end="")
print()

10
Some more fun with nested for
loops
• Prints 4 rows of * with lengths 1, 2, 3, and 4.

11
Some more fun with nested for
loops
• Prints 4 rows of * with lengths 1, 2, 3, and 4.

12
• Special Form of the print Function
• 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, . . ., valuen, end="")

For example, the output of the two statements


print("00", end="")
print(3 + 4)
• is the single line
007
• By including end="" as the last argument to the first print
function, we indicate that an empty string is to be printed after
the first argument is displayed instead of starting a new line. The
output of the next print function starts on the same line where
the previous one left off.
13
Count matches

• We saw how to count the number of values that fulfill a


particular condition. We can also apply this task to strings. For
example, suppose you need to count the number of uppercase
letters contained in a string.
uppercase = 0
for char in string :
if char.isupper() :
uppercase = uppercase + 1

14
Validating a string

• Data validation is not limited to verifying that user input is a


specific value or falls within a valid range. It is also common to
require user input to be entered in a specific format. For
example, consider the task of verifying whether a string contains
a correctly formatted telephone number.

• In the United States, telephone numbers consist of three parts––


area code, exchange, and line number––which are commonly
specified in the form (###)###-####. We can examine a
string to ensure that it contains a correctly formatted phone
number with 13 characters. To do this, we must not only verify
that it contains digits and the appropriate symbols, but that each
are in the appropriate spots in the string.
• This requires an event-controlled loop that can exit early if an
invalid character or an out of place symbol is encountered while
processing the string:

15
16

You might also like