día 7
This commit is contained in:
130
day7/excercises.py
Normal file
130
day7/excercises.py
Normal file
@@ -0,0 +1,130 @@
|
||||
it_companies = {'Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon'}
|
||||
A = {19, 22, 24, 20, 25, 26}
|
||||
B = {19, 22, 20, 25, 26, 24, 28, 27}
|
||||
age = [22, 19, 24, 25, 26, 24, 25, 24]
|
||||
|
||||
"""
|
||||
Part 1
|
||||
1. Find the length of the set it_companies
|
||||
2. Add 'Twitter' to it_companies
|
||||
3. Insert multiple IT companies at once to the set it_companies
|
||||
4. Remove one of the companies from the set it_companies
|
||||
5. What is the difference between remove and discard
|
||||
|
||||
"""
|
||||
|
||||
print(len(it_companies))
|
||||
it_companies.add('Twitter')
|
||||
it_companies.update(['Cloudflare', 'Xiaomi', 'Huawei'])
|
||||
|
||||
print(it_companies)
|
||||
|
||||
it_companies.remove('Google')
|
||||
|
||||
try:
|
||||
it_companies.remove('Meta')
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
it_companies.discard('Meta')
|
||||
|
||||
print(it_companies)
|
||||
|
||||
|
||||
"""
|
||||
Part 2
|
||||
1. Join A and B
|
||||
2. Find A intersection B
|
||||
3. Is A subset of B
|
||||
4. Are A and B disjoint sets
|
||||
5. Join A with B and B with A
|
||||
6. What is the symmetric difference between A and B
|
||||
7. Delete the sets completely
|
||||
"""
|
||||
|
||||
C = A.union(B)
|
||||
|
||||
print(C)
|
||||
|
||||
print(A.intersection(B))
|
||||
|
||||
print(A.issubset(B))
|
||||
|
||||
print(A.isdisjoint(B))
|
||||
|
||||
D = (A.union(B)).union(A)
|
||||
|
||||
print(D)
|
||||
|
||||
print(A.difference(B))
|
||||
print(B.difference(A))
|
||||
|
||||
del(A)
|
||||
del(B)
|
||||
del(C)
|
||||
del(D)
|
||||
del(it_companies)
|
||||
|
||||
|
||||
try:
|
||||
print(A)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
try:
|
||||
print(B)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
try:
|
||||
print(C)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
try:
|
||||
print(D)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
try:
|
||||
print(it_companies)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
|
||||
"""
|
||||
Part 3
|
||||
1. Convert the ages to a set and compare the length of the list and the set, which one is bigger?
|
||||
2. Explain the difference between the following data types: string, list, tuple and set
|
||||
3. I am a teacher and I love to inspire and teach people. How many unique words have been used in the sentence? Use the split methods and set to get the unique words.
|
||||
"""
|
||||
|
||||
ages_lt = list(age)
|
||||
|
||||
if (len(age) < len(ages_lt)):
|
||||
print("Ages list is bigger")
|
||||
elif (len(age) > len(ages_lt)):
|
||||
print("Ages set is bigger")
|
||||
else:
|
||||
print("Both Ages list and set are equal in size")
|
||||
|
||||
"""
|
||||
|
||||
Explicación:
|
||||
Un string es una cadena de carácteres, a diferencia de C donde las cadenas son un arreglo de carácteres, en python se los conisidera un tipo más y en ésta entran cualquier carácter unicode y pueden ser impresas en consola como
|
||||
un texto más.
|
||||
Una lista es una colección de datos de cualquier tipo, indexados y editables en cualquier momento, se usan como los arreglos tradicionales, solo que con la diferencia de que éstos últimos, en las listas se admite cualquier tipo
|
||||
de dato.
|
||||
Una tupla es una colección de datos de cualquier tipo, ordenados y no intercambiables ni editables. Suelen ser usados para ser devueltos en una función con múltiples retornos.
|
||||
|
||||
def rand(): return int1, int2 ==> integer1, integer2 = rand()
|
||||
Un conjunto es una colección de elementos únicos sin indexar, en python se usan para guardar datos únicos y sin repetir
|
||||
|
||||
"""
|
||||
|
||||
teacher = "I am a teacher and I love to inspire and teach people."
|
||||
|
||||
splitted = set(teacher.split())
|
||||
|
||||
print(f"number of unique words: {len(splitted)}")
|
||||
|
||||
Reference in New Issue
Block a user