# harshitagupta3199/lab-computing-7

a year ago
In [ ]:
``````age={'krishna':41,'agham':18,'digvijay':18,'chandani':21}

for k in age:
print(k,age[k])
inv_age={}
for k in age:
v=age[k]
if v not in inv_age:
inv_age[v]=[k]
else:
inv_age[v].append(k)
print(inv_age)
``````
In [ ]:
``````age={'krishna':41,'agham':18,'digvijay':18,'chandani':21,'priyal':14}

name=['krishna','chandani','agham','priyal']
agelist=[18,21,18,14]

searchname=input()
for i in range(len(name)):
if(name[i]==searchname):
print(agelist[i])
break
print(age[searchname])
# searchage=int(input("find the person with age:"));
# for j in range(len(agelist)):
#     if(agelist[j]==searchage):
#         print(name[j])``````
In [ ]:
``````mystr="papa kehte hai, beti engineer banegi"
agelist=[18,21,18,41,18,21]
age={'krishna':41,'agham':18,'digvijay':18,'chandani':21,'priyal':14}

def histogram(s):
hist={}
for i in s:
if i not in hist:
hist[i]=1
else:
hist[i]=hist[i]+1
return hist

print(histogram(mystr))
print(histogram(agelist))
print(histogram(age.values()))
``````
In [ ]:
``````fp=open('lorenz.py')
count_line=0
num_words=0
characters=0
for line in fp:
print(line)
count_line=count_line+1
words=line.split()
print(words)
num_words+=len(words)
print(len(line))
characters=characters+len(line)
print(count_line)
print(num_words)
print(characters)

``````
In [ ]:
``````fp=open('output.txt','w')
fp.write("good morning\n")
fp.write("good afternoon")
fp.close()
``````
In [ ]:
``````fp=open('output.txt')
for line in fp:
print(line)
``````
In [ ]:
``````list1=[1,2,1,2,3,4,5]
dup=[]
for i in range(len(list1)):
j=i+1
for j in range(j,len(list1)):
if(list1[i]==list1[j] and list1[i] not in dup):
dup.append(list1[i])
print(dup)

``````
In [ ]:
``````def dictionary():
key_value={}
key_value[2] = 56
key_value[1] = 2
key_value[5] = 12
key_value[4] = 24
key_value[6] = 18
key_value[3] = 323
#     print(key_value)

for i in sorted(key_value.keys()):
print( key_value[i])

dictionary()
``````
In [ ]:
``````fp=open('output.txt')
fout=open('dup.txt','w')
for line in fp:
print(line)
fout.write("Read this line " + line+"\n")
fout.close()
``````
In [ ]:
``````fp=open("lorenz.py")
# fout=open("dup.txt",'w')
for line in fp:
#     fout.write(line + "\n")
while(line!=' '):
print(line)
#     fout.write(line)
# fout.close()

``````
In [3]:
``````fp=open("lorenz.py")
fout = open("dup.txt",'w')
print(cont)

for i in range(len(cont)):
if(i%2==0):
fout.write(cont[i])
else:
pass
fout.close()

``````
```['hello how are you\n', 'are you good\n', 'hiii\n', 'byeeee\n'] ```
In [8]:
``````fp=open("lorenz.py")
fout = open("dup.txt",'w')
print(cont)
fout.write(cont[0])
fout.write(cont[-1])
# for i in range(len(cont)):

fout.close()
``````
```['hello how are you\n', 'are you good\n', 'hiii\n', 'byeeee\n'] ```
In [1]:
``````d={'a':0,'b':1,'c':2}
t=d.items()
``````
In [2]:
``````t

``````
Out[2]:
``dict_items([('a', 0), ('b', 1), ('c', 2)])``
In [3]:
``````eng2sp=dict()
eng2sp
``````
Out[3]:
``{}``
In [5]:
``eng2sp['one']='uno'``
In [6]:
``eng2sp``
Out[6]:
``{'one': 'uno'}``
In [7]:
``eng2sp['two']='dos'``
In [8]:
``eng2sp``
Out[8]:
``{'one': 'uno', 'two': 'dos'}``
In [9]:
``len(eng2sp)``
Out[9]:
``2``
In [11]:
``'one' in eng2sp``
Out[11]:
``True``
In [12]:
``'two' in eng2sp``
Out[12]:
``True``
In [13]:
``'three'in eng2sp``
Out[13]:
``False``
In [14]:
``'uno' in eng2sp``
Out[14]:
``False``
In [15]:
``vals=eng2sp.values()``
In [16]:
``'uno' in vals``
Out[16]:
``True``
In [17]:
``keys=eng2sp.keys()``
In [18]:
``'one'in keys``
Out[18]:
``True``
In [19]:
``keys``
Out[19]:
``dict_keys(['one', 'two'])``
In [20]:
``vals``
Out[20]:
``dict_values(['uno', 'dos'])``
In [21]:
``t=('a','b','c','d','e')``
In [22]:
``t``
Out[22]:
``('a', 'b', 'c', 'd', 'e')``
In [23]:
``t1='a',``
In [24]:
``type(t1)``
Out[24]:
``tuple``
In [25]:
``t2=('a')``
In [26]:
``type(t2)``
Out[26]:
``str``
In [42]:
``t=tuple('lupins')``
In [43]:
``t``
Out[43]:
``('l', 'u', 'p', 'i', 'n', 's')``
In [44]:
``t[0]``
Out[44]:
``'l'``
In [45]:
``t[1:3]``
Out[45]:
``('u', 'p')``
In [46]:
``t=('A',)+t[1:]``
In [47]:
``t``
Out[47]:
``('A', 'u', 'p', 'i', 'n', 's')``
In [48]:
``t=t[:3]+('X',)+t[4:]``
In [49]:
``t``
Out[49]:
``('A', 'u', 'p', 'X', 'n', 's')``
In [50]:
``d=dict()``
In [52]:
``````d={'a':0,'b':1,'c':2}
t=d.items()
``````
In [53]:
``````for key,values in t:
print(key,values)
``````
```a 0 b 1 c 2 ```
In [61]:
``````def histogram(s):
d=dict()
for c in s:
if c not in d:
d[c]=1
else:
d[c]+=1
return d

result=histogram("lalpinos")
# result=histogram('a')

print(result.get('a',0))
print(result.get('b',0))
print(result)
``````
```1 0 {'l': 2, 'a': 1, 'p': 1, 'i': 1, 'n': 1, 'o': 1, 's': 1} ```
In [66]:
``````def invert_dic(d):
inverse=dict()
for key in d:
val=d[key]
if val not in inverse:
inverse[val]=[key]
else:
inverse[val].append(key)
return inverse

d={'l': 2, 'a': 1, 'p': 1, 'i': 1, 'n': 1, 'o': 1, 's': 1}
idict=invert_dic(d)
print(idict)

``````
```{2: ['l'], 1: ['a', 'p', 'i', 'n', 'o', 's']} ```
In [70]:
``````def checkprime(n):
for i in range(2,n):
if(n%i==0):
return 0
break
return 1

def primefactors(m):
myprime=[]
i=2
while(m>1):
if(m%i==0) and (checkprime(i)):
myprime.append(i)
m=m/i
else:
i=i+1
return myprime

print(primefactors(316))

``````
```[2, 2, 79] ```
In [71]:
``range(0,5)``
Out[71]:
``range(0, 5)``
In [97]:
``````dict={}
def prime(n):
for i in range(2,n):
if(n%i==0):
return 0
break
return 1

list1=[]
def dictionary(list1):
print(list1)
d={}
for i in list1:
if(prime(i)):
val='prime'
if i not in d:
d[i]=val
else:
val='not prime'
d[i]=val
return d

def inverse_dict(dict):
inverse={}
for key in dict:
val=dict[key]
if val not in inverse:
inverse[val]=[key]
else:
inverse[val].append(key)
print(inverse)

list2=[2,3,4,5,6,7,8,9]
dict=dictionary(list2)
print(dict)
inverse_dict(dict)
# print(dict.items())

``````
```[2, 3, 4, 5, 6, 7, 8, 9] {2: 'prime', 3: 'prime', 4: 'not prime', 5: 'prime', 6: 'not prime', 7: 'prime', 8: 'not prime', 9: 'not prime'} {'prime': [2, 3, 5, 7], 'not prime': [4, 6, 8, 9]} ```
In [90]:
``````d={}
list1=[2,3,5,9]
for i in list1:
if i not in d:
d[i]=['prime']
else:
d[i]=['not prime']
print(d)
``````
```{2: ['prime'], 3: ['prime'], 5: ['prime'], 9: ['prime']} ```
In [98]:
``import jovian``
In [ ]:
``jovian.commit()``
```[jovian] Saving notebook.. ```
In [ ]:
`` ``