|
| 1 | +## |
| 2 | +# Dictionaries and Sets Lecture |
| 3 | +## |
| 4 | + |
| 5 | +# -*- coding: utf-8 -*- |
| 6 | + |
| 7 | + |
| 8 | +""" |
| 9 | +A set is a data structure in python like a list to store a variety of hetrogenous unique elements. |
| 10 | +Hetrogenous means a set can contain primitive types integer , string , float in it. |
| 11 | +Unique means that each element can occur only once in a set |
| 12 | +""" |
| 13 | + |
| 14 | +# Declaring a set 'Sets' with different string values in it |
| 15 | +Sets={"Element1","Element2","Element1","Element4"} |
| 16 | +# Printing variable 'Sets' using the 'print' function |
| 17 | +print(Sets) |
| 18 | +# Output |
| 19 | +""" |
| 20 | +{'Element1', 'Element4', 'Element2'} |
| 21 | +""" |
| 22 | +# Notice how the output is different from the input in two ways |
| 23 | +# 1) Only unique elements are printed |
| 24 | +# 2) Elements are not printed in the same order as they were stored because in sets order doesnt matter |
| 25 | + |
| 26 | + |
| 27 | +# Here 'if' condition is used along with 'in' keyword to check whether the value "Element1" is present in set 'Sets' |
| 28 | +if "Element1" in Sets: |
| 29 | + # If the value "Element1" is found print "Yes" to console |
| 30 | + print("Yes") |
| 31 | +# Output |
| 32 | +""" |
| 33 | +Yes |
| 34 | +""" |
| 35 | + |
| 36 | + |
| 37 | +# Declaring a list variable 'CountryList' and assigning empty list to it by using '[]' |
| 38 | +CountryList = [] |
| 39 | + |
| 40 | +# For loop is used with 'range(5)' indicating the loop will run 5 times from 0-4 |
| 41 | +for i in range(5): |
| 42 | + # Taking input from user and assigning it to variable named 'Country' using 'input(range_value)' function |
| 43 | + Country = input("Please Enter Your Country: ") |
| 44 | + # 'append(variable_name)' function is used here which adds a new element into the list 'CountryList' |
| 45 | + CountryList.append(Country) |
| 46 | + |
| 47 | +# A new set 'CountrySet' is created using the 'set(variable_name)' function by passing the variable 'CountryList' which will convert the 'CountryList' to a set |
| 48 | +CountrySet = set(CountryList) |
| 49 | + |
| 50 | +# Printing list 'CountryList' |
| 51 | +print(CountryList) |
| 52 | +# Printing set 'CountrySet' |
| 53 | +print(CountrySet) |
| 54 | +# Output |
| 55 | +""" |
| 56 | +Please Enter Your Country: US |
| 57 | +Please Enter Your Country: France |
| 58 | +Please Enter Your Country: India |
| 59 | +Please Enter Your Country: Brazil |
| 60 | +Please Enter Your Country: France |
| 61 | +['US', 'France', 'India', 'Brazil', 'France'] |
| 62 | +{'France', 'India', 'Brazil', 'US'} |
| 63 | +""" |
| 64 | +# First the program asks to entry country names 5 times and then the list and set is printed |
| 65 | +# Notice how the set has changed order and only prints unique elements which is the property of set |
| 66 | + |
| 67 | +# Here 'if' condition is used along with 'in' keyword to check whether the value "Brazil" is present in set 'CountrySet' |
| 68 | +if "Brazil" in CountrySet: |
| 69 | + # If the value "Brazil" is found print "attended" to console |
| 70 | + print("attended") |
| 71 | +# Output |
| 72 | +""" |
| 73 | +attended |
| 74 | +""" |
| 75 | + |
| 76 | +""" |
| 77 | +A dictionary is another data structure in python that also supports hetrogenous data to be stored inside it. |
| 78 | +Rather than using index like used in list , a dictionary supports key-value structure where the key is used like an index and value is stored besides it like how values are stored in a variable |
| 79 | +A dictionary should also contain unique keys and can contain even lists inside of it. |
| 80 | +""" |
| 81 | + |
| 82 | +# Declaring a dictonary variable named 'Dictonary' and assigning keys and values to it |
| 83 | +# "Key" , "Key1", "Key2" are the keys |
| 84 | +# "Value" , "Value1" , "Value2" are the corresponding values |
| 85 | +Dictionary={"Key":"Value","Key1":"Value1","Key2":"Value2"} |
| 86 | +# Printing the dictonary variable 'Dictionary' |
| 87 | +print(Dictionary) |
| 88 | +# Output |
| 89 | +""" |
| 90 | +{'Key': 'Value', 'Key1': 'Value1', 'Key2': 'Value2'} |
| 91 | +""" |
| 92 | + |
| 93 | + |
| 94 | +# Declaring a list variable 'CountryList' and assigning empty list to it by using '[]' |
| 95 | +CountryList = [] |
| 96 | + |
| 97 | +# For loop is used with 'range(5)' indicating the loop will run 5 times from 0-4 |
| 98 | +for i in range(5): |
| 99 | + # Taking input from user and assigning it to variable named 'Country' using 'input(range_value)' function |
| 100 | + Country = input("Please Enter Your Country: ") |
| 101 | + # 'append(variable_name)' function is used here which adds a new element into the list 'CountryList' |
| 102 | + CountryList.append(Country) |
| 103 | + |
| 104 | + |
| 105 | +# Declaring a dictionary variable 'CountryDictionary' and assigning empty dictionary to it by using '{}' |
| 106 | +CountryDictionary={} |
| 107 | + |
| 108 | +# A for loop is used using 'for in syntax' to access the elements of list 'CountryList' and stored it in local variable named 'Country' |
| 109 | +for Country in CountryList: |
| 110 | + # If statement is used in order to check if the country name is present as a key in dictionary 'CountryDictionary' |
| 111 | + if Country in CountryDictionary: |
| 112 | + # upon finding the key the value is accessed using 'DictionaryName[key_name]' synatx and incremented one to it |
| 113 | + CountryDictionary[Country] +=1 |
| 114 | + else: |
| 115 | + # if the key is not found then creating a new key with country name and assigning one to it |
| 116 | + # Notice how no error will be produced which was occuring in list when tried to access an element whcih didnt existed |
| 117 | + CountryDictionary[Country] = 1 |
| 118 | + |
| 119 | +# Printing the dictionary variable 'CountryDictionary' |
| 120 | +print(CountryDictionary) |
| 121 | +# Output |
| 122 | +""" |
| 123 | +Please Enter Your Country: US |
| 124 | +Please Enter Your Country: France |
| 125 | +Please Enter Your Country: India |
| 126 | +Please Enter Your Country: Brazil |
| 127 | +Please Enter Your Country: France |
| 128 | +{'France': 2, 'India': 1, 'Brazil': 1,'US': 1} |
| 129 | +""" |
| 130 | +# No order is mainatined in dictionary as well |
0 commit comments