BCS-DS-427B: PYTHON Manav Rachna International Institute of Research and Studies, Faridabad 1
PYTHON Loops Swati Hans Associate Professor, CSE MRIIRS BCS-DS-427B: PYTHON
For loop is used to execute a block of statements repeatedly until a given range or sequence is completed For Loop for i in range(0,10): print(“hello”) Specifies range of i from 0-9 print “Hello” 10 times Last element ? Execute code inside for No Yes Loop finish
For loop for i in range(0,10,1): print(“hello ”) Here i goes from 0 to 9 and increasing i by stepsize 1 The range() function generates a sequence of numbers The syntax for range() function is, range(start , stop , step) start → indicates the beginning of the sequence. If the start argument is not specified, then the sequence of numbers start from zero by default. stop → Generates numbers up to this value but not including this number itself. step → indicates the difference between every two consecutive numbers in the sequence. The step value can be both negative and positive but not zero. Note:Both start and step arguments are optional
Demonstrate for Loop Using range() Function Display counting from 0 to 100 using different arguments in range() Using only stop for i in range(100): print( i , end=“ “) b) Using Both ''start'' and ''stop'' argument values specified in range function " for i in range(0, 101): print( i , end=“ “) c) Using all three arguments ''start'', ''stop'' and ''step'' specified in range function") for i in range(0,101, 1 ): print( i , end=“ “)
Write a program to print table of a number n = int (input("Enter a number ")) s um=0 for i in range(1,n+1): sum= sum+I print(sum) Write a program to sum of n natural numbers n = int(input("Enter a number")) for i in range(1, 11): print(n,” x ”,i,” =“, n*i)
Program to display Each Character in the String Using for Loop for ch in "Blue": print( ch ) Write a Program to Find the Sum of All Odd and Even Numbers up to a Number Specified by the User. number = int(input("Enter a number")) even = 0 odd = 0 for i in range(number): if i % 2 == 0: even = even + i else: odd = odd + i print( f"Sum of Even numbers are {even} and Odd numbers are {odd}")
number = int(input('Enter a number')) factorial = 1 if number < 0: print("Factorial doesn't exist for negative numbers") elif number == 0: print('The factorial of 0 is 1’) else: for i in range(1, number + 1): factorial = factorial * i print( f"The factorial of number {number} is {factorial}") Output Enter a number 5 The factorial of number 5 is 120 Write a Program to Find the Factorial of a Number using for loop
While Loop The while loop starts with the while keyword and ends with a colon. With a while statement, the first thing that happens is that the Boolean expression is evaluated before the statements in the while loop block is executed. If the Boolean expression evaluates to False , then the statements in the while loop block are never executed. If the Boolean expression evaluates to True , then the while loop block is executed. After each iteration of the loop block, the Boolean expression is again checked, and if it is True , the loop is iterated again.
While loop is used to execute a block of statements repeatedly until a given a condition is satisfied Syntax: w hile(condition): # do something1 # do something2 While Loop Start=1 While(start<=10): print(“hello”) start=start+1 intialize condition increment To print “Hello” 10 times
Write Python Program to Display First 10 natural nsumbers Using while Loop Starting from 0 i = 1 while i < 11: print( i ) i = i + 1
Write a Program to Find the Average of n Natural Numbers Where n Is the Input from the User n = int(input("Enter a number up to which you want to find the average")) i = 0 sum = while i < n: sum = sum + I i = i + 1 average = sum/n print( f"The average of {number} natural numbers is {average}")
Write a Program to Find the Factorial of a Number using while loop n = int (input("Enter a number ")) i =n fact=1 if n < 0: print("Factorial doesn't exist for negative numbers") elif number == 0: print('The factorial of 0 is 1’) else : while( i >0): fact=fact*I i =i-1 print(“Factorial of ”, n,”is”,fact )
When the condition of loop remains true always then loop goes on execution without stopping i.e Infinite Loop. Example: num =1 while( num ==1): print(“Hmm”) Above code will go to infinite loop until condition is changed or variable is changed. Infinite Loop using while
Break and continue statements The break and continue statements provide greater control over the execution of code in a loop. Whenever the break statement is encountered, the execution control immediately jumps to the first instruction following the loop. To pass control to the next iteration without exiting the loop, use the continue statement. Both continue and break statements can be used in while and for loops .
for i in range(1,11): if( i ==5): break else: print( i ,end =“ “) break is used to exit a for loop or a while loop , Break & Continue continue is used to skip the current block, and return to the "for" or "while" statement for i in range(1,11): if( i ==5): continue else: print( i ) Output: 1 2 3 4 Output: 1 2 3 4 6 7 8 9 10
Write a program to display Fibonacci series Write a program to find number is prime or not Using Loops