If Else Statements in Python | Python for Beginners

Alex The Analyst
15 Nov 202206:40

Summary

TLDRThis video explains how to use the if-elif-else statement in Python. The instructor demonstrates how to create conditional statements that check whether certain conditions are true or false, using comparison and logical operators. The video walks through writing code for various conditions, including nested if statements and one-line if-else expressions. It also highlights how the Python interpreter evaluates these conditions step-by-step. The guide aims to help viewers understand the structure and flexibility of Python's conditional statements, making it ideal for those learning programming basics.

Takeaways

  • ๐Ÿ˜€ The video covers the if-else statement in Python programming.
  • ๐Ÿค“ The if-else statement evaluates conditions and executes a body of code if true or false.
  • ๐Ÿ“Š The 'elif' statement checks a secondary condition if the initial 'if' condition is false.
  • ๐Ÿ” You can have multiple 'elif' statements, but only one 'if' and one 'else' statement in a block.
  • ๐Ÿ’ป An example was shown where 'if 25 is greater than 10' evaluates as true and prints 'it worked'.
  • ๐Ÿ”Ž If a condition in 'if' is false, the code moves to the 'else' block for execution.
  • ๐Ÿ“ You can chain multiple conditions with 'elif', as demonstrated in the 25, 30, 40, and 50 comparisons.
  • โš™๏ธ Logical operators like 'and' or 'or' can be used inside the condition statements for more complex logic.
  • ๐Ÿš€ Python also allows writing if-else statements in a single line for concise conditions.
  • ๐Ÿงฉ Nested if statements are possible, where an if statement is placed within another, allowing more intricate decision-making.

Q & A

  • What is the basic structure of an if-else statement in Python?

    -An if-else statement starts with an 'if' condition. If the condition is true, a block of code is executed. If it's false, the 'else' block of code runs.

  • What is the purpose of the 'elif' statement in Python?

    -'Elif' (short for 'else if') checks another condition if the previous 'if' condition is false. You can have multiple 'elif' statements in a single if-else structure.

  • How many 'if' and 'else' statements can be used in a Python if-else structure?

    -You can only have one 'if' statement and one 'else' statement in a structure, but you can have multiple 'elif' statements between them.

  • What happens when an 'if' statement's condition is true?

    -If the condition is true, Python executes the block of code under the 'if' statement and skips any 'elif' or 'else' blocks.

  • What happens if all conditions in the if-elif structure are false?

    -If all conditions in the 'if' and 'elif' statements are false, the code in the 'else' block is executed.

  • How does Python handle indentation in if-else statements?

    -When writing an if-else statement, Python automatically indents the code inside the body of the statement after a colon (':'). Proper indentation is crucial for defining code blocks.

  • Can you use multiple comparison operators in an if statement?

    -Yes, you can use logical operators like 'and' or 'or' to combine multiple conditions in a single if statement.

  • How does Python handle a one-line if-else statement?

    -Python allows if-else statements to be written on one line using the syntax: 'print(output_if_true) if condition else print(output_if_false)'.

  • What is a nested if statement?

    -A nested if statement is an if statement inside another if statement. It allows for multiple levels of condition checks, creating more complex logic.

  • What happens when a condition in the elif chain becomes true?

    -When a condition in an 'elif' statement becomes true, its corresponding block of code is executed, and the rest of the 'elif' and 'else' statements are skipped.

Outlines

00:00

๐Ÿ’ก Introduction to the If-Else Statement in Python

The first paragraph introduces the concept of the if-else statement in Python. The speaker explains that while it's technically called the 'if-elif-else' statement, they will refer to it simply as 'if-else'. A blurry flowchart is presented to illustrate how the if statement works. If the initial condition is true, a body of code runs; otherwise, the program moves to the elif condition. If the elif condition is true, another block of code is executed. If both conditions are false, the else statement's code runs. It is emphasized that there can only be one 'if' and one 'else' statement, but multiple 'elif' statements.

05:00

๐Ÿ”ค Writing the If-Else Code in Python

In this section, the speaker walks through writing an if-else statement in Python. They start with the 'if' statement, checking if 25 is greater than 10. Since the condition is true, the body of the code prints 'it worked'. Next, the speaker shows how an else statement functions by checking if 25 is less than 10, which is false, leading the program to execute the else block and print 'it did not work'. The process illustrates how Python automatically indents the code for readability, with conditions dictating which block of code is executed.

๐Ÿงฉ Handling Multiple Conditions with Elif

This paragraph introduces the 'elif' statement and shows how it works with multiple conditions. The speaker starts by modifying the previous code to check if 25 is less than 30 using 'elif'. When this condition is true, it prints 'elif worked'. They then demonstrate adding more 'elif' statements with different conditions (25 less than 20, 21, 40, and 50). The first 'elif' condition that evaluates as true (25 < 40) triggers the corresponding code block and prevents the subsequent elifs from running. This section shows how multiple conditions can be evaluated in sequence using 'elif'.

๐Ÿ”— Using Logical Operators in If Statements

The speaker expands on the use of logical operators like 'and' and 'or' in if statements. By using multiple conditions in one if statement, such as checking whether 25 is less than 10 'or' 1 is less than 3, the code runs if at least one condition is true. They explain how logical operators can combine different conditions to evaluate the truth of an if statement. This section also touches on writing if-else statements in a single line for more concise code, although this approach limits the readability and complexity of the conditions.

๐Ÿ”„ Introducing Nested If Statements

In this part, the speaker explains nested if statements, where one if statement is placed inside another. They demonstrate this by creating a new condition inside an already-true if block. For example, after the first condition (25 < 10 or 1 < 3) is met, they add a nested if statement checking if 10 is greater than 5. If true, it prints another message. This concept allows for more complex decision-making, enabling multiple layers of conditions to be evaluated in sequence. Nested if statements are a powerful tool for advanced logic in Python programming.

๐Ÿ“š Summary and Conclusion

The speaker wraps up the video by summarizing the key points: understanding the basic if-else statement, how to use elif for multiple conditions, using logical operators, and creating nested if statements. They emphasize the flexibility and power of these structures in Python, encouraging viewers to practice writing if-else statements for more complex programming logic. The speaker thanks the audience for watching and encourages them to like and subscribe for more content.

Mindmap

Keywords

๐Ÿ’กIf Statement

An if statement is a conditional statement in programming that runs a block of code only if a specified condition is true. In the video, it forms the foundation of the decision-making process, where a condition like '25 is greater than 10' is checked to determine if the code block will be executed.

๐Ÿ’กElse Statement

An else statement provides an alternative block of code to run if the condition in the if statement evaluates to false. In the video, the else statement is shown when the condition '25 is less than 10' fails, leading the code to print 'it did not work'.

๐Ÿ’กElif Statement

The elif (else if) statement is used to check multiple conditions after the initial if statement. If the first condition fails, the elif condition is checked next. In the video, it is demonstrated as a way to handle several conditions in a chain, such as '25 is less than 30'.

๐Ÿ’กCondition

A condition is an expression evaluated by the if, elif, or else statements to determine whether the associated block of code should be executed. For example, in the script, the condition '25 is greater than 10' is evaluated to true, and thus the 'it worked' message is printed.

๐Ÿ’กLogical Operator

Logical operators like 'and' and 'or' allow multiple conditions to be combined. In the video, the operator 'or' is used in the statement '25 is less than 10 or 1 is less than 3', which is true because one of the conditions is true, leading to the execution of the block of code.

๐Ÿ’กComparison Operator

Comparison operators compare values, determining if one is greater than, less than, or equal to another. In the video, operators like '>' (greater than) and '<' (less than) are used in examples such as '25 is greater than 10' to compare numbers and make decisions within the code.

๐Ÿ’กIndentation

Indentation in Python is used to define the blocks of code that belong to an if, elif, or else statement. The video mentions how Python automatically indents lines following an if statement to signal that the indented lines are part of the conditional block.

๐Ÿ’กOne-Line If Statement

A one-line if statement allows for writing a conditional statement and the resulting action on a single line. In the video, the instructor demonstrates this by writing 'print it worked if 10 is greater than 30 else print it did not work' to simplify the structure of if-else logic.

๐Ÿ’กNested If Statement

A nested if statement is when an if statement appears inside another if block. This allows for more complex decision-making. In the video, a nested example is used where one if statement checks if '25 is greater than 10', and inside it, another if checks if '10 is greater than 5'.

๐Ÿ’กBody of Code

The body of code refers to the indented lines following an if, elif, or else statement that get executed if the condition is true. For instance, in the video, when the condition '25 is greater than 10' is true, the body of code 'print it worked' is executed.

Highlights

Introduction to the if-else statement in Python, explaining its structure and function.

Explanation of the basic if statement: if the condition is true, the code block runs.

Clarification of the elif (else if) statement: checks another condition if the initial if condition is false.

Explanation of the else statement: runs when all previous conditions (if and elif) are false.

Demonstration of a simple if statement: checks if 25 is greater than 10 and prints 'it worked' if true.

Example of an else statement: checks if 25 is less than 10; if false, the else block runs and prints 'it did not work.'

Use of multiple elif conditions: additional elif checks are introduced, demonstrating how they evaluate in sequence.

Illustration of using multiple elif statements: each subsequent elif is evaluated only if all prior conditions are false.

Highlight on the use of logical operators such as 'and' and 'or' within if statements to combine conditions.

Introduction to writing a one-line if-else statement in Python for simpler conditional checks.

Example of a one-line if-else: checks if 10 is greater than 30, prints a result based on the evaluation.

Explanation of nested if statements: placing one if statement inside another to create more complex decision structures.

Demonstration of a nested if statement: checks if 25 is less than 10, and then checks another condition within the first if block.

Emphasis on the flexibility of if-else statements in Python to create highly complex decision-making code.

Final encouragement for understanding if-else logic and nested statements for more advanced Python coding.

Transcripts

play00:00

hello everybody today we're going to be

play00:01

taking a look at the if statement within

play00:03

python now it's actually the if lfl

play00:05

statement but that's a mouthful so i'm

play00:07

just going to call it the if else

play00:08

statement

play00:09

now we have this flowchart and i

play00:11

apologize for being blurry but this is

play00:12

the absolute best one that i could find

play00:14

right up top we have our if condition

play00:16

now if this if condition is true we're

play00:19

going to run a body of code but if that

play00:21

condition is false we're going to go

play00:22

over here and go to the lf condition the

play00:25

lf condition or statement is basically

play00:27

saying if the first if statement doesn't

play00:29

work let's try this if statement if this

play00:31

elf statement is true it goes to this

play00:33

body of code if it's false it'll come

play00:35

over here to the else and the else is

play00:37

basically if all these things don't work

play00:40

then run this body of code now you can

play00:42

have as many ill-if statements as you

play00:44

want but you can only have one if

play00:45

statement and one else statement so

play00:48

let's write out some code and see how

play00:49

this actually looks let's first start

play00:50

off by writing if that is our if

play00:52

statement and now we have to write our

play00:54

condition which is about to be either

play00:56

met or not met so we'll say if 25 is

play00:59

greater than 10 which is true

play01:02

we'll say colon and then we're going to

play01:03

hit enter and it's going to

play01:05

automatically indent that line of code

play01:07

for us and this is our body of code so

play01:09

if 25 is greater than 10 our body of

play01:12

code will execute so for us we're just

play01:14

going to write print and we'll say

play01:16

it worked

play01:17

now if we run this it's going to check

play01:19

is 25 greater than 10 if that is true

play01:22

print this so let's hit shift enter and

play01:26

it worked now let's take this exact code

play01:29

paste it right down here and we'll say

play01:31

is less than

play01:33

and right now this if statement is not

play01:36

true so it's not actually going to work

play01:38

as you can see there's no output there's

play01:40

nothing that happened really but it did

play01:42

check to see if 25 was less than 10 but

play01:45

it just wasn't true now we can use our

play01:47

else statement so we're going to come

play01:48

right down here and we're going to say

play01:50

else and we'll do a colon and we'll hit

play01:52

enter again automatically indenting and

play01:54

we're going to say print

play01:56

and we're going to say it

play01:58

did not work

play02:00

dot dot dot so what it's going to do is

play02:02

it's going to come up here and check is

play02:04

25 less than 10

play02:06

no it's not so this body of code is not

play02:08

going to be executed it's going to go

play02:09

right down to this else statement now

play02:11

this else statement is going to be

play02:13

printed there's no condition on this so

play02:14

the if statement has a condition 25 is

play02:17

less than 10 this has no condition so if

play02:19

this doesn't work if this is false it's

play02:21

going to come down here and it will run

play02:23

this body of code let's run this by

play02:25

clicking shift enter

play02:27

and as you can see our output is it did

play02:29

not work now let's go back up here and

play02:32

put greater than because this is now

play02:33

true it's going to say if 25 is greater

play02:36

than 10 print it worked and then it's

play02:39

going to stop it's not going to go to

play02:40

this else statement at all so let's run

play02:43

this and our output is it worked so what

play02:45

if we have a lot of different conditions

play02:47

that we want to try let's come right

play02:49

down here this is where the elf comes in

play02:51

so really quickly let's change this to a

play02:54

not true a false statement we're going

play02:56

to go down and say alif

play02:58

and we're going to say

play02:59

if

play03:00

it is

play03:02

and let's say 30

play03:05

we'll say

play03:06

lf worked

play03:09

so now it's going to check is 25 less

play03:11

than 10 no it's not let's look at the

play03:13

next condition is 25 less than 30 and if

play03:17

it is we'll print alif worked so let's

play03:19

try running this

play03:20

and alif worked now we can do as many of

play03:23

these elf statements as we want we can

play03:26

do

play03:27

let's just try a few of them right here

play03:29

so we'll say if 25 is less than 20

play03:33

is less than 21

play03:35

and let's do 40

play03:37

and let's do 50. so we'll say alif lf2

play03:42

lift 3 and lf4 now if you look at this

play03:46

the first one that is actually going to

play03:47

work is this 25 to 40 right here once

play03:51

this one is checked and it comes out as

play03:53

true none of the other lf or l

play03:55

statements will work so let's try this

play03:57

one it should be lf3

play03:59

and this one ran properly now within our

play04:02

condition so far we've only used a

play04:04

comparison operator we can also use a

play04:06

logical operator like and or or so we

play04:09

can say if 25 is less than 10 which it's

play04:13

not let's say or actually and we'll say

play04:16

or

play04:17

one

play04:18

is less than three which is true if we

play04:20

run this

play04:21

now it will actually work so we can use

play04:23

several different types of operators

play04:25

within our if statement to see if a

play04:27

condition is true or not or several

play04:29

conditions are true there's also a way

play04:31

to write an if else statement in one

play04:33

line if you want to do that so we can

play04:35

write print we'll say it worked

play04:39

and then we'll come over here and say if

play04:41

10 is greater than 30 and then we'll

play04:44

write else

play04:46

print

play04:47

and we'll say

play04:49

it did not work

play04:51

just like we had before except now it's

play04:53

all occurring on one line so let's just

play04:55

try this and see if it works

play04:57

so it's saying print it worked if 10 is

play05:00

greater than 30 which it wasn't so it

play05:01

went to the else statement and then it

play05:03

printed out our body right here although

play05:05

we didn't have any indentation or

play05:07

multiple lines it was all done in one

play05:09

line now there's one other thing that we

play05:10

haven't looked at yet and i'm going to

play05:12

show it to you really quickly and that's

play05:14

a nested if statement so when we run

play05:17

this it's going to say it worked it

play05:19

works because it says 25 is less than 10

play05:21

or 1 is less than 3. since this is true

play05:25

it's going to print out it worked but we

play05:27

can also do a nested if statement so we

play05:29

can do multiple if statements as well so

play05:32

we're going to hit enter and we'll say

play05:34

if and we'll do a true statement here so

play05:36

we'll say if 10

play05:37

is greater than 5 let's do a colon hit

play05:41

enter and then we'll say print and then

play05:43

we'll type a string saying this nested

play05:46

if statement

play05:48

oops worked

play05:50

now let's try this out and see what we

play05:52

get

play05:53

so it went through the first if

play05:54

statement it said it was true and it

play05:56

prints out it worked this is still the

play05:58

body of code so it goes down to this

play06:00

next if statement and it says if 10 is

play06:03

greater than 5 we're going to print this

play06:04

out and you can do this on and on and on

play06:07

it can basically go on forever and you

play06:09

can create a really in-depth logic and

play06:11

that actually happens a lot when you

play06:13

start writing more advanced code so i

play06:14

hope that this was helpful i hope that

play06:16

you understand the if-else statement

play06:17

better i hope that you understand how

play06:19

nested if statements work as well thank

play06:21

you guys so much for watching if you

play06:23

like this video be sure to like and

play06:24

subscribe below and i'll see you in the

play06:26

next video

play06:28

[Music]

play06:39

you

Rate This
โ˜…
โ˜…
โ˜…
โ˜…
โ˜…

5.0 / 5 (0 votes)

Related Tags
Python tutorialif-elsebeginner codingconditional logicprogramming basicselif statementcode exampleslogical operatorsnested ifprogramming tips