Lesson 8: Iterative Statements in Python - PDF
Document Details

Uploaded by EnticingOrbit1193
Tags
Related
- COMP1126 Lecture 03 - Looping I PDF
- Computer Skills II 2024-2025 PDF
- SYSC 2006: Imperative Programming Lecture 3 - Flow of Control (Carleton University)
- Cours Python - Chapitre 3 - Structures de contrôle PDF
- Estructura Programación Iterativas (Bucles) PDF
- Python Control Structures Lecture Notes PDF
Summary
This document introduces iterative statements in Python, covering for loops, while loops, and infinite loops. It explains membership operators and the range() function with code examples. Key topics include repeating statements using loops and understanding control variables, presented with flowcharts and Python code snippets to demonstrate concepts such as membership operators and the use of range() function.
Full Transcript
Here is the transcription of the provided text into a structured markdown format. ### LESSON 8 Iterative Statements in Python #### LEARNING IN THIS CHAPTER - Iterative Statements - for Loop - while Loop - Infinite Loop Earlier you have learnt the basic concepts of Python, its functions, operator...
Here is the transcription of the provided text into a structured markdown format. ### LESSON 8 Iterative Statements in Python #### LEARNING IN THIS CHAPTER - Iterative Statements - for Loop - while Loop - Infinite Loop Earlier you have learnt the basic concepts of Python, its functions, operators, and conditional statements. In this chapter, you will learn about the Iterative statements. In a program, there are situations when you have to repeat one or more statements many times. This repetition can be carried out using loops in programming. Let us understand the concept of a loop with the help of a real-life example. Suppose, your instructor instructs you to run and take five rounds of the playground to warm up. Before you start running, you fix a starting point. Be reaching the starting point again, you complete one round. Then, you take the second round, and so on. This way you keep track of the number of rounds until the process of taking rounds gets completed. Now, let us understand the concept of looping with the help of the below example. Let us say that the control variable is C. In this example, you have taken the initial value of C as 0. When you complete the first round, the value of C becomes 1, after the second round, its value becomes $C = C + 1$, i.e., $1 + 1 = 2$, and so on. This value will get incremented each time the process repeats. After incrementing, the control variable, C will be checked against the maximum number of the repetitions, which is 5 in this case. Let us understand this example with the help of a flowchart: _Image of a flowchart illustrating a loop in Python. The flowchart starts with "START," then "Take starting point as C, Let C = 0," followed by a decision diamond "Are the number of rounds greater than or equal to 5 (C >= 5)?" If "Yes," the path leads to "STOP," otherwise, it goes to "C = C + 1 (Take more rounds)" and loops back to the decision diamond._ This process will continue until the value of C becomes 5. It means, the loop will continue for five times and then it will terminate. Before proceeding further, let us first understand the concept of membership operators. #### MEMBERSHIP OPERATORS Membership operators play an important role in controlling the working of a loop. There are two membership operators, in and not in. Out of these, the in operator plays a significant role in the working of loops. Let us understand the in membership operator. ##### The `in` Operator The in operator is used to check if a given value exists in the sequence or not. It evaluates to True if it finds a value in the specified sequence else it returns False. Syntax: value in [sequence] Program 1: Write a program to check whether number 10 is present in a given sequence of numbers, [1,2,3,10] or not using `in` operator. _Image of a Python program that checks whether number 10 is present in a given sequence of numbers, [1,2,3,10] or not using 'in' operator_ _The Python code is:_ ```python print(10 in [1,2,3,10]) ``` _Image of a Python code output_ _The output code is:_ ``` True ``` The above program checks whether number 10 exists in the given list or not. If yes, the in operator will return the value true, otherwise, it will return a false value. Program 2: Write a program to check whether a number entered by the user is present in the given list or not using `in` operator. _Image of a Python program to check whether a number entered by the user is present in the given list or not using `in` operator._ _The Python code is:_ ```python a=int(input("Enter a number:")) print(a in [10,20,30,40,50]) ``` _Image of a Python code output_ _The output code is:_ ``` True ``` Using `in` membership operator with `for` loop Consider the following program: Program 3: Write a program check whether a number entered by the user is present in the list using "in' operator with `for` loop. _Image of a Python program to check whether a number entered by the user is present in the list using `in` operator with `for` loop._ _The Python code is:_ ```python for1 ``` _The Python code is:_ ```python for i in [10, 20, 30, 40]: print (i) ``` _Image of a program output_ _The output code is:_ ``` 10 20 30 40 ``` The following steps are involved in the execution of the for statement: Step 1: The in operator will first check if there is a value in the list. If yes, then in operator will return value from the sequence. Step 2: Using the `range()` function, a sequence is created. operator will check whether the value of the variable i Step 1: The in operator will first check if there is a value in the list. If yes, then in operator will return value from the sequence. Step 2: Using the `range()` function, a sequence is created. Step 1: Using the range() function, a sequence is created with all the values between 10 and 19, and the value is: i = 10. Step 2: The print statement will print the value of i. ##### Using `range()` Function in for Loop You can use the `range()` function in `for` loop to iterate through a sequence of numbers. Program 4: Write a program to return True value using `range()` function. _Image of a Python program to return True value using `range()` function._ _The Python code is:_ ```python i=10 print(i in range (10,20)) ``` _Image of a output code_ _The output code is:_ ``` True ``` In the above code, the initial value of the variable i is 10 and the final value is 19 (20 - 1 = 19). Each time the loop executes, the value of i is incremented by 1. So, the final sequence will be [10, 11, 12, 13, 14, 15 , 16, 17, 18, 19]. Now, that variable i (which in this case is 10) will be checked within this range. As '10' is present in the given range so the output of the program will be true. Now, try some other range. The output of the program will change accordingly. Program 5: program to return False value using `range()` function _Image of a Python program to return False value using `range()` function._ _The Python code is:_ ```python i=10 print(i in range(100,200)) ``` _Image of a code output_ _The output code is:_ ```program to return False value using``` Step 1: The for loop executes the for loop. Step 2: The for loop will terminate after printing the last value. The range() Function in for loop() funtion in for loop Program 5: Write a program to return False value using `range()` function _Image of a function in for loop_ _The Python code is:_ ```python i=10 ``` - The range () is 10 in the list: The 1 - the 1-1 - 1 if i Program 12: loop with 8 loop, while value and while. _Image of range() in loops_ - with program .The for and while - for i in range 2 - 3, 1 is - and. - is ### EDIT THIS DOC. If these were the key elements of a document to be extracted **Iterattive Statements in Python:** The value of the 1 of values can be understood The Program 6: ### NOTE The value of the can in number in 3D. .program 6,7,8 Program 9: While with program . for i in. 7 a .The with program to print a Fibonacci 9 _Image of a Python Program codes_