0% found this document useful (0 votes)
188 views4 pages

Assignment 2 1

The document contains 14 code snippets demonstrating various Python programming concepts: 1. Printing the length of a set to remove duplicates from a list 2. Printing the keys of a dictionary 3. Validating a password meets criteria for length, characters, and complexity 4. Printing each element and index of a list in a for loop 5. Printing every other character of a string 6. Printing a string in reverse order 7. Counting the frequency of each character in a string 8. Finding the intersection of elements between two lists 9. Removing duplicate values from a list while preserving order 10. Removing a specified value from a list using comprehension 11. Removing specified indices from a

Uploaded by

deepakkashya
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
188 views4 pages

Assignment 2 1

The document contains 14 code snippets demonstrating various Python programming concepts: 1. Printing the length of a set to remove duplicates from a list 2. Printing the keys of a dictionary 3. Validating a password meets criteria for length, characters, and complexity 4. Printing each element and index of a list in a for loop 5. Printing every other character of a string 6. Printing a string in reverse order 7. Counting the frequency of each character in a string 8. Finding the intersection of elements between two lists 9. Removing duplicate values from a list while preserving order 10. Removing a specified value from a list using comprehension 11. Removing specified indices from a

Uploaded by

deepakkashya
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 4

1. What is the output of the following code?

nums = set([1,1,2,3,3,3,4,4])
print(len(nums))

Output-4

2.What will be the output? d = {"john":40, "peter":45} print(list(d.keys()))

output-['john', 'peter']

3.A website requires a user to input username and password to register. Write a
program to check the validity of password given by user. Following are the criteria
for checking password:
1. At least 1 letter between [a-z]
2. At least 1 number between [0-9]
1. At least 1 letter between [A-Z]
3. At least 1 character from [$#@]
4. Minimum length of transaction password: 6
5. Maximum length of transaction password: 12

import re
p= input("Please Enter your password")
x = True
while x:
if (len(p)<6 or len(p)>12):
break
elif not re.search("[a-z]",p):
break
elif not re.search("[0-9]",p):
break
elif not re.search("[A-Z]",p):
break
elif not re.search("[$#@]",p):
break
elif re.search("\s",p):
break
else:
print("Valid Password")
x=False
break

if x:
print("Not a Valid Password")

4. Write a for loop that prints all elements of a list and their position in the
list.
a = [4,7,3,2,5,9]

for i in a:
print((i),a.index(i))

5.Please write a program which accepts a string from console and print the
characters that have even indexes.
x=str(input("Enter the comment"))
y=""
for i in range(len(x)):
if i%2==0:
y=y+x[i]
print(y)

6.Please write a program which accepts a string from console and print it in
reverse order.

x=str(input("Enter the comment"))

print(x[::-1])

7.Please write a program which count and print the numbers of each character in a
string input by console.

x=input("Input your comment")

y={}

for i in x:
if i in y.keys():
y[i]+=1
else:
y[i]=1

print(y)

8.With two given lists [1,3,6,78,35,55] and [12,24,35,24,88,120,155], write a


program to make a list whose elements are intersection of the above given lists

lists1= [1,3,6,78,35,55]
lists2=[1,24,35,24,88,120,155]
lists3=[]
for i in lists1:
if i in lists2:
lists3.append(i)
print(lists3)

9.With a given list [12,24,35,24,88,120,155,88,120,155], write a program to print


this list after removing all duplicate values with original order reserved.

x=[12,24,35,24,88,120,155,88,120,155]
y=set()

for i in x:
if i is not y:
y.add(i)

print(y)

10.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

x=[12,24,35,24,88,120,155]
y=set()

for i in x:
if i is not 24:
y.add(i)

print(y)

11.By using list comprehension, please write a program to print the list after
removing the 0th,4th,5th numbers in [12,24,35,70,88,120,155].

x=[12,24,35,70,88,120,155]

print(x.remove(x[0]),x.remove (x[3]),x.remove(x[3]))

print(x)

12.By using list comprehension, please write a program to print the list after
removing delete numbers which are divisible by 5 and 7 in [12,24,35,70,88,120,155].

x=[12,24,35,70,88,120,155]
y=[]
for i in x:
if i%5==0 and i%7==0:
continue
else:
y.append(i)
print(y)

13. Please write a program to randomly generate a list with 5 numbers, which are
divisible by 5 and 7 , between 1 and 1000 inclusive.

y=[]
for i in range (5):
x = int(input("Enter 5 number which sould be between 1 and 1000"))
if 1<x<1000:
if x%5==0 and x%7==0:
y.append(x)
else:
print("enter the correct value")
break
else:
print("enter the correct value")
break
print(y)

14.14. Write a program to compute 1/2+2/3+3/4+...+n/n+1 with a given n input by


console (n>0).
z=int(input("enter the count"))
x=1
y=0
for i in range (z):
y=y+(x/(x+1))

x=x+1
print(y)

You might also like