P

@praveen11

Access the value of key ‘history’ from the below dict

Python
4 years ago
sampleDict = { "class":{ "student":{ "name":"Mike", "marks":{ "physics":70, "history":80 } } }

merging two dictionaries

Python
4 years ago
d1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30} d2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50} d={**d1,**d2} print(d)

two lists convert it into the dictionary

Python
4 years ago
l1=['ten','twenty','thirty'] l2=[10,20,30] d=dict(zip(l1,l2)) print(d)

accessing dictionary elements

Python
4 years ago
d={"a":1,"b":2,"c":3} print(d['a']) print(d.get('a'))

creating a dictionary

Python
4 years ago
d1={"a":1,"b":2,"c":3} print(d1) #or d2=dict({"a":1,"b":2,"c":3}) print(d2) #or d3=dict([('a',1),('b',2),('c',3)]) print(d3)

Replace each special symbol with # in the following string

Python
4 years ago
import string a=input() # for i in a: # if(i=='!' or i=='@' or i=='$' or i=='%' or i=='^' or i=='&' or i=='*' or i=='+' or i=='-' or i=='/'): for i in string.punctuation: a=a.replace(i,"#") print(a)

Find words with both alphabets and numbers

Python
4 years ago
l=[i for i in input().split()] a=[] for i in l: if(any(c.isalpha() for c in i) and any(c.isdigit() for c in i)): a.append(i) print(a)

Remove special symbols / punctuation from a string

Python
4 years ago
a=input() x="" for i in a: if((i>='a' and i<='z') or (i>='A' and i<='Z') or (i==" ")): x=x+i print(x)

Remove empty strings from a list of strings

Python
4 years ago
l=["Emma", "Jon", "", "Kelly", None, "Eric", ""] r=[] for i in l: if(not (i=='' or i==None)): #if(i) this check for non empty string in list r.append(i) print(r)

Split a string on hyphens

Python
4 years ago
a=input() # x=a.split("-") # for i in x: # print(i) #without using split x="" for i in a: if(i=="-"):

Find the last position of a given substring

Python
4 years ago
a=input() b=input() print(a.rfind(b))

Write a program to count occurrences of all characters within a string

Python
4 years ago
a=input() d={} for i in a: if i in d: d[i]=d[i]+1 else: d[i]=1 print(d)

Calculate the sum and average of the digits present in a string

Python
4 years ago
a=input() l=[] for i in a: if(i>='0' and i<='9'): l.append(int(i)) print(sum(l)) print(sum(l)/len(l))

String characters balance Test

Python
4 years ago
a=input() b=input() if(b in a): print("True") else: print("False")

Arrange string characters such that lowercase letters should come first

Python
4 years ago
a=input() s="".join(sorted(a)) print(s[::-1])

Append new string in the middle of a given string

Python
4 years ago
a=input() b=input() mid=len(a)//2 x=a[:mid]+b+a[mid:] print(x)

Create a string made of the middle three characters

Python
4 years ago
a=input() mid=len(a)//2 x=a[mid-1:mid+2] print(x)

Create a string made of the first, middle and last character

Python
4 years ago
a=input() x=a[0]+a[len(a)//2]+a[-1] print(x)

Given a Python list of numbers. Turn every item of a list into its square

Python
4 years ago
l = [1, 2, 3, 4, 5, 6, 7] l1=[int(l[i])**2 for i in range(len(l))] print(l1)

Concatenate two lists index-wise

Python
4 years ago
l1=["m","na","i","pra"] l2=["y","me","s","veen"] l3=[] for i in range(len(l1)): l3.append(l1[i]+l2[i]) print(l3)