The false bible of commands Flashcards
what does random() do?
random real number between 0 and 1 (not inclusive of 1, but 0 works)
what does randint() do?
returns random integer between a and b INCLUSIVELY (and doesnt work when not provided a range)
what happens if you get an undefined error?
you redefined a core python function/something and is now uncallable, reset python immediately
are while true loops allowed?
NAH
.title()
capitalizes first letter, lowercases all others
what does the keyword del do?
deletes a variable from memory
repr()
returns exactly what string you put in, NOT including quotations (but also performs actions like 1+3 within it, and if you do a list like [1, 4, 2] it wont capture the weird space, and also always single quotes for everything even if you do repr(“hiello”)
abs()
returns absolute value of smth, either in or float or 0 or 1 when used on a boolean, part of standard python (no import needed)
ord()
standard python, converts string input to integer equivalent in unicode code
chr()
standard python, converts to single letter string from unicode code input
len()
standard python, returns length of any iterable data sequence (tuple, string, list)
del
keyword in standard python, deletes variables and elements of mutable data sequences, alterring the sequence itself (deletes parts of lists if you want it to and lessens length by 1 every time)
list()
standard python, typecasts something iterable to a list (does not work on floats or ints or booleans) (tuples and strings work though)
shuffle()
random module, randomly organizes items in data sequence. original object is mutated, DOES NOT CREATE NEW OBJECT
shuffle(x)
in
standard python keyword, returns boolean True or False if x is found anywhere in data sequence y (does not work on ints or floats or bools) (works on tuples and strings)
x in y = True or False
max() and min()
standard python, returns highest and lowest value in data sequence (even works to compare booleans and strings though you prob dont need that)
sum()
standard python, sums numbers in list (works on everything BUT strings)
sum(x)
mean()
statistics class, CANNOT be used on strings, returns average of data sequence elements
median()
statistics class, can be used on strings, returns middlemost element of data sequence
mode()
statistics class, can be used on strings, returns most common element of data sequence
multimode()
statistics class, mode but returns list and list can contain multiple “most common elements” of any data sequence
reverse()
list.reverse()
ONLY WORKS ON MUTABLE DATA SEQUENCE TYPES
reverses all elements in sequence
append
list.append(x)
adds ONE item to end of MUTABLE data sequence
pop
x.pop(i)
removes item at INDEX i from MUTABLE sequence x, UNLIKE OTHER SEQUENCE METHODS returns item removed
no index = defaults to i = -1, or last item in the list
insert
x.insert(2, y)
inserts y into x at index 2 of MUTABLE data sequence
DOES NOT DELETE PREVIOUS ITEM AT THAT INDEX
extend
x.extend([y])
adds list y’s elements to end of MUTABLE sequence x
append would have just added a nested list
count
list.count(x)
returns the number of occurrences of x in a list (works on all data sequences)
index
x.index(y)
returns the index of y within x (works on all data sequences)
remove
x.remove(y)
removes item y in x (NOT index location), works on MUTABLE data sequence
clear
x.clear()
turns mutable sequence x into empty list
sort
ONLY WORKS ON LISTS
list.sort()
sorts list by ascending order (by default), or by descending if reverse flag set to true
sorts list IN PLACE, does not return anything
ex.
numlist.sort(reverse=True)
sorted
sorted(numlist)
RETURNS sorted list, does not change list itself
works on any iterable
range
range(n) or range(number included, n) goes from number included to n-1, creating object of type range
creates ITERATOR object one by one to n-1 to complete the whole iterator
range(x, y, z)
goes from x to y-1 in steps of z, and steps can also be negative just like slices of strings
iter
iter(x)
determines whether input can be iterated through, as with a list or a variable set to equal range(n)
next
next(x)
returns next value in iterator, first call will return the lowest number in the iterator by default (ex. next(range(n)) = 0)
enumerate
returns the index and the item itself of a data sequence in that specific order, can be used with two iterators in a for loop to get both as separate or only one iterator to be assigned a tuple pair (index, item)
index, item = enumerate(x)
choice
choice(data sequence)
selects random element
difference between +=, append, and extend for adding tuples?
+= and extend only add the values to the end, whereas append adds the actual tuple
set function
set(x)
returns a list with only one of each instance
id
id(x)
returns memory location variable or value points to/is stored in respectively
copy
x.copy()
returns deep copy of list???
capitalize
x.capitalize()
returns string with first letter capitalized
title
x.title()
returns string with first and all words separated by space to be capital letters
swapcase
x.swapcase()
returns opposite case string (“hELLO” —> “Hello”)
strip
x.strip()
removes leading and trailing (but not middle) spaces
lstrip
x.lstrip()
returns string w/o left or leading spaces
rstrip
x.rstrip()
returns string w/o right or trailing spaces
center, ljust, rjust
x.function(y, char)
y = amnt of spaces
char = fill character (automatically spaces)
returns centered, left justified , and right justified strings respectively
if odd, one extra trailing space/character is addded
zfill
x.zfill(num)
num = amnt of 0’s
adds a bunch of leading zeroes
requires argument
join
char.join(iterable)
iterable = iterable like a list
char = the intended space character/string (cant be number or boolean)
split
x.split(separator, maxsplit=)
splits string into list of terms separated by separator
separator is not included in list
if delimiter is not a space, consecutive delimiters are treated as empty strings in the returned string
maxsplit = amount of splits desired (1 split = 2 components)
rsplit
same as split but from the right, useful when maxsplit is specified
splitlines
same as split but takes newlines as delimiters
partition
x.partition(separator)
returns a tuple with 3 parts: the string left of the separator, the separator, and to the right of the separator
works on 1st instance of separator
left, seperator, right = x.partition(seperator)
rpartition
partition but starts at instance of partition to the right/last instance
find
x.find(inside)
returns index of first instance but returns -1 not error when not found
rfind and rindex
find and index but from the right
replace
x.replace(thing, replacement)
x.replace(thing, replacement, # of times thing should be replaced)
replaces
startswith and endswith
obvi
isalpha
x.isalpha()
True if all alphabetic chars, if not or empty = False
isalnum
x.isalnum()
True if chars are alphanumeric false if not
isdigit
x.isdigit()
ascii_uppercase, ascii_lowercase, ascii_letters, digits
constants of string module:
1. uppercase alphabet
2. lowercase alphabet
3. lower + upper alphabets concatenated
4. all digits 0123456789
capitalize
x.capitalize()
capitalizes just the first letter in the string, even if its a space
array()
dtype
dtype = and myarray.dtype (no parenthesis)
shape
size
ndim
no parenthesis for any, no np dot thingy
arange
np.arange(start, end (uninclusive), steps)
linspace
np.linespace(start, end, amount of linearly spaced points between)
np.linespace(start, end (inclusive), amount of linearly spaced points between, dtype=int)
vstack
np.vstack((array1, array2)), stacks vertically, takes single tuple argument
hstack
np.hstack((array1, array2)) stacks horizontally (same row), takes single tuple argument
zeros
ones
full
empty
np.zeros((# of arrays, rows of arrays, cols of arrays), dtype=whatevs)
np.zeros((rows, cols))
np.zeroes(cols)
np.ones() ^same as above
np.full((# of arrays, rows of arrays, cols of arrays), number to put in array, dtype=whatevs)
of arrays part can be removed https://numpy.org/doc/stable/reference/generated/numpy.zeros.html
default rng
np.random.default_rng()
random
np.random.default_rng().random(x)
integers
np.random.default_rng().integers(start, end, cols)
np.random.default_rng().integers(start, end, (rows, cols)
logical indexing
flattens array and returns as true/false to the given condition
m1d = np.arange(12)
print(m1d)
log = m1d < 10
print(log)
m1d[log]
^indexes, returning:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
if print(m1d[log] is called,
[0 1 2 3 4 5 6 7 8 9]
reshape
x.reshape(shape)
transpose
x.T
swaps rows and columns
ravel
x.ravel()
returns flattened
sum vs. np.sum
np.sum returns a single value