This is mainly me, documenting my learning about python.

Variables and Data Types

Basic: int float str bool
Compound: list tuple set dict

Type Casting

int() float() str() tuple() list() set() dict()

Operators

All the basics ones, arithmetic, comparison, assignment plus

** - exponent or power. for example 2^3 => 2**3

// - this is integer division, meaning output of this will be an integer. in python 3 / is float division, so even if the operands are integer output will be a float

Logical operators are: and or not

Membership operator: in not in

Identity operator: is is not same object/type+value or not

Comment

There is only one way to comment in Python, using `#`

# print single line comment
print("above is single line comment")

# comments can be added
# in seprate line too!
print("how cool is that!") # everything after `#` is a comment

Strings

Strings can be created using single quote ' or double quote "  and are immutable, meaning single index value can't be changed

s.count() s.find() s.index() s.replace() s.split()

List

append extend multiple items insert in a position remove finds and removes value ... so there are wide list of functions that can be done in lists!

del lst[idx] lst.reverse() lst.sort()

Tuple

Everything like list except you can't change each element, or remove it. Only can add new item which makes a new tuple `old + ('new',)

Set

{"something"} or s = set(["something"]) empty {} will create a dictionary! sets holds only unique values

add() adds single value update({"multi", "value"}) extends

remove('single') removes single value or raise a KeyError if not present

discard('single') removes single value if present, no error

.pop() removes then returns first value. clear() resets the set

A.union(B) A.intersection(B) A.difference(B)

Dictionary

basically hash-map, declares with {'key': 'value'} or dict() function. key an be anything even list, tuple.

A.update(B) adds all the items from B to A

del A[key] removes single element, clear() removes everything. del A removes A completely from program

A.copy() returns a copy of dictionary

A.get(key, default=None) returns value for key or default value

key in A checks if key is in A dictionary and return s boolean value

.items() converts each key:value pair into tuple and return all of them as list

.keys() returns list of keys

.values() returns list of values

Condition

if elif else has : at the end of condition

Loop

while for x in list/set/tuple/dictionary:

else block is executed when loop condition is false, aka after loop is over

while condition:
else:

for x in range:
else

Comprehension

has 3 parts, output input sequence optional condition

list => lst = [i for i in range(10)]

set => st = {i for i in range(10)}

dict => dt = {k:v for k,v in zip(list_a, list_b)}

note the zip in  dict

Function

declares with def keyword. parameters can be 4 types:

  1. required def xyz(a, b, c): call with xyz(1, 2, 3)
  2. keyword def xyz(a, b, c): call with xyz(a=1, c=3, b=2)
  3. default value creates an optional parameter  def xyz(a, b, c=3) call with xyz(1, 2)
  4. variable length, two types
def xyz(*args):
  # args is a tuple
  for x in agrs:
    print(x)

def XYZ(**kwargs):
  # kwargs is a dictionary
  for key, val in kwargs.items():
    print(key, '->', val)

Decorator

Decorators are nothing but wrappers!  

def wrapper(fn):
	def wrap():
		print "do something before original function"
		fn()
	return wrap

@wrapper
def hello():
	print "Hello world!"

hello()

# equivalent to
# hello = wrapper(hello)
# hello()

TODO: Need to learn about class decorator

Meta Programming

TODO: learn more about it