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]]
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" 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...
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...
Write a python code which prints triangle of stars using a loop ( for loop )...
Write a python code which prints triangle of stars using a loop ( for loop ) Remember what 5 * "*" does The number of lines of output should be determined by the user. For example, if the user enters 3, your output should be: * ** *** If the user enters 6, the output should be: * ** *** **** ***** ****** You do NOT need to check for valid input in this program. You may assume the user...
Using Python, write a segment of code to populate the table "employee" of the database "EmployeeDB”...
Using Python, write a segment of code to populate the table "employee" of the database "EmployeeDB” with the data below. Import your choice of DB connector (import MySQLdb/sqlite3…) Create the“employee” table with schema = [name, address, age] Insert this employee: John Doe, 7001 E Williams Field, 32
Complete the following in syntactically correct Python code. Write a program, using a for loop, that...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that calculates the amount of money a person would earn over a period of time if his or her salary is 1 penny for the first day, 2 pennies for the second day, 4 pennies for the third day, and continues to double each day. 1.      The program should ask the user for the number of days the employee worked. 2.      Display a table showing the salary...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT