Python For Loops: Basics and Applications

for loop n.w
1 / 20
Embed
Share

Explore the fundamentals of for loops in Python, including basic usage, break and continue statements, iterating over lists, and using nested loops efficiently. Learn how to control loop execution with range() function and handle loop termination with break and continue. Master the versatility of for loops for iterating over sequences and lists in Python.

  • Python Programming
  • For Loops
  • Iteration Control
  • Loop Statements
  • Python Basics

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. for loop 1

  2. Objectives O This chapter introduces: 1. Basic usage of for loop Use break and continue statements in for loop Use for loop in the list 2. 3.

  3. for loop O The for loop in Python is used to iterate over a sequence (a list, a tuple, a string) or other iterable objects O Iterating over a sequence is called traversal O Use the range() function in for loop to control the number of execution O It gives a sequence of numbers based on the start and stop index given 3

  4. for Single loop start index stop index O Syntax: for variable_1 in range(range1, range2): print() range () will start from start index, and increment the value by 1 till the stop index O E.g: for a in range(1,4): #single loop print(a) O Result: Start from 1 4 Less than 4

  5. Nested for loops O E.g: for a in range(1,2): # first loop for b in range(1,10): # second loop print(a, "+",b,"=",a+b) print() O Result: Variable b Variable a 5

  6. break and continue statements 6

  7. break and continue O 'break' O Used to terminate the current loop and resumes execution at the next statement O continue O Used to return the control to the beginning of the for/while loop 7

  8. break O E.g: for a in range(0, 3): for b in range(0, 3): print("a=",a,"b=",b) break; print("loop") #This will not be executed print() O Result: 8

  9. continue O E.g: for a in range(0, 3): for b in range(0, 3): print("a=",a,"b=",b) continue; print("loop")#This will not be executed print() O Result: 9

  10. for loop with a list O for loop can be used to display the data in a list O The list can be used to contain integer variables, characters, and other different data types 10

  11. for loop with a string string = "Hello"#Declare a string Hello O Characters stored in a list: Index Index value value Character 0 0 1 1 2 2 3 3 4 4 5 5 H e l l o \0 11

  12. for loop with a string O E.g: string = "Hello" for num in range(0,5): print ("string[",num,"]: ", string[num]) print (string) O Result: Displaying all characters one by one 12 Use print (string) to display whole string

  13. for loop with a list O E.g: array = ["Mary", "had", "a", "little", "lamb"] #len(array) is used to get the size of the array for counter in range(0, len(array)): print(array [counter]) print(type(array[0])) #Display the type of the array[0] O Result: 13

  14. for loop in the list O E.g: array = [1,2,4] for counter in range(0, 3): print(array [counter]) print(type(array[0]))#Display the type of the array[0] O Result:

  15. for loop in the list O E.g: array = [1,2,4," String ",5.4] for counter in range(0, 5): print(type(array[counter]),array [counter]) #Display the type of the array[counter] and the data in the list O Result:

  16. Sources O References: O http://openhome.cc/Gossip/Python/IOABC.h tml O http://www.pythondoc.com/pythontutorial3/i ntroduction.html O http://pydoing.blogspot.tw/2011/01/python- operator.html O Python 16

  17. Exercise 1 O Design a program that will ask the user to input a text t and a character c, and then print the characters in t, not including c. O Input: O Text : hello world, welcome to Python! O Character : e O Result:

  18. Exercise 2 O Design a program that will ask the user to input a number k and then display a k times table. O Input: 9 O Result:

  19. Exercise 3 O Write a program that can enter two integers N1 and N2, and then display the greatest common divisor and the least common multiple of N1 and N2 on the screen. O Input : 15 10 O Result:

  20. Exercise 4 O Please write a program that will ask the user to input three integers m,n,k (0 <= m, n <= 255, and k>0), and then print the characters for the ASCII values from min(m,n) to max(m,n), where each line on the screen has at most k pairs of ASCII value and the corresponding character. O Result: Hint: Use the chr() function to get character from its corresponding ASCII value

More Related Content