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

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

Dictionary

A dictionary is a collection which is ordered and changeable that does not allow duplicate members. Sets 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)
del 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 tuple 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

List

A list is a collection which is ordered and changeable that allows duplicate members. Lists are created using square brackets.

List

Sample Codeeven_numbers = [2,4,6,8,10]
print(even_numbers)
print(len(even_numbers)) # Number Of Items In List
duplicate_list = [2,4,6,8,10,2,4,6,8,10]
print(duplicate_list)
print(duplicate_list[1]) # Access List Item By Index Number
print(duplicate_list[2:5]) # Range Of Indexes
even_numbers[5] = 12 # Change Item Value By Index Number
print(even_numbers)
even_numbers.append(14) # Add Item To End
print(even_numbers)
even_numbers.insert(1, 2) # Insert Item Value At Index Number
print(even_numbers)
even_numbers.remove(2) # Remove Item Value
print(even_numbers)
even_numbers.pop(1) # Pop Item At Index Number
print(even_numbers)
del even_numbers.pop[1] # Remove Item At Index Number
print(even_numbers)
even_numbers.clear() # Remove All Items
print(even_numbers)
even_numbers.sort() # Sort All Items In Ascending Order
print(even_numbers)
even_numbers.reverse() # Sort All Items In Reverse Order
print(even_numbers)
copied_list = even_numbers.copy() # Make A Copy
print(copied_list)
list1 = [“John”, “Jake”]
list2 = even_numbers.copy()
list3 = list1 + list2 # Join Two Lists
list1.extend(list2) # Add Elements From One List
print(list1)
print(list3)
print(list1.count(“John”)) # Returns Number Of Times The Value “John” Appears
print(list1.index(“John”)) # Returns Index Of First Time The Value “John” Appears

Table : Common Functions For Manipulating Lists

FunctionDescriptionExample
len()Determine how many itemsprint(len(even_numbers)
append()Append itemseven_numbers.append(12)
insert()Insert item at specified indexeven_numbers.insert(1, 2)
remove()Remove specified itemeven_numbers.remove(2)
pop()Pop the item at specified indexeven_numbers.pop(1)
delDelete the item at specified indexdel even_numbers.pop[1]
clear()Remove all values
even_numbers.clear()
sort()Sort in ascending ordereven_numbers.sort()
reverse()Sort in reverse ordereven_numbers.reverse()
copy()Make a copyeven_numbers.sort()
extend()Add elements from one listlist1.extend(list2)
count()Returns number of elements with specified valuelist1.count(2)
index()Returns index of first element with specified valuelist1.count(4)
# Type Some Code

Chapter 4: Arrays

An array specifies a variable that can be indexed as a list in rows and columns. The first index is zero as is common in most programming languages.

Python does not have an array data type. Instead the data type list, tuple, set or dictionary can be used.

Logical Operators

Python logical operators are used to combine conditional statements. Each operand is a conditional that can be evaluated to a true or false value. The value of the conditions determines the overall value of the operator or grouping.

Logical operators are use to control flow of the application. They are normally found as part of a control statement such as if and while.

Table : Logical Operators

LogicNameExample
andAndnum1 and num2
orOrnum1 or num2
notNotnot(num1 or num2)

Logical Operators

Sample Codenum1 = 5
num2 = 10
if ( num1 > 0 and num2 > 0 ):
	print(“Both AND conditions are true”)
else:
	print(“This condition grouping is false”)
if ( num1 > 0 or num2 > 0 ):
	print(“At least one OR condition is true”)
else:
	print(“Both conditions are false”)
# Type Some Code

Increment And Decrement Operators

Python increment operators are used to increment a variable’s value and decrement operators are used to decrement a variable’s value. Python does not support pre- and post-increment and decrement operators allowing for the increment or decrement of the value by one.

Table : Increment And Decrement Operators

Increment / DecrementNameExample
+=Incrementnum+=1
-=Decrementnum-=1

Increment And Decrement Operators

Sample Codenum = 1
num += 1 # Increment By One
print(num1) # Displays 2
num -= 1 # Decrement By One
print(num1) # Displays 1
# Type Some Code