Python_Unit_-_2
Python_Unit_-_2
2 marks
1. How to create a list in python?What is the use of negative indexing of list with example. 2 Kn CO2
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
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:
example
l = [1, 2, 3, 4]
print(l)
del l[1]
print(l)
Remove:
2. searches the value and removes the first occurence from the list.
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)
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 ().
Allows duplicates
List Tuple
It is mutable It is immutable
Many built-in methods are available. Does not have many built-in methods.
a = (1, 2, 3)
b = (1, 2, 5)
#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
print(tuple + tinytuple)
#output
('abcd', 786, 2.23, 'john', 70.2, 123, 'john')
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:
syntax
slice(stop)
slice(start, stop, step)
String = 'ASTRING'
#output
AST
AST
Python strings are "immutable" which means they cannot be changed after they are created
name = "Python"
print(name)
A Set is an unordered collection data type that is iterable, mutable, and has no duplicate elements.
my_set = {1, 2, 3}
print(my_set)
10. How to split strings and what function is used to perform that operation? 2 Kn CO2
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.
#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.
print(d)
import datetime
date.today()
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]
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
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
i. Roll Number
ii. Name
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.