#A dictionary can be invoked using the below.
names = {}
type(names)
#Adding a key value pair to a dictionary.
names['alex'] = 20
names
names['sam'] = 33
names['john'] = 49
names['albert'] = 10
names['rex'] = 19
names
len(names) #Returns the total number of key-value pairs in a dictionary.
#To get Value of a key in a dictionary
names['alex']
for name in names:
print(name)
for name in names:
print(name, names[name])
'alex' in names
30 in names #Will check the key only
del names['alex']
names
#To check if a key is present in a dictionary.
search = input("Enter your name: ")
if search in names:
print("{} is present".format(search))
else:
print("{} is not present".format(search))
names.get('sam') #To get the value of a key
names.get('syam', 404) #To print a value if the key doesn't exist in dict
msg = "An operating system consists of various fundamental programs which are needed by your computer so that it can communicate and receive instructions from users; read and write data to hard disks, tapes, and printers; control the use of memory; and run other software. The most important part of an operating system is the kernel. In a GNU Linux system, Linux is the kernel component. The rest of the system consists of other programs, many of which were written by or for the GNU Project. Because the Linux kernel alone does not form a working operating system, we prefer to use the term GNU Linux to refer to systems that many people casually refer to as Linux. "
words = msg.replace(';', '').replace(',', '').replace('.', '').lower().split() #Shown in previous day class to split the string into a list
words
#To find the number of occurances of a word in a list using dictionary.
words_counter = {}
for word in words:
if word in words_counter:
words_counter[word] = words_counter[word] + 1
else:
words_counter[word] = 1
words_counter
#To view words with more than 5 occurances.
for word in words_counter:
if words_counter[word] > 5:
print(word, words_counter[word])
#To check the file size of all conf files in a folder and save them to a dictionary.
import os
file_counter = {}
for file in os.listdir('/etc'):
abs_path = os.path.join('/etc/', file)
if abs_path.endswith('.conf'):
size = os.path.getsize(abs_path)
file_counter[abs_path] = size
file_counter
#To add multiple values to a dictionary in a go.
d = {'syam':10 , 'rex': 15 , 'jake': 20 , 'alex': 10 , 'sam': 50}
d
d.keys() #To view all keys
list(d) #To view all keys in a list
d.values() #To view all values in a dict.
#Tuples are immutable version of a list.
t = ('test' ,'hi', 12 , 'how' , 3456 , 'you')
t[0]
for i in t:
print(i)
del t[0] #Tuples doesnt support deletion
t.count('test')
t.index('test')
t.index('you')
t = ('syam', 'fuji',[1,2,3])
len(t[2])
t[2].append(4)
t
del t[2][0]
t
d.items() #The dictionary items will be shown as a list of tuples.
for i in d.items():
name = i[0]
rank = i[1]
if rank >= 15:
print("{} has a rank of {}".format(name,rank))
l = ['syam', 15 , 'syam@gmail.com']
#We can assign the values to a variable like this.
name = l[0]
rank = l[1]
email = l[2]
#Or by this way
name, rank, email = l
for i in d.items():
name,rank = i
if rank >= 15:
print("{} has a rank of {}".format(name,rank))
#It can be further shortened.
for name,rank in d.items():
if rank >= 15:
print("{} has a rank of {}".format(name,rank))
employee = [['alex', 35000] , ['sam' , 40000] , ['jess', 10000] , ['mark' , 23000]]
employee
for name,mark in employee:
if mark > 25000:
print(name)
file = 'environment.yml'
fh = open(file)
for line in fh:
print(line)