Lecture 01
Lecture 01
Introduction
Python Experience?
fi
Academic Honesty
• Do not cheat!
• You will receive a zero for any assignment/exam/etc. where cheating has
occurred
• You will fail the course if you cheat more than once
• Misconduct is reported through the university's system
• You may discuss problems and approaches with other students
• You may not copy or transcribe code from another source
Schedule
• Lectures are 11:00am-12:15pm MW in PM 110
- Better for learning if you are engaged
- Ask questions
- Please advise me of any issues, including those related to your health
• Any changes will be announced as soon as possible
• Slides will be posted to the course website
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 12
Course Material
• Software:
- Anaconda Python Distribution (https://
www.continuum.io/downloads): 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 13
Python
• Started in December 1989 by Guido van Rossum
• “Python has surpassed Java as the top language used to introduce U.S.
students to programming…” (ComputerWorld, 2014)
• Python is also a top language for data science
• High-level, interpreted language
• Supports multiple paradigms (OOP, procedural, functional)
• Help programmers write readable code, use less code to do more
• Lots of libraries for python
• Designed to be extensible, easy to wrap code from other languages like C/C++
• Open-source with a large, passionate community
2 Python
3 Java
4 Typescript
5 C#
6 C++
7 PHP
8 Shell
9 C
10 Ruby
[GitHub]
D. Koop, CSCI 503/490, Spring 2023 15
Even Wider Gap in Google Tutorial Searches
C/C++
Java
JavaScript
Python
R
Log-Scale
2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
JavaScript 65.36%
HTML/CSS 55.08%
SQL 49.43%
Python 48.07%
TypeScript 34.83%
Java 33.27%
Bash/Shell 29.07%
C# 27.98%
C++ 22.55%
PHP 20.87%
C 19.27%
PowerShell 12.07%
Go 11.15%
Rust 9.32%
[Stack Over ow Developer Survey, 2022]
Kotlin 9.16%
D. Koop, CSCI 503/490, Spring 2023 17
Dart 6.54%
fl
fl
Programming, scripting, and markup languages
Rust is on its seventh year as the most loved language with 87% of Rust also ties with Python as the most wanted technology with
Go 64.58% 35.42%
C# 63.39% 36.61%
Rust 17.6%
Python 17.59%
TypeScript 17.03%
Go 16.41%
JavaScript 12.98%
Kotlin 8.08%
C++ 7.67%
SQL 6.51%
C# 6.11%
Java 5.6%
Dart 4.83%
C 4.34%
Swi! 4.3%
[Stack Over ow Developer Survey, 2022]
HTML/CSS 4.09%
D. Koop, CSCI 503/490, Spring 2023 19
fl
fl
Modes of Computation
• Python is interpreted: you can run one line at a line without compiling
• Interpreter in the Shell
- Execute line by line
- Hard to structure loops
- Usually execute whole les (called scripts) and edit those les
• Notebook
- Richer results (e.g. images, tables)
- Can more easily edit past code
- Re-execute any cell, whenever
fi
fi
Python Differences
• Dynamic Typing
- A variable does not have a xed type
- Example: a = 1; a = "abc"
• Indentation
- Braces de ne blocks in Java, good style is to indent but not required
- Indentation is critical in Python
z = 20
if x > 0:
if y > 0:
z = 100
else:
z = 10
D. Koop, CSCI 503/490, Spring 2023 21
fi
fi
[JupyterLab Documentation]
D. Koop, CSCI 503/490, Spring 2023 22
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 23
Jupyter Notebooks
• An interactive programming environment
• Runs in your web browser
• Displays results (even interactive maps) inline
• Originally designed for Python
• Supports other languages, too
• You decide how to divide code into
executable cells
• Shift+Enter (or the "play" button) to execute
a cell
Notebooks in JupyterLab
• Directory view on left
• Create new notebooks using "+" button, "New" from the File menu, or
Launcher window
- Notebook originally has name "Untitled"
- Click on "Untitled" to change the name (do this!)
• Save a notebook using the command under the File menu
• Shutting down the notebook — use Close and Shutdown Kernel
- Web browser is interface to display code and results
- Kernel actually runs the code: usually see messages in a console/terminal
window
Notebooks in JupyterLab
• Open a notebook by going back to the le browser and clicking on it 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)
• Learn keyboard shortcuts
D. Koop, CSCI 503/490, Spring 2023 26
fi
fi
fi
Notebooks in JupyterLab
• 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 that variable is accessible 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
fi
JupyterLab
• More than just notebooks:
- Text editor
- Console
- Custom components (Many extensions)
• Arrange multiple documents and views
• JupyterLab Documentation