Lecture 02
Lecture 02
Why Python?
• High-level, readable
• Productivity
• Large standard library
• Libraries, Libraries, Libraries
• What about Speed?
- What speed are we measuring?
- Time to code vs. time to execute
Administrivia
• Course Web Site
• TA: Gagana Aladhalli Ramegowda (Of ce: PM 356)
• Syllabus
- Plagiarism
- Accommodations
• Assignments
• Tests: 2 (Feb. 22, Apr. 5) and Final (May 10)
• Course is offered to both undergraduates (CS 490) and graduates (CS 503)
- Grad students have extra topics, exam questions, assignment tasks
fi
fi
Course Material
• Textbook:
- Recommended: Python for Programmers
- Good overview + data science examples
• Many other resources are available:
- https://wiki.python.org/moin/
BeginnersGuide
- https://wiki.python.org/moin/
IntroductoryBooks
- http://www.pythontutor.com
- https://www.python-course.eu
- https://software-carpentry.org/lessons/
D. Koop, CSCI 503/490, Spring 2023 5
Course Material
• Software:
- Anaconda Python Distribution (http://
anaconda.com/download/): makes
installing python packages easier
- Jupyter Notebook: Web-based interface for
interactively writing & executing Python
code
- JupyterLab: An updated web-based
interface that includes the notebook and
other cool features
- JupyterHub: Access everything through a
server
D. Koop, CSCI 503/490, Spring 2023 6
Syllabus Questions?
fi
Class Roster
[JupyterLab Documentation]
D. Koop, CSCI 503/490, Spring 2023 10
Jupyter Notebooks
• Display rich representations and text
• Uses Web technology
• Cell-based
• Built-in editor
• GitHub displays notebooks
[Jupyter]
D. Koop, CSCI 503/490, Spring 2023 11
JupyterLab Notebooks
• Open a notebook using the left panel like you would in a desktop view
• Past results are displayed—does not mean they are loaded in memory
• Use "Run All" or "Run All Above" to re-execute past work
- If you shut down the kernel, all of the data and variables you de ned need
to be rede ned (so you need to re-run all)
- Watch Out—Order Matters: If you went back and re-executed cells in a
different order than they are shown, doing "Run All" may not produce the
same results!
• Edit mode (green) versus Command mode (blue == Be Careful)
fi
JupyterLab Notebooks
• Can write code or plain text (can be styled Markdown)
- Choose the type of cell using the dropdown menu
• Cells break up your code, but all data is global
- De ning a variable a in one cell means it is available in any other cell
- This includes cells above the cell a was de ned in!
• Remember Shift+Enter to execute
• Enter just adds a new line
• Use ?<function_name> for help
• Use Tab for auto-complete or suggestions
• Tab also indents, and Shift+Tab unindents
fi
JupyterLab Notebooks
• You can interrupt the kernel or restart if things seem stuck
• You can download your notebooks if working remotely
• Common Keyboard Shortcuts
Programming Principles
Zen of Python
>>> import this
1. Beautiful is better than ugly.
2. Explicit is better than implicit.
3. Simple is better than complex.
4. Complex is better than complicated.
5. Flat is better than nested.
6. Sparse is better than dense.
7. Readability counts.
8. Special cases aren't special enough to break the rules.
9. Although practicality beats purity.
Zen of Python
10. Errors should never pass silently.
11. Unless explicitly silenced.
12. In the face of ambiguity, refuse the temptation to guess.
13. There should be one-- and preferably only one --obvious way to do it.
14. Although that way may not be obvious at rst unless you're Dutch.
15. Now is better than never.
16. Although never is often better than right now.
17. If the implementation is hard to explain, it's a bad idea.
18. If the implementation is easy to explain, it may be a good idea.
19. Namespaces are one honking great idea—let's do more of those!
fi
Explicit Code
• Goes along with complexity
• Bad:
def make_complex(*args):
x, y = args
return dict(**locals())
• Good
def make_complex(x, y):
return {'x': x, 'y': y}
Don't Repeat Yourself
• "Two or more, use a for" [Dijkstra]
• Rule of Three: [Roberts]
- Don't copy-and-paste more than once
- Refactor into methods
• Repeated code is harder to maintain
• Bad • Good
f1 = load_file('f1.dat') for i in range(1,4):
r1 = get_cost(f1) f = load_file(f'f{i}.dat')
f2 = load_file('f2.dat') r = get_cost(f)
r2 = get_cost(f2)
f3 = load_file('f3.dat')
r3 = get_cost(f3)
D. Koop, CSCI 503/490, Spring 2023 26
Defensive Programming
• Consider corner cases
• Make code auditable
• Process exceptions
• Bad
- def f(i):
return 100 / i
• Good:
- def f(i):
if i == 0:
return 0
return 100/i
Object-Oriented Programming
•?
fi
fi
Python in a Notebook
• Richer results (e.g. images, tables)
• Can more easily edit past code
• Re-execute any cell, whenever