Posts

Showing posts with the label Python Flow Control

learnpython24-(Python if...else Statement)

Image
  Python if...else Statement In this article, you will learn to create decisions in a Python program using different forms of if..else statement. What is if...else statement in Python? Decision making is required when we want to execute a code only if a certain condition is satisfied. The  if…elif…else  statement is used in Python for decision making. Python if Statement Syntax if test expression: statement(s) Here, the program evaluates the  test expression  and will execute statement(s) only if the test expression is  True . If the test expression is  False , the statement(s) is not executed. In Python, the body of the  if  statement is indicated by the indentation. The body starts with an indentation and the first unindented line marks the end. Python interprets non-zero values as  True .  None  and  0  are interpreted as  False . Python if Statement Flowchart Flowchart of if statement in Python programmin...

learnpython24-(Python for Loop)

Image
  Python for Loop In this article, you'll learn to iterate over a sequence of elements using the different variations of for loop. What is for loop in Python? The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects. Iterating over a sequence is called traversal . Syntax of for Loop for val in sequence: Body of for Here,  val  is the variable that takes the value of the item inside the sequence on each iteration. Loop continues until we reach the last item in the sequence. The body of for loop is separated from the rest of the code using indentation. Flowchart of for Loop Flowchart of for Loop in Python Example: Python for Loop # Program to find the sum of all numbers stored in a list # List of numbers numbers = [ 6 , 5 , 3 , 8 , 4 , 2 , 5 , 4 , 11 ] # variable to store the sum sum = 0 # iterate over the list for val in numbers: sum = sum+val print ( "The sum is" , sum) When you run the program, the o...

learnpython24-(Python While Loop)

Image
  Python while Loop Loops are used in programming to repeat a specific block of code. In this article, you will learn to create a while loop in Python. What is while loop in Python? The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true. We generally use this loop when we don't know the number of times to iterate beforehand. Syntax of while Loop in Python while test_expression: Body of while In the while loop, test expression is checked first. The body of the loop is entered only if the  test_expression  evaluates to  True . After one iteration, the test expression is checked again. This process continues until the  test_expression  evaluates to  False . In Python, the body of the while loop is determined through indentation. The body starts with indentation and the first unindented line marks the end. Python interprets any non-zero value as  True .  None  and  0 ...