Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Simple if/else expressions (1/2) | Conditional statements
Learn Python from Scratch
course content

Course Content

Learn Python from Scratch

Learn Python from Scratch

1. The basics
2. Arithmetic operations
3. Common data types
4. Conditional statements
5. Other data types
6. Loops
7. Functions

Simple if/else expressions (1/2)

Okay, now we are familiar with conditional operators and know how to deal with them. It means now we can divide our code into pieces depend on the condition. In Python, its implemented like the follows:

1234
if condition: do if true else do if not true
copy

For example, remember our primitive example about deciding whether to go walk or not. We can write it in Python syntax like that:

1234
if (weather is good and (day is Sunday or day is Saturday)): go walk else: stay home :(
copy

Consider some examples: imagine you have some string and want to check if it's wide. Let's call string wide if it has more than 20 symbols.

1234567891011121314151617
# assign some variable test = "small string" # conditional statement if(len(test) > 20): print("This string is wide!") else: print("Nothing special") # check on different string test = "This string is very-very and very large" # conditional statement if(len(test) > 20): print("This string is wide!") else: print("Nothing special")
copy

Please note, that code blocks written below if, else statements need to be indented on the same level (2 spaces, 3, 4, ...). The main condition - the equal number.

Assume you own a small grocery and spend 2000 dollars every day. Obviously, if your revenue per day is less than 2000, then you suffer losses, otherwise - nothing bad. Let's try to write if/else statement for that.

Task

Write conditional statement checking if variable revenue is less than 2000 (in that case print message "We suffer losses!", otherwise - print "Everything is ok!"). Do not change value of revenue.

Task

Write conditional statement checking if variable revenue is less than 2000 (in that case print message "We suffer losses!", otherwise - print "Everything is ok!"). Do not change value of revenue.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 4. Chapter 3
toggle bottom row

Simple if/else expressions (1/2)

Okay, now we are familiar with conditional operators and know how to deal with them. It means now we can divide our code into pieces depend on the condition. In Python, its implemented like the follows:

1234
if condition: do if true else do if not true
copy

For example, remember our primitive example about deciding whether to go walk or not. We can write it in Python syntax like that:

1234
if (weather is good and (day is Sunday or day is Saturday)): go walk else: stay home :(
copy

Consider some examples: imagine you have some string and want to check if it's wide. Let's call string wide if it has more than 20 symbols.

1234567891011121314151617
# assign some variable test = "small string" # conditional statement if(len(test) > 20): print("This string is wide!") else: print("Nothing special") # check on different string test = "This string is very-very and very large" # conditional statement if(len(test) > 20): print("This string is wide!") else: print("Nothing special")
copy

Please note, that code blocks written below if, else statements need to be indented on the same level (2 spaces, 3, 4, ...). The main condition - the equal number.

Assume you own a small grocery and spend 2000 dollars every day. Obviously, if your revenue per day is less than 2000, then you suffer losses, otherwise - nothing bad. Let's try to write if/else statement for that.

Task

Write conditional statement checking if variable revenue is less than 2000 (in that case print message "We suffer losses!", otherwise - print "Everything is ok!"). Do not change value of revenue.

Task

Write conditional statement checking if variable revenue is less than 2000 (in that case print message "We suffer losses!", otherwise - print "Everything is ok!"). Do not change value of revenue.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 4. Chapter 3
toggle bottom row

Simple if/else expressions (1/2)

Okay, now we are familiar with conditional operators and know how to deal with them. It means now we can divide our code into pieces depend on the condition. In Python, its implemented like the follows:

1234
if condition: do if true else do if not true
copy

For example, remember our primitive example about deciding whether to go walk or not. We can write it in Python syntax like that:

1234
if (weather is good and (day is Sunday or day is Saturday)): go walk else: stay home :(
copy

Consider some examples: imagine you have some string and want to check if it's wide. Let's call string wide if it has more than 20 symbols.

1234567891011121314151617
# assign some variable test = "small string" # conditional statement if(len(test) > 20): print("This string is wide!") else: print("Nothing special") # check on different string test = "This string is very-very and very large" # conditional statement if(len(test) > 20): print("This string is wide!") else: print("Nothing special")
copy

Please note, that code blocks written below if, else statements need to be indented on the same level (2 spaces, 3, 4, ...). The main condition - the equal number.

Assume you own a small grocery and spend 2000 dollars every day. Obviously, if your revenue per day is less than 2000, then you suffer losses, otherwise - nothing bad. Let's try to write if/else statement for that.

Task

Write conditional statement checking if variable revenue is less than 2000 (in that case print message "We suffer losses!", otherwise - print "Everything is ok!"). Do not change value of revenue.

Task

Write conditional statement checking if variable revenue is less than 2000 (in that case print message "We suffer losses!", otherwise - print "Everything is ok!"). Do not change value of revenue.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Okay, now we are familiar with conditional operators and know how to deal with them. It means now we can divide our code into pieces depend on the condition. In Python, its implemented like the follows:

1234
if condition: do if true else do if not true
copy

For example, remember our primitive example about deciding whether to go walk or not. We can write it in Python syntax like that:

1234
if (weather is good and (day is Sunday or day is Saturday)): go walk else: stay home :(
copy

Consider some examples: imagine you have some string and want to check if it's wide. Let's call string wide if it has more than 20 symbols.

1234567891011121314151617
# assign some variable test = "small string" # conditional statement if(len(test) > 20): print("This string is wide!") else: print("Nothing special") # check on different string test = "This string is very-very and very large" # conditional statement if(len(test) > 20): print("This string is wide!") else: print("Nothing special")
copy

Please note, that code blocks written below if, else statements need to be indented on the same level (2 spaces, 3, 4, ...). The main condition - the equal number.

Assume you own a small grocery and spend 2000 dollars every day. Obviously, if your revenue per day is less than 2000, then you suffer losses, otherwise - nothing bad. Let's try to write if/else statement for that.

Task

Write conditional statement checking if variable revenue is less than 2000 (in that case print message "We suffer losses!", otherwise - print "Everything is ok!"). Do not change value of revenue.

Switch to desktop for real-world practiceContinue from where you are using one of the options below
Section 4. Chapter 3
Switch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt