Python Flashcards
What is the precedence of comparison operatos?
“The operators and >= all have the same level of precedence and evaluate left to right. The operators == and != have the same level of precedence and evaluate left to right. Their precedence is lower than that of and >=.”
What is cloud computing?
“Cloud computing: You can use software and data stored in the “cloud”—i.e., accessed on remote computers or servers via the Internet and available on demand—rather than having it stored locally on your desktop, notebook computer or mobile device. This allows you to increase or decrease computing resources to meet your needs at any given time, which is more cost effective than purchasing hardware to provide enough storage and processing power to meet occasional peak demands. Cloud computing also saves money by shifting to the service provider the burden of managing these apps such as installing and upgrading the software, security, backups and disaster recovery.”
What is an interpreted language?
“Compiling a large high-level language program into machine language can take considerable computer time. Interpreter programs, developed to execute high-level language programs directly, avoid the delay of compilation, although they run slower than compiled programs.”
What is a list in Python?
” One of the most common is a list, which is a comma-separated collection of items enclosed in square brackets [and]. Python only contains basically lists and dicts and sets and tuples a_list = [1, 2, 3]”
Example of matrix for in Python
“for i, row in enumeratea:\t …: for j, item in enumeraterow:\t …: printf’a[{i}][{j}]={item} ‘, end=’ ‘\t …: print”
Example of combination with map and filter
“listmaplambda x: x 2,\t …: filterlambda x: x 2 != 0, numbers”
Example of filter function
“listfilterlambda x: x 2 != 0, numbers”
Example of map function
“listmaplambda x: x 2, numbers”
Define the format of a lambda in Python
“lambda parameter_list: expression”
Example of a list comprehension
“[item for item in range1, 11 if item 2 == 0]”
How we can construct a list from a given range?
“list2 = listrange1, 6”
Put the list upside down.\t>…
“color_names.reverse”
How do we get the number of ocurrences of an element in a list?
“responses.counti”
How do we delete an element from a list?
“color_names.remove’green’”
How do we add more than one item to a list?
“color_names.extend[‘indigo’, ‘violet’]”
How do we add an item to the list?
“color_names.append’blue’”
How do we add an element to a specified position of a list?
“color_names.insert0, ‘red’”
How can we prevent an index error
“You can use the operator in to ensure that calls to method index do not result in ValueErrors for search keys that are not in the corresponding sequence:\tIn [11]: key = 1000\t\tIn [12]: if key in numbers:\t …: printf’found {key} at index {numbers.indexsearch_key}’\t …: else:\t …: printf’{key} not found’\t …:\t1000 not found”
Order a list without changing its original content.\t>…
“ascending_numbers = sortednumbers”
Order a list beginning at the end.\t>…
“numbers.sortreverse=True”
Order a list.\t>…
“numbers.sort”
Delete a member with a specific index position in a list.
“del numbers[-1]”
How can you create an empty list from a range?
“IPython Session”
Assume you have a list called names. The slice expression _____ creates a new list with the elements of names in reverse order.
Answer:
names[::-1]
How can we slice a list up/until the end?
“In [3]: numbers[:6]\tOut[3]: [2, 3, 5, 7, 11, 13]\t\tIn [4]: numbers[0:6]\tOut[4]: [2, 3, 5, 7, 11, 13]”
How do we repeat a character M times in Python?
“When used with a sequence, the multiplication operator repeats the sequence—in this case, the string ““—value times.”
Code to iterate using appropiate technique.\t>…
“for index, value in enumeratecolors:\t …: printf’{index}: {value}’”
How do we iterate through a list?
“The preferred mechanism for accessing an element’s index and value is the built-in function enumerate.”
Can tuples be modified?
“As with lists, the += augmented assignment statement can be used with strings and tuples, even though they’re immutable.”
Code of an element addition to a list in Python.\t>…
“for number in range1, 6:\t …: a_list += [number]”
What happens to a reference when its variable is modified?
“modifying the variable creates a new object”
Explain how Python handles the storage of:\t> x = 7
“the variable x does not actually contain the value 7. Rather, it contains a reference to an object containing 7 and some other data we’ll discuss in later chapters stored elsewhere in memory.”
How does Python handle references?
“When a function call provides an argument, Python copies the argument object’s reference—not the object itself—into the corresponding parameter.”
What is the approach of passing parameters in Python?
“Python arguments are always passed by reference. Some people call this pass-by-object-reference, because “everything in Python is an object.””
Define a function with an arbitrary number of parameters.\t>…
“def averageargs:\t …: return sumargs / lenargs”
Does the order of function parameters matter in Python?
“When calling functions, you can use keyword arguments to pass arguments in any order.”
Define a function with default parameters.\t>…
“def rectangle_arealength=2, width=3:”
Do Python support constant variables?
“True/False Python does not have constants.\tAnswer: True.”
Given a module you can use it…\timport module\tmodule.function
“math.sqrt900”
Answer: module
“Fill-In Every Python source code .py file you create is an .”