









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










National Highway, Cr. Rubber, Tupi, South Cotabato
In this Lesson, you will learn to create decisions in a Python program using different forms of if..else statement.
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.
National Highway, Cr. Rubber, Tupi, South Cotabato Python if Statement Flowchart Flowchart of if statement in Python programming Example: Python if Statement
num = 3 if num > 0 : print(num, "is a positive number.") print("This is always printed.") num = - 1 if num > 0 : print(num, "is a positive number.") print("This is also always printed.") When you run the program, the output will be: 3 is a positive number This is always printed This is also always printed. In the above example, num > 0 is the test expression. The body of if is executed only if this evaluates to True. When the variable num is equal to 3, test expression is true and statements inside the body of if are executed. If the variable num is equal to - 1, test expression is false and statements inside the body of if are skipped. The print() statement falls outside of the if block (unindented). Hence, it is executed regardless of the test expression.
National Highway, Cr. Rubber, Tupi, South Cotabato Output Positive or Zero In the above example, when num is equal to 3, the test expression is true and the body of if is executed and the body of else is skipped. If num is equal to - 5, the test expression is false and the body of else is executed and the body of if is skipped. If num is equal to 0, the test expression is true and body of if is executed and body of else is skipped. Python if...elif...else Statement Syntax of if...elif...else if test expression: Body of if elif test expression: Body of elif else: Body of else The elif is short for else if. It allows us to check for multiple expressions. If the condition for if is False, it checks the condition of the next elif block and so on. If all the conditions are False, the body of else is executed. Only one block among the several if...elif...else blocks is executed according to the condition. The if block can have only one else block. But it can have multiple elif blocks.
National Highway, Cr. Rubber, Tupi, South Cotabato Flowchart of if...elif...else Flowchart of if...elif....else statement in Python Example of if...elif...else '''In this program, we check if the number is positive or negative or zero and display an appropriate message''' num = 3.
if num > 0 : print("Positive number") elif num == 0 : print("Zero") else: print("Negative number") When variable num is positive, Positive number is printed. If num is equal to 0, Zero is printed. If num is negative, Negative number is printed.
National Highway, Cr. Rubber, Tupi, South Cotabato
In this lesson, you'll learn to iterate over a sequence of elements using the different variations of for loop.
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: loop body 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
National Highway, Cr. Rubber, Tupi, South Cotabato
numbers = [ 6 , 5 , 3 , 8 , 4 , 2 , 5 , 4 , 11 ]
sum = 0
for val in numbers: sum = sum+val print("The sum is", sum) When you run the program, the output will be: The sum is 48 The range() function We can generate a sequence of numbers using range() function. range(10) will generate numbers from 0 to 9 (10 numbers). We can also define the start, stop and step size as range(start, stop,step_size). step_size defaults to 1 if not provided. The range object is "lazy" in a sense because it doesn't generate every number that it "contains" when we create it. However, it is not an iterator since it supports in, len and getitem operations. This function does not store all the values in memory; it would be inefficient. So it remembers the start, stop, step size and generates the next number on the go. To force this function to output all the items, we can use the function list(). The following example will clarify this. print(range( 10 )) print(list(range( 10 ))) print(list(range( 2 , 8 ))) print(list(range( 2 , 20 , 3 ))) Output
National Highway, Cr. Rubber, Tupi, South Cotabato 1 5 No items left. Here, the for loop prints items of the list until the loop exhausts. When the for loop exhausts, it executes the block of code in the else and prints No items left. This for...else statement can be used with the break keyword to run the else block only when the break keyword was not executed. Let's take an example:
student_name = 'Soyuj' marks = {'James': 90 , 'Jules': 55 , 'Arthur': 77 } for student in marks: if student == student_name: print(marks[student]) break else: print('No entry with that name found.') Output No entry with that name found.
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.
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
National Highway, Cr. Rubber, Tupi, South Cotabato 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 are interpreted as False. Flowchart of while Loop Flowchart for while loop in Python Example: Python while Loop
n = 10
sum = 0 i = 1 while i <= n: sum = sum + i i = i+ 1 # update counter
National Highway, Cr. Rubber, Tupi, South Cotabato Here, we use a counter variable to print the string Inside loop three times. On the fourth iteration, the condition in while becomes False. Hence, the else part is executed.
In this lesson, you will learn to use break and continue statements to alter the flow of a loop.
In Python, break and continue statements can alter the flow of a normal loop. Loops iterate over a block of code until the test expression is false, but sometimes we wish to terminate the current iteration or even the whole loop without checking test expression. The break and continue statements are used in these cases.
The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop. If the break statement is inside a nested loop (loop inside another loop), the break statement will terminate the innermost loop. Syntax of break break
National Highway, Cr. Rubber, Tupi, South Cotabato Flowchart of break Flowchart of break statement in Python The working of break statement in for loop and while loop is shown below. Working of the break statement Example: Python break
for val in "string": if val == "i": break print(val) print("The end")
National Highway, Cr. Rubber, Tupi, South Cotabato How continue statement works in python Example: Python continue
for val in "string": if val == "i": continue print(val) print("The end") Output s t r n g The end This program is same as the above example except the break statement has been replaced with continue. We continue with the loop, if the string is i, not executing the rest of the block. Hence, we see in our output that all the letters except i gets printed.
In this article, you'll learn about pass statement. It is used as a placeholder for future implementation of functions, loops, etc.
National Highway, Cr. Rubber, Tupi, South Cotabato What is pass statement in Python? In Python programming, the pass statement is a null statement. The difference between a comment and a pass statement in Python is that while the interpreter ignores a comment entirely, pass is not ignored. However, nothing happens when the pass is executed. It results in no operation (NOP). Syntax of pass pass We generally use it as a placeholder. Suppose we have a loop or a function that is not implemented yet, but we want to implement it in the future. They cannot have an empty body. The interpreter would give an error. So, we use the pass statement to construct a body that does nothing. Example: pass Statement '''pass is just a placeholder for functionality to be added later.''' sequence = {'p', 'a', 's', 's'} for val in sequence: pass We can do the same thing in an empty function or class as well. def function(args): pass class Example: pass