Show Menu
Cheatography

Cheat Sheet - Python (Adrián) Cheat Sheet (DRAFT) by

Resumen esquema / chuletario

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Introd­uction

Variable & Operator & Casting & Input
String
name = "­Cho­pit­o"
Int
number = 13
Operator
+ -  * / // %
Input
example = input(­"­Who­?:")
Casting
number = int(in­put­("is 1?:"))
Print & Type
Print
print(­"­nam­e:", name)
Printf
print(­f"na­me:­{na­me}­")
Type
type(name)
Condit­ional
IF
(if elif else)
MATCH
match option: case 1: case _:
Loop
For
for i in range(10) | for x in y
While
while n <= 13:
List
List
exampl­e_list = ["hi­", "­bye­", 13]
Len
len(ex­amp­le_­list)
Position
exampl­e_l­ist[0]
Range
exampl­e_l­ist­[0:1] | exampl­e[-­1:-2]
In
"­hi" in exampl­e_list
Index
exampl­e_l­ist.in­dex­("by­e")
Dictionary
Dictionary
numbers = {"on­e": 1, "­thi­rte­en": 13}
Position
number­s["t­hir­tee­n"]
Key
number­s.k­eys()
Value
number­s.v­alues()
For in Dictionary
for key, value in list.i­tems():
Add with key
number­s["t­wo"] = 2
Remove with key
number­s.p­op(­"­two­")
Update with key
number­s["t­wo"] = 5
 

Introd­uction

Function
Function
def name(a, b):
Compre­ssion List
List
new_list = [exp for x in y if con]

OOP

OOP
Init
def __init­__(­self, x: str = "­y"):
Str
def __str_­_(s­elf): return f"y:­{se­lf.x­}"
@class­method
You can call it without a instance
Inheri­tance
Inheri­tance
class Car(Ve­hicle):
Super
super(­)._­_in­it_­_(name)
Override
@override
Override
from typing­_ex­ten­sions import override
Protected & Private
(Pro)_ (Pri)__

Map Filter Reduce

High level orders
def saluda­(no­mbre, funcion):
lambda
even = lambda n: n % 2 == 0
map()
list(m­ap(­lambda n: n % 2 == 0, list)
filter()
list(f­ilt­er(­lambda n: n < 0, list))
reduce()
from functools import reduce
reduce() example
`reduc­e(l­ambda x, y: x + y,
list(f­ilt­er(­lambda n: n < 0, list)))`