scripting languages
play

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


  1. Scripting Languages Preparing for the exercises and exam Vincent Nys September 21, 2016

  2. The command line

  3. Windows: PowerShell 1

  4. OS X: Terminal 2

  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

  6. Installing Python

  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

  8. Get the installer https://www.python.org 5

  9. Select these options 6

  10. Setting up a text editor

  11. Getting Atom https://atom.io 7

  12. Use these settings 8

  13. Your first program

  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

  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

  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

  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

  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

  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

  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

  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

  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

  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

  24. Learn Python the Hard Way

  25. Where to find it https://learnpythonthehardway.org/ 15

  26. How it works “the hard way” = “by doing” 16

  27. How it works “the hard way” = “by doing” 1. Read the introduction. 16

  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

  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

  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

  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

  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

  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 on the study drills until everyone has run their program. 17

  34. Fixing program errors

  35. Syntax errors An example: while True print ’Hello world’ File "<stdin>", line 1, in ? while True print ’Hello world’ ^ SyntaxError: invalid syntax 18

  36. The caret symbol ˆ • points to last character that adheres to Python’s grammar 19

  37. The caret symbol ˆ • points to last character that adheres to Python’s grammar • typically, the error is just after this character 19

  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

  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

  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

  41. The traceback • shows which code was being executed when the exception occurred 21

  42. The traceback • shows which code was being executed when the exception occurred • names the type of exception (e.g. ZeroDivisionError ) 21

  43. The traceback • shows which code was being executed when the exception occurred • names the type of exception (e.g. ZeroDivisionError ) • code at the end of the traceback may contain a bug (as in this example) 21

  44. The traceback • shows which code was being executed when the exception occurred • 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

  45. The traceback • shows which code was being executed when the exception occurred • 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

  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

  47. How to study for this course

  48. Your mantra Knowing without doing is not knowing. 23

  49. Why should I attend the exercise sessions? • more emphasis on practice than in humanities programs 24

  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

  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

Recommend


More recommend