Text Size
Text Size
Exit Full Screen
Python3 Editor
Show
Source Code
View Source Code in Full Screen
# boolean values can be set to True or False isRaining = True isSunny = False # Spacing is really important in Python. If a line ends with a colon, it is a "block" of code. # All code that goes inside that block, must start with 1 tab (or 4 spaces) from the start of the line # defining that block. In this case, it would need 4 spaces from the start of the if-statement. # This spacing is given to you automatically, when you press Enter after typing a colon in Python3 Editor. # The format for a condition statement in Python is: # if condition: # code that runs when condition is True # you can check if a variable is True by just writing its name if isRaining: print("It's raining") # elif means "otherwise if". It must follow an if-statement and cannot stand on its own. # An elif statement is only checked if all if and elif-statements before it are evaluated as False. elif isSunny: print("It's sunny") # else means "otherwise". It will run the code within only when all if and elif-statements that come before it in the chain # are evaluated as False. else: print("It's cloudy out.") # 'and' and 'or' are written using the full word in Python. The conditions on both sides must be FULL conditions. # These keywords also have "short-circuiting". # Since A and B is True if and only if both are True, then if the first condition is False, it will NOT check the second condition. # Since A or B is False if and only if both are False, then if the first condition is True, it will NOT check the second condition. if isRaining and isSunny: print("Where's the rainbow?") else: print("Aww ... no rainbows ...") # The keyword 'not' is used to check if the condition that follows is False. if not isRaining and not isSunny: print("It MUST be cloudy") # The condition below is logically equivalent to the one on line 38. if not (isRaining or isSunny): print("It MUST be cloudy") print() #----------------------------------------------------- # Comparing Numbers #----------------------------------------------------- # > greater than # < less than # >= greater than OR equal to # <= less than OR equal to # == equals # != not equals x = 5 y = 10 if x > 5: print("x > 5") elif x <= 5 and y > 8: print("x <= 5 and y > 8") elif y == 10 and x == y/2: print("y == 10 and x == y/2") # The cool thing about Python and comparing numbers is that, if you are checking if a value is in a specific range, # you can use one line without an 'and' unlike other programming langauges. Remember: this does NOT work in Java. if 5 <= x <= 8: # checks if x >= 5 AND x <= 8 print('5 <= x <= 8') print() #------------------------------------------------------ # Comparing Strings #------------------------------------------------------ # == equals # != not equals # not s == also not equals s = "Hello" if s == "Hello": print(s) elif not s == "Hello": # could also write it as: elif s != 'Hello' print("Goodbye!") print() #--------------------------------------------------------- # Be careful with 'and' and 'or' #--------------------------------------------------------- # When you use 'and' and 'or', you need to have a FULL condition on both sides of the keyword. s = 'Cube' # BAD EXAMPLE: This will always trigger the if-statement to be true, since the first condition is checking if # s == 'Rock', but the second condition just checks if 'Paper' is not None. It's a string literal so it's not None. # Since there is one true condition in the statement combined with 'or', the whole statement is true, so it reads as VALID. if s == 'Rock' or 'Paper' or 'Scissors': print("Valid") else: print("Invalid") s = 'Rock' # CORRECT EXAMPLE: if s == 'Rock' or s == 'Paper' or s == 'Scissors': print('Valid') else: print("Invalid") # Alternatively, if you are using or to check a value, you can check if the value is in a list like this: if s in ['Rock', 'Paper', 'Scissors']: print("Valid input") else: print("Invalid input") print() #-------------------------------------------------------- # None and the is keyword #-------------------------------------------------------- # The keyword 'is' should only be used to check EXISTENCE, or to check if two values are pointing to the same location in memory. # It SHOULD NOT be used to check if two values are equal a = ['Bob', 5, 'Hello'] b = ['Bob', 5, 'Hello'] c = a # Since c = a, they are pointing to the same spot in memory, so the if-statement is false, but the elif is true, # and it will print out that 'a is c'. if a is b: print('a is b') elif a is c: print('a is c') d = None # Since d is None, it will go to the else statement. # You could switch these two conditions by changing the if-statement to check if d is None, # then swap the print statements. if d: print("d has a value") else: print("d does not have a value") print() #--------------------------------------------------------------------------- # REVIEW #--------------------------------------------------------------------------- # Any of the functions we went over in previous classes that return booleans (True or False) values, # can be used in an if-statement. s = input("Enter something: ") if s.isalpha(): print("All letters.") if s.isdigit(): print("All digits.") if s.isalnum(): print("Has letters and/or digits.") if s.isspace(): print("All spaces.") if s.islower(): print("All lowercase.") if s.isupper(): print("All uppercase.") # The same goes with collections of data and the use of the keyword 'in'. d = { 'x': 200, 'y': 150, 'color': 'blue' } if 'color' in d: print(d['color']) # the following if-statement prints nothing since 'name' is not in the dictionary d if 'name' in d: print(d['name'])
CS110_L4_Notes.py
( around 146 lines Python3 code )
Published By:
Mentor Sabrina
Created on
2020-11-02T14:19:28Z
Python3 Editor
- an
OYOclass
application,
own your own class today
.
Run
Result
Status:
Program is not running