Question

Python code: 1. This question practices the use of a list method. Assign to the variable...

Python code:

1. This question practices the use of a list method. Assign to the variable grades a list of 10 letter

grades from among ‘A’, ‘B’, ‘C’, ‘D’, and ‘F’. For example:

grades = [‘A’, ‘F’, ‘C’, ‘F’, ‘A’, ‘B’, ‘A’, ‘C’, ‘A’, ‘B’]

Write a Python expression that creates a list named frequency, in which the elements are the

number of times each of the letters A, B, C, D and F appear in grades. For example, for the above value

of grades, the following would be correct output:

frequency = [4, 2, 2, 0, 2]

Your expression must give the correct values for any list of grades, not just the one in your list. Hint:

use the list method count.

2. This question practices list membership, list indexes and list slices.

a. Write a Python statement that creates a list named dog_breeds that contains the elements

‘collie’, ‘sheepdog’, ‘Chow’, and ‘Chihuahua’ in the order given.

b. Write a Python statement that uses list slicing to create a list herding_dogs that is made up of

the first two elements of dog_breeds.

c. Write a Python statement that uses list indexing to create a list tiny_dogs that is made up of

the last element of dog_breeds.

d. Write a Python statement that tests whether ‘Persian’ is in the list dog_breeds and prints

either True or False depending on the answer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

OUTPUT :

CODE :

# -*- coding: utf-8 -*-
"""
Created on Tue Jan 29 09:01:30 2019

@author: pmaddala
"""

#1
grades = ['A', 'F', 'C', 'F', 'A', 'B', 'A', 'C', 'A', 'B']
frequency = [grades.count(x) for x in ['A','B','C','D','F']]
print(frequency)

#2
#a.
dog_breeds = ['collie', 'sheepdog', 'Chow', 'Chihuahua']
print('Dog Breeds ',dog_breeds)

#b
herding_dogs = [x[:2] for x in dog_breeds]
print('Herding Dogs ',herding_dogs)

#c
tiny_dogs = [x[-1] for x in dog_breeds]
print('Tiny Dogs ',tiny_dogs)

#d
print('Persiann in dog_breeds',( 'Persian' in dog_breeds))


            

Add a comment
Know the answer?
Add Answer to:
Python code: 1. This question practices the use of a list method. Assign to the variable...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write Python code examples of statements to remove the name at position 2 from the list...

    Write Python code examples of statements to remove the name at position 2 from the list in problem 32, and to remove one of the remaining names by referencing that name. Write Python code to declare a tuple named stuck that holds the three remaining names from famfri by referencing those names as elemens of famfri. Declare a Python set containing the names of the four seasons. What happens if you declare a set using a list that contains two...

  • Use python to write a code Problem 1: Environmental Monitoring You are given a nested list...

    Use python to write a code Problem 1: Environmental Monitoring You are given a nested list in which the sublists contain tuples! Example: samples = [ [('frog','H'), ('toad','D') ], [('trout','H'), ('salmon','S'), ('catfish','H')] ] Here each species is either healthy H, dead D or sick S. Access the health status of a specified animal using multi-indexing. Count how many are healthy using two nested for loops. List will be bigger and different on each version.

  • Using Python Programming Language: 3. Write a function flatten that takes a 2D list and returns...

    Using Python Programming Language: 3. Write a function flatten that takes a 2D list and returns all the items of each list concatenated together into one new 1D list. For example: flatten ([["a", "b"],["c","0"],["e","f"]]) would return ["a", "b","C","d", "e","f"] Save the function in a PyDev library module named functions.py Write a program t03.py that tests flatten function and prints the returned flat list to the screen. Test your program with a different list, hardcoded in t03.py • Copy the results...

  • 1)Given two lists L1 and L2, create a new list L3 consisting of the first element...

    1)Given two lists L1 and L2, create a new list L3 consisting of the first element of L1 and the last element of L2. For example, if L1=[1,2,3] and L2=["a","b","c"], L3 should be [1,"c"] 2)given a string s, create a new string odd_letters that contains only the odd letters of s (two ways to solve this problem are to use slicing, or a while or for loop), i.e. starting at the first letter or the zeroth index: if s="banana", odd_letters...

  • Hello can anyone help solve this please in Python the way it's asked? Question 22 40 pts List Comprehensions Write...

    Hello can anyone help solve this please in Python the way it's asked? Question 22 40 pts List Comprehensions Write the solution to a file named p2.py and upload it here. Comprehensions will show up at the midterm exam, so it's important to practice. a) Write a list comprehension that returns a list [0, 1,4, 9, 16, .. 625] starting from range(0, 26). (lots of list elements were omitted in ...) b) Write a list comprehension that returns the list...

  • These are questions in python please! 1) What is the value of variable answer as the...

    These are questions in python please! 1) What is the value of variable answer as the result of the function call in the preceding question. Here is the code again: def my_function(a, b): c = (a + b) * (b - a) return c The call was made with a value of 3 for a and 7 for b. 2)  Write a function named formula that takes three parameters and returns, as its value, the first number plus 5 times the...

  • 1. Make a function make_list_of_lists(n, m) that creates and returns a list of n lists of...

    1. Make a function make_list_of_lists(n, m) that creates and returns a list of n lists of size m, where each of the m elements in a sublist are randomly generated integers between 1 and 9. For example, you might obtain l = make_list_of_lists(2,2) >>> print(l) [[1, 4], [5, 7]] Big Hint: You can do this using numpy along the lines of the following code: import numpy >>> l = list(numpy.random.randint(a,b,c)) where you need to sort out what the values of...

  • 3. Write Python statements that will do the following: a) Input a string from the user....

    3. Write Python statements that will do the following: a) Input a string from the user. *** Print meaningful messages along with your output.* b) Print the length of the String. Example input: The sky is blue. Correct output: The length of the string is 16 Incorrect output: 16 c) Print the first character of the string. d) Print the last character of the string. 4. Save the program. Double-check the left edge for syntax errors or warning symbols before...

  • python 2..fundamentals of python 1.Package Newton’s method for approximating square roots (Case Study 3.6) in a...

    python 2..fundamentals of python 1.Package Newton’s method for approximating square roots (Case Study 3.6) in a function named newton. This function expects the input number as an argument and returns the estimate of its square root. The script should also include a main function that allows the user to compute square roots of inputs until she presses the enter/return key. 2.Convert Newton’s method for approximating square roots in Project 1 to a recursive function named newton. (Hint: The estimate of...

  • "PYTHON" 1. a. Create a file (module) named camera_picture.py. b. Write a function, take_picture_effect, that takes...

    "PYTHON" 1. a. Create a file (module) named camera_picture.py. b. Write a function, take_picture_effect, that takes two parameters, the filename and an effect. c. Take a picture with the filename and effect. Use camera.effect to set the effect. Use help(PiCamera) to find the effects which you can apply. d. Create a second file use_camera.py. e. Import the take_picture_effect function from the camera_picture module. f. Prompt the user for the filename. g. Prompt the user for the image_effect. help(PiCamera) to see...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT