PYTHON Basics Flashcards
What is a Jupyter Notebook?
The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more.
CoCalc has Jupyter Notebook functionality in it
Has time travel to go through iterations and recover past work
Markdown :
The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more.
A code cell is one which allows execution of programs. In a code cell one types in code as one normally would in a general IDE. To execute a code cell, one hits Shift+Enter. See below for an example:
For more information, see here: https://www.markdownguide.org/basic-syntax/
Similar to HTML, Markdown is a language for producing formatted text using a plain text editor (like this).
Markdown is in contrast to a what you see is what you get format: https://en.wikipedia.org/wiki/WYSIWYG . In other words, if I type in two dollar signs surrounding a letter x, I get: $x$
as opposed to $x$
LAtex Formatting
LaTex is similar to Markdown in that it allows one to format text. LaTex is particularly useful for mathematical and scientific documents. In Jupyter notebook, markdown cells will also evaluate basic LaTex code.
The short introduction to LaTex is as follows: Normal text in LaTex appears like you think it would in a Microsoft Document. You can enter math mode by surrounding a piece of text with dollar signs. There are two basic math modes:
Inline math mode (single dollar signs): used for formatting basic math, such as x^2 = y+6x
2=y+6.
Displayed math mode (double dollar signs):
The itemize environment allows a bulleted list in LaTex. The enumerate environment allows a numbered list in LaTex:
A theorem, proposition, lemma, corollary, etc. environment allows you to highlight a specific mathematical statement. And these are usually followed by a proof environment:
The SageMath programming language is built on Python, but has a few subtle differences.
Ultimately for this class, the differences between Python and SageMath are inconsequential (and eventually we will simply default to SageMath). But it is good to be aware that differences exist!
In jupyter notebook the different cells in a Jupyter Notebook “talk to” each other:
..
What is Python?
Python is: “an interpreted, interactive, object-oriented programming language…Python combines remarkable power with very clear syntax.”
Why Python?¶
Python is (fairly) easy to learn/relearn.
Understanding 4 key concepts can get you very far:
Basic data structures (strings, lists, sets, dictionaries, etc.)
Iteration (for/while loops)
Conditionals (if, elif, else statements)
Writing your own functions
Many open source libraries extend Python’s applicability to many situations.
Pandas: Library for statistics
NumPy: Library for fast linear algebra computations
SageMath: Many different packages combined into one system for mathematical computations and experimentation
Scikit-learn: Library for machine learning
Geopandas: Project for geospatial analysis
Python is marketable. The language is used by: Mathematical researchers Google Spotify Wikipedia JP Morgan Chase & Co. ... The language is useful for: Data analysis Basic scripts to improve quality of life Developing APIs Writing bots/web scraping ...
The “print” function is Python’s way of outputting some object onto your computer screen.
The parentheses following the print function collect the input that is passed into the print function.
Variables in Python:
Much like in algebra, we use variables in programming to make programs more concise and readable. A variable name in Python can consist of:
Upper case characters
Lower case characters
digits (0 - 9)
The underscore character,
Although a variable name can contain digits, variable name cannot start with a digit:
Variables can be instantiated to hold a large amount of information, without having to manually type that information by hand. Example: The following code produces a list of 1000 numbers. It took ~30 characters to write: x = [i for i in range(1000)] Variables can (occasionally) be updated on the fly, making it very easy to create objects iteratively. Variables can be assigned to the output of very complicated function, and then passed into other complicated functions. ...
Be careful though! In Jupyter Notebook, variables can be assigned in multiple places throughout the document:
Parts of a for-loop
Syntax : for i in range (N):
print(i)
INDENTATION: need to indicate what code to run in a loop
VARIABLE NAME: keeps track of an etry in your-list like object, “i”
LIST LIKE Object: “range(N)” is the list like object created
Syntax : for i in range (N):
print(i)
List Comprehension:
[f (item) for item in listLikeObject if conditional (item)]
f : function to apply t data
variable name: “item”
in : python syntax
listLikeObject : could be a string, set, list dictionary, range(N)…..
if : syntax
conditional : Boolean expressionl f(items) only added if conditional is TRUE
item : variable name
list comprehension format
Comparison Operators:
he common comparison operators (for numeric values) in Python are:
Equality: == Inequality: != Strictly less than: less than Strictly greater than: "greater than" Less than or equal to: less than = Greater than or equal to: less than =
Truth Table
a b a OR b a AND b
T T T T
T F T F
F T T F
F F F F
IF else conditions
if else
if
elif
else
Recall that a string in Python is an ordered collection of unicode characters, and a list in Python is an ordered sequence of values. Note: in Python, you do not have to declare the type of elements that a list can contain, and in fact your list can contain several different types of elements:
Another useful way to collect data in Python is a set. Sets are unordered collections of distinct objects.
You can convert a list or string into a set using the set()
command
Similarly you can convert a set or string into a list via the list()
command. This puts an artificial ordering on your set.
Lists and sets are mutable in Python. The very simplif meaning you can “mutate” (change) them on the fly.