0% found this document useful (0 votes)
13 views3 pages

Pyhon Assignment 15

Uploaded by

Shubham Chaubey
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)
13 views3 pages

Pyhon Assignment 15

Uploaded by

Shubham Chaubey
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/ 3

24/08/2024, 22:13 Pyhon_assignment_15

In [4]: #WAP
#List = ['hyd','mumbai','chennai']
# op = ['HYD','MUMBAI','CHENNAI']

List = ['hyd','mumbai','chennai']
l=[a.upper() for a in List]
print(l)

['HYD', 'MUMBAI', 'CHENNAI']

In [24]: # WAP
#List = ['hyd','mumbai','chennai']
# op = ['Hyd','Mumbai','Chennai']

List = ['hyd','mumbai','chennai']
l=[a.capitalize() for a in List]
print(l)

['Hyd', 'Mumbai', 'Chennai']

In [23]: #WAP
#List = ['hyd','mum#bai','chen#nai']
# op = ['Mum#bai','Chen#nai']

L = ['hyd', 'mum#bai', 'chen#nai']


o = [city for city in L if '#' in city]
print(o)

['mum#bai', 'chen#nai']

In [25]: #WAP
#List = ['hyd','mum#bai','chen#nai']
# op = ['hyd']

L = ['hyd', 'mum#bai', 'chen#nai']


o = [city for city in L if '#' not in city]
print(o)

['hyd']

In [26]: # Q6): string='hello hai how are you'


# op= ['Hello','Hai','How','Are','You']

string = 'hello hai how are you'


op = [word.capitalize() for word in string.split()]
print(op)

['Hello', 'Hai', 'How', 'Are', 'You']

In [30]: # Q7):
# input:
# string1='[email protected], [email protected],
# [email protected]'

# output
# fnames=['virat','Rohit','Dhoni']
# sname=['kohli,sharma,Mahendar']
# cname=['rcb','Mi','Csk']

string1 = '[email protected], [email protected], [email protected]'

file:///C:/Users/Shubham/Downloads/Pyhon_assignment_15.html 1/3
24/08/2024, 22:13 Pyhon_assignment_15

emails = [email.strip() for email in string1.split(',')]

fnames = [email.split('.')[0] for email in emails]


sname = [email.split('.')[1].split('@')[0] for email in emails]
cname = [email.split('@')[1].split('.')[0] for email in emails]

print('fnames:', fnames)
print('sname:', sname)
print('cname:', cname)

fnames: ['virat', 'Rohit', 'Dhoni']


sname: ['kohli', 'sharma', 'Mahendra']
cname: ['rcb', 'Mi', 'csk']

In [31]: # Q8) string1='can canner can you can not canner can be can'
# Get the count of each word in above string in a list
# o.p: ['can-5','canner-2','you-1','not-1','be-1']

string1 = 'can canner can you can not canner can be can'
words = string1.split()

word_count = {}
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1

result = [f"{word}-{count}" for word, count in word_count.items()]


print(result)

['can-5', 'canner-2', 'you-1', 'not-1', 'be-1']

In [32]: # Q9) get 5 randm numbers between 1 to 100


# append in a list
# Find the maximum and minimu value of a given list
# with out using max and min functions

import random

numbers = [random.randint(1, 100) for _ in range(5)]


print("Generated numbers:", numbers)

max_value = numbers[0]
min_value = numbers[0]

for num in numbers:


if num > max_value:
max_value = num
if num < min_value:
min_value = num

print("Maximum:", max_value)
print("Minimum:", min_value)

Generated numbers: [62, 94, 31, 27, 59]


Maximum: 94
Minimum: 27

In [33]: # Q10) list_qns=['Who is PM of india','Who is ICT ODI captain',


# 'How many states are there in India']

file:///C:/Users/Shubham/Downloads/Pyhon_assignment_15.html 2/3
24/08/2024, 22:13 Pyhon_assignment_15

# list_ans= ['Modi','Rohit',29]
# step-1: Iterate each qn
# step-2: ask the user enter the answer for corresponding qn
# step-3: match that answer with list_ans
# Note: Make sure first qns ans match with first index of the ans
# step-4: if both are match give the one mark
# step-5: Finally print how many qns are correct
# how many marks got

list_qns = ['Who is PM of India?', 'Who is ICT ODI captain?', 'How many states a
list_ans = ['Modi', 'Rohit', 29]

score = 0

for i in range(len(list_qns)):
user_ans = input(list_qns[i] + " ")

# Convert user's answer to match the expected type (string or int)


if isinstance(list_ans[i], int):
user_ans = int(user_ans)

if user_ans == list_ans[i]:
score += 1

print(f"You got {score} out of {len(list_qns)} correct!")

You got 1 out of 3 correct!

In [ ]:

file:///C:/Users/Shubham/Downloads/Pyhon_assignment_15.html 3/3

You might also like