Advance Python
Advance Python
1. List
Lists are mutable or changeable and consist of a sequence of elements.
1.1Operations on Lists
a) Joining Lists: Two lists can be added using concatenation operator(+).
Example: >>> List_1 = [10,11,12,13]
>>> List_2 = [14,15,16,17]
>>>List_join = List_1+ List 2
>>>print(“The new list after joining both Lists:”, List_join)
b) Replicating Lists: Like string replication, you can replicate a list as many times as you
need.Example: >>>List_1 = [10,11,12,13]
>>>print(“List after replication is:”, List_1*3)
c) Slicing Lists: Slicing refers to the process of extracting a slice from a string.
Example: >>> List_1 = [10,11,12,13]
>>> List_2 = List_1[2:4]
>>>print(“The slicing list is: “, List_2)
1.2 List Built-in Functions
a) Append(): This function is used to add a new item to the end of the list.
Example: >>>List_1= [10,11,12,13]
>>> List_1.append(14)
>>>print(“List after adding new element:”, List_1)
b) Extend(): This function is used to add multiple elements in an existing list.
Syntax: Listname.extend([new items])
Example:>>> List_1 = [10,11,12,13]
>>> List_1.extend([14,15,16])
>>>print(“List after adding new set of elements:”, List_1)
c) Insert(): This function is used to insert an element in the existing list at a specified
location.Syntax: Listname.insert(index_value, new_item)
Example: >>>List_1 = [10,11,12,13]
>>>List_1.insert(2,15)
>>>print(“list after inserting new element at 2nd index:”, List_1)
1.3 Deleting Elements from List
a) pop(): This function is used to remove the desired element from the list using index
value as parameter.Syntax: Listname.pop(index_value)
Example: >>>List _1 = [40,20,19,13,14,13]
>>>List_ 1.pop(3)
>>>print(“list after popping element from the specified index:” List _1)
b) remove(): Used to remove the first occurrence of the specified item from the list when
we have no idea about the index value. Syntax: Listname.remove(list_item)
Example:>>> List_1 = [40,20,19,13,14,13]
>>> List_1.remove(13)
>>>print(“list after removing element 13 from the list:”. List_1)
c) del: Used to remove the individual element, the sublist of elements or delete the list
completely.Syntax:del Listname[index_value],del Listname[start:stop:step]
del Listname #it will be used to delete list completely
Example :>>> List_1 = [40,20,19,13,14,13]
>>> del List_ 1[2]
>>>print(“List after deleting element from the desired index:”, List_1)
1.4 Some Other List Functions
a) count(): Used to count the number of duplicated items available in the defined list.
Syntax: Listname.count (list_item)]
[1]
CLASS X UNIT III ADVANCE PYTHON
Example: >>> List_1 =[10,11,12,13,13,13]
>>> List_2= List_1.count (13)
>>> print (“The result is: “, List_2)
b) reverse(): This function is used to reverse the order of the elements of the list.
Syntax: Listname.reverse()
Example:>>> List _1=[10,11,12,13]
>>>List _1.reverse()
>>> print (“list after using reverse () function:”, List_1)
c) sort():It is used to arrange the elements in a specific order. Syntax: Listname.sort()
Example: >>> List_1 = [40,20,19,13,29]
>>> List_1.sort() # To arrange the items in ascending
>>>print(“list after using sort () function: “, List_1)
d) index(): Used to search the location or the index value of the element in the defined
list. Syntax: Listname.index (list_item)
Example: >>> List_1 = [40,20,19,13,13,13]
>>> print (“The index value of element 19 in the list is:”, List1.index (19))
ASSIGNMENT
1. Write a program to find remainder & quotient after fetching dividend and divisor from user.
2. Write a program to fetch weight(in kg) from user & convert it to grams.
3. Write a program to find Simple & Compound Interest by taking principal, rate & time from user.
4. Write a program to evaluate (a+b)2, where value of a=5 and b=3.
5. Write a program to find the perimeter of the following objects by taking dimensions from user:
a. Square b. Rectangle c. Triangle
6. Write a program to fetch temperature in Fahrenheit from user & convert to Celsius.
(c=(f-32)*5/9)
7. Write a program to fetch a number from user &check whether it is positive or not.
8. Write a program to fetch a number from user &check whether it is fully divisible by 5 or not.
9. Write a program to fetch a number from user &check whether it is odd or even.
10.Write a program to input 5 numbers from the user and check whether their sum is greater than
480 or not.
11.Write a program to print ranks as per the condition below by fetching marks from the user:
If Marks >=90, print “Rank 1”
If 80< Marks <=90, print “Rank 2”
If 70< Marks <=80, print “Rank 3”
If 60< Marks <=70, print “Rank 4”
If below 60, print “Work Hard”
12.Write a program to print the day of week by fetching day from user (i.e. 1 - Sunday,
2-Monday...).
13.Write a program to input two numbers from the user and print the smaller one.
14.Write a program to check if a person’s age is greater than or equal to 18, if yes, then print “You
are eligible for vote”, otherwise print “You are not eligible for vote”.
15.Write a program to print the counting from 1 to 20.
16.Write a program to print the table of a number by fetching the number from user.
17.Write a program to print the Fibonacci series given: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
18.Write a program to print all the elements of a list taken by user.
19.Write a program to add any value to a list taken by user by specific index position.
20.Write a program to remove any element of a list by entering index.
21.Write a program to perform the following operations on the given list:
[‘Orange’, ‘Mango’, ‘Papaya’, ‘Apple’, ‘Grapes’]
a) Remove “Papaya” and add “Guava” at the same position.
b) Sort the list in ascending order.
c) Add the “Cherry, Banana, Coconut” to the list.
[2]