PYTHON Basics Flashcards

1
Q

What is a Jupyter Notebook?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

CoCalc has Jupyter Notebook functionality in it

A

Has time travel to go through iterations and recover past work

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

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$

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

LAtex Formatting

A

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):

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The itemize environment allows a bulleted list in LaTex. The enumerate environment allows a numbered list in LaTex:

A

A theorem, proposition, lemma, corollary, etc. environment allows you to highlight a specific mathematical statement. And these are usually followed by a proof environment:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The SageMath programming language is built on Python, but has a few subtle differences.

A

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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In jupyter notebook the different cells in a Jupyter Notebook “talk to” each other:

A

..

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

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
...
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The “print” function is Python’s way of outputting some object onto your computer screen.

A

The parentheses following the print function collect the input that is passed into the print function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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,

A

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:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

A

Syntax : for i in range (N):

print(i)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A

list comprehension format

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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 =
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

IF else conditions

A

if else

if

elif

else

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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:

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

List comprehension

A list comprehension in Python is a beautiful way of creating complicated lists in a short amount of code. Recall the first participation check, where we created the list of even integers from 0 to 10 (inclusive) as well as the list of odd integers in that range.

A
Evens = [i for i in range(11) if i% 2 == 0]
Odds = [i for i in range(11) if i%2 != 0]
17
Q

Python syntax allows you to write conditional statements in ways that seem strange, but are very convenient once you get used to it.

A

..

18
Q

Functions in Python

Often you will want to use the same bit of code on multiple inputs. It would be a pain to have to manually change the input everytime you ran the code. For example, maybe you want to evaluate the polynomial

A

You can have multiple return statements in a Python function. This is often useful in combination with conditional statements:

19
Q

Dictionaries
A dictionary in Python is a way of associating a value to a given key. This is often called a hash map in other languages.

Think of a normal (English) dictionary of words. You look up a word (the “key”) and are given the definition (the “value”).

A

Note: for obvious reasons, you cannot repeat a key in a dictionary. In other words, the dictionary keys form a set.

You also cannot use certain objects in Python as a key:
Note: for obvious reasons, you cannot repeat a key in a dictionary. In other words, the dictionary keys form a set.

You also cannot use certain objects in Python as a key:

20
Q

Key Errors¶

If you try to access an element of the dictionary using an invalid key, you get a key error:

A

Key Errors¶

If you try to access an element of the dictionary using an invalid key, you get a key error:

21
Q

Iteration over dictionaries¶

A final note: you can iterate over a dictionary just as with a list, but be careful what you’re getting with it:

A

It defaults to iterating over the keys of the dictionary. Which is fine, because you can get the value from the key!

22
Q
Classes
In Python a class is a type of object with methods that can operate on that object. We have seen many already! Lists, sets, integers, ...

You can initialize your own class in Python. Let’s define a vector class, where we can ad

A
import math
class vector:
    def \_\_init\_\_(self,x,y):
        self.x = x
        self.y = y
        self.length = math.sqrt(x**2 + y**2)
    def \_\_add\_\_(self, vec):
        return(vector(self.x+vec.x,self.y+vec.y))
    def dotProduct(self, vec):
        return(self.x*vec.x + self.y*vec.y)
23
Q
String Formatting
This is not crucial to know for this class but can be very useful in real life!

You can write a generic format for a string, and then fill in details based on the results of a function/script, using string formatting.

A

See here for more: https://realpython.com/python-string-formatting/

24
Q

What is Sage?

SageMath is a free open-source mathematics software system licensed under the GPL.

A

It builds on top of many existing open-source packages: NumPy, SciPy, matplotlib, Sympy, Maxima, GAP, FLINT, R and many more

25
Q

Functions from Lecture (Sage):

f.primedivisors()

for i in srange(N): gives sage control to cast as integers

is_prime() , prime function in sage

primeLessThan(), generates a list of primes less than number in parameter

prime_range() , more list of prime numbers

mod(x, y) , modular arithmetic for number x mod y

expand() , distribute product of factors

factor() , factors polynomial

poly. degree() returns the degree of the polynomial provided
poly. coefficient() , returns the coefficient of a certain variable power

symbolic calculus

A

e Sage documentation is really good: https://doc.sagemath.org/

26
Q

Symbolic Calculus

A

..