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))

Python Dictionaries with AI

After a few months of break from blogging and, above all, coding in Python, I wanted to remind myself how to use Dictionaries in this programming language. I took refresher lessons based on my own materials available here: Sets & Dictionaries, but also by chatting with ChatGPT

#Dic example
person = {
    "name": "Agnieszka",
    "age": 18,
    "country": "Poland",
    "city": "Olsztyn"
}
# basic ops
print(person)
print(person["name"])
print(person.get("age"))
person["age"] = 27
print(person)
person["sex"] = "woman"
print(person)
del person["city"]
print(person)
#loops
for key in person:
    print(key)

for value in person.values():
    print(value)

for key, value in person.items():
    print(f"{key}:{value}")
#check, if name is in dict
if "name" in person:
    print("Name is present")
elif "name" not in person:
    print("Not present")
else:
    print("nothing")

#result: Name is present

#check, if color is in dict and name is not 
if "color" in person:
    print("Name is present")
elif "name" not in person:
    print("Not present")
else:
    print("nothing")

#result: nothing

#check, if color is in person or is not 
if "color" in person:
    print("Name is present")
elif "color" not in person:
    print("Not present")
else:
    print("nothing")

#result: Not present

#Nasted Dictionaries

animals = {
    "cats":{
       "name": "Sweetie",
       "age": "12",
       "color": "black"
    },
    "dogs": {
        "name": "Jack",
        "age": "9",
        "color": "brown"
    }
}
print(animals["cats"]["name"])
print(animals["dogs"]["color"])
print(animals["cats"]["age"],animals["dogs"]["name"])

#ops on dicts with ranges
numbers = {x:x for x in range(0,9)}
print(numbers)

numbers = {x:x*2 for x in range(0,9)}
print(numbers)

numbers = {x:(x*2)/(x+1) for x in range(0,9)}
print(numbers)
#update, sum in
dict1 = {x:x for x in range(0,3)}
dict2 = {y:y for y in range(2,5)}
print(dict1)
print(dict2)
dict1.update(dict2)
print(dict1)
combined_dict = {**dict1, **dict2}
print(combined_dict)

#merging dictionaries
D1 = dict(name = "Aga", age = 35, year = 1989, hobby = "dance")
#print(D1)
D2 = dict(name = "Kuba", age = 41, year = 1984, hobby = "soccer")
D3 = {**D1,**D2}
print(D3)
#output:for same keys we will get values from second dict:
#{'name': 'Kuba', 'age': 41, 'year': 1984, 'hobby': 'soccer'}

D1 = dict(name = "Aga", age = 35, year = 1989, hobby = "dance")
#print(D1)
D2 = dict(zodiac = "Cancer", hair = "brown", sex = "woman")
D3 = {**D1,**D2}
print(D3)
#output:
#{'name': 'Aga', 'age': 35, 'year': 1989, 'hobby': 'dance', \
# 'zodiac': 'Cancer', 'hair': 'brown', 'sex': 'woman'}
#counting letters
text= "welcome really really fat fat Joe"
word_count = {}

for word in text.split():
    word_count[word]= word_count.get(word,0) +1

print(word_count)