0
How to use if statement in Python
I NEED HELP
3 Answers
+ 2
first, you write if:
if
then, you write a condition. here is an example:
x = 5
if x == 5
after that put a colon:
x = 5
if x == 5:
then press enter to go to a new line. make sure that that new line is indented. then type what you want to happen. here is another example:
x = 5
if x == 5:
print("Yay!")
+ 1
Note that the condition can be any *expression*, and that None is evaluated to False, while any non-zero value is evaluated to True.
For example:
if 5:
print("Hello!")
is completely valid and would print out "Hello!"
num = True
if num:
print("I ran!")
is also valid.
Lastly, this is also valid:
my_list = [] # Empty lists are evaluated False
if not my_list:
print("The list is empty!")
0
Thanks