Matthieu Choplin
t1 = () # Create an empty tuple
t2 = (1, 3, 5)
t3 = tuple([2 * x for x in range(1, 5)])
t4 = tuple("abac")
tuple2 = tuple([7, 1, 2, 23, 4, 5]) # Create a tuple from a list
print(tuple2)
print("length is", len(tuple2)) # Use function len
print("max is", max(tuple2)) # Use max
print("min is", min(tuple2)) # Use min
print("sum is", sum(tuple2)) # Use sum
print("The first element is", tuple2[0]) # Use indexer
tuple1 = ("green", "red", "blue") # Create a tuple
tuple2 = tuple([7, 1, 2, 23, 4, 5]) # Create a tuple from a list
tuple3 = tuple1 + tuple2 # Combine 2 tuples
print(tuple3)
tuple3 = 2 * tuple1 # Multiply a tuple
print(tuple3)
print(tuple2[2 : 4]) # Slicing operator
print(tuple1[-1])
print(2 in tuple2) # in operator
for v in tuple1:
print(v, end = " ")
print()
tuple1 = ("green", "red", "blue")
tuple2 = tuple([7, 1, 2, 23, 4, 5])
list1 = list(tuple2) # Obtain a list from a tuple
list1.sort()
tuple4 = tuple(list1)
tuple5 = tuple(list1)
print(tuple4)
print(tuple4 == tuple5) # Compare two tuples
s1 = set() # Create an empty set
s2 = {1, 3, 5} # Create a set with three elements
s3 = set((1, 3, 5)) # Create a set from a tuple
# Create a set from a list (comprehension here)
s4 = set([x * 2 for x in range(1, 10)])
# Create a set from a string
s5 = set("abac") # s5 is {'a', 'b', 'c'}
s1 = {1, 2, 4}
s1.add(6)
print(s1) # {1, 2, 4, 6}
s1.remove(4)
print(s1) # {1, 2, 6}
s1 = {1, 2, 4}
s2 = {1, 4, 5, 2, 6}
s1.issubset(s2) # s1 is a subset of s2, print True
s2.issuperset(s1) # s2 is a superset of s1, print False
s1 = {1, 2, 4}
s2 = {1, 4, 2}
s1 == s1 # True
s2 != s1 # False
s1 = {1, 2, 4}
s2 = {1, 3, 5}
s1.union(s2) # {1, 2, 3, 4, 5}
s1 | s2 # equivalent of s1.union(s2)
s1 = {1, 2, 4}
s2 = {1, 3, 5}
s1.intersection(s2) # {1}
s1 & s2 # equivalent of s1.intersection(s2)
s1 = {1, 2, 4}
s2 = {1, 3, 5}
s1.difference(s2) # {2, 4}
s1 - s2 # equivalent of s1.difference(s2)
s1 = {1, 2, 4}
s2 = {1, 3, 5}
s1.symmetric_difference(s2) # {2, 3, 4, 5}
s1 ^ s2 # equivalent of s1.symmetric_difference(s2)
Usage of a set SetDemo.py
Set and List performance compared:
dictionary = {} # Create an empty dictionary
dictionary = {"john":40, "peter":45}
Equivalent to:
dictionary = dict()
dictionary =dict(john=40, peter=45)
To add an entry to a dictionary, use dictionary[key] = value
>>> dictionary["susan"] = 50
>>> print(dictionary)
{'john': 40, 'susan': 50, 'peter': 45}
To delete an entry from a dictionary, use del dictionary[key]
>>> del dictionary[“susan”]
>>> print(dictionary)
{'john': 40, 'peter': 45}
for key in dictionary:
print(key + ":" + str(dictionary[key]))
len(dictionary) returns the number of the elements in the dictionary
>>> dictionary = {"john":40, "peter":45}
>>> "john" in dictionary
True
>>> "johnson" in dictionary
False
>>> len(dictionary)
2
Methods | Meaning |
---|---|
list(dictionary.keys()): list | Returns a dict_keys type of object, that you can convert in a sequence of values with list(dictionary.keys()) |
list(dictionary.values()): list | Returns a dict_values type of object, that you can convert with list(dictionary.values()) |
list(dictionary.items()): tuple | Returns a dict_items type of object, that you can convert in a sequence of tuples (key, value) with list(dictionary.items()). |
clear(): None | Deletes all entries. |
get(key): value | Returns the value for the key. |
pop(key): value | Removes the entry for the key and returns its value. |
popitem(): tuple | Returns a randomly-selected key/value pair as a tuple and removes the selected entry |
Solution
Hide solution
import random
from list_of_countries import COUNTRIES
def main():
countries = list(COUNTRIES.keys())
country_to_guess = random.choice(countries)
capital = input("What is the capital of "
+ country_to_guess + "? ").strip()
if capital.lower() == COUNTRIES[country_to_guess]\
.lower():
print("Your answer is correct")
else:
print("The correct answer should be "
+ COUNTRIES[country_to_guess])
main()