Python If-Else Statements

RightPiano avatar
RightPiano
·
·
Download

Start Quiz

Study Flashcards

16 Questions

What is the primary function of decision making in programming languages?

To execute a block of code for a particular decision

Why does Python not allow the use of parentheses for block-level code?

To achieve simplicity and ease of programming

What is the typical amount of indentation used in Python?

Four spaces

What is the purpose of the if statement in Python?

To check the validity of a condition and execute a block of code

What is the syntax of the if-statement in Python?

if expression: statement

What is the output of the following Python program: num = int(input("enter the number:")); if num%2 == 0: print("The Given number is an even number")?

The program will print 'The Given number is an even number' if the number is even

What is the role of indentation in Python?

It is used to declare a block of code

What is the condition of the if statement in Python?

Any valid logical expression

What is the purpose of the first Python program in the text?

To find the largest number among three input numbers

What is the function of the else block in the if-else statement?

To execute the block of statements when the condition is false

What is the condition to check whether a person is eligible to vote in the given program?

Age >= 18

What does the elif statement enable us to do?

Check multiple conditions and execute a block of statements

What is the purpose of the modulus operator (%) in the second Python program?

To check whether a number is divisible by 2

What is the output of the first Python program if the input numbers are a = 10, b = 20, and c = 30?

From the above three numbers given c is largest

What is the syntax of the if-else statement?

if condition: #block of statements, else: #another block of statements

What is the output of the second Python program if the input number is 25?

The Given Number is an odd number

Study Notes

Decision Making in Python

  • Decision making is a crucial aspect of programming languages, allowing us to run specific code blocks based on particular conditions.
  • In Python, decision making is performed using if-else statements and indentation is used to declare code blocks.

Indentation in Python

  • Python doesn't use parentheses for block-level code, instead, it uses indentation to define code blocks.
  • Typically, four spaces are used for indentation in Python.
  • Statements with the same indentation level belong to the same block.

The if Statement

  • The if statement is used to test a condition, and if true, execute a block of code (if-block).
  • The condition can be any valid logical expression that evaluates to true or false.
  • The syntax is: if expression: statement

Examples of if Statements

  • Example 1: Check if a number is even: if num%2 == 0: print("The Given number is an even number")
  • Example 2: Find the largest of three numbers using multiple if statements.

The if-else Statement

  • The if-else statement combines an if statement with an else block, which is executed when the condition is false.
  • Syntax: if condition: ... else: ...
  • If the condition is true, the if-block is executed, otherwise, the else-block is executed.

Examples of if-else Statements

  • Example 1: Check if a person is eligible to vote: if age>=18: print("You are eligible to vote !!"); else: print("Sorry!you have to wait !!");
  • Example 2: Check if a number is even or odd: if num%2 == 0: print("The Given number is an even number"); else: print("The Given Number is an odd number");

The elif Statement

  • The elif statement enables checking multiple conditions and executing specific blocks of statements depending on the true condition.

Learn about decision making in Python using if-else statements, conditional checking, and indentation for simplicity.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Python Conditional Statements Explained
5 questions
Python Basic Concepts Quiz
13 questions
Conditional Statements in Python
18 questions
Use Quizgecko on...
Browser
Browser