Answers for "python cheatsheet"

7

python cheat sheet

Best Python Cheat Sheet PDF:
https://websitesetup.org/wp-content/uploads/2020/04/Python-Cheat-Sheet.pdf
Posted by: Guest on January-04-2021
6

python cheat sheet

print('Hello World') # print
string_name = "This is a string" # string
string_name[5:] # slicing returns "is a string"
integer_name = 4 # integer
float_name = 3.0 # float
list_name = ['a','b','cats','dogs', 1, 2.0] # list
list_name.append(3) # add to list
# list comprehension filters for int types from the previous list
list_comp = [x for x in list_name if type(x) is int]
# dictionary
dic = {"Bob":9766692343,"Alice":6123336678}
dic["Alice"] # returns 6123336678
dic_phone_numbers = dic.values() # returns list of values
dic_names = dic.keys() # returns list of keys
# class (cat object with 9 lives)
class Cat:
  def __init__(self,name):
    self.name = name
    self.lives = 9
    print(f"{name} is a cat!")
# recursive function (factorial example)
def factorial(x):
    if x == 1:
        return 1
    else:
        return (x * factorial(x-1))
Posted by: Guest on October-02-2020
0

python collections cheat sheet

sum_of_elements  = sum(<collection>)
elementwise_sum  = [sum(pair) for pair in zip(list_a, list_b)]
sorted_by_second = sorted(<collection>, key=lambda el: el[1])
sorted_by_both   = sorted(<collection>, key=lambda el: (el[1], el[0]))
flatter_list     = list(itertools.chain.from_iterable(<list>))
product_of_elems = functools.reduce(lambda out, el: out * el, <collection>)
list_of_chars    = list(<str>)
Posted by: Guest on August-12-2020
0

cheat sheet python

>>> int(7.7) + 1
8
Posted by: Guest on March-02-2021
0

cheat sheet python

>>> a = [1, 2, 3]
>>> if a:
>>>     print("the list is not empty!")
Posted by: Guest on March-02-2021
0

cheat sheet python

>>> spam = 'Hello'
>>> spam
'Hello'
Posted by: Guest on March-02-2021

Python Answers by Framework

Browse Popular Code Answers by Language