Skip to content

Commit 36988c1

Browse files
author
Tania Allard
committed
finshed session1
1 parent bec09ee commit 36988c1

File tree

1 file changed

+76
-78
lines changed

1 file changed

+76
-78
lines changed

session-1.md

Lines changed: 76 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,27 @@ During the beginners course you learned how to make HTML websites, to show infor
3939

4040
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.
4141

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.
4343

4444
#### Creating and running your first Python file script
4545

4646
Open your code editor (Atom or Sublime), and create a new file. In this file, let's just type one thing:
4747

4848
`print 'Hello, World!'`
4949

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).
5153

5254
Did you notice anything change in your text editor when you saved the file?
5355

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!
5557

5658
Now you've got your brand new Python file saved, let's run it!
5759

5860
| MAC / Linux users | Windows users |
5961
| --- | --- |
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. |
6163

6264
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.
6365

@@ -110,21 +112,21 @@ You might have seen a few results you didn't expect in the task above.
110112

111113
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.
112114

113-
If you give Python decimal numbers (called &quot;floating point numbers&quot; or &quot;floats&quot; 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.
114116

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 % (&quot;modulo&quot;) 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.
116118

117119
For a few more examples you can try, have a look at the homework at the end of these notes.
118120

119121
#### Strings in Python
120122

121-
At the start of today's session we printed some text. In most programming languages, values like this are called &quot;strings&quot;, 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.
122124

123-
To write a string in Python you can either use ' or &quot;. In Python, 'hello' and &quot;hello&quot; 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!
124126

125127
Python can do some clever things with strings. Let's see what happens when we run our file with this:
126128

127-
`print 'hello' + 'world'`
129+
`print ('hello' + 'world')`
128130

129131
As you see here, you can combine strings using +, which appends the second one to the first.
130132

@@ -140,123 +142,119 @@ print("the lord of the rings".title())
140142
```
141143
---
142144

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).
146147

147-
print&quot;Bob&quot;\*3
148-
print&quot;Bob&quot;+3
149-
print&quot;hello&quot;.upper()
150-
print&quot;GOODBYE&quot;.lower()
151-
print&quot;the lord of the rings&quot;.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.
152149

153-
.upper(), .lower(), and .title() are called &quot;methods&quot;. 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.
156151

157152
#### Names &amp; Variables
158153

159154
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.
160155

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:
162157

163-
age =5
158+
`age = 5 `
164159

165160
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):
166161

167-
age =&quot;almost three&quot;
162+
`age ="almost three"`
168163

169164
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:
170165

171-
a\_longer\_name =&quot;hello, CFG!&quot;
166+
`a_longer_name ="hello, CFG!"`
172167

173168
You can print the a variable's value like this:
174169

175-
print age
176-
print a\_longer\_name
170+
```Python
171+
print (age)
172+
print (a_longer_name)
173+
```
177174

178175
#### String formatting
179176

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.
181178

182179
Let's use an example of a 5 year old who likes to paint. We need two variables to hold this information:
183180

184-
age =5
185-
like =&quot;painting&quot;
181+
```python
182+
age = 5
183+
like ="painting"
184+
```
186185

187186
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:
188187

189-
age\_description =&quot;My age is {} and I like {}.&quot;.format(age, like)
188+
```python
189+
age_description ="My age is {} and I like {}.".format(age, like)
190+
```
190191

191192
or we could also do it this way:
192193

193-
age\_description =&quot;My age is {0} and I like {1}.&quot;.format(age, like)
194+
```python
195+
age_description = "My age is {0} and I like {1}.".format(age, like)
196+
```
194197

195198
These will both give you the same result:
196199

197200
'My age is 5 and I like painting.'
198201

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, &quot;s&quot; is for string, &quot;d&quot; is for decimal, and &quot;r&quot; 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:
206203

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.
211207

212208
#### Comments
213209

214210
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.
215211

216212
Here are some comments in action:
217213

218-
greeting =&quot;Hello World!&quot; _#This creates a variable_
219-
greeting.upper() _#This converts the string to uppercase_
220-
print greeting \*3 _#This prints the string 3 times_
221214

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+
```
222220

221+
---
223222

224223
## Homework
225224

226225
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 =&quot;hello&quot;
239-
print b
240-
c = b.title()
241-
print b
242-
print c
243-
d =&quot;hello&quot;
244-
e = d.title()
245-
print d
246-
print e
247-
name =&quot;Dave&quot;
248-
f =&quot;Hello {0}! &quot;.format(name)
249-
print f
250-
name =&quot;Sarah&quot;
251-
print f
252-
print f \*5
253-
254-
It might seem obvious, but it's worth pointing out that = is an &quot;assignment operator&quot;. This means &quot;set the name on the left equal to the value on the right&quot;. 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 = &quot;Hello {0}!&quot;.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).
259257

260258
## Extra Homework (optional)
261259

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

Comments
 (0)