Loops for lists, tuples, dicts

Tpl = (1,8,2,4,3,5,7,6,9,0)
Lst = [1,2,2,3,3,3,1.1,2.1,3.1]
Dct1 = dict(name = "Aga", age = 35, year = 1989, hobby = "dance")
Dct2 = {'zodiac': "Cancer",'hair': "brown",'sex': "woman"}

for x in Tpl:
    print(x)
for x in sorted(Lst):
    print(x)
for item in Dct1:
    print(item)
for val in Dct1.values(): 
    print(val)

#Tuples

def calcs(a,b,c):
    return a+b, a**a, b/a, b**c
print(calcs(1,2,3))

names = ("Peter", "Cassandra", "April")
ages = (22, 47, 89)
for name, age in zip(names,ages):
    print(f"{name} is {age} years old")

t_array = ((1,2),(3,4),(5,6))

for in_tuple in t_array:
    for item in in_tuple:
        print(item, end = "::")
    print()

even_numbers = sorted(tuple(num for num in Tpl if num % 2 == 0))
squared_numbers = tuple(num ** 2 for num in Tpl)
print(even_numbers)
print(squared_numbers)

# A dictionary with tuple keys
points = {
    (1, 2): 'low',
    (3, 4): 'medium',
    (5, 6): 'high'
}

# Iterate through the dictionary items
for coordinates, label in points.items():
    print(f"Point {coordinates} is a {label} score")

#Dictionaries and lists


Lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Dictionary to hold even and odd numbers
categorized_numbers = {"even": [], "odd": []}

# Loop through each number in the list
for number in Lst:
    # Check if the number is even
    if number % 2 == 0:
        categorized_numbers["even"].append(number)
    else:
        categorized_numbers["odd"].append(number)

# Print the categorized numbers
print(categorized_numbers)

#Calculates the average grade for each worker

workers = {
    "Peter": [5, 4, 5],
    "Cassandra": [3, 4, 4],
    "April": [2, 1, 3]
}

# Dictionary to hold the average grades and pass/fail status
workers_score = {}

# Loop through each student in the dictionary
for student, grades in workers.items():
    # Calculate the average grade
    average_grade = round((sum(grades) / len(grades)),2)

    # Determine if the student passed
    if average_grade >= 3.5:
        status = "Passed"
    else:
        status = "Failed"

    # Store the result in the dictionary
    workers_score[student] = {"average": average_grade, \
                              "status": status}

# Print the student results
print(workers_score)

#Example from AI – manages an inventory of items

inventory = {
    "apples": 10,
    "bananas": 5,
    "oranges": 8
}

sales = {
    "apples": 4,
    "oranges": 2
}

restocks = {
    "bananas": 10,
    "oranges": 5
}

# Update inventory based on sales
for item, quantity_sold in sales.items():
    if item in inventory:
        inventory[item] -= quantity_sold

# Update inventory based on restocks
for item, quantity_restocked in restocks.items():
    if item in inventory:
        inventory[item] += quantity_restocked
    else:
        inventory[item] = quantity_restocked

# Print the updated inventory
print(inventory)

Functions for Tuples and Lists

Another step of refreshing my knowledge of programming in Python, using AI support, was to check whether I was able to build useful functions for lists and tuples.

#Tuples
T1 = ()
T2 = (1, 3, 2)
T3 = 4, 5, 6
T4 = ("Agnieszka", "Michal", "Kuba")
T5 = ("Nie", "lubie", "poniedzialkow")

print(T1, T2, T3)
print(T4, T5)
print(type(T1),type(T2),type(T3),type(T4),type(T5))
print(T2+T3)
print(T4+T5)

#Functions
print(sorted(T2))
print(max(T2))
print(min(T3))

print(len(T2))
print(len(T4))

print(sum(T2))
print(sum(T3))
print(sum(T2+T3))

print(any(T1))
print(any(T3))
print(all(T1))
print(all(T5))

#indexing
print(T2[0])
print(T2[0:2])
print(T4[1:3])
print(T4.index("Kuba"))

#enlarge tuples
print(T2*2)
print(T3*3)
print(T4*4)

T6 = T5 *5
print(T6)
print(T6.count("Nie"))
print(T6.count("nie"))
#Lists
T1 = (1,3,2)
L1 = list(T1)
L2 = [6,8,9]
L3 = ["Agnieszka", "Michal", "Kuba"]

print(T1,L1,L2,L3)
print(type(T1),type(L1),type(L2),print(L3))
print(sorted(L1))
print(sorted(L2))

L2.append(7)
print(sorted(L2))
L1.extend(L2)
print(L1)
L1.insert(3,11)
print(L1)
L1.pop(3) #usuwanie dodanej liczby 11 wg indexu
print(len(L1))
L1.insert(8,21)
print(L1)
L1.remove(21) #usunwanie konkretnej wartości
print(L1)
L1.sort(reverse = True)
print(L1)
L2.clear()
L2 = [1,2,2,3,3,3]
print(L2)
print(L2.count(3))