Python Flashcards

1
Q

What are functions and methods in Python?

A

Functions are executable blocks of code which complete a specific task.
Methods are similar to functions, but are tied to specific data types.

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

How do you create lists, dictionaries, sets, tuples?

A

List = []
Dict = {k:v}
Set = {}
Tuple = ()

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

File handling operations and modes:

A

Operations: open/read/write/close
Mode: ‘r’ (read), ‘w’ (write), ‘a’ (append), ‘r+’ (read or write), ‘a+’ (append or read)
file_object = open(file_name,mode)

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

Ways to read from a file

A

read() - reads the content of the file
readline() - reads the first line of the file
readlines() - returns all lines in the file as a list

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

Assertion - what is it used for?

A

You can use assert when debugging code - it lets you test a condition and returns True, if not an AssertionError is raised. Do not use asserts for data validation.

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

What is an exception?

A

An exception is a Python object which represents an error. When Python encounters a situation it cannot handle, it raises an exception. It must be handled immediately otherwise it breaks the flow and terminates the program. It can either be a syntax error or a logical error.

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

Exception handling

A

try: (run this code)
except: (execute this code when there is an exception)
else: (no exceptions? run this code)
finally: (always run this code)

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

Raise keyword

A

Allows you to raise your own exceptions

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

User defined Exceptions

A

You can create your own exceptions.
class MyDefinedError(ValueError):
pass

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

Debugging

A

Bugs cause the programme to produce unintended behaviour. Debugging is the process of locating, analysing and correcting bugs.

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

Why is testing important? What are the key phases?

A

Testing allows you to uncover defects/bugs before delivery to client. It makes the software more reliable and easy to use.
Quality Assurance - focuses on processes
Quality Control - focuses on the product
Testing - focuses on actual testing

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

Types of testing

A

Manual
Automated
Black box testing

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

What is unit testing?

A

A scripted code level test in Python to verify a small unit of functionality.

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

What is test driven development?

A

TDD recommends writing tests to check the functionality of the code before writing the actual code. This means you carefully plan the code you write in order to pass the tests.

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

What is mock?

A

Mock is a fake object used in Python unit testing to test functionality without affecting real objects.

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