Professional Documents
Culture Documents
21EC014 - Assignment 4
21EC014 - Assignment 4
ASSIGNMENT - 4
1. What is a list?
Ans: List is a class of data structures that can store one or more values/objects.
2. Solve
a. [0] * 4 and
b. [1, 2, 3, 1, 2, 3, 1, 2, 3]
a) list[1:3]
b) t[:4]
b. Error
c. Error
1
EC 21EC014
Ans: This means we can change an item in a list by accessing it directly as part of the
assignment statement. For example, using Sqaure brackets on the left side of an
assignment, we can update one of the list items.
Ans: Aliasing happens whenever one variable’s value is assigned to another variable.
Ans: This method is considered when we want to modify a list and also keep a copy of the
original.
Ans: The list() parameters takes a single argument: iterable – an object that could be a
sequence(string, tuples) or collection(set, dictionary) or any iterator object.
Alpha.pop(0)
11. Write a program in Python returns a list that contains all but the first element of the
given list.
2
EC 21EC014
Ans: The contents of a dict can be written as a series of key-value pairs within braces{}.
tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9)
return tuple
numbers = create_tuple()
print(numbers)
Ans: clear()
Copy()
Ans: clear() : The clear() method removes all the elements from a dictionary.
3
EC 21EC014
Eg: car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
car.clear()
print(car)
Eg: car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
x = car.copy()
print(x)
Ans: List comprehension is a short way of creating lists from the ones that already exist.
Ans: x = 3
y=8
temp= x
x=y y = temp
4
EC 21EC014
20. Write the syntax for list comprehension. Ans: fruits = [“apple”, “banana”, “chikoo”,
print(newlist)
Ans: Lists:
You can use Python lists to store data of multiple types simultaneously.
Lists help preserve data sequences and further process those sequences in other ways.
Tuples:
5
EC 21EC014
List=[‘p’,’r’,’i’,’n’,’t’,]
Print list[8:]
Ans: Error.
24. Give the syntax required to convert an integer number into string?
Ans: str(int_value)
Ans: List are mutable data types as the elements of the list can be modified, individual
elements can be replaced, and the order of elements can be changed even after the list has
been created.
Ans: del:
Remove:
Ans: The pop() function removes the last element or the element based on the index given.
6
EC 21EC014
Ranges of indexes.
Ans: A dictionary is an unordered and mutable python container that stores mappings of
unique keys to values.
31. Write a python program using list looping Ans: list = [“apple”, “banana”, “cherry”] for
x in list:
print(x)
32. What do you mean by mutability and immutability? Ans: Objects whose value can
change are said to be mutable; objects whose value is unchangeable once they are
Ans: A histogram is a graph used to represent the frequency distribution of a few data
points of one variable.
7
EC 21EC014
Eg:
Ans: Aliasing:
Cloning:
Cloning makes a copy of the list itself, not just the reference.
Ans: List Cloning makes a copy of the list itself, not just the reference.
38. State the difference between pop and remove method in list.
print(type(tuple))
Ans: x = 3
y=8
print(“Before swapping: ”)
print(“Value of x: ”, x, “and y: ”, y)
8
EC 21EC014
swapping: ”) print(“Value of x: ”, x,
“and y: ”, y)
Ans: The values in the dictionary can be of any type, while the keys must be immutable like
numbers, tuples, or strings.
Ans: car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
x = car[“model”]
Ans: Delete:
Clear:
Ans: squeeze() function is used when we want to remove a single – dimensional entries
from the shape of an array.
9
EC 21EC014
List = list(tuple)
print(type(tuple)) print(List)
10
EC 21EC014
tuple_names = tuple(list_names)
print(tuple_names) print(type(tuple_names))
Newlist = []
in x:
Newlist.append(x)
print(Newlist)
Ans: The map() function executes a specified function for each item in a iterable.
Ans: We can return multiple values from a function using either a dictionary, a tuple, or a
list.
11
EC 21EC014
Ans: The sort() method is a built-in method that, by default, sorts the list in ascending order.
Types of sorting:
Selection sort
Bubble sort
Insertion sort
Merge sort
Quick sort
52. Find length of sequence without using library function. Ans: number =
raw_input(“Enter number: ”)
count = 0
for i in number:
count = count+1
print(count)
a,b = (1,2)
(a,b) = 1,2
54. How to pass a list as argument. Ans: def my_fun(food) for x in food:
print(x)
my_fun(fruits)
12
EC 21EC014
Ans: Parameters are the means to pass value from the calling function to the called
function.
Types of parameter:
Default parameter
Keyword parameter
Positional parameter
13
EC 21EC014
Ans: A key-value pair is a data type that includes two pieces of data that have a set of
associated values and a group of key identifiers.
58. Mention different data types can be used in key and value.
Ans: Immutable data types are int, float, decimal, string, tuple, range.
Ans: The from keys() returns a dictionary with specified keys and the specified value.
14