



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Learn about Python's If-Else statements, including the If, If-Else, and Nested If statements, as well as the use of indentation and the If-elif statement. Additionally, explore Python's For and While loops, the break and continue statements, and the range() function.
What you will learn
Typology: Cheat Sheet
1 / 5
This page cannot be seen from the preview
Don't miss anything!
Decision making is the most important aspect of almost all the programming languages. As the name implies, decision making allows us to run a particular block of code for a particular decision. Here, the decisions are made on the validity of the particular conditions. Condition checking is the backbone of decision making. In python, decision making is performed by the following statements. Statement Description If Statement The if statement is used to test a specific condition. If the condition is true, a block of code (if-block) will be executed. If - else Statement The if-else statement is similar to if statement except the fact that, it also provides the block of the code for the false case of the condition to be checked. If the condition provided in the if statement is false, then the else statement will be executed. Nested if Statement Nested if statements enable us to use if? else statement inside an outer if statement.
For the ease of programming and to achieve simplicity, python doesn't allow the use of parentheses for the block level code. In Python, indentation is used to declare a block. If two statements are at the same indentation level, then they are the part of the same block. Generally, four spaces are given to indent the statements which are a typical amount of indentation in python.
The if statement is used to test a particular condition and if the condition is true, it executes a block of code known as if- block. The condition of if statement can be any valid logical expression which can be either evaluated to true or false. Figure - If else condition Flowchart: The syntax of the if-statement is given bel
if expression: statement
num = int(input("enter the number?")) if num%2 == 0: print ("Number is even") Output: enter the number? Number is even
The if-else statement provides an else block combined with the if statement which is executed in the false case of the condition. If the condition is true, then the if-block is executed. Otherwise, the else-block is executed. The syntax of the if-else statement is given below. if condition: #block of statements else : #another block of statements (else-block)
With the break statement we can stop the loop before it has looped through all the items:
Exit the loop when x is "banana": city = ["Delhi", "Mumbai", "Agra"] for x in city: print(x) if x== "Mumbai": break
With the continue statement we can stop the current iteration of the loop, and continue with the next:
Do not print banana: city = ["Delhi", "Mumbai", "Agra"] for x in city: if x == "Mumbai": continue print(x)
To loop through a set of code a specified number of times, we can use the range() function, The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.
Using the range() function: for x in range( 6 ): print(x)
The else keyword in a for loop specifies a block of code to be executed when the loop is finished:
Print all numbers from 0 to 5, and print a message when the loop has ended: for x in range( 6 ): print(x) else: print("Finally finished!")
In coding, loops are designed to execute a specified code block repeatedly.
The Python while loop iteration of a code block is executed as long as the given condition, i.e., conditional_expression, is true. If we don't know how many times we'll execute the iteration ahead of time, we can write an indefinite loop.
i= while i<=5: print(i * ‘*’) i=i+