Python 3 == and if-else | Sololearn: Learn to code for FREE!

+2

Python 3 == and if-else

Hello everybody I have been really confused on what the == sign does and im confused on the if statements and the else statements.

7/28/2019 9:04:56 PM

Learning Code

3 Answers

New Answer

+6

the == sign is a comparator. It returns a boolean that determines if two values are equal. 3 == 3 true 1 == 1 true "hi" == "hi" true 'q' == 'h' false 3 == '3' false if checks a condition (condition returns a boolean) and if that condition is true, it executes the code block, else runs if the if statement was not true if (i need food) go to the store else eat all the food /////// if (time == 3:00) sleep else play ///////// money=5 if money == 6 buy(); else print("not enough")

+5

the == tests if two 'things' (numbers, Strings, ...) are the same. When they are equal the result is True, when not the result is False. example: x = 3 y = (x == 3) print(y) z = (x == 2) print(z) In if-statments the indented block of code gets executed when the expretion is True. When it's False the else-block is executed, if there is one ...: if True: print("nice") if False: print("0") else: print("1") The two can be combined: x = 3 if x == 3: print("nice") else: print("math doesnt work!") hope this helped(/s) happy coding! 😄

+1

Anton Bohler ..well explained