How to Use If, Elif, and Else in Python

1.4M views
•
December 11, 2022
by
CodeWithHarry
YouTube video player
How to Use If, Elif, and Else in Python

TL;DR

Python uses if, elif, and else statements to execute different code according to conditions that evaluate to True or False. Write a colon after each condition and use indentation to define its code block. Comparison operators such as >, <=, ==, and != create Boolean results, while elif supports additional checks when the preceding condition is false.

Transcript

Do you remember we used to skip our school on a rainy day in childhood We used to say that if it rains, I will not go to school And if it doesn't rain, we'll go to school We used to do decision making in this situation And we do a lot of decision making in our life For eg. If I'm tired so I won't go for picnic and if I'm not so I will... So we usua... Read More

Key Insights

  • Python conditional statements are decision-making structures that select code according to whether an expression evaluates to True or False. The tutorial connects this behavior to everyday choices, such as attending school based on rain or ordering food based on hunger and available money.
  • The input function returns a string by default in the tutorial, so int is used to convert an entered age into an integer. This converted value can then be compared numerically with 18 to decide which driving message should appear.
  • Comparison operators are used to create the Boolean conditions required by if statements. The demonstrated operators include >, <, >=, <=, ==, and !=, covering greater-than, less-than, equality, and inequality checks.
  • Equality testing is written with == rather than a single =. In the age example, a == 18 evaluates to True when the entered age is exactly 18, while a != 18 evaluates to False for that same input.
  • An if and else pair selects only one of its two blocks. When the if condition is true, Python executes the indented if block. When it is false, Python skips that block and executes the indented else block instead.
  • Python blocks are defined through colons and indentation rather than curly brackets. A colon follows the if, elif, or else header, and the indented statements below it remain part of that branch until the indentation ends.
  • An unindented statement after an if or else structure is independent of both branches. It runs whether the age satisfies the if condition or reaches the else branch, because its indentation does not place it inside either conditional block.
  • An elif chain supports more than two possible outcomes by checking another condition after an earlier condition fails. The number example classifies an entered value as negative, zero, or positive by using if, elif, and else in sequence.

Install to Summarize YouTube Videos and Get Transcripts

Explore YouTube Video Summarizer or Get YouTube Transcript Extractor

Questions & Answers

Q: How do if and else statements work in Python?

An if statement checks a condition that evaluates to True or False. If the condition is true, Python executes the indented code beneath the if header. Otherwise, it skips that block and executes the indented else block. In the tutorial's age example, the program prints a driving message based on whether the entered age is greater than 18.

Q: How do you use elif for multiple conditions in Python?

Elif adds another condition after an if condition and before the final else branch. Python first checks the if condition. If it is false, Python moves to the elif condition and tries to match it. The tutorial uses this structure to print that a number is negative, zero, or positive, depending on the entered value.

Q: Why is indentation required in Python conditional statements?

Indentation identifies which statements belong to each conditional block. After a header ending with a colon, indented lines are treated as part of that if, elif, or else branch. Removing the indentation produces an error in the demonstrated case. Unlike the referenced C-style approach, the Python example does not use curly brackets to mark the start and end of blocks.

Q: What comparison operators are used in Python conditions?

The tutorial demonstrates >, <, >=, <=, ==, and != as conditional operators. Each comparison returns a Boolean result, either True or False, that an if statement can evaluate. For an age of 18, age > 18 is false, age <= 18 is true, age == 18 is true, and age != 18 is false.

Q: What is the difference between = and == in Python conditions?

The tutorial states that a single = is not a conditional operator for checking equality. To test whether two values are equal, the condition must use ==. For example, a == 18 checks whether the stored age is exactly 18 and returns a Boolean result. The equality test can then guide an if or elif branch.

Q: How can Python check whether someone can drive by age?

The demonstrated program asks the user to enter an age, converts that input to an integer with int, and stores it in a variable. It then checks whether the age is greater than 18. When that condition is true, it prints that the person can drive. Otherwise, the else branch prints that the person cannot drive.

Q: What happens to code placed outside an if or else block?

A statement without the branch's indentation is independent of the if and else blocks. The tutorial demonstrates that such a print statement runs when the entered age is greater than 18 and also when it is less than 18. Indenting that statement under else changes its membership, so it runs only when the else branch is selected.

Q: How can Python classify a number as negative, zero, or positive?

The tutorial takes a number from user input and checks it with an if, elif, and else chain. If the number is less than 0, the program prints that it is negative. If the value matches 0, the elif branch prints that it is zero. Otherwise, the final else branch identifies the number as positive.

Summary & Key Takeaways

  • Python conditional statements let a program make decisions from user input or stored values. The driving-age example converts input to an integer, compares the age with 18, and prints one of two messages. The if block runs when its condition is true, while the else block handles the remaining case.

  • Comparison operators produce Boolean values that conditions can evaluate. The examples test whether an age is greater than 18, less than or equal to 18, exactly equal to 18, or not equal to 18. Equality requires == because a single = is not presented as a conditional operator.

  • Python uses a colon and indentation to define conditional blocks instead of curly brackets. Statements sharing the same indentation belong to the same if, elif, or else block. An unindented statement is independent and executes regardless of which conditional branch runs. Elif adds further mutually checked conditions between if and else.


Read in Other Languages (beta)

Share This Summary 📚

Explore More Summaries from CodeWithHarry 📚