#!/usr/bin/env python3 """ A 'Hello world! program written in Python. """ print("Hello World!")
if
statement
if boolean_expression_1: # suite 1 elif boolean_expression_2: # optional # suite 2 # ... elif boolean_expression_N: # optional # suite N else: # optional # else suite
#!/usr/bin/env python3 LANG = 'en' # ... if LANG == 'en': output = "Good Morning!" elif LANG == 'de': output = "Guten Morgen!" else: output = "Bonjour!" print(output)
expression1 if boolean else expression2
#!/usr/bin/env python3 LANG = 'en' # ... output = "Good Morning!" if LANG == 'en' else "Guten Morgen!" print(output)
try: # try suite except exception_name1 as variable1: # except suite 1 except exception_name2 as variable2: # except suite 2
#!/usr/bin/env python3 length = 3 rectangle_area = 12 # ... try: width = rectangle_area / length except ZeroDivisionError as err: print("The length of a rectangle must not be 0") # deal with problem or re-raise the exception raise print(width)
while condition: # statements
#!/usr/bin/env python3 amount = 3000 # initial investment in euro GOAL = 4000 INTEREST = 0.03 # 3% years = 0 while amount < GOAL: years += 1 amount = amount * (1 + INTEREST) print(f"you'll reach your investment goal after {years} years")
if/ else
brancheswhile
loops