Pacific Beach Drive

Mike's Drive.

Follow me on GitHub

Strings || Variables || Lists || Tuples || Dictionary || Control || Function|| Files || Exceptions || while loop || for loop OOP || Algorithm ||Data Structure ||back ***

Conditional

Syntax

if <expression-boolean>:
    <statement>
if x < y:                           
if x or y:                           
if x and y:                           
if 'aul' in 'grault': 
if 'quux' in ['foo', 'bar', 'baz']:
    print('yes')

Equality (equal to each other)

print("cat" == "dog")
False
print("cat" > "Cat")
# OUTPUT
True

=> uppercase letters are alphabetically sorted before lowercase letters.

print(1<"1")
# OUTPUT
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    print(1<"1")
TypeError: '<' not supported between instances of 'int' and 'str'

print(1 == "1")
False

if-else conditional

Syntax

	if <expr>:
    		<statement(s)>
	else:
    		<statement(s)>
if <expr>:
    <statement(s)>
elif <expr>:
    <statement(s)>
elif <expr>:
    <statement(s)>
    ...
else:
    <statement(s)>
def hint_username(username):
	if len(username) < 3:
		print("Invalid username")
	elif len(username) > 15:
		print("Invalid username - 15 char minimum, dude.")
		
	else:
		print("FullblownValid")

def number_group(number):
  if number > 0:
    return "Positive"
  elif number < 0:
    return "negative"
  else:
    return "Zero"

print(number_group(10)) # Positive
print(number_group(0)) # Zero
print(number_group(-5)) #Should be Negative

One-Line if Statements || Ternary Operator (Short Hand If)

#standard
if <expr>:
    <statement>

# shorter
if <expr>: <statement>
if <expr>: <statement_1>; <statement_2>; <statement_n>

Example

if 'f' in 'foo': print('1'); print('2'); print('3')
#Output
1
2
3

if-else short hand

Syntax

<expr1> if <conditional_expr> else <expr2>

Example

'yes' if ('qux' in ['foo', 'bar', 'baz']) else 'no'
#OUTPUT
'no'
>>> raining = False
>>> print("Let's go to the", 'beach' if not raining else 'library')
Let's go to the beach
>>> raining = True
>>> print("Let's go to the", 'beach' if not raining else 'library')
Let's go to the library

short hand in a function

def main():
	x,y = 10,8
	st = "x is less than y" if (x < y) else "x is greater than or equal to y"
	print(st)
	
if __name__ == "__main__":
	main()

#OUPUT

if a > b:
...     m = a
... else:
...     m = b
#becomes
>>> m = a if a > b else b

Boolean

print(10>1)
True