You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: python/lesson2/tutorial.md
+24-12Lines changed: 24 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ Now in the REPL type:
15
15
16
16
In this example you have now stored the value `2015` into the variable `year`. See what happens next when you type `year' into the REPL. Does it show it back to you?
17
17
18
-
How about saving your age into a variable or your lucky number? Have a play around with storing numbers into variables. Let us know if you think of something more exciting to store in variables than the year.
18
+
How about saving your age into a variable or your lucky number? Have a play around with storing numbers into variables.
19
19
20
20
### Storing numbers in variables
21
21
@@ -29,13 +29,13 @@ Now in the REPL type the following:
29
29
30
30
Now type `profit` to see the results of this calculation.
31
31
32
-
How much money would a sponsor at codebar spend on pizza if they had 60 students turn up?
32
+
Now work out how much money a sponsor at codebar would spend on pizza if they had 60 students turn up?
33
33
34
34
Along with pizza, students and cost, what oher variables can you think of that could go into this calculation?
35
35
36
36
### Storing text in variables
37
37
38
-
As well as numbers varaiables are able to store text, known in Python as string.
38
+
As well as numbers varaiables are able to store text, known in Python as strings.
39
39
40
40
Now in the REPl type:
41
41
@@ -48,6 +48,10 @@ For exmaple:
48
48
49
49
message = "I'm a string"
50
50
51
+
Sometimes you will need to use an apostrophe within a single quote, on occasions like this it is recommended to use string escaping. This would look like:
52
+
53
+
message ='I\'m a string'
54
+
51
55
Try storing a string within a varable without quotes, see what happens? Numbers do not require quotation marks, whereas they are mandatory for storing strings.
52
56
53
57
Now store some strings in variables that contain apostrophes and some that do not.
@@ -56,43 +60,51 @@ What happens when you store a number in a variable wrapped in quotes?
56
60
57
61
### Storing user input in variables
58
62
59
-
Now we are going to look at capturing user input using the python input command.
63
+
Now we are going to look at capturing user input using the python input command. Let's create a variable in which to store the user input.
60
64
61
-
Let's create a variable in which to store the user input. Now type this into your REPL:
65
+
Now type this into your REPL:
62
66
63
67
lucky_number = input("What is your lucky number?")
64
68
65
69
Type back your answer after it asks you.
66
70
67
-
Now remember how strings need either single or double quotes?
68
-
69
71
Now in the REPL type:
70
72
71
-
food = input("What is your favouirte food?")
73
+
food = input("What is your favourite food?")
72
74
73
75
When you give the REPL your repsonse make sure you wrap it in quotes as this is storing your response as a string.
74
76
77
+
Now we are going to put your response into another variable.
78
+
75
79
Now try:
76
80
77
81
my_name = input("What is your name?")
78
82
greeting = "Hello " + my_name
79
83
80
84
Then type `greeting` into your REPL to receive your message.
81
85
82
-
83
86
### Decision making using variables
84
87
85
-
Now that we know how to use variables and know how to store data, let's play round with decision making and changing prints based on your answer. Now that we are getting more in depth with Python, we should say that Python is very particular about tabbing. Tabbing is the indents created when writing code. With Python, if certain lines are not indented correctly the code will not run.
88
+
Now that we know how to use variables and know how to store data, let's play around with decision making and changing prints based on your answer. In Python (and many other languages), one of the most commons ways in which this is done is using an if statement. For example:
89
+
90
+
if number > 3:
91
+
print "Bigger than three"
92
+
elif number < 3:
93
+
print "Smaller than three"
94
+
95
+
Here we can see that if a number we have passed into this decision making code is bigger than three, we will recieve a messag telling us so, and the same relevant message if the number is smaller than three.
96
+
97
+
Also, now that we are getting more in depth with Python, we should say that Python is very particular about tabbing. Tabbing is the indents created when writing code. With Python, if any lines are not indented correctly the code will not run. If you are running into bugs, this is a good place to start.
86
98
87
-
In this final exercise we are going to ask you the number of coffees you have drank today and then change the statament back at you depending on your answer.
99
+
In this final exercise we are going to ask you the number of coffees you have drank today and then change the statament returned to you, depending on your answer.
88
100
89
101
Let's create a variable called:
90
102
91
103
coffee = input("How many cups of coffee have you consumed today?")
0 commit comments