Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Python If-Else and Loop Statements: Understanding Decision Making and Iteration, Cheat Sheet of Mathematics

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

  • What is the difference between an If statement and an If-else statement in Python?
  • What is the purpose of the elif statement in Python?
  • How do you use nested if statements in Python?

Typology: Cheat Sheet

2022/2023

Uploaded on 12/22/2022

Sahil1239
Sahil1239 🇮🇳

4 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Python If-else statements
Decision making is the most important asp ect 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.
Indentation in Python
For the ease of programming and to achieve simplicity, python doesn't allow the use of parentheses for the b lock 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
The if statement is used to test a particular condition and if the condition is true, it executes a b lock 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
pf3
pf4
pf5

Partial preview of the text

Download Python If-Else and Loop Statements: Understanding Decision Making and Iteration and more Cheat Sheet Mathematics in PDF only on Docsity!

Python If-else statements

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.

Indentation in Python

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

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

Example 1

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

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)

The break Statement

With the break statement we can stop the loop before it has looped through all the items:

Example

Exit the loop when x is "banana": city = ["Delhi", "Mumbai", "Agra"] for x in city: print(x) if x== "Mumbai": break

The continue Statement

With the continue statement we can stop the current iteration of the loop, and continue with the next:

Example

Do not print banana: city = ["Delhi", "Mumbai", "Agra"] for x in city: if x == "Mumbai": continue print(x)

The range() Function

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.

Example

Using the range() function: for x in range( 6 ): print(x)

Else in For Loop

The else keyword in a for loop specifies a block of code to be executed when the loop is finished:

Example

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!")

Python While Loops

In coding, loops are designed to execute a specified code block repeatedly.

Introduction of Python While Loop

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.

  1. while conditional_expression:
  2. Code block of while The given condition, i.e., conditional_expression, is evaluated initially in the Python while loop. Then, if the conditional expression gives a boolean value True, the while loop statements are executed. The conditional expression is verified again when the complete code block is executed. This procedure repeatedly occurs until the conditional expression returns the boolean value False.

o The statements of the Python while loop are dictated by indentation.

o The code block begins when a statement is indented & ends with the very first unindented statement.

o Any non-zero number in Python is interpreted as boolean True. False is interpreted as None and 0.

Python While Loop Example

i= while i<=5: print(i * ‘*’) i=i+