🎁GET 20% OFF Using Code 👉SCHOOL👈 Until End 2024🎁

Chapter 8: Objects

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.

Function Declaration

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)
# Type Some Code

Chapter 7: Functions

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

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.

While Loop

Sample Codecounter = 0
while counter < 10:
	counter += 1
	print(counter)
# Type Some Code

For 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)
# Type Some Code

Chapter 6: Loops

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

TypeDescriptionSyntax
forloops through code block a specific number of timesfor (expr1; expr2; expr3) statement
whileloops through code block until specific condition is falsewhile (expr) statement

Without Loops

Sample Codeeven_numbers = [2,4,6,8,10]
print(even_numbers[0])
print(even_numbers[1])
print(even_numbers[2])
print(even_numbers[3])
print(even_numbers[4])
names = (“Jake”, “John”, “James”)
print(names[0])
print(names[1])
print(names[2])

Table : Common Functions Used With Loops

FunctionDescriptionExample
breakEnd execution of current structurebreak
continueSkip rest of current loop iterationcontinue
# Type Some Code
# Type Some Code

Chapter 5: If Else Conditional Statement

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”)
# Type Some Code

Dictionary

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

FunctionDescriptionExample
len()Determine how many itemsprint(len(pickup_truck)
get()Returns the value of the specified keypickup_truck.get(“model”)
update()Add elements from one setpickup_truck.update({“color”: “Silver”})
items()Returns a list containing a tuple for each key value pairthe_items = pickup_truck.items()
keys()Returns a list containing the dictionary’s keysthe_keys = pickup_truck.keys()
values()Returns a list of all the values in the dictionarythe_values = pickup_truck.values()
clear()Remove all valuespickup_truck .clear()
pop()Removes the element with the specified keypickup_truck.pop(“model”)
# Type Some Code

Set

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

FunctionDescriptionExample
len()Determine how many itemsprint(len(fruits)
add()Add one itemfruits.add(“Orange”)
update()Add elements from one setfruits.update(fruits2)
remove()Remove specified itemfruits.remove(“Banana”)
discard()Discard specified itemfruits.discard(“Cranberry”)
clear()Remove all valuesfruits.clear()
difference()Returns a set containing the difference between two or more setsset3 = set1.difference(set2)
difference_update()Removes the items in this set that are also included in another, specified setset1.difference_update(set2)
intersection()Returns a set, that is the intersection of two other setsset3 = 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 setsset3 = set1.symmetric_difference(set2)
symmetric_difference_update()Inserts the symmetric differences from this set and anotherset1.symmetric_difference_update(set2)
union()Return a set containing the union of setsset3 = set1.union(set2)
count()Returns number of elements with specified valuelist1.count(2)
index()Returns index of first element with specified valuelist1.count(4)
# Type Some Code

Tuple

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

FunctionDescriptionExample
len()Determine how many itemsprint(len(even_numbers)
count()Returns number of elements with specified valuelist1.count(2)
index()Returns index of first element with specified valuelist1.count(4)
# Type Some Code