Question

In: Computer Science

By using Python code: 1. list1 = ['smith', 'john', 'andy'] list2 = ['kim', 'mary', 'paul'] Write...

By using Python code:

1. list1 = ['smith', 'john', 'andy']
list2 = ['kim', 'mary', 'paul']
Write a python program which does the following:

a. add items in list2 to list1
b. Remove mary from list1
c. Insert sam at 5th position
d. Sort the elements of list1 in ascending order

2. Write a python program that asks the user to enter a sentence and creates a list of words in that sentence.

3. friends = ('sam','andy','mary','andy','john','andy','mary')
Write a python program which does the following:

a. for the above string calculate the number of times "andy" appears in the tuple

b. change sam with smith

c. print index of john

Solutions

Expert Solution

Q1

for i in list2:
list1.append(i)

list1.remove('mary')
list1.insert(5,'sam')
list1.sort()
print(list1)

The output is

Q2

string=input('Enter a sentence')
words=list(set(string.split()))#set is used to get unique words from sentence
print(words)

Q3

friends = ('sam','andy','mary','andy','john','andy','mary')
count=0
for i in friends:
if i=='andy':
count+=1
print('Andy appears',count,'times')

temp=list(friends)
temp[0]='smith'
friends=tuple(temp)
print(friends)
print(friends.index('john'))

The output is -


Related Solutions

Python...Count the number of times a element of list1 occurs in in list2 list1 = ['a',...
Python...Count the number of times a element of list1 occurs in in list2 list1 = ['a', 'b', 'c','e','j']; list2 = ['a', 'c', 'd', 'b','e','z']; {'a': 1, 'c': 1, 'b': 1, 'e': 1, 'j': 0} How do I get to this to work without the Collections counter?
This program is about list comprehension. list1 = [2, 5, 7, 8] list2 = [1, 2]...
This program is about list comprehension. list1 = [2, 5, 7, 8] list2 = [1, 2] (e) Use nested list comprehension with list1 and list2 as input sequences to generate this list: [[3, 4], [6, 7], [8, 9], [9, 10]] Display the list. The following is the expected output. Part e: [[3, 4], [6, 7], [8, 9], [9, 10]]
write a Python program (with comments) to do the following: Define a list list1 = [['Rose',...
write a Python program (with comments) to do the following: Define a list list1 = [['Rose', 'Daisy'], 7, 0, 5.5, [1, 22, 2.45, 3, 6, 5.8]] and print it. Print the length of list1 (i.e. number of items in list1) with a suitable message. Print the first character of the first string in the first item of list1 with a suitable message (HINT: The first item is the item with index 0). Print the sum of the last 4 numbers...
Python #Exercise 1 #Assign the following first names to a variable name as a list: peter,paul,mary...
Python #Exercise 1 #Assign the following first names to a variable name as a list: peter,paul,mary #Assign the following last names to a variable name as a list: ng,lo,lau #Display each first name in a separate line in turn so that your screen shows (ignore the #): #peter #paul #mary #Combine the elements in the list in turn so that you display the full names of each person in separate lines #Output should look like the following (ignore the #):...
For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
Using python as the coding language please write the code for the following problem. Write a...
Using python as the coding language please write the code for the following problem. Write a function called provenance that takes two string arguments and returns another string depending on the values of the arguments according to the table below. This function is based on the geologic practice of determining the distance of a sedimentary rock from the source of its component grains by grain size and smoothness. First Argument Value Second Argument Value Return Value "coarse" "rounded" "intermediate" "coarse"...
Python: Using Jupyter Notebook 1. Write code to generate Fibonacci series. Fibonacci numbers – 1, 1,...
Python: Using Jupyter Notebook 1. Write code to generate Fibonacci series. Fibonacci numbers – 1, 1, 2, 3, 5, 8, … 2. Check if a number is an Armstrong number A positive integer is called an Armstrong number of order n if abcd... = a^n + b^n + c^n + d^n + ... In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number itself. For example: 153 = 1*1*1...
"PYTHON" Write some code " USING PYTHON" to keep reading numbers from the user until the...
"PYTHON" Write some code " USING PYTHON" to keep reading numbers from the user until the users enters a negative number. The program then prints out: a) the sum of all the numbers b) the average of all the numbers c) the max of the numbers d) the min of the numbers Note we did not cover lists (array) so you will not use them in this problem. Finally, ask the user for their name, then print their name as...
(Python) a) Using the the code below write a function that takes the list xs as...
(Python) a) Using the the code below write a function that takes the list xs as input, divides it into nss = ns/nrs chunks (where nrs is an integer input parameter), computes the mean and standard deviation s (square root of the variance) of the numbers in each chunk and saves them in two lists of length nss and return these upon finishing. Hint: list slicing capabilities can be useful in implementing this function. from random import random as rnd...
Write a python code that calculates π using numpy rand function.
Write a python code that calculates π using numpy rand function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT