-
iscompares equality -
==compares identity -
ex. 2 cats: cat
==cat, however usingisthey are "different cats"
len("Hello") #5
len([1, 2, 6, 3, 4]) #5
sorted([5, 4, 3, 2, 1]) #[1, 2, 3, 4, 5]
sorted(["C", "B", "a", str(3), str(1)]) #['1', '3', 'B', 'C', 'a']- Write a function implementation in the future; Avoid compilation errors
- Ignore
ifimplementation
snake_case
def function_a(str1 = "hello"):
print(str1)
print("there")
function_a("hi") #hi\nthere
function_a() #hello\nthere
def print_name(first, last):
print("First:", first, "Last:", last)
print_name(last = "w", first = "d") #First: d Last: w # * denotes a list/array
def print_people(*people):
for person in people:
print("Name:", person)
print_people('a', 'b', 'c', 'd') # Name: 'a' / Name: 'b'...class Pokemon
def __init__(self, health, atk):
self.hp = health
self.atk = atk
def getStats:
print(self.hp)
print(self.atk)
Pokemon pikachu(100, 50)
pikachu.getStats() # 100 / 50# 'w+' is for writing to file
newfile = open("test.txt", "w+")
string = "The quick brown fox jumps over the lazy dog"
newfile.write(string)- Writing to a file
import json
import os
# if file exists and file is not empty
if os.path.isfile("./test.json") and os.stat("./test.json").st_size != 0:
# open file to read
file_text = open("./test.json", "r+")
data = json.loads(file_text.read()) # returns a Python Dictionary- Returns a dictionary object from a JSON file
- Returns a JSON string from a dictionary object
pip install requests
import requests
r = requests.get("url")
'''
r.url - url
r.text - html
r.status_code - http code
'''An iterator object must implement __iter__() and __next__()
- These 2 functions can be called through
iter(obj)andnext(obj) iter(obj)returns an iterator from the passed inobjnext(obj)is used to manually iterate through all items of an iterator- Reaching the end raises the
StopIterationexception - For loops on iterators are actually infinite while loops of
next()calls untilStopIterationis raised - Accepts an optional 2nd argument ex.
next(obj, 1)which in this example returns1uponStopIteration
- Reaching the end raises the
Generators are functions that return iterator objects
- To create a generator, define a normal function with a
yieldstatement instead of areturnstatement yieldpauses the function and saves its state, continuing on successive calls
Usage
- Memory friendly implementation of sequences - produces one item at a time
- Good for infinite streams
Decorators takes a function and adds some functionality
def make_pretty(func):
def inner():
print("I got decorated")
func()
return inner
def ordinary():
print("I am ordinary")
pretty = make_pretty(ordinary)
pretty()
# I got decorated
# I am ordinary@make_pretty
def ordinary():
print("I am ordinary")
# Equivalent statements
def ordinary():
print("I am ordinary")
ordinary = make_pretty(ordinary)@syntactic sugar