Basic Rehash Flashcards
Identify places in Python Fundamentals that need more work.
Name 3 components of a print() statement
String, value, end argument
Identify two ways to print an entire list
print(list) or print(list[:])
Identify list index components
list[position:range:steps]
Name all list methods
append()
insert()
extend()
del
remove()
pop()
reverse()
list.append(object)
Adds an object to the end of a list.
list.insert(position, value)
Adds a value to the position identified in a list specified.
list.extend(object)
Adds the value(s) inside of an object to a list
del list[position]
Deletes the value at a particular position. This acts as an operator in this example, not a method.
list.remove(value)
Removes a value within the list without specifying a location.
list.pop()
Removes the last element of a list.
What happens if you add an argument to the pop() method?
It will treat it as a position and remove that value from that position.
list.reverse()
Displays a list from right to left without changing the actual positioning of the list variable.
Identify the layout of an “inclusion” if statment
if value in list:
What should you add to an end of a While loop?
An else statement.
Go over the funny inclusive/exclusive property of an index range.
When you select a range from a list index, such as var[0:3], the number on the left of the ‘:’ is inclusive, meaning it is included in the range, but the value on the right of the ‘:’ is exclusive, meaning it is NOT included in the requested range. So, in a list of 1, 2, 3, 4, the index request of var[0:3] would be: 1, 2, 3
Tuples are…
immutable, meaning the value cannot be altered
How do you get a list of all keys in a dictionary?
dictionary_var.keys()
outputs: dict_keys([…
How do you get a list of all values in a dictionary?
dictionary_var.values()
outputs: dict_values([…
What tells the print() function to print on the next line?
‘\n’ in the same string.
range()
Often used in loops to generate a sequence of numbers.
Versatile tool in creating iterable stop, start, and step values.
How do you pop from a dictionary?
Identify the key you want to remove from the dictionary. Since the dictionary is unordered, a blank .pop() will return an error.
Type Casting
The process of converting one data type to another.
Commonly used when working with data in loops to ensure compatibility and perform specific operations.
What’s the benefit of using range()
Allows to loop through a large amount of data without consuming a lot of memory.
Format a list comprehension for applying a 5% discount to a list of prices in a price_list list variable.
discounted_price_list = [x-(x*(5/100)) for x in price_list)
Format a list that will return each value of the list as yes or no according to comparisons on a budget value to other items in the discounted_price_list list variable.
budget_list = [‘Yes’ if x <= budget else ‘No’ for x in discouted_price_list]
What is the typical syntax of a function?
def function_name(parameters):
‘'’explain what the function does’’’
statement(s)
What parameter controls the space between parameter values output by the print() function?
sep=
return statement
A function that returns a value.
lambda function
A very short function that is done in a single line (inline function) to make the code more Pythonic.
lambda keyword does need to be called:
function_name = lambda parameter(s),.. : statement
What is the purpose of a lambda function?
Creates anonymous, small, and simple functions.
What value will display when you run this code:
mul = lambda a,b: return a*b
mul(5,6)
It will return an error, specifically:
SyntaxError: invalid syntax
What is the order of Positional arguments to Keyword arguments in a function?
positional before keyword
Keyword arguments are stored in a…
dictionary
How do you identify a Keyword argument in a function call?
Put a ** in front of the parameter passed to the function. i.e.
function_name(**kwargs)