Python Flashcards
Study about Python programming language (79 cards)
What is the default return value of a function which doesn’t have an explicit return statement
None
What are the “keyword arguments” in functions
Keyword arguments allow to specify the argument names making the code more readable.
What does “**kwargs” represent
Dictionary containing the arbitrary number of keyword arguments passed to a function.
How can one modify the value of a global variable in a function
By declaring the variable of interest as global at the beginning of the function definition.
What is ‘a list’ in Python
Data structure which can hold multiple values called items.
There is a numbers list defined as: numbers[1, 5, -10, 500, 766]. What value is returned by following statement: numbers[-2]
500
What is a slice
A way to extract a part of a sequence like - sequence[start:stop:step]
There is a numbers list defined as: numbers[1, 2, 3, 4, 5]. What is returned by following slice: numbers[:4]
1, 2, 3, 4
There is a numbers list defined as: numbers[1, 2, 3, 4, 5]. What is returned by following slice: numbers[2:4]
3, 4
There is a numbers list defined as: numbers[1, 2, 3, 4, 5]. What is returned by following slice: numbers[1:]
2, 3, 4, 5
There is a numbers list defined as: numbers[1, 2, 3, 4, 5]. What is returned by following slice: numbers[:4:2]
1, 3
How to determine the size of a list?
By calling len() function with the list as argument.
What does list() function do
It creates a list from an iterable object or either an empty list.
What does following list() invokation do:
tuple_data = (1, 2, 3, 4)
newList = list(tuple_data)
It creates a list with same elements as in tuple: [1, 2, 3, 4]
What does following list() invokation do:
dict_data = { ‘a’: 1, ‘b’:2, ‘c’:3}
newList = list(dict_data)
It creates a list with ‘key’ from a dictionary:
[‘a’, ‘b’, ‘c’]
What does index method do on a list
It returns an index of the first occurrence of the element provided as argument if one is found. Otherwise it throws a “ValueError” exception.
What does append method do on a list
It adds an element to end of the list.
What does insert method do on a list
It allows insertion of an element anywhere in the list
What does remove method do on a list
It removes the first occurrence of given element if such element exists.
Are lists mutable or immutable?
Mutable
Are strings mutable or immutable
Immutable
What is the line continuation character which can be used to stretch the python statement over several lines
\
What is a dictionary?
A key-value like data structure.
What does dict() function do?
It creates a dictionary.