An object is a compound data type. Values of more than one type can be stored together in a single variable. An object is an instance of either a built-in or user defined class. An object is initialized by creating a new statement to instantiate a class.
Multiple objects can be created from a class. Each object has all the properties and methods defined in a class but will have different property values.
A function is a unit of code that represents a sequence of statements. Functions can accept arguments or values and return a single value (or not). In Python, a function is defined using the def keyword.
Function
Sample Codedef my_function():
print(“Hello from my function”)
my_function() # Execute the function
def my_name_function(first_name):
combined = f”My first name is {first_name}”
return combined
greeting = my_name_function(“John”)
print(greeting)
A function is a subprogram that can be called by code externally or internally to the function. It is composed of a sequence of statements called the function body. Values can be passed to a function as parameters or arguments and the function can return a value if applicable.
Python comes with built-in functions that can be called directly to perform a specific task. It is also possible to create custom functions also called user defined functions.
A function is therefore a block of statements that can be used repeatedly in a program. It will not execute automatically and must be executed by a call to the function.
While loop executes a set of statements as long as a condition is true. It requires relevant variables to be ready such as an indexing variable to iterate through a loop.
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
For Loop
Sample Codeeven_numbers = [2,4,6,8,10]
for n in even_numbers:
print(n)
for r in range(2, 44, 3):
print(r)
names = (“Jake”, “John”, “James”)
for name in names:
print(name)
fruits = {“Apple”, “Banana”, “Cranberry”}
for fruit in fruits:
print(fruit)
Loops execute one or more statements up to a specific number of times. Loops allow repetitive tasks to be executed using minimal code. Loops allow complex tasks to be simplified by automating similar operations.
Table : Loops
Type
Description
Syntax
for
loops through code block a specific number of times
for (expr1; expr2; expr3) statement
while
loops through code block until specific condition is false
Python supports logical conditionals for mathematics in the form of comparison operators. Logical conditions are used to perform different actions based of different decisions.
Python has the following conditional statements, if, elif, else and switch.
Use if to specify a block of code to be executed, if a specified condition is true.
If
Sample Codenum1 = 10
num2 = 5
if num1 > num2:
print(“num1 is greater than num2”)
Use elif to specify a new condition to test, if the first condition is false.
Elif
Sample Codenum3 = 11
num4 = 11
if num3 > num4:
print(“num3 is greater than num4”)
elif num3 == num4:
print(“num3 and num4 are equal”)
Use else to specify a block of code to be executed, if the same condition is false.
Else
Sample Codenum5 = 22
num6 = 33
if num5 > num6:
print(“num5 is greater than num6”)
elif num5 == num6:
print(“num5 and num6 are equal”)
else:
print(“num5 is not greater than num6”)
Use switch to specify many alternative blocks of code to be executed. Python uses match case pair instead of the switch case pair of other programming languages.
Switch
Sample Codenum7 = 41
num8 = 77
match num7:
case num7 if(num7 > num8):
print(“num7 is greater than num8”)
case num7 if(num7 == num8):
print(“num7 and num8 are equal”)
case _:
print(“num8 is not greater than num7”)
A dictionary is a collection which is ordered and changeable that does not allow duplicate members. Dictionaries are created using curly brackets and have key and value pairs.
Dictionary
Sample Codepickup_truck = {“brand”: “Ford”, “model”: “F-Series”, “year”: 1984}
print(pickup_truck)
print(len(pickup_truck)) # Number Of Items In Dictionary
the_model = pickup_truck[“model”] # Access Item By Key Name
print(the_model)
the_model = pickup_truck.get(“model”) # Access Item By Key Name And Get Value
print(the_model)
the_keys = pickup_truck.keys() # Get Keys
print(the_keys)
the_values = pickup_truck.values() # Get Values
print(the_values)
the_items = pickup_truck.items() # Get Items As Tuples In A List
print(the_items)
pickup_truck[“year”] = 2024 # Change Item Value By Key Name
print(pickup_truck)
pickup_truck.update({“year”: 2000}) # Modify Item By Update
print(pickup_truck)
pickup_truck[“color”] = “Red” # Add Item Value By Key Name
print(pickup_truck)
pickup_truck.update({“color”: “Silver”}) # Add Or Modify Item By Update
print(pickup_truck)
pickup_truck.pop(“model”) # Pop Item By Key Name
print(pickup_truck)
pickup_truck.pop(“color”) # Remove Item by Key Name
print(pickup_truck)
pickup_truck.clear() # Remove All Items
print(pickup_truck)
copied_dict = pickup_truck.copy() # Make A Copy
print(copied_dict)
copied_dict2 = dict(pickup_truck)
print(copied_dict2)
Table : Common Functions For Manipulating Dictionaries
Function
Description
Example
len()
Determine how many items
print(len(pickup_truck)
get()
Returns the value of the specified key
pickup_truck.get(“model”)
update()
Add elements from one set
pickup_truck.update({“color”: “Silver”})
items()
Returns a list containing a tuple for each key value pair
the_items = pickup_truck.items()
keys()
Returns a list containing the dictionary’s keys
the_keys = pickup_truck.keys()
values()
Returns a list of all the values in the dictionary
A set is a collection which is unordered and unchangeable that does not allow duplicate members. Sets are created using curly brackets.
Set
Sample Codefruits = {“Apple”, “Banana”, “Cranberry”}
print(fruits)
print(len(fruits)) # Number Of Items In Set
fruits.add(“Orange”) # Add Item To Set
print(fruits)
fruits2 = {“pineapple”, “mango”}
fruits.update(fruits2) # Add Items From Another Set
print(fruits)
fruits.remove(“pineapple”) # Remove Item From Set
print(fruits)
fruits.discard(“pineapple”) # Discard Item From Set
print(fruits)
fruits.clear() # Remove All Items
print(fruits)
set1 = {“AA”, “BB”, “CC”}
set2 = {11, 22, 33}
set3 = set1.union(set2) # New Set With All Items From Both Sets
print(set3)
Table : Common Functions For Manipulating Sets
Function
Description
Example
len()
Determine how many items
print(len(fruits)
add()
Add one item
fruits.add(“Orange”)
update()
Add elements from one set
fruits.update(fruits2)
remove()
Remove specified item
fruits.remove(“Banana”)
discard()
Discard specified item
fruits.discard(“Cranberry”)
clear()
Remove all values
fruits.clear()
difference()
Returns a set containing the difference between two or more sets
set3 = set1.difference(set2)
difference_update()
Removes the items in this set that are also included in another, specified set
set1.difference_update(set2)
intersection()
Returns a set, that is the intersection of two other sets
set3 = set1 & set2
intersection_update()
Removes the items in this set that are not present in other, specified set(s)
set1.intersection_update(set2)
symmetric_difference()
Returns a set with the symmetric differences of two sets
set3 = set1.symmetric_difference(set2)
symmetric_difference_update()
Inserts the symmetric differences from this set and another
set1.symmetric_difference_update(set2)
union()
Return a set containing the union of sets
set3 = set1.union(set2)
count()
Returns number of elements with specified value
list1.count(2)
index()
Returns index of first element with specified value
A tuple is a collection which is ordered and unchangeable that allows duplicate members. Tuples are created using round brackets.
Tuple
Sample Codenames = (“Jake”, “John”, “James”)
print(names)
print(len(names)) # Number Of Items In Tuple
print(names[1]) # Display Second Item In Tuple
print(names[-1]) # Display Last Item In Tuple
Table : Common Functions For Manipulating Tuples
Function
Description
Example
len()
Determine how many items
print(len(even_numbers)
count()
Returns number of elements with specified value
list1.count(2)
index()
Returns index of first element with specified value