Python Examples
Python Examples
php***
1)a='''host,host1
host2,host3
host4,host5'''
b=a.split('\n')#Converts string to list
c=[x.strip() for x in b] #Removes whitespaces
d=','.join(c)
e=d.split(',')
print(e)
cmd_list=['hostname','ip']
for cmd in cmd_list:
print(cmd)
args=cmd.split()
print(args)
O/P:hostname
['hostname']
ip
['ip']
eg:
2)Convert List to dictionary:
a=[1,2,3,4,5,6]
b={}
for i in range(0,len(a),2):
if i==len(a)-1:
print(i)
break
else:
b[a[i]]=a[i+1]
print(b)
o/p: {1: 2, 3: 4, 5: 6}
a=[1,2,3]
b=[4,5,6]
c=dict(zip(a,b))
print(c)
a={1: 2, 3: 4, 5: 6}
b=[]
for i,j in a.items():
b.append(i)
b.append(j)
print(b)
2
9587456281
1252478965
Sample Output
YES
NO
import re
b=int(input())
for j in range(b):
i=raw_input()
pat='^[789]\d{9}'
try:
mat=re.match(pat,i)
num=mat.group()
if num==str(i):
print("YES")
else:
print("NO")
except Exception,e:
print("NO")
6)E-mail validation.
^[a-z][-|.|_]?[0-9]?\w+?@\w+.\w+$
7)Please write a program which accepts a string from console and print the
characters that have even indexes.
Example:
If the following string is given as input to the program:
H1e2l3l4o5w6o7r8l9d
Then, the output of the program should be:
Helloworld
a="H1e2l3l4o5w6o7r8l9d"
print(a[::2])
8)Please write a program which count and print the numbers of each character in a
string input by console.
Example:
If the following string is given as input to the program:
abcdefgabc
a,2
c,2
b,2
e,1
d,1
g,1
f,1
9) By using list comprehension, please write a program to print the list after
removing the value 24 in [12,24,35,24,88,120,155].
Hints:
Use list's remove method to delete a value.
Solution:
li = [12,24,35,24,88,120,155]
li = [x for x in li if x!=24]
print li
10)By using list comprehension, please write a program to print the list after
removing the 0th, 2nd, 4th,6th numbers in [12,24,35,70,88,120,155].
li = [12,24,35,70,88,120,155]
li = [x for (i,x) in enumerate(li) if i%2!=0]
print li
@staticmethod
def is_palindrome(word):
flag=0
rev1=word.lower()
rev=rev1[::-1]
#print(rev)
for i in range(len(word)-1):
if rev1[i]==rev[i]:
pass
else:
flag=1
break
if flag==1:
return False
else:
return True
13), for dictionary {'Input.txt': 'Randy', 'Code.py': 'Stan', 'Output.txt':
'Randy'} the group_by_owners function should return {'Randy': ['Input.txt',
'Output.txt'], 'Stan': ['Code.py']}.
class FileOwners:
@staticmethod
def group_by_owners(files):
file1={}
for i,j in files.items():
file1[j]=file1.get(j,[])+[i]
return file1
files = {
'Input.txt': 'Randy',
'Code.py': 'Stan',
'Output.txt': 'Randy'
}
print(FileOwners.group_by_owners(files))
print(Palindrome.is_palindrome('Deleveled'))
break
except EOFError:
pass
print(a)
17) End the list when -1 is encountered and find odd numbers in that list.
a=[]
b=[]
try:
while True:
n=input()
if n!='-1':
a.append(n)
else:
break
except EOFError:
pass
print(a)
for i in range(len(a)-1):
if int(a[i])%2!=0:
b.append(a[i])
for j in b:
print(j)
a='''count
qwerty
words '''
a=a.strip()
a=a.replace(" ","")
a=a.replace("\n","")
a=a.replace("\t","")
b=len(a)
print(b)
o/p : 16