Python: Easy to Learn
Clifford French, CAS London Conference, 25 Feb 2017
Easy to misunderstand From Pycon Conference
Ned Batchelder, Montreal 2015
2 May not be a very satisfactory session
For some participants, too basic
For some participants, too advanced
For the presenter, wish I could do more
Please do get in touch - [email protected] if you want a multi-session course, a one off half day or day, one to one coaching…
3 Three themes
1. Brief look behind the scenes
2. How understanding “behind the scenes” helps in the classroom
3. Speaking like a Python native and a little extra if we have time.
[email protected] 4 Everything is an object
Other languages use variables
Key concept in Python is objects
A variable is basically a name for a value
No concept of declaring variables
● spring into life when needed ● removed by garbage collector when a new value is assigned to a name
5 Theme 1 - “behind the scenes”
www.ranker.com
6 REPL
Read
Evaluate
Loop
If no name, object is swept away
7 Every object has a type and an id a = ‘pizza!’ type(a) id(a) a = 9 type (a) id(a)
8 Analogies - box, cup, aunty
In some other C based languages, we declare a variable int(x)
It can hold only integers - box a good analogy
In Python, we don’t declare a variable and a type is dynamic
We can use several names for the same value, as a person can be “aunty”, “sister”, “Ms ..”
Box, cup - not good analogies in Python
9 Meet my aunties - first anna
aunty Like us, this Russian doll has many names.
Here she is aunty to some people
Anna to others.
anna Two names, one value.
This doll has one id but can have many names
10 Meet another aunty - dolly
This Russian doll has the name dolly.
aunty The name aunty stays the same
But the id is different because dolly is a different object.
In Python, names are associated with values. dolly Assignment does not change value - Ned will explain more!
11 “Myths about Names and Values”
Extra to watch after the conference - perhaps just 12 minutes.
12 Functions are objects
Returning a value from a function is basically assigning it to a name
Does this help students grasp a difficult idea?
Could be a whole presentation - one example follows
13 Double and add ten
or
14 How Understanding can Help
15 Using “type” function
Reading text files is basic KS4 learning
We will look at three approaches
❏ Using type() to understand what is happening “behind the scenes”
❏ Reading txt files from the Gutenberg project - copyright free
16 Using readlines():
Using type shows us that text is a list.
We can find how many lines by using the len() function
We can iterate/ cycle through it, line by line
17 It’s a list
Why the double line spacing? Use print(text) to see the list:
Displays each line with no end character (\n)
18
Does the type matter?
This seems simpler:
But this line throws a TypeError:
Use type(f) to find out what sort of object f is and why the error occurs.
19 Using readline()
This produces a really weird output.
● Each letter on a new line ● There are apparently 40 lines.
Why?
20 Use type() to debug - diagnose
Running this, we see:
21 t More sensible now to refactor ‘text’ to ‘line’
Recommended approach with
● very large files or ● with reading from an external source, eg an API or file on a website
22 So what?
We can “slice” the sonnet.
❏ Slicing the list (using readlines()), we get the last line first - reverse order ❏ Slicing each line (using readline()), we get each line back to front
23 Je voudrais une tasse de cafe
My school boy French is so bad, the garcon replies in English. I’m not proud of this.
www.livinglanguage.com
24 Speak Python Like a Native
Why?
❏ Its eezier to understood iif u rightlyk evryone els ❏ You are more likely to get help from professionals if follow conventions ❏ We need to raise the game - for further study, for careers
Coding examples easy to find, eg Writing Idiomatic Python by Jeff Knupp
Style conventions are in PEP8
25 Avoid comparisons with True
26 No sentry variable, no comparison with True
.upper() deals with y and Y
[0] takes y or Y out of Yes, Yeah, Yup, etc
27 Names for values - “variables”
Always written using underscores player_name, not playerName
Professional IDEs will highlight and refactor these for you
● Wing IDE ● Pycharm - for teacher or sixth form use
These IDEs are free for you and your students if you email a request (www.wingware.com, www.jetbrains.com)
28 Operators - space both sides
Trivial example to make the point
Wing IDE can be set up to enforce spaces around operators
Pycharm tests will highlight all PEP8 violations
29 But not in setting arguments to parameters
You saw:
Where a blank space is set to the end of each line
30 Slightly more advanced
A docstring for each function
● In double quotes, with the closing double quotes on its own line ● Except for single line docstrings
Two line spaces after a function
31 A Little Extra - demand Python 3.6.0 f string formatting - for me, the big win.
Say goodbye to: Confusing, off- putting, irritating
Welcome: Easier, neater, inclusive
Also retains order in dictionaries.
32 Develop that Function
33 There are only functions
There is no distinction between
● Procedures - which do not return a value ● Functions - which return a value
In Python, all functions return a value
If no return statement, function returns None
In Python, there are no procedures, only functions
34 Want more?
Propose a course
➔ from beginners ➔ to A-level
Get one to one tuition or coaching
Request in class support
Contact: [email protected]
35 Introduction to Databases for KS4
At Camden School for Girls - Sunday 2 April
£15 only
36