LISTS, TUPLES AND DICTIONARIES
1. What is a list?
A list is
an ordered set of values, where each value is identified by an index. The
values that make up a list are called its elements. Lists are similar to
strings, which are ordered sets of characters, except that the elements of a
list can have any type.
2. Solve a)[0] * 4 and b) [1, 2, 3] * 3.
>>> [0] * 4 [0, 0, 0, 0]
>>> [1, 2, 3]
* 3 [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:] .
>>> list =
[’a’, ’b’, ’c’, ’d’, ’e’, ’f’]
>>> list[1:3]
[’b’, ’c’]
>>> list[:4]
[’a’, ’b’, ’c’, ’d’]
>>> list[3:]
[’d’, ’e’, ’f’]
4. Mention any 5 list methods.
append()
,extend () ,sort(), pop(),index(),insert and remove()
5. State the difference between lists and
dictionary.
List is a
mutable type meaning that it can be modified whereas dictionary is immutable
and is a key value store. Dictionary is not ordered and it requires that the
keys are hashable whereas list can store a sequence of objects in a certain
order.
6. What is List mutability in Python? Give an
example.
Python represents all its data as objects. Some of these objects
like lists and dictionaries are mutable, i.e., their content can be
changed without changing their identity. Other objects like integers, floats,
strings and tuples are objects that cannot be changed. Example:
>>> numbers =
[17, 123]
>>> numbers[1]
= 5
>>> print
numbers [17, 5]
7. What is aliasing in list? Give an example.
An object
with more than one reference has more than one name, then the object is said to
be aliased. Example: If a refers to
an object and we assign b = a, then both variables refer to the same object:
>>> a = [1,
2, 3]
>>> b = a
>>> b is a
True
8. Define cloning in list.
In order
to modify a list and also keep a copy of the original, it is required to make a
copy of the list itself, not just the reference. This process is called
cloning, to avoid the ambiguity of the word “copy”.
9. Explain List parameters with an example.
Passing a
list as an argument actually passes a reference to the list, not a copy of the
list. For example, the function head takes a list as an argument and returns
the first element:
def
head(list):
return
list[0]
output:
>>> numbers =
[1, 2, 3]
>>> head(numbers)
10. Write a program in Python to delete first
element from a list.
def
deleteHead(list): del list[0]
Here’s
how deleteHead is used:
>>> numbers =
[1, 2, 3]
>>> deleteHead(numbers)
>>> print
numbers [2, 3]
11. Write a program in Python returns a list that
contains all but the first element of the given list.
def
tail(list): return list[1:]
Here’s
how tail is used:
>>>
numbers = [1, 2, 3]
>>>
rest = tail(numbers)
print
rest [2, 3]
12. What is the benefit of using tuple assignment
in Python?
It is
often useful to swap the values of two variables. With conventional assignments
a temporary variable would be used. For example, to swap a and b:
>>> temp = a
>>> a = b
>>> b = temp
This
solution is cumbersome; tuple assignment is more elegant:
>>> a, b = b,
a
13. Define key-value pairs.
The
elements of a dictionary appear in a comma-separated list. Each entry contains
an index and a value separated by a colon. In a dictionary, the indices are
called keys, so the elements are called key-value pairs.
14. Define dictionary with an example.
A dictionary is an associative array (also known as hashes). Any
key of the dictionary is associated (or mapped) to a value. The values of a
dictionary can be any Python data type. So dictionaries are unordered
key-value-pairs.
Example:
>>>
eng2sp = {} # empty dictionary
>>> eng2sp[’one’]
= ’uno’
>>> eng2sp[’two’]
= ’dos’
15. How to return tuples as values?
A
function can only return one value, but if the value is a tuple, the effect is
the same as returning multiple values. For example, if we want to divide two
integers and compute the quotient and remainder, it is inefficient to compute
x/y and then x%y. It is better to compute them both at the same time.
>>> t =
divmod(7, 3)
>>> print t
(2, 1)
16. List two dictionary operations.
·
Del -removes key-value pairs from a dictionary
·
Len - returns the number of key-value pairs
17. Define dictionary methods with an example.
A method
is similar to a function—it takes arguments and returns a value— but the syntax
is different. For example, the keys method takes a dictionary and returns a
list of the keys that appear, but instead of the function syntax keys(eng2sp),
method syntax eng2sp.keys() is used.
>>> eng2sp.keys()
[’one’, ’three’, ’two’]
18. Define List Comprehension.
List
comprehensions apply an arbitrary
expression to items in an iterable rather than applying function. It
provides a compact way of mapping a list into another list by applying a
function to each of the elements of the list.
19.Write a Python program to swap two variables.
x = 5
y = 10
temp = x
x = y
y = temp
print('The
value of x after swapping: {}'.format(x))
print('The
value of y after swapping: {}'.format(y))
20.Write the syntax for list comprehension.
The list
comprehension starts with a '[' and ']',
to help us remember that the result is going to be a list. The basic syntax is
[expression for item in list if conditional ].
PART - A
1.
What is slicing?
2.
How can we
distinguish between tuples and lists?
3.
What will be the
output of the given code?
a.
List=[‘p’,’r’,’i’,’n’,’t’,]
b.
Print list[8:]
4.
Give the syntax
required to convert an integer number into string?
5.
List is mutable.
Justify?
6.
Difference
between del and remove methods in List?
7.
Difference
between pop and remove in list?
8.
How are the
values in a tuple accessed?
9.
What is a
Dictionary in Python
10.
Define list
comprehension
11.
Write a python
program using list looping
12.
What do you
meant by mutability and immutability?
13.
Define Histogram
14.
Define Tuple and
show it is immutable with an example.
15.
state the
difference between aliasing and cloning in list
16.
what is list
cloning
17.
what is deep
cloning
18.
state the
difference between pop and remove method in list
19.
create tuple
with single element
20.
swap two numbers
without using third variable
21.
define
properties of key in dictionary
22.
how can you
access elements from the dictionary
23.
difference
between delete and clear method in dictionary
24.
What is
squeezing in list? give an example
25.
How to convert a
tuple in to list
26.
How to convert a
list in to tuple
27.
Create a list
using list comprehension
28.
Advantage of
list comprehension
29.
What is the use of
map () function.
30. How can you return multiple values from
function?
31. what is sorting and types of sorting
32. Find length of sequence without using library
function.
33. how to pass tuple as argument
34. how to pass a list as argument
35. what is parameter and types of parameter
36. how can you insert values in to dictionary
37. what is key value pair
38. mention different data types can be used in
key and value
39. what are the immutable data types available in
python
40. What is the use of fromkeys() in dictioanary.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.