IF a = 10 b = 100 if a > b: print("a {0} is greater than b {1}".format(a, b)) IF .. ELIF c = 20 if a > b: print("a {0} is greater than b {1}".format(a, b)) elif b > c: print("b {0} is greater than c {1}".format(b, c)) IF .. ELSE if a > b: print("a {0} is greater than b {1}".format(a, b)) else: print("Whatever") IF with no statement … err b = 2 if c > b: print("c {0} is greater than b {1}".
Functions simple function def say_hello_world(): """Prints hello world""" print("Hello World") calling function
say_hello_world() simple function with parameter def say_hello(someone): """Says Hello to someone""" print(f"Hello {someone}") calling function
say_hello("Friends") simple function with multiple parameters def say_hello_and_more(someone, and_more): """Says Hello to some and append more text""" print(f"Hello {someone}, {and_more}") calling function
say_hello_and_more("Friends", "GreatDay!") simple function with arbitrary parameters def say_hello_to_many(*people): """Says hello to many people :people: names in tuple """ for name in people: print(f"Hello {name}") calling function
Collection types List
fruits_list = ["apple", "banana", "cherry"] Tuple
fruits_tuple = ("apple", "banana", "cherry") Set
fruits_set = {"apple", "banana", "cherry"} Printing elements of lists, tuple and sets are similar
print("Print Fruits list") for x in fruits_list: print(x) print("") print("Print Fruits tuple") for x in fruits_tuple: print(x) print("\n\nPrint Fruits Set") for x in fruits_set: print(x) But, they are on similar
print("But is list is equal to tuple {0}", \ "yes" if fruits_list == fruits_tuple else "no") print("\n\ngood lord.
#1. loop through an array
fruits = ["apple", "banana", "cherry", "dragon fruit"] print("Example 1: Printing Fruits") for x in fruits: print(x) Notice there is no begin and end blocks! Indenting implies a block has begun
#2. Loop through a string
a_fruit = "banana" print("Example 2: Banana in loop") for x in a_fruit: print(x) #3. Break a loop
print("Example 3: Break example") print("Fruit Array has these values {0}\n\n".format(fruits)) #^^^^ string interpolation print("If we are going to break at banana we only"\ "should be printing an apple\n") #^^^^ statements that span across multiple lines have backslash for x in fruits: if x == "banana": break print(x) #4 For i = 1 to 10