1/41
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Required as an input parameter
class Student():
def __init__(self, name)getting data in when the function is being called
Set at instantiation
class Student():
def __init__(self, name)
self.name = nameobject has the data - required when class is created
Instance
An instance is a specific object created from a class or is part of a data type, with its own data and behaviour.
Instance Attribute
def __init__(self, name, cid, year, tutor):
super().__init__(str(name), int(cid)) #calling parent constructor
self.year = year #instance attribute
self.tutor = tutor #instance attribute
self.ace_induction = False #instance attributequality specific to that method relating to the class
An instance attribute is a variable defined in a class using self that stores data specific to each instance of the class.
Class Attribute
class DEStudent():
'''class just for DesEng students'''
Faculty = 'Engineering'bf the def __init__(self) which is a method
A class attribute is a variable defined in a class that is shared by all instances (objects) of that class
Function
def greet(name):
print("Hello", name)Reusable block of code that performs a specific task
Method
class Student:
def __init__(self, name):
self.name = name
def say_hello(self): # ← this is a method
print("Hi, I'm", self.name)A method is a function defined inside a class that operates on instances of that class, typically using self.
Super()
class DEStudent(ImperialStudent):
def__init__(self):
super().__init__(name, cid)where name and cid were defined in an earlier class.
req sub class to have the super class in its brackets
oop
object oriented programming - class, attributes and methods
ssh
secure shell - establishes a secure encrypted tunnel btwn client and a server over insecure networks.
prevents attacks and maintains confidentiality and integrity.
when Git is used to securely push code to a remote repository
seaborn
a Python data visualisation library built on top of Matplotlib that provides a higher-level interface for creating attractive and informative statistical graphics
pandas
an extension of Python to process and manipulate tabular data
matplotlib
a Python library that allows users to generate a wide range of static, animated, and interactive visualisations, such as line graphs, bar charts, and scatter plots, from numerical data
num.py
is a Python library used for efficient numerical computing, providing support for arrays, matrices, and mathematical operations on large datasets
Faster than standard Python lists
module
file containing Python definitions and statements; could contain one or more classes; the name of the module is the name of the file (with .py on the end)
can be imported to organise and reuse code.
isinstance
built-in Python function used to check whether an object is an instance of a specified class or data type
Boolean
can only have 2 values, True or False
Pass by reference
for mutable data, a method of passing arguments to a function where the function receives a reference to the original object, allowing it to modify the original data
pass by value
for immutable data, a method of passing arguments where a copy of the value is passed to the function, so changes made inside the function do not affect the original variable
mutable
object whose value can be changed after it is created
iterators
An iterator is something that lets you go through a collection one item at a time.
numbers = [10, 20, 30]
it = iter(numbers) # create iterator
print(next(it)) # 10
print(next(it)) # 20
print(next(it)) # 30.append
a list method in Python that adds a single element to the end of a list
df
DataFrame (df) is a two-dimensional data structure in Python (from the pandas library) used to store and manipulate tabular data with rows and columns
df.head()
Prints the first few rows of the data
brackets holds a number only
df.info()
Shows you the columns, if any columns have null data, and the variable type of each column
df.shape
Will give you the size of the data (number of rows x number of columns)
df.mean()/.median()
Gives you the average value of each column
df.std()
Gives you the standard deviation of each column
df.unique()
Gives you the different values that exist
whitespace sensitive
A language where spaces, indentation, and newlines affect the syntax. In Python, indentation defines code blocks.
Dictionaries
dictionary_name = { 'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3'}
print(dictionary_name.get('key_1')) # 'value_1'
dictionary_name['key_1'] = 20 # update
dictionary_name['key_4'] = "A" # add new key-value pair
for key, value in dictionary_name.items():
print(key, value)
student.keys() # returns all keys
student.values() # returns all values
student.items() # returns (key, value) pairs
student.clear() # empties dictionaryoutput:
‘value_1’
'key_1' 'value_1'
‘key_2' 'value_2'
'key_3' 'value_3'
‘key_4’ ‘A’
len()
for a string
len('hello') #5gives number of characters
for a list
numbers = [1, 2, 3, 4]
len(numbers) #4gives # items
for dictionaries
dict = { 'name': 'Sally', 'age': 19}
len(dict) #2 gives number of key-value pairs
iterable object
A Python object with items that can be counted in a sequence. Examples
include lists, tuples, and dictionaries.
Version Control
Software used to track the changes made in a collection of code files. In
this module we have used git, which is an open-source version control
system that tracks code kept in repositories. We have used the
commercial service GitHub to manage our git repositories.
UML class diagram
classes
attributes
methods
PEP8
python style guidelines
e.g. snakecase
Comparison of code
naming
style
clarity
functionality
whether one is preferable and why
Duck typing
Objects are used based on the methods/attributes they have, not their type. If an object behaves correctly (e.g. has __len__), it can be used.
Inheritance
A class (child) is created from another class (parent) and automatically gets its attributes and methods, with the option to override or extend them.
Encapsulation
Bundling data and methods inside a class while limiting direct access to some data (e.g. using _), to protect internal state.
Operator overloading
Defining special methods (e.g. __add__) to change how operators like +, -, == work for objects of a class.
Garbage collection
Automatic memory management where unused objects are removed, mainly using reference counting (and handling cycles).