Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

EC 21EC014

EC262 : PYTHON PROGRAMMING

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] * 3 Ans: a. [0, 0, 0, 0]

b. [1, 2, 3, 1, 2, 3, 1, 2, 3]

3. Let list = [’a’, ’b’, ’c’, ’d’, ’e’, ’f’]. Find

a) list[1:3]

b) t[:4]

c) t[3:] Ans: a. ['b', 'c']

b. Error

c. Error

4. Mention any 5 list methods.

Ans: insert() , sort(), append(), extend(), pop().

5. State the difference between lists and dictionary.

Ans: List is Mutable and Dictionary is Immutable.

1
EC 21EC014

6. What is List mutability in Python? Give an example.

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.

7. What is aliasing in list? Give an example.

Ans: Aliasing happens whenever one variable’s value is assigned to another variable.

For example, a = [81, 82, 83] b = a print(a is b)

8. Define cloning in list.

Ans: This method is considered when we want to modify a list and also keep a copy of the
original.

9. Explain List parameters with an example.

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.

10. Write a program in Python to delete first element from a list.

Ans: alpha = [‘a’, ‘b’, ‘c’, ‘d’]

Alpha.pop(0)

11. Write a program in Python returns a list that contains all but the first element of the
given list.

Ans: alpha = [‘a’, ‘b’, ‘c’, ‘d’]

print(“The given list:”, alpha[1:])

12. What is the benefit of using tuple assignment in Python?

2
EC 21EC014

Ans: Tuples are faster than lists.

Tuples make the code safer from any accidental modification.

Tuples can be sorted.

Tuples can be iterated.

Tuples maintain their order automatically.

13. Define key-value pairs.

Ans: The contents of a dict can be written as a series of key-value pairs within braces{}.

14. Define dictionary with an example.

Ans: Dictionary is used to store data values in key-value pairs.

Eg: capitals = {"USA":"Washington D.C.", "France":"Paris", "India":"New Delhi"}

15. How to return tuples as values? Ans: def create_tuple():

tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9)
return tuple

numbers = create_tuple()

print(numbers)

16. List two dictionary operations.

Ans: clear()

Copy()

17. Define dictionary methods with an example.

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)

copy(): The copy() method returns a copy of the specified dictionary.

Eg: car = {

“brand”: “Ford”,

“model”: “Mustang”,

“year”: 1964

x = car.copy()

print(x)

18. Define List Comprehension.

Ans: List comprehension is a short way of creating lists from the ones that already exist.

19. Write a Python program to swap two variables.

Ans: x = 3

y=8

temp= x

x=y y = temp

4
EC 21EC014

print(“The value of x after swapping: {}”.format(x))

print(“The value of x after swapping: {}”.format(x))

20. Write the syntax for list comprehension. Ans: fruits = [“apple”, “banana”, “chikoo”,

“mango”] newlist = [x for x in fruits if “a” in x]

print(newlist)

21. What is slicing?

Ans: Slice object can be used to get a subsection of the collection.

22. How can we distinguish between tuples and lists?

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.

Lists are dynamic.

Lists are mutable.

Lists are ordered.

An index is used to traverse a list.

Tuples:

Tuples are used to store heterogeneous and homogeneous data.

Tuples are immutable in nature.

Tuples are ordered

An index is used to traverse a tuple.

Tuples are similar to lists. It also preserves the data sequence.

5
EC 21EC014

23. What will be the output of the given code?

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)

25. List is mutable. Justify?

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.

26. Difference between del and remove methods in List?

Ans: del:

The del works on index.

The del deletes an element at a specified index number.

The del is simple deletion.

Remove:

The remove() works on value.

It removes the first matching value from the list.

The remove() searches the list to find the item.

27. Difference between pop and remove in list?

Ans: The pop() function removes the last element or the element based on the index given.

6
EC 21EC014

remove() function removes the first occurrence of the specified element.

28. How are the values in a tuple accessed?

Ans: Access tuple items.

Negative/ positive indexing.

Ranges of indexes.

Check if item exists.

29. What is a Dictionary in Python

Ans: A dictionary is an unordered and mutable python container that stores mappings of
unique keys to values.

30. Define list comprehension

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

created are called immutable.

33. Define Histogram.

Ans: A histogram is a graph used to represent the frequency distribution of a few data
points of one variable.

7
EC 21EC014

34. Define Tuple and show it is immutable with an example.

Ans: Tuples are ordered collection of elements of different data types.

Eg:

Tuple = (‘hi’, 21, 8.19)

35. State the difference between aliasing and cloning in list.

Ans: Aliasing:

Aliasing happens whenever one variable's value is assigned to another variable.

Cloning:

Cloning makes a copy of the list itself, not just the reference.

36. What is list cloning.

Ans: List Cloning makes a copy of the list itself, not just the reference.

37. What is deep cloning.

Ans: A deep copy is a copy of all elements of the original object.

38. State the difference between pop and remove method in list.

39. Create tuple with single element. Ans: tuple = (“adi”)

print(type(tuple))

40. Swap two numbers without using third variable.

Ans: x = 3

y=8

print(“Before swapping: ”)

print(“Value of x: ”, x, “and y: ”, y)

8
EC 21EC014

x,y = y,x print(“After

swapping: ”) print(“Value of x: ”, x,

“and y: ”, y)

41. Define properties of key in dictionary.

Ans: The values in the dictionary can be of any type, while the keys must be immutable like
numbers, tuples, or strings.

42. How can you access elements from the dictionary.

Ans: car = {

“brand”: “Ford”,

“model”: “Mustang”,

“year”: 1964

x = car[“model”]

43. Difference between delete and clear method in dictionary.

Ans: Delete:

del keyword is used to delete a particular element.

Clear:

The clear( ) function is used to delete all the elements in a dictionary.

44. What is squeezing in list? give an example.

Ans: squeeze() function is used when we want to remove a single – dimensional entries
from the shape of an array.

9
EC 21EC014

45. How to convert a tuple in to list.

Ans: tuple = (True, 21, ‘adi’)

List = list(tuple)

print(type(tuple)) print(List)

10
EC 21EC014

46. How to convert a list in to tuple.

Ans: list_names = [“Sakshi”, “Jainil”, “Ansh”]

tuple_names = tuple(list_names)

print(tuple_names) print(type(tuple_names))

47. Create a list using list comprehension.

Ans: fruits = [“apple”, “banana”, “chikoo”, “mango”]

Newlist = []

for x in fruits: if “a”

in x:

Newlist.append(x)

print(Newlist)

48. Advantage of list comprehension.

Ans: Facilitates writing the code in fewer lines.

Allows writing codes that are easier to understand.

Converts iterable into a formula.

Filters and maps the items in a list to boost code performance.

49. What is the use of map () function.

Ans: The map() function executes a specified function for each item in a iterable.

50. How can you return multiple values from function?

Ans: We can return multiple values from a function using either a dictionary, a tuple, or a
list.

11
EC 21EC014

51. What is sorting and types of sorting.

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(“Length of the string is: ”)

print(count)

53. How to pass tuple as argument. Ans: a,b = 1,2

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)

fruits = [“appple”, “banana”, “chikoo”]

my_fun(fruits)

12
EC 21EC014

55. What is parameter and types of parameter.

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

56. How can you insert values in to dictionary.

Ans: Using the Assignment operator

Using a for loop

Using the ** operator

57. What is key value pair.

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: Numeric data types: int, float

String data types: str

Sequence types: list, tuple, range

Mapping data types: dict.

59. What are the immutable data types available in python.

Ans: Immutable data types are int, float, decimal, string, tuple, range.

60. What is the use of from keys() in dictionary.

Ans: The from keys() returns a dictionary with specified keys and the specified value.

14

You might also like