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. 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
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 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
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
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
Function
Description
Example
len()
Determine how many items
print(len(even_numbers)
append()
Append items
even_numbers.append(12)
insert()
Insert item at specified index
even_numbers.insert(1, 2)
remove()
Remove specified item
even_numbers.remove(2)
pop()
Pop the item at specified index
even_numbers.pop(1)
del
Delete the item at specified index
del even_numbers.pop[1]
clear()
Remove all values
even_numbers.clear()
sort()
Sort in ascending order
even_numbers.sort()
reverse()
Sort in reverse order
even_numbers.reverse()
copy()
Make a copy
even_numbers.sort()
extend()
Add elements from one list
list1.extend(list2)
count()
Returns number of elements with specified value
list1.count(2)
index()
Returns index of first element with specified value
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
Logic
Name
Example
and
And
num1 and num2
or
Or
num1 or num2
not
Not
not(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”)
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 / Decrement
Name
Example
+=
Increment
num+=1
-=
Decrement
num-=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