You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
Jure Šorn 69ad545b4d Update 7 years ago
CONDENSED.md Update 7 years ago
LIBRARIES.md Update 7 years ago
README.md Update 7 years ago

README.md

Python and Libraries Cheatsheet

Main

if __name__ == '__main__':
    main()

List

<list>[<inclusive from>:<exclusive to>:<step size>]
<list>.extend(<list>)
<list>.sort()
<list>.reverse()
sum(<list>)
sorted_by_second = sorted(<list>, key=lambda tup: tup[1])

Dictionary

.items() .get(, ) .setdefault(, ) .update()


Set
---

= set() .add() .update() .union() .intersection() .difference()


Range
-----

range() range(, ) range(, , ) # Negative step for backward


Enumerate
---------

for i, in enumerate()



Type
----

type() is int/str/set/list

import numbers isinstance(, numbers.Number)



String
------

str.replace(, , ) .isnumeric() .split() .strip() ''.join()


### Print

print(, , end='', sep='', file=)


### Regex

import re re.sub(, , ) re.search(, )


### Format

'{}'.format()


{:} -> ' ' {:>} -> ' ' {:^} -> ' ' {:} -> '___' {:.} -> '' {:.} -> ' ' {:.f} -> ' 3.14'


### Text Wrap

import textwrap textwrap.wrap(, )



Random
------

import random random.random() random.randint(, ) random.shuffle()


Infinity
--------

float("inf")


Datetime
--------

import datetime now = datetime.datetime.now() now.strftime('%Y%m%d') now.strftime('%Y%m%d%H%M%S')



Inline
------
### Lambda

lambda , : lambda:


### Comprehension

[i+1 for i in range(10)] [i for i in range(10) if i>5] [i+j for i in range(10) for j in range(10)] {i: i*2 for i in range(10)} (x+5 for x in range(0, 10)) - generator!


### Map, Filter, Reduce

A. map(lambda x: x+1, range(10)) B. filter(lambda x: x>5, range(10)) functools.reduce(combining_function, list_of_inputs)



Closure
-------

def mult_clos(x): def wrapped(y): return x * y return wrapped

mul_by_3 = mult_clos(3)



Decorator
---------

@closure_name def function_that_gets_passed_to_closure(): ...



Generator
---------

def step(start, step): while True: yield start start += step

stepper = step(10, 2) next(stepper)



Class
-----
### Class

class : def init(self, ): self.a = def repr(self): return str({'a': self.a}) def str(self): return str(self.a)


### Enum
----

import enum class (enum.Enum): =


### Copy

import copy copy.copy(