SLIDE 1
Scripting Languages Preparing for the exercises and exam Vincent - - PowerPoint PPT Presentation
Scripting Languages Preparing for the exercises and exam Vincent - - PowerPoint PPT Presentation
Scripting Languages Preparing for the exercises and exam Vincent Nys September 21, 2016 The command line Windows: PowerShell 1 OS X: Terminal 2 The command line vs. the REPL Installed programs and utilities can be run using the command line
SLIDE 2
SLIDE 3
Windows: PowerShell
1
SLIDE 4
OS X: Terminal
2
SLIDE 5
The command line vs. the REPL
Installed programs and utilities can be run using the command line. Short snippets of Python code can be run using the read-evaluate-print-loop (which is, itself, a program).
3
SLIDE 6
Installing Python
SLIDE 7
Check your version
- First number should be 2
- Second number should be 7
- Third number does not matter
3 > 2? Python 3 is a perfectly usable language, but it is not fully backwards-compatible with Python 2. If you have installed Python 3, you will also need to install Python 2.
4
SLIDE 8
Get the installer
https://www.python.org
5
SLIDE 9
Select these options
6
SLIDE 10
Setting up a text editor
SLIDE 11
Getting Atom
https://atom.io
7
SLIDE 12
Use these settings
8
SLIDE 13
Your first program
SLIDE 14
Hello, World!
- Create a new file in Atom
- Type print "Hello, World!"
- Save it in a separate folder for Scripting Languages as
“helloworld.py”
9
SLIDE 15
Run it!
Assuming helloworld.py was saved under C:\Scripting Languages
- Open a command line
- Type cd "C:\Scripting Languages"
- Type python helloworld.py
10
SLIDE 16
A word of explanation
- The command line “sees” a specific directory
- The “path” to this directory is typically shown before what
you type (like an address in your file system)
- To change a directory, use cd (stands for “change directory”)
- There is a space between “Scripting” and “Languages”, so
put quotes around the entire path
- cd "C:\Scripting Languages" works, cd C:\Scripting
Languages does not
11
SLIDE 17
Your second UNIX command: mkdir
- Open your home folder (or “My Documents”) in your file
browser and navigate to the same folder on the command line using cd.
12
SLIDE 18
Your second UNIX command: mkdir
- Open your home folder (or “My Documents”) in your file
browser and navigate to the same folder on the command line using cd.
- Type in mkdir something something.
12
SLIDE 19
Your second UNIX command: mkdir
- Open your home folder (or “My Documents”) in your file
browser and navigate to the same folder on the command line using cd.
- Type in mkdir something something.
- Observe.
12
SLIDE 20
Your second UNIX command: mkdir
- Open your home folder (or “My Documents”) in your file
browser and navigate to the same folder on the command line using cd.
- Type in mkdir something something.
- Observe.
- What do you think mkdir stands for?
12
SLIDE 21
Your second UNIX command: mkdir
- Open your home folder (or “My Documents”) in your file
browser and navigate to the same folder on the command line using cd.
- Type in mkdir something something.
- Observe.
- What do you think mkdir stands for?
- Navigate to your Scripting Languages folder and use mkdir to
create directories “exercise session ...” for the next 8 exercise sessions.
12
SLIDE 22
Your third UNIX command: ls
Once you have created directories for the next sessions, enter ls. What do you think it stands for?
13
SLIDE 23
Other useful tricks
- Type in cd exe without hitting <ENTER>, then hit <TAB>.
- Complete the command by adding a number and hitting
<ENTER>.
- Go back to the parent directory with cd ...
- With the command line still open, enter <UP-ARROW> twice.
- Hit <ENTER> to re-enter the directory.
Powershell is not UNIX These tricks and commands are common to Powershell and UNIX shells and they should be sufficient for now, but do not rely on UNIX documentation for Powershell commands or vice versa.
14
SLIDE 24
Learn Python the Hard Way
SLIDE 25
Where to find it
https://learnpythonthehardway.org/
15
SLIDE 26
How it works
“the hard way” = “by doing”
16
SLIDE 27
How it works
“the hard way” = “by doing”
- 1. Read the introduction.
16
SLIDE 28
How it works
“the hard way” = “by doing”
- 1. Read the introduction.
- 2. Copy the code shown, by typing (not through copy-paste).
Pay attention to details.
16
SLIDE 29
How it works
“the hard way” = “by doing”
- 1. Read the introduction.
- 2. Copy the code shown, by typing (not through copy-paste).
Pay attention to details.
- 3. Think about what the code will do.
16
SLIDE 30
How it works
“the hard way” = “by doing”
- 1. Read the introduction.
- 2. Copy the code shown, by typing (not through copy-paste).
Pay attention to details.
- 3. Think about what the code will do.
- 4. Check the actual output and the expected output.
16
SLIDE 31
How it works
“the hard way” = “by doing”
- 1. Read the introduction.
- 2. Copy the code shown, by typing (not through copy-paste).
Pay attention to details.
- 3. Think about what the code will do.
- 4. Check the actual output and the expected output.
- 5. Explain any differences between what you thought the code
would do, what it really does and what it should do according to the book.
16
SLIDE 32
How it works
“the hard way” = “by doing”
- 1. Read the introduction.
- 2. Copy the code shown, by typing (not through copy-paste).
Pay attention to details.
- 3. Think about what the code will do.
- 4. Check the actual output and the expected output.
- 5. Explain any differences between what you thought the code
would do, what it really does and what it should do according to the book.
- 6. Do the extra study drills and read the frequently asked
questions.
16
SLIDE 33
Try it out!
Do exercise 1 and 2 of LPtHW now. Notify the T.A. when you have reasoned about the code and run your program. Then work
- n the study drills until everyone has run their program.
17
SLIDE 34
Fixing program errors
SLIDE 35
Syntax errors
An example: while True print ’Hello world’ File "<stdin>", line 1, in ? while True print ’Hello world’ ^ SyntaxError: invalid syntax
18
SLIDE 36
The caret symbol ˆ
- points to last character that adheres to Python’s grammar
19
SLIDE 37
The caret symbol ˆ
- points to last character that adheres to Python’s grammar
- typically, the error is just after this character
19
SLIDE 38
The caret symbol ˆ
- points to last character that adheres to Python’s grammar
- typically, the error is just after this character
- while True:
print ’Hello world’ is correct
19
SLIDE 39
The caret symbol ˆ
- points to last character that adheres to Python’s grammar
- typically, the error is just after this character
- while True:
print ’Hello world’ is correct
- error often directly follows the caret symbol, sometimes
precedes it, is never any further down
19
SLIDE 40
Exceptions
Grammatically correct programs execute code which contains a semantic error. Put this in a file, “exception.py”, and run it: print 5 / 0 Traceback (most recent call last): File "exception.py", line 1, in <module> print 5 / 0 ZeroDivisionError: integer division or modulo by zero
20
SLIDE 41
The traceback
- shows which code was being executed when the exception
- ccurred
21
SLIDE 42
The traceback
- shows which code was being executed when the exception
- ccurred
- names the type of exception (e.g. ZeroDivisionError)
21
SLIDE 43
The traceback
- shows which code was being executed when the exception
- ccurred
- names the type of exception (e.g. ZeroDivisionError)
- code at the end of the traceback may contain a bug (as in
this example)
21
SLIDE 44
The traceback
- shows which code was being executed when the exception
- ccurred
- names the type of exception (e.g. ZeroDivisionError)
- code at the end of the traceback may contain a bug (as in
this example)
- code higher up in the traceback may contain a bug (e.g. other
code supplies a denominator)
21
SLIDE 45
The traceback
- shows which code was being executed when the exception
- ccurred
- names the type of exception (e.g. ZeroDivisionError)
- code at the end of the traceback may contain a bug (as in
this example)
- code higher up in the traceback may contain a bug (e.g. other
code supplies a denominator)
- worst case scenario: bug may not be in the traceback (e.g.
invalid global settings)
21
SLIDE 46
Simple and effective logging
At the top of your file: import logging logging.basicConfig(level=logging.DEBUG) In your code: logging.debug("This is for very detailed info.") logging.info("Info which is likely to be relevant.") logging.warning("Signals potential problems.") logging.error("Something is definitely wrong.") logging.critical("Your power plant has a meltdown.") Change logging.DEBUG to logging.INFO,. . . to hide unnecessary
- details. Consult this slide when you are working on the projects!
22
SLIDE 47
How to study for this course
SLIDE 48
Your mantra
Knowing without doing is not knowing.
23
SLIDE 49
Why should I attend the exercise sessions?
- more emphasis on practice than in humanities programs
24
SLIDE 50
Why should I attend the exercise sessions?
- more emphasis on practice than in humanities programs
- online solutions provide answers; T.A. provides
problem-solving tools
24
SLIDE 51
Why should I attend the exercise sessions?
- more emphasis on practice than in humanities programs
- online solutions provide answers; T.A. provides
problem-solving tools
- you can get tech support and ask questions about how to
approach the projects (at the end of the session)
24
SLIDE 52
Guidelines: how much should I study?
- for a maximum grade (at the exam): until you can do all
exercises
25
SLIDE 53
Guidelines: how much should I study?
- for a maximum grade (at the exam): until you can do all
exercises
- for a solid grade: until you can do all the non-challenge
exercises
25
SLIDE 54
Guidelines: how much should I study?
- for a maximum grade (at the exam): until you can do all
exercises
- for a solid grade: until you can do all the non-challenge
exercises
- to be a digital humanist: you do not stop
25
SLIDE 55