Contenido del Curso
Learn Python from Scratch
Learn Python from Scratch
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:
if condition: do if true else do if not true
For example, remember our primitive example about deciding whether to go walk or not. We can write it in Python syntax like that:
if (weather is good and (day is Sunday or day is Saturday)): go walk else: stay home :(
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.
# 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")
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.
Tarea
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
.
¡Gracias por tus comentarios!
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:
if condition: do if true else do if not true
For example, remember our primitive example about deciding whether to go walk or not. We can write it in Python syntax like that:
if (weather is good and (day is Sunday or day is Saturday)): go walk else: stay home :(
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.
# 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")
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.
Tarea
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
.
¡Gracias por tus comentarios!
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:
if condition: do if true else do if not true
For example, remember our primitive example about deciding whether to go walk or not. We can write it in Python syntax like that:
if (weather is good and (day is Sunday or day is Saturday)): go walk else: stay home :(
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.
# 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")
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.
Tarea
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
.
¡Gracias por tus comentarios!
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:
if condition: do if true else do if not true
For example, remember our primitive example about deciding whether to go walk or not. We can write it in Python syntax like that:
if (weather is good and (day is Sunday or day is Saturday)): go walk else: stay home :(
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.
# 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")
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.
Tarea
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
.