The second part of the intro to Python covering if logic, loops, & functions.
(Part 1 of this series is here)
# Comparison Operators ''' == equal to (Note: DOUBLE equals sign) != not equal to > greater than < less than >= greater than or equal to <= less than or equal to ''' # If Elif Else x, y = 1, 2 if (x == y): print('Match!') if (x == y): print ('Match') else: print ('No Match') x, y = 25, 5 * 5 if x > y: print ('X Greater') elif (x < y): print ('Y Greater') else: print ('Equal') # Loops # while loops x = 0 while x < 10: print(x) x += 1 # Watch out for never ending loops! # Ctrl+C will 'break' a loop y = 9 while y: print 'Rah!' # for loop # For loops are much more useful than while loops (imho) # Loop over letters in a string for letter in 'Hello World': print('Current letter:', letter) # Loop over a range for num in range(0, 10): print(num) # Loop over a list (or tuple) fruits = ['banana', 'apple', 'orange', 'mango'] for fruit in fruits: print('Yum a', fruit) # Mix in a bit of logic fruits = ['banana', 'apple', 'orange', 'mango'] for fruit in fruits: firstLetter = fruit[0] if (firstLetter == 'a' or firstLetter == 'o'): print('Yum an', fruit) else: print('Yum a', fruit) # Make it bullet proof! vowels = 'a', 'e', 'i', 'o', 'u' fruits = ['banana', 'apple', 'orange', 'mango', 'ugli fruit'] for fruit in fruits: firstLetter = fruit[0].lower() if (firstLetter in vowels): print('Yum an', fruit) else: print('Yum a', fruit) # Mix it up for num in range(10, 20): # to iterate between 10 to 20 for i in range(2, num): # to iterate on the factors of the number if num % i == 0: # to determine the first factor j = num / i # to calculate the second factor print('{} equals {} * {}').format(num, i, j) break # to move to the next number, the #first FOR else: # else part of the loop print(num, 'is a prime number') # More loop control # break # breaks out of a loop while x < 10: print(x) if (x == 5): break x += 1 # continue # skips to next iteration of the loop for letter in 'Hello World': if letter.lower() == 'l': continue print('Current letter:', letter) # FIZZBUZZ! # Write a program that counts from 1 - 100 # If the number is divisable by 3 print Fizz # If the number is divisible by 5 print Buzz # If the number is divisible by 3 AND 5 print FizzBuzz # Or just print the number # (Hint: You'll need to modulous operator %, loops and if elif else) # Functions (vs. methods) # functions are reusbale blocks of code # we've alrady used a few built in functions print('Print is a function!') # But we can write (define) our own! def myFirstFunction(): print('Hello Reusable Code') # Nothing happens :( myFirstFunction() # Winner! # Functions take "parameters" def printProductOfTwoNumbers(x, y): print(x * y) printProductOfTwoNumbers(10, 5) printProductOfTwoNumbers(1342, 76) # And functions can return variables def getProductOfTwoNumbers(x, y): return x * y getProductOfTwoNumbers(10, 5) # Nothing printed x = getProductOfTwoNumbers(10, 5) # That's more like it! print(x) # Data inside a function is isolated total = 0 def sumValues(x, y): total = x + y print('Total inside the function is', total) return total sumValues(4, 6) print('Total outside the function is', total) # Functions can call other functions def sumAndMultiply(x, y): print('Sum of values = ', sumValues(x, y)) print('Product of values = '. getProductOfTwoNumbers(x, y)) return # mind your naming! ''' def print(string): print(string) print('Hello World') This would error badly! '''