Python Flashcards

(16 cards)

1
Q

BubbleSort:
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.

A

def sort(container: Collection[_Tt]) -> Collection[_Tt]:
“””
Sort input container with bubble sort

:param container: container of elements to be sorted
:return: container sorted in ascending order
"""
j = 0
for k in range(len(container) - 1):
	swapped = False
	for i in range(len(container) - 1 - j):
		if container[i] > container[i + 1]:
			container[i + 1], container[i] = container[i], container[i + 1]
			swapped = True
	if not swapped:
		break
	j += 1
return container
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

If a list is passed to the function as an argument, deleting any of this argument using the del instruction

A

will affect the argument

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

tup = (1, ) + (1, )
tup = tup + tup
print(len(tup))

A

4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What dir() function returns?

A

The function returns an alphabetically sorted list containing all entities’ names available in the module identified by a name passed to the function as an argument.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The platform module lets you access the underlying platform’s data, i.e., hardware, operating system, and interpreter version information.

A

There is a function that can show you all the underlying layers in one glance, named platform, too. It just returns a string describing the environment; thus, its output is rather addressed to humans than to automated processing (you’ll see it soon).
Sometimes, you may just want to know the generic name of the processor which runs your OS together with Python and your code - a function named machine() will tell you that. As previously, the function returns a string.
A function named system() returns the generic OS name as a string.
python_implementation() → returns a string denoting the Python implementation (expect CPython here, unless you decide to use any non-canonical Python branch).
python_version_tuple() → returns a three-element tuple filled with:
the major part of Python’s version;
the minor part;
the patch level number.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

There is a file named (more or less) module.cpython-xy.pyc where x and y are digits derived from your version of Python (e.g., they will be 3 and 4 if you use Python 3.4).

A
The name of the file is the same as your module's name (module here). The part after the first dot says which Python implementation has created the file (CPython here) and its version number. The last part (pyc) comes from the words Python and compiled.
You can look inside the file - the content is completely unreadable to humans. It has to be like that, as the file is intended for Python's use only.
When Python imports a module for the first time, it translates its contents into a somewhat compiled shape. The file doesn't contain machine code - it's internal Python semi-compiled code, ready to be executed by Python's interpreter. As such a file doesn't require lots of the checks needed for a pure source file, the execution starts faster, and runs faster, too.
Thanks to that, every subsequent import will go quicker than interpreting the source text from scratch.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the id() function do?

A

For CPython, id(x) is the memory address where x is stored.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you destroy an object in Python?

A

CPython currently uses a reference-counting scheme with (optional) delayed detection of cyclically linked garbage, which collects most objects as soon as they become unreachable, but is not guaranteed to collect garbage containing circular references.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What will happen if you put a list in a tuple and then change it?

A

So, if an immutable container (like a tuple) contains a reference to a mutable object, its value changes if that mutable object is changed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is Ellipsis?

A

This type has a single value. There is a single object with this value. This object is accessed through the literal … or the built-in name Ellipsis. Its truth value is true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between ord() and chr() functions?

A

The built-in function ord() converts a code point from its string form to an integer in the range 0 - 10FFFF; chr() converts an integer in the range 0 - 10FFFF to the corresponding length 1 string object. str.encode() can be used to convert a str to bytes using the given text encoding, and bytes.decode() can be used to achieve the opposite.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does hashable mean?

A

An object is hashable if it has a hash value which never changes during its lifetime (it needs a __hash__() method), and can be compared to other objects (it needs an __eq__() method). Hashable objects which compare equal must have the same hash value.

Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.

Most of Python’s immutable built-in objects are hashable; mutable containers (such as lists or dictionaries) are not; immutable containers (such as tuples and frozensets) are only hashable if their elements are hashable. Objects which are instances of user-defined classes are hashable by default. They all compare unequal (except with themselves), and their hash value is derived from their id().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Что делает функция round ()?

A

Она принимает два аргумента: то, что нужно округлить и до какого знака. Если число отрицательное, то округляет до десятых, сотых и тд.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Какой модуль позволяет избежать особенностей процессора при работе с вычислениями десятичных дробей?

A

Decimal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Queue realisation using collections:

A

from collections import deque
queue = deque([“Eric”, “John”, “Michael”]) queue.append(“Terry”) # Terry arrives queue.append(“Graham”) # Graham arrives queue.popleft() # The first to arrive now leaves ‘Eric’
queue.popleft() # The second to arrive now leaves ‘John’
queue # Remaining queue in order of arrival deque([‘Michael’, ‘Terry’, ‘Graham’])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

[].sort() and sorted([]). What is the difference?

A

Sort() changes the object.

Sorted() creates a new one.