Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Lecture 7 Programming

Uploaded by

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

Lecture 7 Programming

Uploaded by

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

Programming-1 Using Python

Dr. Yasmin Hosny Alkady

Faculty of Information Technology & Computer Science


Sinai University
E-Mail: yassmin.hosny@su.edu.eg
Programming-1
Using Python
Lecture 8
Loop

3
Outlines

1) For Loop

4
2
For Loop

5
For Loop

▰ A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set,
or a string).

▰ Example: Print each fruit in a fruit list:

6
Looping Through a String

➢ Even strings are iterable objects, they contain a sequence of characters:

➢ Example: Loop through the letters in the word "banana":

7
Looping Through Range

➢ To loop through a set of code a specified number of times, we can use the range() function,

➢ The range() function returns a sequence of numbers, starting from 0 by default, and
increments by 1 (by default), and ends at a specified number.

➢ Example: Loop through the range of 10 (means from 0 to 9)

8
Looping Through Range

➢ The range( ) function defaults to 0 as a starting value, however it is possible to specify the
starting value by adding a parameter: range(3, 9), which means values from 3 to 9 (but not
including 9):

9
Example:

➢ Use function multiply to product all numbers from (1 to 10) by using for loop.

10
Example:

➢ Create empty list, with name “cube” then add values between 0 and 10 and each value is
raised to the power of three values using for loop.

11
Looping Through Range

➢ The range() function defaults to increment the sequence by 1, however it is possible to


specify the increment value by adding a third parameter: range(2, 30, 3):

➢ Example: Increment the sequence with 3 (default is 1):

12
Using len( )

➢ Len( ) is a function that used for computing length of string or length of list as example

13
Using len( ) with Range For Loop

14
Using len( ) with Range For Loop

To print the index of list using range ( ) and len ( ) in for loop

15
Else in For Loop

➢ The else keyword in a for loop specifies a block of code to be executed when the loop is
finished:

➢ Example: Print all numbers from 0 to 5, and print a message when the loop has ended:

16
Using for loop to print even and odd number

To print the even and odd numbers

17
Else in For Loop

➢ The else keyword in a for loop specifies a block of code to be executed when the loop is
finished:

➢ Example: Print all numbers from 0 to 5, and print a message when the loop has ended:

18
Else in For Loop

➢ Note: The else block will NOT be executed if the loop is stopped by a break statement.

➢ Example: Break the loop when x is 3, and see what happens with the else block:

19
Else in For Loop

➢ Note: The else block will NOT be executed if the loop is stopped by a break statement.

➢ Example: Break the loop when x is Dalia, and see what happens with the else block:

20
Example

To search for an index in the list

21
The break Statement

➢ With the break statement we can stop the loop before it has looped through all the items:

➢ Example: Exit the loop when x is "banana":

22
The break Statement

➢ Exit the loop when x is "banana", but this time the break comes before the print:

23
Nested Loops

➢ A nested loop is a loop inside a loop.

➢ The "inner loop" will be executed one time for each iteration of the "outer loop":

➢ Example: Print each adjective for every fruit:

24
The pass Statement

➢ for loops cannot be empty, but if you for some reason have a for loop with no content, put
in the pass statement to avoid getting an error.

25
The continue Statement

➢ With the continue statement we can stop the current iteration of the loop, and continue
with the next:

➢ Example: Do not print banana:

26
THANK YOU

27

You might also like