Python Flashcards

1
Q

How is Python interpreted?

A

Dynamically, which means the code is interpreted at runtime. There is no compiling before execution and no linking.

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

What is the type system for Python?

A

Duck typing, which means it doesn’t matter if you tell me it is a duck, if it quacks, it is a duck. So the code is not constrained by data types, but it executes an action based on the type of built-in objects being processed.

In other words, the type of an object/expression is determined by the operations involved.

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

What is the cons of duck typing?

A

No compile time and runs right away, so there are very few checks that can be done. Need a lot of testing just to be safe.

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

What are the other implementations of Python (CPython)?

A

PyPy, Cython, Jython

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

What is IPython?

A

An interactive shell for Python. Can run in shell with $ipython

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

What is the pros of using Jupyter Notebook?

A

Collaboration on computational documents, markdown for documentation, many new libraries for data processing,

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

What is a unit test?

A

A unit test is used to test the behaviour of a unit of functionality.

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

What is the module for unit test

A

unittest

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

If we want to test whether add(3, 5) returns 8, what would the unit test look like?

A

def test1(self):
self.assertEqual(add(3, 5), 8)

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

If we want to test whether fat(“Benson”) returns false, what would the unit test look like?

A

def test2(self):
self.assertFalse(fat(“Benson”)

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

How to express not a-z in regex?

A

[^a-z]

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

What is + in regex?

A

1 or more

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

What is * in regex?

A

0 or more

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

How to find “ethelia0” from a string?

A

import re
r = re.compile(r”ethelia[0-9]+”)
v = r.findall(“My name is ethelia”)

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

What is virtualenv?

A

It is a tool to create isolated python virtual environments. It is not a virtual machine! The benefits of having this is when your code needs specific libraries and versions of these.

Different environments provide different libraries and these can change when the system administer updates the system. Using virtualenv ensures that if it works on this machine, it works on others.

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

What is a tool that we can use to identify how much of a program has been executed by a given test?

A

coverage.py

17
Q

What does coverage.py do?

A

It is used to monitor the code coverage of a program, noting parts of the program that is executed and parts that are not executed but could be. Coverage measurement is used to gauge the effectiveness of tests.

18
Q

What is a statement coverage?

A

It tracks if each individual line or statement in the code is executed at least once during a test run.

19
Q

What is a branch coverage?

A

It is a metric that measures whether each possible path (branch) in the code has been executed during testing. Specifically, it ensures that all the branches of conditional statements (like if, else, while, for, try, etc.) are executed at least once.

Branch coverage tracks every potential path that the code could take in conditional statements. For example, in an if statement, branch coverage ensures that both the True and False branches are tested.

20
Q

How to run coverage.py

A

coverage run PYTHON_PROGRAM