Python Glossary Flashcards
What is a Class in Python?
Python has classes which are defined wireframes of objects. Python supports class inheritance. A class may have subclasses but may only inherit directly from one superclass.
What are comments?
Augmenting code with human readable descriptions can help document design decisions.
What are dictionaries?
Dictionaries are Python’s built-in associative data type. A dictionary is made of key-value pairs where each key corresponds to a value. Like sets, dictionaries are unordered. A single dictionary can contain keys of varying types and values of varying types.
What are functions?
Python functions can be used to abstract pieces of code to use elsewhere.
What are function objects?
Python functions are first-class objects, which means that they can be stored in variables and lists and can even be returned by other functions.
What does the len() function do?
Using len(some_object) returns the number of top-level items contained in the object being queried.
What is a list comprehension?
Convenient ways to generate or extract information from lists
EXAMPLE:
» x_list = [1,2,3,4,5,6,7]
» even_list = [num for num in x_list if (num % 2 == 0)]
» even_list
[2,4,6]
> > m_list = [‘AB’, ‘AC’, ‘DA’, ‘FG’, ‘LB’]
A_list = [duo for duo in m_list if (‘A’ in duo)]
A_list
[‘AB’, ‘AC’, ‘DA’]
What are lists?
A Python data type that holds an ordered collection of values, which can be of any type. Lists of Python’s ordered mutable data type. Unlike tuples, lists can be modified in-place.
What are the types of loops in Python?
- For loops - provide a clean iteration syntax
- While loops - permits code to execute repeatedly until a certain condition is met. This is useful if the number of iterations required to complete a task is unknown prior to flow entering the loop.
What does the range() function do?
The range() function returns a list of integers, the sequence of which is defined by the arguments passed to it.
EXAMPLE:
» range(4)
[0, 1, 2, 3]
> > range(2, 8)
[2, 3, 4, 5, 6, 7]
> > range(2, 13, 3)
[2, 5, 8, 11]
What are sets in Python?
Sets are collections of unique but unordered items. It is possible to convert certain iterables to a set.
What is a slice?
A Pythonic way of extracting “slices” of a list using a special bracket notation that specifies the start and end of the section of the list you wish to extract. Leaving the beginning value blank indicates you wish to start at the beginning of the list, leaving the ending value blank indicates you wish to go to the end of the list. Using a negative value references the end of the list (so that in a list of 4 elements, -1 means the 4th element). Slicing always yields another list, even when extracting a single value.
What does the function str() do?
Using the str() function allows you to represent the content of a variable as a string, provided that the data type of the variable provides a neat way to do so. It does not change the variable in place, it returns a “stringified” version of it. It calls the special __str__ method of the object passed to it.
What are String?
Strings store characters and have many built-in convenience methods that let you modify their content. Strings are immutable, meaning they cannot be changed in place.
What are tuples?
A Python data type that holds an ordered collection of values, which can be of any type. They are immutable, meaning they cannot be changed once created.
EXAMPLE:
» x = (1, 2, 3, 4)
» y = (‘spam’, ‘eggs’)
> > my_list = [1,2,3,4]
my_tuple = tuple(my_list)
my_tuple
(1, 2, 3, 4)