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

Kursusindhold

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

book
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.

Opgave

Swipe to start coding

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.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 3
toggle bottom row

book
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.

Opgave

Swipe to start coding

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.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 3
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt