Prerequisites
- Python3
- Unix command line
- A basic understanding of computers
Variable Types
Python programming language has several variable types, some of which include:- Strings: "hello", "goodbye"
- Integers: 1, 2, 3
- Lists: [1, 2, "hello", "goodbye"]
- Dictionaries: {1: "hello", 2: "goodbye"}
- Tuples: (1, 2, 3, 4, 5)
- Floats, etc.
In Python, these variable types are usually assigned to variables by using an equals sign ("=") with the name of the variable on the left and the variable type on the right.
name_of_variable = variable_type
Variable Examples
x = "hello"
y = 3
Functions
Functions handle a variety of processes based on utilizing the variable types defined above. Functions are written by using the keyword 'def' followed by the name of the function, a set of parentheses, and then a colon. The body of the function is written with a single indentation and is ended with a 'return' statement or by unindenting the next line.Function Example with Print Statement
def function_name():
greeting = "hello"
print(greeting)
If this function is saved to a python file, the program can be run in the terminal using:
Import Statement
$ python3
>>> from file_name import function_name
>>> function_name()
hello
Python Interpreter
$ python3 -i file_name.py
>>> function_name()
hello
In order for the above methods to work, the terminal needs to be in the directory where the python file is saved at.
$ pwd
/Users/username/.../python_directory
$ ls
file_name.py
Another way for the function to run is by declaring it at the bottom of the python file and running the python file in the terminal using either of the above methods.
Function Example Calling the Function Within the File
# file_name.py
def function_name():
greeting = "hello"
print(greeting)
function_name()
To Run in Terminal
$ python3 file_name.py
hello
To Run with Python Interpreter
$ python3 -i file_name.py
hello
>>>
Function Parameters and Return Statement
Functions are able to have multiple parameters assigned to them that the user can use to implement variables within the function. These parameters are optional and are implemented within the parentheses following the function name.
def function_name(parameter1, parameter2):
... some code written here
Instead of using a 'print' statement to end the function, programmers will usually 'return' the output to utilize it in a variety of ways. The example below showcases a simple addition function using two parameters, a variable assignment, and a return statement.
Addition Function Example
# math.py
def add(x, y):
output = x + y
return output
To Run in Terminal and Assigning a Variable for Use Cases
$ python3
>>> from math import add
>>> add(2, 3)
5
>>> solution = add(2, 3)
>>> solution * 2
10
>>> solution + 2
7
To Run with Python Interpreter and Assigning a Variable for Use Cases
$ python3 -i math.py
>>> add(2, 3)
5
>>> solution = add(2, 3)
>>> solution * 2
10
>>> solution + 2
7
Multiple Functions in File
Multiple functions can be declared in a file to perform different processes. These functions can act as standalone, self-contained code or interact with one another by calling on them within other functions.Multiple Functions Example
# math.py
def add(x, y):
addition = x + y
return addition
def subtract(w, z):
subtraction = w - z
return subtraction
def multiply_answers(a, b, c, d):
sum = add(a, b)
difference = subtract(c, d)
multiply = sum * difference
return multiply
Running in Terminal with Import Statement
$ python3
>>> from math import add, subtract, multiply_answers
>>> add(2, 3)
5
>>> subtract(5, 2)
3
>>> multiply_answers(2, 3, 5, 2)
15
>>> (2 + 3) * (5 - 2)
15
>>> addition = add(2, 3)
>>> subtraction = subtract(5, 2)
>>> addition * subtraction
15
Running in Python Interpreter
$ python3 -i math.py
>>> add(2, 3)
5
>>> subtract(5, 2)
3
>>> multiply_answers(2, 3, 5, 2)
15
>>> add(2.2, 3.2)
5.4
>>> subtract(5, 10)
-5
Classes
Classes are used to define a module of a program. These modules serve as the basic building blocks to complete complex objectives or solve problems with multiple parts. Each module can be used to solve a portion of a problem or written to represent a section of a program.Classes are written by using the 'class' keyword followed by the class name and a colon. Class names are written using CamelCase, which is the first letter in each word capitalized with no spaces.
Class Name Example
class ClassName:
... code written here
Within a python class, functions are written to serve singular purposes and these functions combine to make up the use cases for the class. In the Python programing language, the class is initialized with an __init__() function that specifies the variables being used within the class.
Class Example
# math.py
class MathClass:
def __init__(self, a, b):
self.a = a
self.b = b
The first parameter of any function within the class will usually refer to itself, hence the 'self' keyword being used in the above example. And, the variables within the __init__() function refer to the MathClass by using 'self.variable_name = variable_name'. This allows those variables to be inherited by the other functions within the class or another class if it specifies this class as a parameter.
Class Inheritance Example
# math.py
class MathClass:
def __init__(self, a, b):
self.a = a
self.b = b
def add(self):
addition = self.a + self.b
return addition
def multiply(self):
multiplication = self.a * self.b
return multiplication
To Import a Class
Importing a class imports all of its functions as well.
from file_name import ClassName
Running in Terminal with Import Statement
$ python3
>>> from math import MathClass
>>> math_var = MathClass(2, 3)
>>> math_var.add()
5
>>> math_var.multiply()
6
Running with Python Interpreter
$ python3 -i math.py
>>> math_var = MathClass(2, 3)
>>> math_var.add()
5
>>> math_var.multiply()
6
Parent Class Inheritance
A class can inherit from another class' functions and variables by specifying the second class in the first class' parameters.
class ParentClass:
__init__(self, variable_name):
... some code here
class ChildClass(ParentClass):
def function_name(self):
... some more code here
Parent Class Inheritance Example
# aircraft.py
class Aircraft:
def __init__(self, name):
self._name = name
def name(self):
return self._name
class AirbusA319(Aircraft):
def model(self):
return "Airbus A319"
class Boeing777(Aircraft):
def model(self):
return "Boeing 777"
Running in Terminal
$ python3
>>> from aircraft import AirbusA319, Boeing777
>>> airbus = AirbusA319("Fly High")
>>> airbus.name()
'Fly High'
>>> airbus.model()
'Airbus A319'
>>> boeing = Boeing777("The Big Sis")
>>> boeing.name()
'The Big Sis'
>>> boeing.model()
'Boeing 777'
Running with Python Interpreter
$ python3 -i aircraft.py
>>> airbus = AirbusA319("Fly High")
>>> airbus.name()
'Fly High'
>>> airbus.model()
'Airbus A319'
>>> boeing = Boeing777("The Big Sis")
>>> boeing.name()
'The Big Sis'
>>> boeing.model()
'Boeing 777'
Inheritance with the Super Method
To inherit the parent class' functions and variables while being able to specify their values in a child class, the __super__() method is utilized in the __init__ function of a child class.Super Method Example
# contact.py
class Contact:
def __init__(self, name, address):
self.name = name
self.address = address
class Friend(Contact):
def __init__(self, name, address, phone):
super().__init__(name, address)
self.phone = phone
Running in Terminal
$ python3
>>> from contact import Friend
>>> friend = Friend("Name", "Address", "555-555-5555")
>>> friend.name
'Name'
>>> friend.address
'Address'
>>> friend.phone
'555-555-5555'
Running with Python Interpreter
$ python3 -i contact.py
>>> friend = Friend("Name", "Address", "555-555-5555")
>>> friend.name
'Name'
>>> friend.address
'Address'
>>> friend.phone
'555-555-5555'