Skip to content

Commit ffbec64

Browse files
committed
Merge branch 'KimberleyCook-python-tutorial2' into gh-pages
2 parents b0b4552 + e56fca4 commit ffbec64

3 files changed

Lines changed: 116 additions & 10 deletions

File tree

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ <h2>Python</h2>
7070

7171
<ul>
7272
<li><a href="python/lesson1/tutorial.html">Lesson 1 - Introduction to Python</a></li>
73+
<!-- <li><a href="python/lesson2/tutorial.html">Lesson 2 - Playing with variables</a></li> -->
7374
</ul>
7475
</section>
7576
<footer>

python/lesson1/tutorial.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@ layout: page
33
title: Introduction to Python
44
---
55

6-
# Introduction to Python
7-
8-
Welcome to the first Python tutorial. We're going to look at getting Python up and running on your computer, then trying a few simple operations to get to grips with the development environment, the syntax and basic computation.
6+
Welcome to the first Python tutorial. We're going to look at getting Python up and running on your computer, then look at a few simple operations to get to grips with the development environment, the syntax and basic computation.
97

108
## Installation
119

12-
First thing, install Python.
13-
14-
On Mac, you already have Python.
10+
On Mac, you'll already have Python installed.
1511

1612
On Windows:
1713

@@ -38,7 +34,7 @@ If you see it, make sure you're running Python 2, not Python 3, otherwise parts
3834

3935
## Hello, World!
4036

41-
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:
4238

4339
print "Hello, World!"
4440

@@ -64,7 +60,7 @@ Subtraction, multiplication and division work the same way.
6460
8 * 4
6561
9 / 3
6662

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

6965
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.
7066

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

8379
10 - 2 * 4
8480

85-
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.
8682

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

@@ -110,7 +106,7 @@ The answer should go on forever, but it doesn't. This is because there is only a
110106

111107
### Concluding
112108

113-
Now let's combine what we have learnt today. We can tell `print` to print multiple things at once, separated by a space:
109+
Now let's combine what we have learnt today. We can tell `print` to print multiple things at once, separated by a comma:
114110

115111
print 'The result of 2 + 2 is', 2 + 2
116112

python/lesson2/tutorial.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
layout: page
3+
title: Playing with variables
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 familiar 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 the cost of running a codebar workshop if 60 people turned up and pizza cost £8 per 2 people?
33+
34+
Along with pizza, students and cost, what other variables can you think of that could go into this calculation?
35+
36+
### Storing text in variables
37+
38+
As well as numbers variables 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 example:
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 variable 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 common 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 receive a message 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 statement 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+

0 commit comments

Comments
 (0)