A variable that can be passed to a function.
def add(a, b): # The add function gets two arguments: a and b
return a + b # The arguments a and b can be used in the function
Is what you do when you connect a value to a variable. Example:
name = "Vera" # assignment of value "Vera" to variable name
employee = Employee("Vera") # assignment of an Employee object to variable employee
Something that describes an object. From one class you can instantiate as many objects as you want.
class Employee:
name = "Vera"
This is a function that will be called when you create an instance of a class. You can pass arguments to this function to inject data in the new object.
class Employee:
def __init__(self, name, salary): # this is a constructor that allows
self.name = name # you to set a name and salary when
self.salary = salary # you instantiate an employee.
e = Employee("Vera", 2200)
Coupling is when two parts of the system are linked in a way that changes in part A causes changes in part B or at least make part B recompile.
As an alternative, use loose coupling, which in OO Principles means: Program to interfaces, not implementation.
Dunder (Double Under) or magic methods in Python are the methods having two prefix and suffix underscores in the method name. Examples: __init__
, __len__
, __repr__
.
Used to loop over elements in a list.
names = ["Vera", "Chuck", "Dave"]
for n in names: # go through all elements in the names list
print(n) # n is one element from the list here
Output:
Vera
Chuck
Dave
A function is a piece of code that only runs when it is called. Calling a function is called a function call
def add(a, b): # this is the function definition
return a + b
add(4, 5) # this is the function call
Idiom is the syntactical, grammatical, or structural form peculiar to a language. When something is idiomatic, it is accepted as structurally correct.
The space between the left margin and the code. Python expects a block of code after a colon(:
). Every line that is indented is part of that code block.
if x > 10:
print("x is greater than 10") # this line is indented
print("enter a new value") # this line is also indented
What you use to get one element from a list or tuple. Remember that the first index in a Python list is 0.
names = ["Vera", "Chuck", "Dave"]
print(names[0]) # will show the first element from the names list
print(names[1]) # will show the seconds element from the names list
Is what you do if you create an object from a class
class Employee:
name = "Vera"
employee = Employee() # an object is instantiated
Input/Output
A collection of code with a specific purpose. Examples of libraries are:
Are values in the code that are unexplained.
total = 4 * 2.5 # values 4 and 2.5 are not explained
Prevent this by creating variables for the values
price = 2.5
amount = 4
total = amount * price # this calculation explains what is does
A Module is a file containing Python code. There are two common reasons to create a module:
Is a way of programming where code is organised into objects. These objects usually combine data and methods that operate on that data. To create an object, you first need to define its design. This design is called a class and from one class you can create as much objects as you need.
A method to combine literal text and variables to compose a message that can be shown to the user.
seconds_ago = 5
print(f"Jenny wrote {seconds_ago} seconds ago")
output:
Jenny wrote 5 seconds ago
A variable connects a name that means something to you to a value. Once you assign a value to a name, the value will be stored in the memory of your computer.
age = 26 # assigning the value 26 to a variable called age
print(age) # getting the age back from memory
output:
26