0% found this document useful (0 votes)
8 views

Programming for Data Science- Assignment 1

The document is an assignment by Priyanka Kilaru discussing various programming concepts in Python, specifically focusing on the use of integers and lists as dictionary keys, the output of list concatenation, the return values of functions, and the flexibility of for loops. It clarifies misconceptions about dictionary keys needing to be immutable and explains that functions can return multiple outputs. The assignment emphasizes the importance of understanding data types and control structures in Python programming.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Programming for Data Science- Assignment 1

The document is an assignment by Priyanka Kilaru discussing various programming concepts in Python, specifically focusing on the use of integers and lists as dictionary keys, the output of list concatenation, the return values of functions, and the flexibility of for loops. It clarifies misconceptions about dictionary keys needing to be immutable and explains that functions can return multiple outputs. The assignment emphasizes the importance of understanding data types and control structures in Python programming.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Programming for Data Science

Assignment -1

Name: Priyanka Kilaru


Student ID: PXK220009
Date: 09/14/2023

1. The statement ‘Integers cannot be used as a key in a dictionary. So for the dictionary my_dict,
the code my_dict[0] will always return errors, is false because in python you can use integers as keys
in dictionaries and this will not result in any errors. Here’s why the statement is false

 Python's dictionaries are flexible: Key-value pairs may be stored using the
fundamental data structure known as a dictionary in Python. A dictionary's keys may
be of several data kinds, including integers.
 Hashing Mechanism: Dictionaries in Python use a hashing mechanism to efficiently
store and retrieve values associated with keys. The hash value of an integer is well-
defined and can be used as an index to store and retrieve values in the dictionary.
 Immutable Keys: Dictionaries need immutable data types for their keys. Python has
immutable integers, which means that once produced, they cannot be altered. They
are appropriate for usage as dictionary keys because of their immutability.

2. The statement "A list can be used as the key in a dictionary, so for the dictionary my_dict,
the code my_dict[[3, 3, 3]] = 'error?' will execute with no errors" is false in Python. It’s
because of the following reasons that the statement is false.
 Dictionary keys must be immutable: In Python, dictionary keys must be of an
immutable data type. Objects that are immutable are ones whose values cannot be
altered after creation. This criterion is necessary because dictionaries internally
employ a hash table to store and retrieve key-value pairs quickly.
 Lists are mutable: Python lists are mutable, which means that when they are
generated, you may edit, add, or delete elements from them. Lists cannot serve as
dictionary keys due to their mutability.
To use a collection of values as a key in a dictionary, you can use a tuple instead of a list.
Tuples are immutable and can be used as dictionary keys.

3. The output of the syntax list(range (3)) + [2, 1, 0]==[3, 3, 3] is False. Here’s an explanation for it.
 List(range(3)) will give an output as [0,1,2]
 The + symbol between 2 lists is concatenation symbol and therefore the output will be
[0,1,2]+[2,1,0]-> [0,1,2,2,1,0]
 So if you compare the obtained result with RHS of the statement it’s not matching.
Therefore the above statement is false.

4. A function can return one and only one output but can take multiple inputs (parameters) is a false
statement. A function can also return multiple outputs. This output can be a single data structure
like tuple, list or it can be multiple variables.

5. A for loop provides flexibility in how you iterate over a collection, and it's not mandatory to apply
its body to every element. You can use conditional statements like if, continue, and break to control
the flow of the loop and make decisions about which elements to process.

You might also like