🚉
Python Unit - 2 (1)
2 marks
1. How to create a list in python?What is the use of negative indexing of list with example. 2 Kn CO2
In python list is a collection of items with same or different datatypes.
A list is created in Python by placing items inside [] , separated by commas .
# A list with 3 integers
numbers = [1, 2, 5]
print(numbers)
# Output: [1, 2, 5]
Negative indexing accesses items relative to the end of the sequence. The index -1 reads the last element, -2 the second
last, and so on.
nums = [1, 2, 3, 4]
last = nums[-1]
second_last = nums[-2]
print(last, second_last)
#output
4 3
To reverse a list in Python, you can use negative slicing:
nums = [1, 2, 3, 4, 5, 6, 7]
part = nums[::-1]
print(part)
#output
[7, 6, 5, 4, 3, 2, 1]
2. What is the difference between del() and remove() methods of list? 2 Un CO2
Del:
1. del is a python keyword
2. del removes a element from list using index
3. no searching of elements is done
4. index is used to delete
example
l = [1, 2, 3, 4]
print(l)
del l[1]
print(l)
Python Unit - 2 (1) 1
#output
[1, 2, 3, 4]
[1, 3, 4]
Remove:
1. remove() is a build in list function
2. searches the value and removes the first occurence from the list.
3. value is used to delete
example
l = [4, 5, 8, 2, 3]
print(l)
l.remove(3)
print(l)
#output
[4, 5, 8, 2, 3]
[4, 5, 8, 2]
3. Give the python code to find the minimum among the list of 10 numbers. 2 Un CO2
a = [18, 52, 23, 41, 32, 68, 29, 48, 52, 10]
smallest = min(a)
print('Smallest number in the list is : ', smallest)
4. What is tuple? What is the difference between list and tuple? 2 Un CO2
Python Tuple is a collection of objects separated by commas. and enclosed in ().
A tuple is ordered, the elements have a defined order
A tuple is unchangable, ie) we cannot change, add or remove items after the tuple has been created.
Allows duplicates
Differences b/w Tuple and List
List Tuple
It is mutable It is immutable
The implication of iterations is time-consuming in
Implications of iterations are much faster in tuples.
the list.
Operations like insertion and deletion are better
Elements can be accessed better.
performed.
Consumes more memory. Consumes less memory.
Many built-in methods are available. Does not have many built-in methods.
5. Is tuple comparison possible? Give an example. 2 Un CO2
A tuple can be compared with other tuple using comparison operators.
the first item of the first tuple is compared to the first item of the second tuple; if they are not equal, this is the result of the
comparison, else the second item is considered, then the third and so on.
a = (1, 2, 3)
b = (1, 2, 5)
Python Unit - 2 (1) 2
print(a == b)
print(a < b)
#output
False
True
6. What is the output of print tuple + tinytuple, if tuple = ( 'abcd', 786 , 2.23, 'john', 70.2) and tinytuple = (123, 'john')? 2
Un CO2
tuple = ('abcd', 786, 2.23, 'john', 70.2)
tinytuple = (123, 'john')
print(tuple + tinytuple)
#output
('abcd', 786, 2.23, 'john', 70.2, 123, 'john')
7. What is string slicing? 2 Kn CO2
String slicing in Python is about obtaining a sub-string from the given string by slicing it respectively from start to end.
Python slicing can be done in two ways:
Using a slice() method
syntax
slice(stop)
slice(start, stop, step)
Using array slicing [ : : ] method
syntax
arr[start:stop] # items start through stop-1
arr[start:] # items start through the rest of the array
arr[:stop] # items from the beginning through stop-1
arr[:] # a copy of the whole array
arr[start:stop:step] # start through not past stop, by step
String = 'ASTRING'
# Using slice constructor
s1 = slice(3)
print(String[s1])
# Using indexing sequence
print(String[:3])
#output
AST
AST
8. What are python strings? 2 Kn CO2
Strings in python are surrounded by either single quotation marks, or double quotation marks.
'hello' is the same as "hello".
Python strings are "immutable" which means they cannot be changed after they are created
Python Unit - 2 (1) 3
# create string type variables
name = "Python"
print(name)
message = "I love Python."
print(message)
9. What is python sets? 2 Kn CO2
A Set is an unordered collection data type that is iterable, mutable, and has no duplicate elements.
a set itself is mutable. We can add or remove items from it.
A set is created by placing all the items (elements) inside curly braces {} , separated by comma, or by using the built-
in set() function.
Different data types can be stored in a single list
my_set = {1, 2, 3}
print(my_set)
my_set = set([1, 2, 3, 2])
print(my_set)
10. How to split strings and what function is used to perform that operation? 2 Kn CO2
11. What is meant by key-value pairs in a dictionary? 2 Kn CO2
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. Within a key-value pair, there are two related data elements. The first is a constant used to define the data set.
The other is a value, which is a variable belonging to the data set.
A Python dictionary is a collection of key-value pairs where each key is associated with a value.
A value in the key-value pair can be a number, a string, a list, a tuple, or even another dictionary. In fact, you can use a
value of any valid type in Python as the value in the key-value pair.
A key in the key-value pair must be immutable. In other words, the key cannot be changed, for example, a number, a
string, a tuple, etc.
Python uses curly braces {} to define a dictionary. Inside the curly braces, you can place zero, one, or many key-value
pairs.
person = {
'first_name': 'John',
'last_name': 'Doe',
'age': 25,
'favorite_colors': ['blue', 'green'],
'active': True
}
12. What is the difference is between modify and copy operations performed in dictionary? 2 Un CO2
When the copy() method is used, a new dictionary is created which is filled with a copy of the references from the original
dictionary.
original = {1:'one', 2:'two'}
new = original.copy()
Python Unit - 2 (1) 4
print('Orignal: ', original)
print('New: ', new)
#output
Orignal: {1: 'one', 2: 'two'}
New: {1: 'one', 2: 'two'}
The update() method updates the dictionary with the elements from another dictionary object or from an iterable of
key/value pairs.
d = {1: "one", 2: "three"}
d1 = {2: "two"}
# updates the value of key 2
d.update(d1)
print(d)
13. Define packages. 2 Kn CO4
A package is a directory containing multiple modules and other sub-packages.
A package is a directory of Python modules that contains an additional __init__.py file, which distinguishes a package
from a directory that is supposed to contain multiple Python scripts.
Example of importing a package
import datetime
date.today()
14. List some built in modules in python. 2 Kn CO4
built-in modules.
os module
random module
math module
time module
sys module
collections module
statistics module
15. Can you use the addition assignment operator, +=, with two lists? What is the result?
The addition assignment operator += can be used with two lists, It adds the list on the left to the contents of list on the right
example
a = [1, 2, 4]
a += [5, 3]
print(a)
#output
[1, 2, 4, 5, 3]
Python Unit - 2 (1) 5
16 Marks
1. Write a python program that prints the intersection of two lists. (without using list comprehension/sets) 16 Ap CO2
2. What are python libraries? Explain in detail the important standard libraries. 16 Un CO4
3. Demonstrate with code the various operations that can be performed on tuples. 16 Un CO2
4. Explain in detail python sets with example. 16 Un CO2
5. What is Dictionary? Discuss in detail the methods and operations of python dictionaries with example.16 Un CO2
6. Write a python program that counts the number of occurrences of a letter in a string using dictionaries. 16 Ap CO2
7. What is module in python? Explain about how you can use modules in your program with example. 16 Un CO4
8. How will you create a package and import it? Explain it with an example program. 16 Un CO4
9. List out the types of modules and explain any two types in detail. 16 Un CO4
10. Demonstrate the working of +,* and slice operators in python lists and tuples. 16 Un CO
11.
a. Explain join(), split() and append() methods of a list with examples 8 Un CO2
b. Write python code to input information about 20 students as given below:
i. Roll Number
ii. Name
iii. Total Marks
Get the input from the user for a student name. The program should display the RollNumber and total marks for the
given student name. Also, find the average marks of all the students. Use dictionaries.8 Ap CO2
12 i)
Write a recursive python function that recursively computes sum of elements in a list of lists.
Sample Input: [1, 2, [3,4], [5,6]]
Expected Result: 21
8 Ap CO2
12 ii) Write a program to delete all the duplicate elements in a list. 8 Ap CO2
13 i) Analyze string slicing. Illustrate how it is done in python with example. 8 Un CO2
13 ii) Write a python code to search a string in the given list. 8 Ap CO2
14 i) Write a python script to check the given string is Palindrome or not? 8 Ap CO2
14 ii) Write a python program to implement sets and dictionaries. 8 Ap CO2
15 Do the case study and perform the following operation in tuples i) sum of two tuples ii) duplicate a tuple iii) slicing
operator and iv) Compare two tuples.
Python Unit - 2 (1) 6