s = "msc"Assignment 26: str
Write a Python script to check if a given string has only alphabets in it.
Write a Python script to check if a given character is present in a given string or not.
Write a Python script to count vowels in a given string.
Write a Python script to count words in a given string.
Write a Python script to reverse a string.
s.isalpha()True
"msc" in sTrue
len(s)3
s = "I am a Masters student"s.split()['I', 'am', 'a', 'Masters', 'student']
"-".join(s.split())'I-am-a-Masters-student'
sum([1 for ch in s])22
"apple" and "banana"'banana'
function handles - lambda
function_name = lambda inputs: body
def function_name(input):
\(~~\) body
\(~~\) return
reverss_s = lambda s: s and s[:-1]+reverss_s(s[:-1])reverss_s("apple")'applappapa'
reverss_s = lambda s: s and s[-1]+reverss_s(s[:-1])reverss_s("apple")'elppa'
s = "apple"s[::-1]'elppa'
Assignment 27: More on str
Write a Python script to reverse a string word-wise.
Example: “mysirg education services”
Result: “services education mysirg”
Write a Python script to extract numbers from a given text and store all the numbers in a list.
Write a Python script to check whether a string is a palindrome or not.
Write a Python script to transform a given string to uppercase.
Write a Python script to find the maximum-length word in a given text.
sentence = "mysirg education services"" ".join(sentence.split()[::-1])'services education mysirg'
text = "siva1"text = "mam"print(text == text[::-1])True
[ch for ch in text if ch.isdigit()]['1']
text.upper()'MAM'
sentence = "I am a master student"max_len = max([len(string) for string in sentence.split()])for word in sentence.split():
if len(word)==max_len:
print(word)student
Assignment 28: List and str Mixed
Write a Python script to remove all non-int values from a list.
Write a Python script to print distinct elements along with their frequencies of occurrences in the list.
Write a Python script to sort a list of strings.
Write a Python script to find the first repeated string in a list of strings.
Write a Python script to count strings which end with the character ‘s’ in a list of strings.
dummy_list = ["siva", "k", 1, 2, 3]for elements in dummy_list:
if type(elements) == int:
dummy_list.remove(elements)new_list =[]
for elements in dummy_list:
if type(elements) == int:
new_list.append(elements)new_list[1, 2, 3]
dummy_list,['siva', 'k', 2]
dummy_list = [1,2,3,4,5,1,2,3,1,2,3]dummy_set = set(dummy_list)dummy_set{1, 2, 3, 4, 5}
frequency_list = []for elemnets in dummy_set:
count = 0
for elements in dummy_list:
if elements == elemnets:
count+=1
frequency_list.append(count)frequency_list[3, 3, 3, 1, 1]
name_of_people = ["siva", "ganesh", "madhan"]"apple"<"aqple"True
sorted(name_of_people)['ganesh', 'madhan', 'siva']
print(ord('s'))115
text = "apple"text_set=set([ch for ch in text])for ch1 in text_set:
count = 0
for ch2 in text:
if ch1 ==ch2:
count+=1
if count >=2:
print(ch1)
breakp
sentence = "I am having class of MSC AM and MSC SC"set(sentence.split()){'AM', 'I', 'MSC', 'SC', 'am', 'and', 'class', 'having', 'of'}
sentence.split()['I', 'am', 'having', 'class', 'of', 'MSC', 'AM', 'and', 'MSC', 'SC']
for word1 in set(sentence.split()):
count =0
for word2 in sentence.split():
if word1 == word2:
count+=1
if count >=2:
print(word1)
breakMSC
dummy_list = ['dogs', 'cats', 'dog']text = "dogs"
text.endswith('s')True
[string for string in dummy_list if string.endswith('s')]['dogs', 'cats']
dummy_list = ['dogs', 'cats', 'dog', 12][string for string in dummy_list if type(string)==str if string.endswith('s')]['dogs', 'cats']
Assignment 29: Tuple
Write a Python script to create a tuple from a given list.
Write a Python script to reverse a tuple.
Write a Python script to create a list of tuples from a given list of strings, where each tuple is a collection of strings beginning with the same character.
Write a Python script to create a list of tuples, where each tuple is a pair of elements: first element is an uppercase character and second element is its Unicode.
Write a Python script to find the sum of all odd numbers stored in a tuple.
(2,34)[0] =0--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_867/3547268627.py in <cell line: 0>() ----> 1 (2,34)[0] =0 TypeError: 'tuple' object does not support item assignment
dummy_list = [2,3]
dummy_list[0]=0dummy_list[0, 3]
tuple(dummy_list)(0, 3)
tuple(dummy_list)[::-1](3, 0)
list_of_strings = ["apple", "banna"][(ele_from_str[0], ele_from_str) for ele_from_str in list_of_strings][('a', 'apple'), ('b', 'banna')]
[(ele_from_str[0], ord(ele_from_str[0])) for ele_from_str in list_of_strings][('a', 97), ('b', 98)]
dummy_tuple = (1,2,3,4,5,6,7,8,9)sum([num for num in dummy_tuple if num%2!=0])25
Assignment 30: Set
Write a Python script to print all distinct elements of a list. Use set to solve the problem.
Create two sets from a given set of numbers to separate even and odd numbers.
Given a set of five player names, write a Python script to form all possible pairs of players by selecting two players at a time.
Given a list of candidates wearing black hats and another list of candidates wearing red shoes, find the candidates wearing both black hats and red shoes.
Write a Python script to create a set of tuples, where each tuple has two dice values. Take N from the user and generate all possible tuples such that the tuple elements sum to N.
set([1,2,1,2]){1, 2}
players_name = {"A", "B", "C", "D", "E"}[(player1, player2) for player1 in players_name for player2 in players_name if player1!=player2][('B', 'D'),
('B', 'A'),
('B', 'E'),
('B', 'C'),
('D', 'B'),
('D', 'A'),
('D', 'E'),
('D', 'C'),
('A', 'B'),
('A', 'D'),
('A', 'E'),
('A', 'C'),
('E', 'B'),
('E', 'D'),
('E', 'A'),
('E', 'C'),
('C', 'B'),
('C', 'D'),
('C', 'A'),
('C', 'E')]
[(list(players_name)[i], list(players_name)[j])
for i in range(len(players_name))
for j in range(i+1,len(players_name))][('B', 'D'),
('B', 'A'),
('B', 'E'),
('B', 'C'),
('D', 'A'),
('D', 'E'),
('D', 'C'),
('A', 'E'),
('A', 'C'),
('E', 'C')]