Skip to content

Commit 7fc28cb

Browse files
committed
Merge pull request #189 from KimberleyCook/python-tutorial2
Python turorial 2
2 parents ca553c1 + 19fe701 commit 7fc28cb

2 files changed

Lines changed: 115 additions & 3 deletions

File tree

python/lesson1/tutorial.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ If you see it, make sure you're running Python 2, not Python 3, otherwise parts
3434

3535
## Hello, World!
3636

37-
In keeping with tradition, we're going start by printing "Hello, World!" to the console. In Python, the command to achieve this is aptly named `print`. Type the following:
37+
In keeping with tradition, we're going to start by printing "Hello, World!" to the console. In Python, the command to achieve this is aptly named `print`. Type the following:
3838

3939
print "Hello, World!"
4040

@@ -60,7 +60,7 @@ Subtraction, multiplication and division work the same way.
6060
8 * 4
6161
9 / 3
6262

63-
Now try a few more to see what you get.
63+
Now try a few more to see what results you get.
6464

6565
Let's see what happens when you divide 14 by 4. Notice the remainder is chopped off. We can use the *modulus* operator, `%`, to get the remainder instead. Try `14 % 4` and see.
6666

@@ -78,7 +78,7 @@ Did you notice any unexpected results when you started combining operations? If
7878

7979
10 - 2 * 4
8080

81-
Python follows the traditional mathematical rules of precedence, which state that multiplication and division are done before addition and subtraction. (You may remember what *BODMAS* means.) This means in our example above, 2 and 4 are multiplied first, and then the result is subtracted from 10.
81+
Python follows the traditional mathematical rules of precedence, which state that multiplication and division are done before addition and subtraction. (You may remember *BODMAS*.) This means in our example above, 2 and 4 are multiplied first, and then the result is subtracted from 10.
8282

8383
We can change the order of operations by using parentheses. Anything inside parentheses is executed first.
8484

python/lesson2/tutorial.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
layout: page
3+
title: Python tutorial 2
4+
---
5+
6+
In this tutorial we are going to look at variables, user input and decision making.
7+
8+
### Creating a variable
9+
10+
Python allows us to store data in something called variables so that we are able to use this data at a later point. To place an item in a variable we give it a name then set its value.
11+
12+
Now in the REPL type:
13+
14+
year = 2015
15+
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+
18+
How about saving your age into a variable or your lucky number? Have a play around with storing numbers into variables.
19+
20+
### Storing numbers in variables
21+
22+
Now that you are familair with the use of variables, we are able to combine variables with the maths operations we learnt in the previous tutorial.
23+
24+
Now in the REPL type the following:
25+
26+
revenue = 1000
27+
costs = 200
28+
profit = revenue - costs
29+
30+
Now type `profit` to see the results of this calculation.
31+
32+
Now work out how much money a sponsor at codebar would spend on pizza if they had 60 students turn up?
33+
34+
Along with pizza, students and cost, what oher variables can you think of that could go into this calculation?
35+
36+
### Storing text in variables
37+
38+
As well as numbers varaiables are able to store text, known in Python as strings.
39+
40+
Now in the REPl type:
41+
42+
name = 'codebar'
43+
url = "codebar.io"
44+
45+
Now type `name' and `url` to see these strings shown back to you. As you can see Python allows both single and double quotes to denote a string variable. Double quotes are required if there is going to be an apostrophe in the string.
46+
47+
For exmaple:
48+
49+
message = "I'm a string"
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+
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.
56+
57+
Now store some strings in variables that contain apostrophes and some that do not.
58+
59+
What happens when you store a number in a variable wrapped in quotes?
60+
61+
### Storing user input in variables
62+
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.
64+
65+
Now type this into your REPL:
66+
67+
lucky_number = input("What is your lucky number?")
68+
69+
Type back your answer after it asks you.
70+
71+
Now in the REPL type:
72+
73+
food = input("What is your favourite food?")
74+
75+
When you give the REPL your repsonse make sure you wrap it in quotes as this is storing your response as a string.
76+
77+
Now we are going to put your response into another variable.
78+
79+
Now try:
80+
81+
my_name = input("What is your name?")
82+
greeting = "Hello " + my_name
83+
84+
Then type `greeting` into your REPL to receive your message.
85+
86+
### Decision making using variables
87+
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.
98+
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.
100+
101+
Let's create a variable called:
102+
103+
coffee = input("How many cups of coffee have you consumed today?")
104+
105+
if coffee >= 4:
106+
print "You have a coffee problem"
107+
elif:
108+
print "You do not have a coffee problem"
109+
110+
111+
### Further Reading
112+

0 commit comments

Comments
 (0)