How to use if statement in Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to use if statement in Python

I NEED HELP

26th Jun 2022, 8:22 PM
Moe Williams
Moe Williams - avatar
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!")
26th Jun 2022, 8:37 PM
Cubsicle
+ 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!")
26th Jun 2022, 9:06 PM
EricMcDerrick
EricMcDerrick - avatar
0
Thanks
26th Jun 2022, 8:38 PM
Moe Williams
Moe Williams - avatar