Slide 1

Slide 1 text

Intro to Python for Non-Programmers Ahmad Alhour / Team Lead, TrustYou 23 April, 2019

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Overview ● A very basic intro to programming ● Intended for people who have no clue about it ● Mainly, to give you a taste of what it is about ● Hopefully, it’ll put you on the right track to study on your own 3 aalhour.com

Slide 4

Slide 4 text

Hello! I am Ahmad I have been doing programming as a hobby since I was a kid, and professionally since 2008. I also have a B.Sc. degree in Computer Science. At TrustYou, I lead several projects, all powered by Python! 4

Slide 5

Slide 5 text

15 mins Intro and Setting Expectations 30 mins What is Programming? 10 mins Intro to REPL.it 45 mins Numbers, Variables and Strings 30 mins Functions (Part 1) Plan Until Lunch

Slide 6

Slide 6 text

15 mins Recap and Review 45 mins Conditionals 45 mins While Loops and Collections 45 mins For Loops and Functions (Part 2) 30 mins Wrapping Up Plan After Lunch

Slide 7

Slide 7 text

1. What Are Your Expectations? 7 aalhour.com

Slide 8

Slide 8 text

“ “Blessed is he who expects nothing, for he shall never be disappointed.” ― Alexander Pope 8 aalhour.com

Slide 9

Slide 9 text

Group Exercise: Let’s Set Expectations Together ● Learn the basics of Programming ● Learn the basics of Python ● There will be no installation 9 aalhour.com

Slide 10

Slide 10 text

2. What is Programming? 10 aalhour.com

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

12 Programming 101 ● Is a way of telling the computer how to do certain things by giving it a set of instructions ● These instructions are called Programs ● Everything that a computer does, is done using a computer program aalhour.com

Slide 13

Slide 13 text

Example Programs 13 aalhour.com

Slide 14

Slide 14 text

3. What is a Programmer? 14 aalhour.com

Slide 15

Slide 15 text

15 What is a Programmer? “A programmer is an Earthling who can turn big amounts of caffeine and pizza into code!” ― Anonymous aalhour.com

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

17 Seriously, what is a programmer? ● A person who writes instructions is a computer programmer ● These instructions come in different languages ● These languages are called computer languages aalhour.com

Slide 18

Slide 18 text

4. What is a Programming Language? 18 aalhour.com

Slide 19

Slide 19 text

19 What is a Programming Language? ● A programming language is a type of written language that tells computers what to do ● They are used to make all the computer programs ● A P.L. is like a set of instructions that the computer follows to do something aalhour.com

Slide 20

Slide 20 text

20 Machine Language ● The computer’s native language ● The set of instructions have to be coded in 1s and 0s in order for the computer to follow them ● Writing programs in binary (1s and 0s) is tedious ● Lowest level language aalhour.com

Slide 21

Slide 21 text

21 High-level Languages ● Programming languages that are closer to English than Machine code ● Human-readable ● Expresses more by writing less aalhour.com

Slide 22

Slide 22 text

5. Example Programs 22 aalhour.com

Slide 23

Slide 23 text

23 Displaying “Hello, World!” in Machine Code 0110100001100101011011 0001101100011011110010 0000011101110110111101 1100100110110001100100

Slide 24

Slide 24 text

24 Displaying “Hello, World!” in Assembly global _main extern _GetStdHandle@4 extern _WriteFile@20 extern _ExitProcess@4 section .text _main: ; DWORD bytes; mov ebp, esp sub esp, 4 ; hStdOut = GetstdHandle( STD_OUTPUT_HANDLE) push -11 call _GetStdHandle@4 mov ebx, eax ; WriteFile( hstdOut, message, length(message), &bytes, 0); push 0 lea eax, [ebp-4] push eax push (message_end - message) push message push ebx call _WriteFile@20 ; ExitProcess(0) push 0 call _ExitProcess@4 ; never here hlt message: db 'Hello, World', 10 message_end:

Slide 25

Slide 25 text

25 #include int main(void) { printf("Hello, World!\n"); return 0; } Displaying “Hello, World!” in C

Slide 26

Slide 26 text

26 print(“Hello, World!”) Displaying “Hello, World!” in Python

Slide 27

Slide 27 text

6. Enter: Python! 27 aalhour.com

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

29 The Python Programming Language ● High-level programming language ● Named after BBC’s "Monty Python’s Flying Circus" show ● One of the most adopted languages world-wide ● Most TY products are powered by it aalhour.com

Slide 30

Slide 30 text

30 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for each_number in numbers: print(each_number) Example Python Program: Counting to 10

Slide 31

Slide 31 text

Let’s take a break!

Slide 32

Slide 32 text

7. Let’s REPL.IT 32 aalhour.com

Slide 33

Slide 33 text

33 Demo Time! ● General walk through REPL.IT aalhour.com

Slide 34

Slide 34 text

8. Hello, World! Your First Python Program 34 aalhour.com

Slide 35

Slide 35 text

35 Numbers in Python ● Create a new repl on REPL.IT ● Type in the main.py tab: print(“hello, world!”) ● Click on run >! aalhour.com

Slide 36

Slide 36 text

9. Numbers The Art of Calculating Stuff 36 aalhour.com

Slide 37

Slide 37 text

37 Numbers in Python ● One of value types ● Demo: ○ Numbers in interactive mode ○ Basic arithmetic aalhour.com

Slide 38

Slide 38 text

38 Evaluation Order of Operations ● First, “*” and “/” are evaluated ● Then, “-” and “+” are evaluated ● All operations are evaluated left-to-right aalhour.com

Slide 39

Slide 39 text

39 Guess the result of the following: ● 9 * 9 - 9 * 10 / 15 ● 1 + 1 + 1 * 2 + 6 / 2 ● 10 + 1 * 2 - 4 / 20 ● 50 - 5 - 5 - 10 / 2 ● 1 + 2 - 5 * 3 - 7 + 4 * 9 / 12 aalhour.com

Slide 40

Slide 40 text

40 Grouping Calculations with Parentheses ● Used for separating calculations into distinct groups ● For example: ○ (10 * 2) + (5 - 3) / (4 + 2) ○ → 20 + 2 / 6 ○ → 20 + 0.333 ○ → 20.333 aalhour.com

Slide 41

Slide 41 text

41 Evaluation Order of Operations: Revisited! ● First, what is in “(” and “)” is evaluated ● Then, “*” and “/” are evaluated ● Then, “-” and “+” are evaluated ● All operations are evaluated left-to-right aalhour.com

Slide 42

Slide 42 text

42 Guess the result of the following: ● 9 * (9 - 9) * 10 / 15 ● (1 + 1 + 1) * (2 + 6) / 2 ● 10 + 1 * (2 - 4) / 20 ● 50 - 5 - (5 - 10) / 2 ● (1 + 2) - 5 * (3 - 7 + 4) * 9 / 12 aalhour.com

Slide 43

Slide 43 text

10. Variables The Art of Naming Stuff 43 aalhour.com

Slide 44

Slide 44 text

44 Variables in Python ● Used to give names to values ● You give names to stuff to refer to them later on in your program ● You give names to stuff using the “=” symbol, known as “assignment operator” ● Variable names must begin with a letter aalhour.com

Slide 45

Slide 45 text

45 ● Implement one plus one operations using variables Demo Time aalhour.com

Slide 46

Slide 46 text

46 Example usage: ● Calculate the average population for Earth in 2015 ● Give it a name ● Everytime you want to refer to it you just use the name rather than to recalculate it again Variables in Python aalhour.com

Slide 47

Slide 47 text

47 Break the calculation down using variables: ● (10 * 2) + (5 - 3) / (4 + 2) ● X + Y / Z ● Store result in “result” name Demo Time aalhour.com

Slide 48

Slide 48 text

48 Break the following calculations down into sub-calculations and store them into variables: ● 9 * (9 - 9) * 10 / 15 ● (1 + 1 + 1) * (2 + 6) / 2 ● 10 + 1 * (2 - 4) / 20 ● 50 - 5 - (5 - 10) / 2 ● (1 + 2) - 5 * (3 - 7 + 4) * 9 / 12 Exercise aalhour.com

Slide 49

Slide 49 text

11. Strings The Art of Texting Stuff 49 aalhour.com

Slide 50

Slide 50 text

50 ● Used to represent text in Python ● Examples: ○ “Hello, World!” ○ “My name is Ahmad.” ○ “Bier und Brezel.” ● Any text in between “” is a string in Python Strings in Python aalhour.com

Slide 51

Slide 51 text

51 ● Type in the following strings in the interactive shell: ○ “Hello, World!” ○ “My name is Ahmad.” ○ “Bier und Brezel.” ● Use all of the above with print ● Store all of the above in variables and then print them Demo Time aalhour.com

Slide 52

Slide 52 text

52 Print the following poem: Doubt thou the stars are fire; Doubt that the sun doth move; Doubt truth to be a liar; But never doubt I love. ― William Shakespeare, Hamlet Exercise aalhour.com

Slide 53

Slide 53 text

Let’s take a break!

Slide 54

Slide 54 text

54 12. Functions The Art of Naming Computations aalhour.com

Slide 55

Slide 55 text

55 ● Just like how variables give name to values, functions give name to calculations/instructions ● It is easier to refer to a group of instructions with a name than write them all over again ● You need three main words to define functions: “def”, “” and “return” Functions in Python aalhour.com

Slide 56

Slide 56 text

56 ● You know functions already! ● Print is a Python function that takes whatever value you’d like and prints it on the screen ● Print can take several values separated by commas, e.g.: ○ print(“My name is:”, “Slim Shady”) ○ print(“One plus one is:”, 2) Functions in Python aalhour.com

Slide 57

Slide 57 text

57 ● Define a new function ● Print William Shakespeare’s poem ● repl.it/@aalhour/WilliamShakespeare Demo Time aalhour.com

Slide 58

Slide 58 text

58 ● Define a new function ● Code the one plus one example in variables ● Return the result Demo Time aalhour.com

Slide 59

Slide 59 text

59 ● Define a function to calculate each of the following, with variables, and return the result: ○ 9 * (9 - 9) * 10 / 15 ○ (1 + 1 + 1) * (2 + 6) / 2 ○ 10 + 1 * (2 - 4) / 20 ○ 50 - 5 - (5 - 10) / 2 ○ (1 + 2) - 5 * (3 - 7 + 4) * 9 / 12 Exercise aalhour.com

Slide 60

Slide 60 text

60 ● Functions can take values as arguments! ● Useful for doing tasks with user input, for example: ○ Given 5 numbers, return the avg. ○ Given weight and height of a person, calculate their BMI. Functions in Python aalhour.com

Slide 61

Slide 61 text

61 ● Define a new function for printing different values ● Define a new function for calculating BMI given height and weight ● repl.it/@aalhour/FuncsWithVars Demo Time aalhour.com

Slide 62

Slide 62 text

62 ● Define a function that accepts two arguments and returns the result of their addition ● Define a function that takes five arguments and returns the average Exercise aalhour.com

Slide 63

Slide 63 text

Lunch Break See you in an hour!

Slide 64

Slide 64 text

15 mins Recap and Review 45 mins Conditionals 45 mins While Loops and Collections 45 mins For Loops and Functions (again!) 30 mins Wrapping Up Plan After Lunch

Slide 65

Slide 65 text

13. Recap What Have We Learned So Far? 65 aalhour.com

Slide 66

Slide 66 text

66 ● Numbers: 1, 2, 3 ● Order of operations: (, ), *, /, +, - ● Strings: “Hi!” ● Variables: x = 1 ● Functions: def greet(): print(“Hello, world!”) Recap aalhour.com

Slide 67

Slide 67 text

14. Conditionals The Art of Comparing Stuff 67 aalhour.com

Slide 68

Slide 68 text

68 ● They are the Yes/No questions of programming ● The answer is either a True or a False ● These values are called Booleans ● Conditionals compare values together ● Using simple operators such as: ○ >, <, >=, <= and == Conditionals aalhour.com

Slide 69

Slide 69 text

69 Compare several numbers together and check the results: ● 1 < 10 ● 1 <= 10 ● 5 > 2 ● 5 >= 2 ● 1 == 1 ● 0 <= 0 Demo Time aalhour.com

Slide 70

Slide 70 text

70 Evaluate the following in your mind and then on REPL.IT: ● 1 + 1 + 1 + 1 + 1 > 4 ● 2 * 2 < 5 / 3 ● 5 * 3 >= 15 ● 10 / 2 == 5 ● (25 * 3) - 100 / 2 < (25 * 2) + 1 Exercise aalhour.com

Slide 71

Slide 71 text

71 ● First, what is in “(” and “)” is evaluated ● Then, “*” and “/” are evaluated ● Then, “-” and “+” are evaluated ● Lastly, comparisons are evaluated ○ “>”, “>=”, “<”, “<=”, “==” ● Operations get evaluated left-to-right Evaluation Order of Operations: Expanded aalhour.com

Slide 72

Slide 72 text

72 ● A Python statement for making decisions based on a condition ● In a nutshell: if this, then that ● For example: If your age is greater than 18, then you are allowed to enter the bar The If Statement aalhour.com

Slide 73

Slide 73 text

73 ● If old enough to enter bar, print “proceed to bar!”. ● If not old enough to enter bar, print “too young still!” Demo Time aalhour.com

Slide 74

Slide 74 text

74 Teenager Exercise 01: Define an “age” variable and insert your age. Write an if statement to display “teenager” if age is less than 21 Exercises aalhour.com

Slide 75

Slide 75 text

75 Exercises Positive Number 01: Define a “number” variable and insert a random value. Write an if statement to display “positive” if the number is greater than 0 aalhour.com

Slide 76

Slide 76 text

76 ● Expanded If-Statement for handling cases that don’t match the condition. ● In a nutshell: if this, then that; otherwise, something else ● For example: ○ If your age is greater than 18, then you are allowed to enter the bar; otherwise, you are asked to leave The If-Else Statement aalhour.com

Slide 77

Slide 77 text

77 Translate the following into Python: If old enough to enter bar then print “proceed to bar!” else print “please, leave!” Demo Time aalhour.com

Slide 78

Slide 78 text

78 ● Teenager Exercise 02: Expand the code and add an else block which prints “adult” if the teenager check is not met ● Positive Number 02: Expand the code and add an else block which prints “negative” if the positive check is not met Exercises aalhour.com

Slide 79

Slide 79 text

79 ● Teenager Exercise 03: Wrap the exercise’s code in a function that accepts an “age” argument and returns nothing. ● Positive Number 03: Wrap the exercise’s code in a function that accepts a “number” argument and returns nothing. Exercises aalhour.com

Slide 80

Slide 80 text

Let’s take a break!

Slide 81

Slide 81 text

15. While Loops The Art of Repeating Stuff 81 aalhour.com

Slide 82

Slide 82 text

82 ● Loops are good for repeating instructions for a number of times ● They’re good because we don’t have to duplicate code, but we just tell the program to keep repeating stuff ● Loops repeat stuff according to a condition as well While Loops aalhour.com

Slide 83

Slide 83 text

83 ● Example: ○ While my BMI is above 100, keep working out! ○ While my coffee is not empty, keep writing code! ● You need two words to loop stuff: ○ “while” and “break” ○ Break is used to stop looping or else you will loop forever! While Loops aalhour.com

Slide 84

Slide 84 text

84 while True: print(1) # Prints 1 forever while 1 == 1: print(1) # Prints 1 forever while 5 > 2: print(1) # Prints 1 forever Code Examples aalhour.com

Slide 85

Slide 85 text

85 # Prints 1 once, then stops while True: print(1) break # Prints 1 once, then stops while 1 == 1: print(1) break Code Examples aalhour.com

Slide 86

Slide 86 text

86 ● Print “Hello, World!” forever! ● Print your own name forever! ● Calculate the following formula and print the result forever: ○ 10 * 312 / 57 + 23 * 5 + 823 / 74 Play Time aalhour.com

Slide 87

Slide 87 text

87 ● To avoid running forever, we need to maintain a counter and a limit ● Counters are variables that keep track of how many times a loop ran ● Limits allow us to break out of the loop once the loop runs for a max. number of times Counters and Limits aalhour.com

Slide 88

Slide 88 text

88 counter = 1 while counter < 10: print(counter) counter = counter + 1 Code Example aalhour.com

Slide 89

Slide 89 text

89 ● Add a counter number and increment it inside the loop. Print the counter to show that its value changes. ● Add a limit check before the work with a break. Demo Time aalhour.com

Slide 90

Slide 90 text

90 ● Print numbers from 0 until 1 million ● Teenager Exercise 04: Define age variable and assign it to 1. Run the loop with the teenager check. The loop should print the age and then increment it until the age is no longer teeanger, after which the loop should terminate Exercises aalhour.com

Slide 91

Slide 91 text

91 ● You can ask for the user’s input using the built-in “input” function ● For example: input(“Enter a number:”) ● Useful for asking the user for data. User Input aalhour.com

Slide 92

Slide 92 text

92 ● Ask the user for their name and then print it out. ● Ask the user for their age and print it out with the statement: “Your age is:” Demo Time aalhour.com

Slide 93

Slide 93 text

93 Teenager Exercise 05: Ask the user for their age. Run the loop with the teenager check on the user input. The loop should print “You are still a teenager” alongside the age and then increment it until the age is no longer teeanger, after which the loop should terminate. Exercises aalhour.com

Slide 94

Slide 94 text

94 Teenager Exercise 06: Wrap the Teenager Exercise #05 with a function that asks the user for their age and then prints out the message: “You are still a teenager:” alongside their age. Increments the age number until the loop no longer runs. Exercises aalhour.com

Slide 95

Slide 95 text

Let’s take a break!

Slide 96

Slide 96 text

16. Collections The Art of Grouping Stuff 96 aalhour.com

Slide 97

Slide 97 text

97 ● Collections are a way of keeping items together, e.g.: bags! ● We will take a look at lists in Python ● Lists are good for memorizing the order in which we keep things too Collections aalhour.com

Slide 98

Slide 98 text

98 A list of numbers: [1, 2, 3, 4, 5] A list of strings: [“Alice”, “Bob”, “Claire”] A mixed list of things: [123, “Ahmad”, message] Examples of Collections aalhour.com

Slide 99

Slide 99 text

99 Anything in between “[” and “]” is considered a list A list can contain anything in Python: ● Numbers ● Strings ● Variables ● Other lists How can I create a collection? aalhour.com

Slide 100

Slide 100 text

100 ● Create a list of the English alphabet ● Create a list that contains your: ○ first name ○ last name ○ your lucky number ○ your current address ● Print the above lists using the print() function Exercises aalhour.com

Slide 101

Slide 101 text

101 ● Ranges in Python are useful functions for generating collections of numbers ● More like a shortcut ● Easy to use ● Example: ○ range(1, 10): generates a collection of all numbers from 1 until 9 (stops at 10 but doesn’t include it) Ranges aalhour.com

Slide 102

Slide 102 text

102 ● range() takes three arguments: ○ Start: number to start with ○ End: number to stop at ○ Step: increment size ● Examples: ○ range(0, 5, 1) → [0,1,2,3,4,5] ○ range(0, 6, 2) → [0, 2, 4] ○ range(0, 6, 3) → [0, 3] Ranges: In Detail aalhour.com

Slide 103

Slide 103 text

103 ● start = 0 by default, unless specified ● step = 1 by default, unless specified ● end is mandatory and must be always specified ● Examples: ○ range(6) → [0,1,2,3,4,5] ■ Same as: range(0, 6, 1) Ranges: Default Behavior aalhour.com

Slide 104

Slide 104 text

104 ● Generate a list of all odd numbers greater than 0 and less than 1000 ● Generate a list of all even numbers greater than 29 and less than 250 ● Generate a list of all numbers less than -1 and greater than -132 ● Generate a list of all even numbers less than -250 and greater than -277 Exercises aalhour.com

Slide 105

Slide 105 text

Let’s take a break!

Slide 106

Slide 106 text

17. For Loops The Art of Iterating Over Collections 106 aalhour.com

Slide 107

Slide 107 text

107 ● For loops are another way to apply the same computation more than once according to a check in your program ● Similar to While Loops but easier to maintain ○ No need for a counter ○ No need for a beaking check For Loops aalhour.com

Slide 108

Slide 108 text

108 For loops need a collection in order to… well, loop! :P Example: for every item in shopping cart, print it out on the screen: for item in [“Shoes”, “Tablet”]: print(item) For Loops aalhour.com

Slide 109

Slide 109 text

109 for number in [1, 2, 3, 4, 5]: print(number) for number in range(6): print(number) for person in [“AA”, “AM”, “DW”, “DV”]: print(person) For Loops: Examples aalhour.com

Slide 110

Slide 110 text

110 ● Syntax: ○ for in : ■ List of commands ● The variable name can be whatever you want ● The collection can have whatever you want but it must be a collection, not anything else How do I make a for loop? aalhour.com

Slide 111

Slide 111 text

111 1. For every item in the collection: a. Assign the item to the variable name that the programmer wrote b. Enter the block c. Execute all commands d. Go to 1 and grab the next the item 2. If the collection is empty or has no more items to see, then exit the block How do for loops work? aalhour.com

Slide 112

Slide 112 text

112 for number in [1, 2, 3, 4, 5]: print(number) Loop execution log: number = 1 → print(1) number = 2 → print(2) number = 3 → print(3) number = 4 → print(4) number = 5 → print(5) Demo aalhour.com

Slide 113

Slide 113 text

113 ● Using range() and a for loop: ○ Print your name 25 times ○ Print “Hello, World!” 10 times Play time! aalhour.com

Slide 114

Slide 114 text

114 Teenager Exercise 01: Reloaded Ask the user for their age. Check if the user is a teenager. Run a for loop over a range of ages until an age value that is not a teenage number anymore. Run a for loop and display the “you’re a teenager” message according to the list of ages (range) Exercises aalhour.com

Slide 115

Slide 115 text

115 Teenager Exercise 02: Reloaded Wrap your solution for the “Teenager Exercise 01: Reloaded” exercise with a function that asks the user for their age and then prints out the message: “You are still a teenager:” alongside their age. Exercises aalhour.com

Slide 116

Slide 116 text

Let’s take a break!

Slide 117

Slide 117 text

18. Functions: Reloaded The Art of Naming Computations 117 aalhour.com

Slide 118

Slide 118 text

118 Let’s solve the FizzBuzz challenge! Functions: Reloaded aalhour.com

Slide 119

Slide 119 text

17. Wrapping Up 119 aalhour.com

Slide 120

Slide 120 text

120 ● Numbers: 1, 2, 3 ● Order of operations: (, ), *, /, +, -, <, >, >=, <=, == ● Strings: “Hi!” ● Variables: x = 1 ● Functions: def greet(person): print(“Hello, ”, person) ● Conditionals: 1 >= 10 What You’ve Learned So Far aalhour.com

Slide 121

Slide 121 text

121 ● While Loops: while True: greet(“Ahmad”) ● Lists: [1, 2, 3, “Alice”, “Bob”] ● Ranges: range(10) → [1, 2, 3, …, 9] ● For Loops: for number in range(10): print(number) What You’ve Learned So Far aalhour.com

Slide 122

Slide 122 text

18. Resources for Further Study 122 aalhour.com

Slide 123

Slide 123 text

123 Online Courses ● Intro to Python - by Udacity ○ https:/ /bit.ly/2N45h6F ○ 5 weeks long. Completely FREE. ● Intro to Python: Absolute Beginner - by edX ○ https:/ /bit.ly/2NJg2MW ○ 5 weeks. Completely FREE. ● Python for Everybody - by Coursera ○ https:/ /bit.ly/1KSzsbb ○ 7 weeks long. Only first seven days are free. Resources for Further Study aalhour.com

Slide 124

Slide 124 text

124 Resources for Further Study Interactive Learning ● Codecademy - Learn by Coding ○ https:/ /www.codecademy.com ○ Completely Free. Books ● Think Python, 2nd edition ○ http:/ /greenteapress.com/thinkpython2 ○ Completely Free Online. aalhour.com

Slide 125

Slide 125 text

Thanks! Any questions? 125 Find me at: aalhour.com github.com/aalhour twitter.com/ahmad_alhour aalhour.com