0% found this document useful (0 votes)
17 views

Pavan csW2

The document contains 10 programming problems related to string and list manipulation in Python. The problems cover topics like splitting a sentence into words, checking characters in strings, rearranging elements in a list, and more. For each problem, the code to solve it is provided along with sample input/output.

Uploaded by

Daksha Bangarwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Pavan csW2

The document contains 10 programming problems related to string and list manipulation in Python. The problems cover topics like splitting a sentence into words, checking characters in strings, rearranging elements in a list, and more. For each problem, the code to solve it is provided along with sample input/output.

Uploaded by

Daksha Bangarwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

pavan_csW2(1)

January 20, 2024

[1]: '''
1. Make a python program to input n integers of List name L. If the number is␣
↪even then add the number in list L1 otherwise add

the number in list L2 and then print both the list.


'''
L = []
L1 , L2 = [] , []
for i in range(int(input("Total No: "))):
L.append(int(input("Num: ")))
print('Complete List: ',L)
for j in L:
if j%2==0:
L1.append(j)
else:
L2.append(j)
print('even list: ',L1)
print('odd list: ',L2)

Total No: 4
Num: 1
Num: 2
Num: 3
Num: 4
Complete List: [1, 2, 3, 4]
even list: [2, 4]
odd list: [1, 3]

[18]: '''
2. Make a list of n integers and arrange the all the integers in ascending␣
↪order and descending order using built in function.

'''
L = []
for i in range(int(input("Total No: "))):
L.append(int(input("Num: ")))
print('Complete List: ',L)
L.sort()
print('ascending: ',L)

1
L.sort(reverse=True)
print('descending: ',L)

Total No: 4
Num: 1
Num: 4
Num: 2
Num: 3
Complete List: [1, 4, 2, 3]
ascending: [1, 2, 3, 4]
descending: [4, 3, 2, 1]

[ ]: '''
3. Make a list of n integers and input a number. If the input number is exist␣
↪then delete this number from the list and print the

updated list.
'''
L = []
for i in range(int(input("Total No: "))):
L.append(int(input("Num: ")))
print('Complete List: ',L)
num = int(input('Num to remove: '))
if num in L:
L.remove(num)
print('updated list: ',L)

[16]: '''
4. Make a list of n integers and delete all the even numbers from the list and␣
↪print the updated list.

'''
L = []
for i in range(int(input("Total No: "))):
L.append(int(input("Num: ")))
print('Complete List: ',L)
L_even = []
for j in L:
if j%2==0:
L_even.append(j)
for even in L_even:
L.remove(even)
print('updated list: ',L)

Total No: 4
Num: 1
Num: 2
Num: 4
Num: 3

2
Complete List: [1, 2, 4, 3]
updated list: [1, 3]

[15]: '''
5. Make a list of n integers and input a number and location of the number and␣
↪add this numbers with given location in the list

and then print the updated list.


'''
L = []
for i in range(int(input("Total No: "))):
num = int(input("Num: "))
index = int(input('ind: '))
L.insert(index,num)
print(L)

Total No: 3
Num: 1
ind: 1
Num: 2
ind: 2
Num: 0
ind: 0
[0, 1, 2]

[11]: '''
Make a list of n integers and print the sum of all the prime numbers from the␣
↪list.

'''
L = []
for i in range(int(input("Total No: "))):
num = int(input("Num: "))
L.append(num)
print(L)
sum = 0
for j in L:
factors = 0
for nums in range(1,j+1):
if j%nums==0:
factors += 1
if factors == 2:
print(j, ' is a prime.')
sum += j
print(sum)

Total No: 4
Num: 1
Num: 2
Num: 3

3
Num: 4
[1, 2, 3, 4]
2 is a prime.
3 is a prime.
5

[9]: '''
7. Make a list of n integers and arrange/append the numbers in another list as␣
↪first all negative numbers then zero then positive

numbers and then print the list.


'''
# ish question ke meaning meh doubt hai .
L = []
for i in range(int(input("Total No: "))):
num = int(input("Num: "))
L.append(num)
print('before: ',L)
L.sort()
new_L = L
print('after: ',new_L)

Total No: 3
Num: -1
Num: 1
Num: 0
before: [-1, 1, 0]
after: [-1, 0, 1]

[8]: '''
Make a list of n strings/words and print only those word from the list whose␣
↪first and last character is vowel.

'''
L = []
for i in range(int(input("Total No: "))):
word = (input("Word: "))
L.append(word)
print('List: ',L)
for i in L:
if i[0].lower() in 'aeiou' and i[-1].lower() in 'aeiou':
print(i)

Total No: 3
Word: a2bd
Word: a2be
Word: a2bu
List: ['a2bd', 'a2be', 'a2bu']
a2be

4
a2bu

[6]: '''
9. Make a list of n strings/words and print only those word from the list which␣
↪words contain any vowel.

'''
L = []
for i in range(int(input("Total No: "))):
word = (input("Word: "))
L.append(word)
print('List: ',L)
for word in L:
t=0
vowel = ''
for letter in word:
if letter.lower() in 'aeiou':
t=1
vowel = letter
if t==1:
print(word,' has as a vowel', vowel)

Total No: 3
Word: abcd
Word: qrst
Word: uvwx
List: ['abcd', 'qrst', 'uvwx']
abcd has as a vowel a
uvwx has as a vowel u

[4]: '''
10. Make a list of n strings/words and print only those word from the list in␣
↪reverse order which words length is 3.

'''
L = []
for i in range(int(input("Total No: "))):
word = (input("Word: "))
L.append(word)
print('List: ',L)
for word in L:
if len(word)>4:
print(word,' > ',word[::-1])

Total No: 3
Word: army
Word: public
Word: school
List: ['army', 'public', 'school']

5
public > cilbup
school > loohcs

[ ]: '''
String Based Program
'''

[26]: '''
1. Input a sentence and print only those words which contain only consonants.
'''
words = (input('sentence: ')).split()
for j in words:
check=0
for ltr in j:
if ltr.lower() in 'aeiou':
check = 1
if check == 0:
print(j)

sentence: with vowel and without vowel rhytm


rhytm

[27]: '''
2. Input a sentence and print only those words which second alphabet is in␣
↪uppercase.

'''
words = (input('sentence: ')).split()
for j in words:
if j[1].isupper():
print(j)

sentence: oNe Two thR fOu


oNe
fOu

[31]: '''
3. Input a sentence and print in following pattern:
If input string is “APS SP MARG LUCKNOW
Then output will be:
APS
APS SP
APS SP MARG
APS SP MARG LUCKNOW
'''
L = ['APS','SP','MARG','LUCKNOW']
for i in range(len(L)):
for j in range(0,i+1):

6
print(L[j],end=' ')
print()

APS
APS SP
APS SP MARG
APS SP MARG LUCKNOW

[32]: '''
. Input a sentence and print all the words without space:
If input sentence is : APS SP MARG LUCKNOW then output will be: APSSPMARGLUCKNOW
'''
sentence = input('sentence: ').split()
for i in sentence:
print(i,end='')

sentence: with vowel and without vowel rhytm


withvowelandwithoutvowelrhytm

[33]: '''
5. Input a sentence and input a word and check the occurrence of word in a␣
↪sentence.

'''
sentence = input('sentence: ').split()
for i in sentence:
print(i, 'occured', sentence.count(i))

sentence: one two one one thre four three two


one occured 3
two occured 2
one occured 3
one occured 3
thre occured 1
four occured 1
three occured 1
two occured 2

[38]: '''
6. Input a sentence and replace all the ‘a’ or ‘A’ in a sentence by symbol @.
'''
sentence = input('sentence: ')
print('method 1 using replace keyword')
for i in sentence:
sentence=sentence.replace('a','@')
sentence=sentence.replace('A','@')
print(sentence)
print('method 2 without funct')

7
new = ''
for j in sentence: # easch chr
if j!='a' or j!='A':
new += j
else:
new += '@'
print(new)

sentence: with vowel and without vowel rhytm


method 1 using replace keyword
with vowel @nd without vowel rhytm
method 2 without funct
with vowel @nd without vowel rhytm

[40]: '''
7. Input a sentence and display n number of characters from the right side of␣
↪the stirng.

If input sentence is APS SP MARG LUCKNOW and n=5 then output will be: CKNOW
'''
sentence = input("Sentence: ")
print(sentence[-int(input("from index: ")):])

Sentence: with vowel and without vowel rhytm


from index: 15
out vowel rhytm

[45]: '''
8. Input a word and shift all the character 1 position left in the word and␣
↪then print the updated word.

If input word is APS then output will be: SAP


'''
sentence = input("Sentence: ")
words = sentence.split()
words.append(words.pop(0))
sentence = ''
for i in words:
sentence += i + ' '
print(sentence)

Sentence: with vowel and without vowel rhytm


vowel and without vowel rhytm with

[48]: '''
9. Input a sentence and reverse each and every word in their position and then␣
↪print the sentence.

e.g: if input sentence is : APS SP MARG then output will be: SPA PS GRAM
'''

8
sentence = input("Sentence: ")
words = sentence.split()
new = ''
for i in words:
new += i[::-1] + ' '
print(new)

Sentence: with vowel and without vowel rhytm


htiw lewov dna tuohtiw lewov mtyhr

[2]: '''
10. Input a sentence and print the largest word from the sentence.
'''
sentence = input("Sentence: ")
words = sentence.split()
large = words[0] # letting first element to be the largest
for i in words:
print(i, ' length: ',len(i))
if len(i) > len(large):
large = i
print('largest: ',large)

Sentence: lett first elem to be the largest


lett length: 4
first length: 5
elem length: 4
to length: 2
be length: 2
the length: 3
largest length: 7
largest: largest

[ ]: '''
dictonary based program'
'''

[4]: '''
1. Make a dictionary with key as an integer and values is its cube. Eg: D={n:n3␣
↪} as D={2:8,3:27,4:64,7:243}

'''
d={}
for n in range(2,int(input('n: '))):
d[n]=n**3 # or d[n]=n*n*n
print(d)

n: 8
{2: 8, 3: 27, 4: 64, 5: 125, 6: 216, 7: 343}

9
[6]: '''
2. Make a dictionary as key its integer number between 1 to 9 and value its in␣
↪number name. eg D = { 1:”One”,3: “Three”,6:”Six”,4:”Four”}

'''
dictonary={}
for d in range(1,10):
key=d
value=''
if d==1:
value='first '
if d==2:
value='two '
if d==3:
value='three '
if d==4:
value='four'
if d==5:
value='five '
if d==6:
value='six '
if d==7:
value='seven '
if d==8:
value='eight '
if d==9:
value='nine '
dictonary[key]=value
print(dictonary)

{1: 'first ', 2: 'two ', 3: 'three ', 4: 'four', 5: 'five ', 6: 'six ', 7:
'seven ', 8: 'eight ', 9: 'nine '}

[7]: '''
3. Make a dictionary as key and values both are integers and print the maximum␣
↪and minimum values from the dictionary.

'''
d={}
for i in range(int(input('NO of items: '))):
key=int(input('key: '))
val=int(input('val: '))
d[key]=val
print('largest key: ',max(d.keys()))
print('largest val: ',max(d.values()))

NO of items: 3
key: 1
val: 3
key: 9

10
val: 2
key: 6
val: 5
largest key: 9
largest val: 5

[34]: ''''
4. Write a program to remove the duplicate values from the dictionary.
e.g: Original Dictionary: {1,”Aman”,2:”Suman”,3:”Aman”}
New Dictionary : {1:”Aman”, 2:”Suman”}
'''
d={}
for i in range(int(input('NO of items: '))):
key=(input('key: '))
val=input('val: ')
d[key]=val
print(d)
dcopy=d
# first convert d.items to list() and run it in reverse
''' simplified n complex'''
# for i,j in list(dcopy.items())[::-1]:
# if list(d.values()).count(j)>1:
# d.pop(i)
# print(d)
'''simple'''
newd=d
items=list(d.items())[::-1] # to check from last to first
values=list(d.values())
for k,v in items:
if values.count(v)>1:
values.remove(v)
d.pop(k)
print(d)

NO of items: 3
key: 1
val: aman
key: 2
val: suman
key: 3
val: aman
{'1': 'aman', '2': 'suman', '3': 'aman'}
{'1': 'aman', '2': 'suman'}

[4]: '''

11
5. Write a program to store information of products like product id, product␣
↪name and product price in a dictionary name “PRODUCT” by taking product id␣

↪as a

key and print details of all the product whose price is greater than 250.
'''
PRODUCT={}
for i in range(int(input("no of products: "))):
id=input("product id: ")
name=input("product name: ")
price=float(input("product price: "))
PRODUCT[id]=[name,price]
for id,(name,price) in PRODUCT.items():
if price>250:
print('id: ', id,' name: ',name,' price: ',price)

no of products: 3
product id: 01
product name: ai course
product price: 1000
product id: 09
product name: data science
product price: 3000
product id: 11
product name: gui dev
product price: 3000
id: 01 name: ai course price: 1000.0
id: 09 name: data science price: 3000.0
id: 11 name: gui dev price: 3000.0

[1]: '''
6. Write a program to accept employee id and employee name as key and value␣
↪pair of 5 employees in a dictionary name EMPLOYEE. Display only those

employee details whose name contains any vowel.


'''
employee = {}
for i in range(int(input("no of emp: "))):
id,name=input("id:"),input("name")
employee[id]=name
for id,name in employee.items():
for ltr in name:
if ltr.lower() in 'aeiou':
print('id: ',id,', name: ',name)
break

no of emp: 3
id: 1
name aman
id: 2

12
name qwrt
id: 3
name pavam
id: 1 , name: aman
id: 3 , name: pavam

[3]: '''
7. Write a program to accept employee name and salary as key and value pair of␣
↪5 employees in a dictionary name EMP . If salary is less than 25000 then

increase the salary by 1000 and print the updated dictionary.


'''

employee = {}
for i in range(int(input("no of emp: "))):
name,sal=input("name:"),int(input("sal"))
employee[name]=sal
print(employee)
for name,sal in employee.items():
if sal<25000:
employee[name]=sal+1000
print(employee)

no of emp: 3
name: qwert
sal 24000
name: asdfg
sal 29000
name: zxcvb
sal 12345
{'qwert': 24000, 'asdfg': 29000, 'zxcvb': 12345}
{'qwert': 25000, 'asdfg': 29000, 'zxcvb': 13345}

[4]: '''
8. Write a program to accept roll number, name and marks of five students and␣
↪store the details in dictionary name STUDENT using the below format:

STUDENT={ROLLNO:(NAME,MARKS)}. Display all the details of student and also␣


↪print the sum of all the five students marks.

'''
STUDENT={}
for i in range(5):
rno=int(input('roll no: '))
name=input('name: ')
marks=float(input("marks: "))
STUDENT[rno]=(name,marks)
print(STUDENT)
sum=0

13
for rno,(name,marks) in STUDENT.items():
sum+=marks
print('roll: ',rno,' name: ',name,' scored ',marks)
print('sum of mks: ',sum)

roll no: 1
name: aman
marks: 30
roll no: 2
name: chaman
marks: 32
roll no: 3
name: pavan
marks: 38
roll no: 4
name: suman
marks: 33
roll no: 5
name: praman
marks: 34
{1: ('aman', 30.0), 2: ('chaman', 32.0), 3: ('pavan', 38.0), 4: ('suman', 33.0),
5: ('praman', 34.0)}
roll: 1 name: aman scored 30.0
roll: 2 name: chaman scored 32.0
roll: 3 name: pavan scored 38.0
roll: 4 name: suman scored 33.0
roll: 5 name: praman scored 34.0
sum of mks: 167.0

[8]: '''
9. Write a program to make of given format of 5 items as dictionary␣
↪COMPUTER={Computer peripheral name: price} and print the details of those␣

↪peripherals

which have maximum and minimum price.


'''
COMPUTER={}
for i in range(5):
name=input('peripheral name :')
price=float(input('peripheral price:'))
COMPUTER[name]=price
print(COMPUTER)
prices = list(COMPUTER.values())
names=list(COMPUTER.keys())
maxp=max(prices)
minp=min(prices)
maxindex=prices.index(maxp)
minindex=prices.index(minp)

14
print('max price item',names[maxindex],maxp)
print('min price item',names[minindex],minp)

peripheral name : mic


peripheral price: 250
peripheral name : keyboard
peripheral price: 4000
peripheral name : mouse
peripheral price: 25-
peripheral name : mousepad
peripheral price: 250
peripheral name : speaker
peripheral price: 340
{'mic': '250', 'keyboard': '4000', 'mouse': '25-', 'mousepad': '250', 'speaker':
'340'}
max price item keyboard 4000
min price item mouse 25-

[ ]:

15

You might also like