python 3

cheat sheet. you’re welcome.

Python was invented in the late 1980s by Guido van Rossum at Centrum Wiskunde & Informatica (CWI) in the Netherlands

Python is one of the most popular data science programming languages that is used by data scientists.

Python 3.0, released in 2008, was a major revision to Python 2.0 which was released in 2000. Due to concern about the amount of code written for Python 2, support for Python 2.7 (the last release in the 2.x series) was extended to 2020.


Quick Tips

  • Order is important because computer will run the codes/instruction in order

  • White space (indentation) is important for how the program runs the functions

  • Use the “+” sign to connect strings and values inside the print function, but both need to be the same data types; e.g. print(“Good afternoon, “ + name + “!”); if the data types are different, use a comma; e.g. print(“You are”, 12, “years old.”)

  • You can have simultaneous assignment like so: L[0], L[1] = L[1], L[0]

Comments & Docstrings

  • Comments (preceded by the hash sign #) are usually 1-2 liners that explain the tricky or non-obvious portions of code (i.e. WHY not WHAT), clarify intentions behind code, or set up reminders

  • Docstrings (preceded by triple quotes ‘‘‘) are enhanced, multi-liners used to provide detailed description of a function, class, or module. These also allow you embed small tests. You can write these within functions, so when user runs the function later, they can see the docstring details.

    • Computer will ignore comments or docstrings when running the program

    • To apply ‘#’ on multiple lines, highlight the lines and hit CTRL + ‘/’

    • (Jupyter) SHIFT + TAB over a function will give you the docstring details of the function

Mutations

  • Every object lives somewhere in memory, and the location of which can be determined using the id() function

  • When an object is mutated, the contents change but the hex ID remains the same

  • If a function mutates an object, there is usually no return; but exceptions include pop(), next(), readllines()

  • When creating mutating functions, you do not want to include a ‘return’ statement because a different user may assume that the function is non-mutating

Hex ID / Mutating Operations

Hex ID does not change. Useful verification to check when contents of object changed after an operation because that would point to a mutation.

Variable Assignment

Variables are just a reference point. If the variable is assigned to a different object (different from mutation), then the hex ID changes. Second example shows two variables assigned to the same object.

Same Content, Different Objects

Examples of variables assigned to the same content with different hex ID’s. Verified same values with == (equal) operator but different objects with ‘is’ operator.


Useful Terminology

ASCII - American Standard Code for Information Interchange; character encoding standard that uses numbers from 0 to 127 to represent English characters

Byte - the basic unit of information in computer storage and processing

Bytestream - a sequence of bytes

Concatenation - appending (or attaching) another value but both data types must be of the same; e.g. if intro = “Hello! ” then print(intro + “How are you?) will output “Hello! How are you?”

Exception - errors detected during executions; refer to Try/Except

Expression - a piece of syntax which can be evaluated to some value; i.e. anything that returns a value like operators or function calls

Function - a block of reusable code that can be called upon and perform a single action; data that passes through the function (also known as parameters) can return data as a result in most cases

Hash Value - an assigned integer which is used to quickly look up sets or dictionaries (faster than going through a long list through “for x in list”)

Hex Code - short for hexadecimal; used in many areas of computing to simplify binary codes

Index - position, denoted in brackets [ ]; first position starts from zero; negative index starts from the end of the string and goes backward. E.g.; for the string ‘HELLO’, the letter ‘H’ has an index of [0] or [-5], ‘E’ is [1] or [-4], ‘L’ is [2] or [-3], ‘L’ is [3] or [-2], and ‘O’ is [4] or [-1]

Instance - an object that belongs to a class; e.g. when we create a list, we have an instance of the list class

Lambda - “anonymous function”; a function that can be defined without a name

Method - a function that belongs to an object or class, usually preceded by a period; e.g. list.append()

Parsing - the process of converting codes to machine language to analyze the correct syntax of the code

Program - a sequence of instructions that specifies how to perform a computation; the computation can be mathematical or symbolic/textual

Recursion - defining something in terms of itself to achieve your objective

Recursive function - a function that calls itself in its body

Scope - the region of the program that Python is looking at; the order that Python will search are LEGB:

  • Local - exists within the function; if a local variable is the same as a global variable, the local variable will trump the global variable within the function. Outside the block of function code, Python will look for the global variable.

  • Enclosed environment - (or formal parameters) are the variables defined in the function parameters

  • Global - (or general) looks at the entirety of the Python workbook

  • Built-in - these are built-in function names (e.g., ‘abs’)

Semantic error - when program runs successfully without error messages but does not do the right thing

Statement - a block of code that is either an expression or one of several constructs with a keyword, such as ‘if’, ‘while’, or ‘for’

Syntax - the structure of a program and the rules about that structure

Variable - a name assigned to a value or object so it can be referred to or called on later

  • this adds interpretability to the function; e.g. instead of running $299 * 1.1475 straight into the program, I can assign the variables rate = $299 and taxes = 1.1475 and create an interpretable function rate * taxes

  • case sensitive and no spaces

  • always start with a letter or underscore, but the rest can include letters, numbers, and underscores

  • single-letter variables are usually frowned upon; but i, j, and k are often used for indexes

  • avoid using reserved names because you will override them; alternatively, add an underscore or number at the end

  • df is used for “dataframe”


Operators

Chained comparisons allow multiple operators in a single line; e.g. x<y<z vs. x<y and y<z

This is the order in which Python runs the operators in chained comparisons:

  • ** (exponentiation)

  • *, /, //, % (multiplication, division, modulus)

  • +, - (addition, subtraction)

  • & (Bitwise AND)

  • ^ (Bitwise XOR)

  • | (Bitwise OR)

  • comparison, identity, and membership operators

  • not (Boolean NOT)

  • and (Boolean AND)

  • or (Boolean OR)