Daily set: three random terms.
-
What this creature is
An if statement runs a block of code only when a condition is true. You can add elif for other tests and else for a default.
Why you will regret ignoring it
Programs need to choose what to do. if lets you branch based on values, user input, or errors.
Example
score = 80
if score >= 50:
print("Pass")
else:
print("Review")
Common disgrace
Forgetting the colon (:) after if / elif / else, or not indenting the block under the condition — both cause syntax errors.
Trial by code
Write an if/else that prints one message if a number is greater than 0 and another if it is not.
-
What this creature is
A function is a named block of code you define once and call by name. It can take parameters and return a value with return.
Why you will regret ignoring it
Functions organize logic, avoid repetition, and make programs easier to test and fix.
Example
def greet(name):
return "Hello, " + name
print(greet("visitor"))
Common disgrace
Calling a function before Python has read its def, or using print when you meant to return a value to use later.
Trial by code
Define a function that doubles a number and returns it. Call it with 21 and print the result.
-
What this creature is
print() writes values to standard output (usually the terminal) as text. It adds a newline at the end unless you change end.
Why you will regret ignoring it
Printing is the simplest way to see what your program is doing while you learn and debug.
Example
print("Done.")
print(42, "items", sep=" | ")
Common disgrace
Printing inside a tight loop and producing so much output that you cannot find the line you care about.
Trial by code
Print three values on one line using print with sep and end set to values you choose.