You are on page 1of 7

ლექცია 10 და 11

# 1

my_dict = {"name":"John", "age":25}

def add_pair(my_dict, key, value):


return my_dict.update({key:value})

add_pair(my_dict, "City", "London")

# 2

my_dict = {"name":"John", "age":25}

def is_key_value(my_dict, key_or_value):


if my_dict.get(key_or_value):
print("Key is found, and its values is {}".format(my_dict.get(key_or_valu
e)))
else:
print("Key is not present in dictionary")

is_key_value(my_dict, "age")
# 3

my_dict = {"first":100, "second":200, "third":300}

def sum_values(my_dict):

sum = 0

for i in my_dict.values():
sum = sum + i

return sum

sum_values(my_dict)

### მეორე გზა

def sum_values(my_dict):
return sum(my_dict.values())

sum_values(my_dict)

# 4

def find_common_key(dict_1, dict_2):


result = {key: dict_1.get(key) for key in dict_1
if key in dict_2 and dict_1.get(key) == dict_2.get(key)}
print(result)

dict_1 = {"name" : "John", "age" : 25, "has_wife" : "No"}


dict_2 = {"name" : "John", "age" : 25, "has_sister" : "Yes"}

find_common_key(dict_1, dict_2)
### მეორე გზა

def find_common_key(dict_1, dict_2):


for (key, value) in set(dict_1.items()) & set(dict_2.items()):
print("{}:{} is presented in both dictionary".format(key, value))

find_common_key(dict_1, dict_2)

# 5

my_dict = [{"a":1}, {"b":2}, {"c":3}]

flattened_dict = {key:value for i in my_dict for key, value in i.items()}

print(flattened_dict)

# 6

n = int(input("Inpit an integer: "))

d = {}

for i in range(n):
d[i] = i ** 2

print(d)

# 7

def min_max(my_dict):
maximum = max(my_dict.values())
minimum = min(my_dict.values())

print("Max is {}, Min is {}".format(maximum, minimum))

my_dict = {'x':500, 'y':5874, 'z': 560}

min_max(my_dict)
# 8

def make_dict(list_1, list_2):


result = dict(zip(keys, values))

return result

keys = ["Apple", "Cherry", "Orange"]


values = ["Green", "Red", "Orange"]

make_dict(keys, values)

# 9

def count_letter(string):
letter_counts = {}

for i in string:
letter_counts[i] = letter_counts.get(i, 0) + 1

return letter_counts

my_string = "Mississippi"

print(count_letter(my_string))

# 10

my_string = "Mississippi"

result = {i:list(my_string).count(i) for i in my_string}

print(result)
###### File I/O Operations

# 11

with open("simple_text.txt", "r") as f:


print(f.read())

# 12

def read_n_lines(file_name, n):


result = open(file_name, "r")

n_lines = result.readlines()[0:n]

return n_lines

read_n_lines("simple_text.txt", 3)

# 13

def read_and_save(file_name):
with open(file_name, "r") as f:
content = f.readlines()

return content

x = read_and_save("simple_text.txt")

print(x)
# 14

def unique_words(file_name):
with open(file_name) as f:
result = set(f.read().split())
return result

print(unique_words("simple_text.txt"))

# 15

def unique_words_write(file_name):
with open(file_name, "r") as f:
result = set(f.read().split())

file = open("unique_word.txt", "w")

for word in result:


file.write(str(word) + "\n")

unique_words_write("simple_text.txt")

# 16

def count_words(file_name):
with open(file_name, "r") as f:
result = len(list(f.read().split()))

return result

count_words("simple_text.txt")
# 17

def copy_text(file_name):
with open(file_name, "r") as f_read:
with open("copied_text.txt", "w") as f_write:
f_write.write(f_read.read())

copy_text("simple_text.txt")

# 18

def write_text(my_list):
with open("new_file.txt", "w") as f:
for i in my_list:
f.write(i + "\n")

colors = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']

write_text(colors)

You might also like