Skip to content

corytu/python-notes

Repository files navigation

Python Notes

General

Naming conventions

How functions work

Precedence

Getting help

  • help. For instance, help(len), or help(tuple.index) for known functions or methods.
  • In IPython (e.g. Jupyter Notebook), ? len also works.

While The Python Language Reference describes the exact syntax and semantics of the Python language, this library reference manual describes the standard library that is distributed with Python. It also describes some of the optional components that are commonly included in Python distributions.

  • dir returns all methods (attributes) of specified instance (object).
  • type returns the data type of the object in Python.
  • import is like source and library in R, sourcing packages or script files.
  • len returns the length of the object. (cf: length in R)
  • set returns unique values in the object, and that allows mathematical set operations. list(set(a_list)) is a way to have unique values in the list a_list. (cf: unique in R; pandas.Series.unique in pandas library)
  • zip returns a mapped iterable from multiple instances (objects).
  • enumerate returns an enumerate object. It is useful with itemgetter in operator module when finding maximum value and its index in a list.

Assigning values

  • =. a = b means that a is b is True and they share the same memory, i.e. changing a will change b. (cf: a = b or a <- b in R copies b and assigns that to a, i.e. changing a won't change b. Read also Use "<-" or "=".)

Comparison

  • My Gist comparison.py demonstrates the difference between == and is. Besides I mention pd.DataFrame.equals there for comparing DataFrames.

Functions for programming

Unlike R, Python is usually not a functional programming language. Still, these following functionalities provide essential help or comparison.

  • lambda are Python's way of creating anonymous functions. This is unlike R.

    You declare a lambda function with the word lambda followed by a list of arguments, followed by a colon and then a single expression and this is key. There's only one expression to be evaluated in a lambda. The expression value is returned on execution of the lambda.

  • map is kind of like sapply in R. See defining functions in R and Python for an example.

Useful packages (ordered alphabetically)

Engineering tools using Python

About

My personal notes of Python language

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published