hw1

docx

School

Bluegrass Community and Technical College *

*We aren’t endorsed by this school

Course

244

Subject

Computer Science

Date

Apr 3, 2024

Type

docx

Pages

1

Uploaded by MegaClover13750

Report
1. Which of the declarations below are legal and which are illegal in Python 3? Defend your answer and describe the result of each legal declaration. a. Illegal. When declaring a list, you don’t declare the size, and [friday] is trying to assign it values, which will declare a size. b. Both illegal and legal. The statement is trying to assign four elements to the first three elements of a list called workers. So if the list already has at least four elements, it will work, but if the list only has three, it will raise an error. c. Legal. It creates a list named vowels with the specified vowels. d. Legal. It creates a list and declares a single element, which is a list itself. That list gets repeated five times. 2. If a Python list, x, has ten elements, what are the effects of the loops below? a. The loop prints the elements of the list ‘x’ in reverse order, starting from the last element and going up to the first. Each iteration of the loop subtracts 1 from i to achieve this effect. b. The loop prints every other element in the list ‘x’, starting from the first element and skipping one element each iteration until it reaches the end of the list. 3. What is the output of the following Python code? The output of the following code is a SyntaxError because of indentation. However, when formatted correctly, the output will end up being 7 because it is the largest number in the 2d list created by the function. 4. Answer the following questions: a. How do you delete an element from a dictionary in Python? You can delete an element from a dictionary in Python using the del statement. Example: del dict[‘key3’] b. How do you determine the number of elements that are stored in a dictionary in Python? You determine the number of elements that are stored in a dictionary with the len() statement. c. What values will the following code display? The values that will be displayed are 1, 2 and 3. 5. Which of the following dictionaries are created correctly? Justify why and why not. a. Incorrect. Keys must be immutable, but the keys given are mutable. b. Correct. Tuples are being used for keys and tuples are immutable. c. Correct. Integers are being used for keys and strings for values. Both are valid. d. Correct. Strings are used as keys and integers for values. Both are valid.
Discover more documents: Sign up today!
Unlock a world of knowledge! Explore tailored content for a richer learning experience. Here's what you'll get:
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help