Python Flashcards
What are functions and methods in Python?
Functions are executable blocks of code which complete a specific task.
Methods are similar to functions, but are tied to specific data types.
How do you create lists, dictionaries, sets, tuples?
List = []
Dict = {k:v}
Set = {}
Tuple = ()
File handling operations and modes:
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)
Ways to read from a file
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
Assertion - what is it used for?
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.
What is an exception?
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.
Exception handling
try: (run this code)
except: (execute this code when there is an exception)
else: (no exceptions? run this code)
finally: (always run this code)
Raise keyword
Allows you to raise your own exceptions
User defined Exceptions
You can create your own exceptions.
class MyDefinedError(ValueError):
pass
Debugging
Bugs cause the programme to produce unintended behaviour. Debugging is the process of locating, analysing and correcting bugs.
Why is testing important? What are the key phases?
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
Types of testing
Manual
Automated
Black box testing
What is unit testing?
A scripted code level test in Python to verify a small unit of functionality.
What is test driven development?
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.
What is mock?
Mock is a fake object used in Python unit testing to test functionality without affecting real objects.