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: session-1.md
+76-78Lines changed: 76 additions & 78 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,25 +39,27 @@ During the beginners course you learned how to make HTML websites, to show infor
39
39
40
40
On this course, you'll learn how to take things to the next level – you'll be making an interactive website that uses both Python and HTML to get information from your website's visitors, do stuff with it using internet services, and show results to your visitors, either on your website, or via email, Twitter, etc.
41
41
42
-
You might hear us using the terms "web application" or "web app" or "interactive website" – for the purposes of this course, they all mean the same thing.
42
+
You might hear us using the terms "web application" or "web app" or "interactive website" – for the purposes of this course, they all mean the same thing.
43
43
44
44
#### Creating and running your first Python file script
45
45
46
46
Open your code editor (Atom or Sublime), and create a new file. In this file, let's just type one thing:
47
47
48
48
`print 'Hello, World!'`
49
49
50
-
It doesn't matter if you use single or double quotation marks, as long as you use them together, in pairs. Now save your file – let's call it **hello.py** (the .py extension means we're dealing with a Python file).
50
+
It doesn't matter if you use single or double quotation marks, as long as you use them together, in pairs.
51
+
52
+
Now save your file – let's call it **hello.py** (the .py extension means we're dealing with a Python file).
51
53
52
54
Did you notice anything change in your text editor when you saved the file?
53
55
54
-
It's important that you don't name any of your folders for this course **ython.py** – things won't work if you do!
56
+
It's important that you don't name any of your folders for this course **python.py** – things won't work if you do!
55
57
56
58
Now you've got your brand new Python file saved, let's run it!
57
59
58
60
| MAC / Linux users | Windows users |
59
61
| --- | --- |
60
-
| To begin, right click anywhere inside the file you just created, and pick the Copy File Path option: Next, you'll need to open your command line. You'll remember this from the pre-course notes. Note this is your Terminal app. Once the command line is open, type `python` (always in lowercase) followed by a space, and then paste the file path you just copied, inside quotation marks. The quotation marks make sure that everything works properly if you have spaces in any of your folder names. ** What you type will look something like this: `python "/users/andreas/cfg-python work/hello.py` **. Now all you need to do is hit enter, and you should see `Hello, World!`` printed in your command line window. | Find the location of the file you just created (use your file Explorer) and right click anywhwere in there. Now click on **Git Bash Here.** A terminal will open at the file location. Once the terminal is open type: `python hello.py`. Now all you need to do is hit **enter** , and you should see `Hello, World!` printed in your command line window. |
62
+
| To begin, right click anywhere inside the file you just created, and pick the Copy File Path option. <img src='assets/s1_path.png' style='display: block; padding:10px' width='200px'> <br/> Next, you'll need to open your command line. You'll remember this from the pre-course notes. Note this is your Terminal app. <br/> Once the command line is open, type `python` (always in lowercase) followed by a space, and then paste the file path you just copied, inside quotation marks. The quotation marks make sure that everything works properly if you have spaces in any of your folder names. <br/><br/>What you type will look something like this: ```python "/users/andreas/cfg-python work/hello.py"```.<br/> Now all you need to do is hit enter, and you should see `Hello, World!` printed in your command line window. | Find the location of the file you just created (use your file Explorer) and right click anywhere in there. Now click on **Git Bash Here.** <img src='assets/s1_pathw.png' style='display: block; padding:10px' width='200px'> <br/> A terminal will open at the file location. <br/><br/>Once the terminal is open type: `python hello.py`. <br/>Now all you need to do is hit **enter** , and you should see `Hello, World!` printed in your command line window. |
61
63
62
64
Each line you type in your code editor is a single Python `statement` – a small piece of code that Python can evaluate to either produce a result or to do something. Python programs are simply long lists of statements spread across one or more (sometimes thousands!) of files. Python reads and performs each of these statements one after another.
63
65
@@ -110,21 +112,21 @@ You might have seen a few results you didn't expect in the task above.
110
112
111
113
If you give Python integers (whole numbers), it will do integer division. For example, 5 / 2 gives an answer of 2, because that's the largest whole number of times you can remove 2 from 5.
112
114
113
-
If you give Python decimal numbers (called "floating point numbers" or "floats" in many programming languages) it will do normal division. Any number with a decimal point is considered to be a float.
115
+
If you give Python decimal numbers (called "floating point numbers" or "floats" in many programming languages) it will do normal division. Any number with a decimal point is considered to be a float.
114
116
115
-
You might remember from your school days that a remainder is just what's left over from your whole number division. We can use % ("modulo") to get the remainder of an expression. So, 5 % 2, gives the answer 1.
117
+
You might remember from your school days that a remainder is just what's left over from your whole number division. We can use % ("modulo") to get the remainder of an expression. So, 5 % 2, gives the answer 1.
116
118
117
119
For a few more examples you can try, have a look at the homework at the end of these notes.
118
120
119
121
#### Strings in Python
120
122
121
-
At the start of today's session we printed some text. In most programming languages, values like this are called "strings", because they're formed from a string of individual characters.
123
+
At the start of today's session we printed some text. In most programming languages, values like this are called "strings", because they're formed from a string of individual characters.
122
124
123
-
To write a string in Python you can either use ' or ". In Python, 'hello' and "hello" are exactly the same. It doesn't matter whether you use a single quote or a double quote, but pick one and be consistent!
125
+
To write a string in Python you can either use ' or ". In Python, 'hello' and "hello" are exactly the same. It doesn't matter whether you use a single quote or a double quote, but pick one and be consistent!
124
126
125
127
Python can do some clever things with strings. Let's see what happens when we run our file with this:
126
128
127
-
`print 'hello' + 'world'`
129
+
`print ('hello' + 'world')`
128
130
129
131
As you see here, you can combine strings using +, which appends the second one to the first.
130
132
@@ -140,123 +142,119 @@ print("the lord of the rings".title())
140
142
```
141
143
---
142
144
143
-
TASK:
144
-
145
-
Let's add some code to our **hello.py** file to do some cool stuff with strings.
145
+
`.upper(), .lower(), and .title()` are called "methods".
146
+
Attaching a method to a string tells Python to do some processing on the string – in this case, it converts all of the characters to uppercase, lowercase, or a title. You can find out more about string methods in the Python documentation [here](https://docs.python.org/2/library/string.html).
146
147
147
-
print"Bob"\*3
148
-
print"Bob"+3
149
-
print"hello".upper()
150
-
print"GOODBYE".lower()
151
-
print"the lord of the rings".title()
148
+
Sometimes when you type things in a Python file and run it, you'll see an error message instead of a result. Turns out that you can't add a string to an integer. Have another read of the error message that was given out.
152
149
153
-
.upper(), .lower(), and .title() are called "methods". Attaching a method to a string tells Python to do some processing on the string – in this case, it converts all of the characters to uppercase, lowercase, or a title. You can find out more about string methods in the Python documentation [here](https://docs.python.org/2/library/string.html).
154
-
155
-
Sometimes when you type things in a Python file and run it, you'll see an error message instead of a result. Turns out that you can't add a string to an integer. Have another read of the error message that was given out. Can you figure out what it's saying? When something goes wrong, Python tries to be as helpful as it can.
150
+
Can you figure out what it's saying? When something goes wrong, Python tries to be as helpful as it can.
156
151
157
152
#### Names & Variables
158
153
159
154
Programming becomes a lot more powerful when you're able to give values names. A name is just something you can use to refer to a value in the future.
160
155
161
-
In Python you create a name by using the assignment operator = . For example:
156
+
In Python you create a name by using the assignment operator `=` . For example:
162
157
163
-
age =5
158
+
`age = 5 `
164
159
165
160
You can change the value associated with a name at any point. The new value doesn't even have to be the same type as the old one. Here we change the value associated with age from an integer (number) to a string (text):
166
161
167
-
age ="almost three"
162
+
`age ="almost three"`
168
163
169
164
In Python it's convention for variable names to start with a lowercase letter. If you want to use multiple words in a name, you can separate them with an underscore, like this:
170
165
171
-
a\_longer\_name ="hello, CFG!"
166
+
`a_longer_name ="hello, CFG!"`
172
167
173
168
You can print the a variable's value like this:
174
169
175
-
print age
176
-
print a\_longer\_name
170
+
```Python
171
+
print (age)
172
+
print (a_longer_name)
173
+
```
177
174
178
175
#### String formatting
179
176
180
-
String formatting is a way of taking one or more variables and putting them inside a string, using placeholders for the values. There are a few ways you can do this in Python, but not all of them work in different versions of Python, so on this course we use {} because it's the most reliable.
177
+
String formatting is a way of taking one or more variables and putting them inside a string, using placeholders for the values. There are a few ways you can do this in Python, but not all of them work in different versions of Python, so on this course we use `{}` because it's the most reliable.
181
178
182
179
Let's use an example of a 5 year old who likes to paint. We need two variables to hold this information:
183
180
184
-
age =5
185
-
like ="painting"
181
+
```python
182
+
age =5
183
+
like ="painting"
184
+
```
186
185
187
186
So, we've got some variables, but how do we print this information in a sentence that looks nice? There are a few different ways we can do this:
188
187
189
-
age\_description ="My age is {} and I like {}.".format(age, like)
188
+
```python
189
+
age_description ="My age is {} and I like {}.".format(age, like)
190
+
```
190
191
191
192
or we could also do it this way:
192
193
193
-
age\_description ="My age is {0} and I like {1}.".format(age, like)
194
+
```python
195
+
age_description ="My age is {0} and I like {1}.".format(age, like)
196
+
```
194
197
195
198
These will both give you the same result:
196
199
197
200
'My age is 5 and I like painting.'
198
201
199
-
Sometimes, when you're doing exercises or looking at code online, you might see string formatting done with % instead of {}. This is from older versions of Python. If you see %, it just means this:
200
-
201
-
%s → {} %d → {} %r → {}
202
-
203
-
If you're wondering what the letters stand for, "s" is for string, "d" is for decimal, and "r" is used to format a string in a particular way (you can read
204
-
#
205
-
[ANNOTATION:
202
+
Sometimes, when you're doing exercises or looking at code online, you might see string formatting done with `%` instead of `{}`. This is from older versions of Python. If you see `%`, it just means this:
206
203
207
-
BY 'Tania Sanchez'
208
-
ON '2017-08-07T11:38:00'TS
209
-
NOTE: 'Broken link here']
210
-
more about %r here). It's best to use {} to make sure your code works.
204
+
`%s → {} %d → {} %r → {}
205
+
`
206
+
If you're wondering what the letters stand for, "s" is for string, "d" is for decimal, and "r" is used to format a string in a particular way.
211
207
212
208
#### Comments
213
209
214
210
In Python, any part of a line that comes after a # is ignored. This is useful when you're writing complicated programs and working with others, because it means you can write short comments in plain English to help others to follow your code and understand what it does.
215
211
216
212
Here are some comments in action:
217
213
218
-
greeting ="Hello World!"_#This creates a variable_
219
-
greeting.upper() _#This converts the string to uppercase_
220
-
print greeting \*3 _#This prints the string 3 times_
221
214
215
+
```python
216
+
greeting ="Hello World!"#This creates a variable_
217
+
greeting.upper() #This converts the string to uppercase_
218
+
print (greeting*3) #This prints the string 3 times_
219
+
```
222
220
221
+
---
223
222
224
223
## Homework
225
224
226
225
1. Add code your Python file to print the answers to these expressions in your command line:
227
-
228
-
- 10/3
229
-
0/0
230
-
10%3
231
-
0%0
232
-
233
-
1. Create a new Python file with the following statements in it, then run it. Were the things that were printed what you expected to see?
234
-
235
-
- a =1
236
-
a = a +1
237
-
print a
238
-
b ="hello"
239
-
print b
240
-
c = b.title()
241
-
print b
242
-
print c
243
-
d ="hello"
244
-
e = d.title()
245
-
print d
246
-
print e
247
-
name ="Dave"
248
-
f ="Hello {0}! ".format(name)
249
-
print f
250
-
name ="Sarah"
251
-
print f
252
-
print f \*5
253
-
254
-
It might seem obvious, but it's worth pointing out that = is an "assignment operator". This means "set the name on the left equal to the value on the right". It isn't the same equals as you see in maths!
255
-
256
-
This means that strings are a little bit different. String formatting happens when you write it down. So, when you first write f = "Hello {0}!".format(name) Python immediately looks up name and bakes it straight into the string called f. Setting name to something different later on won't change f.
257
-
258
-
1. Work your way through exercises 1 to 10 on [Learn Python The Hard Way](https://learnpythonthehardway.org/book/ex1.html).
226
+
- 10 / 3
227
+
- 0 / 0
228
+
- 10 % 3
229
+
- 0 % 0
230
+
231
+
2. Create a new Python file with the following statements in it, then run it. Were the things that were printed what you expected to see?
232
+
233
+
- a =1
234
+
- a = a +1
235
+
- print (a)
236
+
- b = "hello"
237
+
- print (b)
238
+
- c = b.title()
239
+
- print b
240
+
- print c
241
+
- d ="hello"
242
+
- e = d.title()
243
+
- print (d)
244
+
- print (e)
245
+
- name = "Dave"
246
+
- f = "Hello {0}! ".format(name)
247
+
- print (f)
248
+
- name = "Sarah"
249
+
- print (f)
250
+
- print (f * 5)
251
+
252
+
It might seem obvious, but it's worth pointing out that = is an "assignment operator". This means "set the name on the left equal to the value on the right". It isn't the same equals as you see in maths!
253
+
254
+
This means that strings are a little bit different. String formatting happens when you write it down. So, when you first write `f = "Hello {0}!".format(name)` Python immediately looks up name and bakes it straight into the string called `f`. Setting name to something different later on won't change `f`.
255
+
256
+
3. Work your way through exercises 1 to 10 on [Learn Python The Hard Way](https://learnpythonthehardway.org/book/ex1.html).
259
257
260
258
## Extra Homework (optional)
261
259
262
-
1. Continue up to exercise 35 on Learn Python The Hard Way.
260
+
- Continue up to exercise 35 on Learn Python The Hard Way.
0 commit comments