1. Write python program to print list item by passing index number.
2. Write a program that show following arithmetic operators.
PLEASE EXPLAIN WITH : [ REQUIREMENTS, CODING, EXAMPLE ETC. ]
Answers
Answer:
Python List index()
index() is an inbuilt function in Python, which searches for a given element from the start of the list and returns the lowest index where the element appears.
Syntax:
list_name.index(element, start, end)
Parameters:
element – The element whose lowest index will be returned.
start (Optional) – The position from where the search begins.
end (Optional) – The position from where the search ends.
Returns:
Returns the lowest index where the element appears.
Error:
If any element which is not present is searched, it returns a ValueError
Example 1: Find the index of the element
# Python3 program for demonstration
# of list index() method
list1 = [1, 2, 3, 4, 1, 1, 1, 4, 5]
# Will print the index of '4' in list1
print(list1.index(4))
list2 = ['cat', 'bat', 'mat', 'cat', 'pet']
# Will print the index of 'cat' in list2
print(list2.index('cat'))
Output:
3
0
Example 1.1
# Python3 program for demonstration
# of index() method
list1 = [1, 2, 3, 4, 1, 1, 1, 4, 5]
# Will print index of '4' in sublist
# having index from 4 to 8.
print(list1.index(4, 4, 8))
# Will print index of '1' in sublist
# having index from 1 to 7.
print(list1.index(1, 1, 7))
list2 = ['cat', 'bat', 'mat', 'cat',
'get', 'cat', 'sat', 'pet']
# Will print index of 'cat' in sublist
# having index from 2 to 6
print(list2.index('cat', 2, 6 ))
Output:
7
4
3
Example 1.2
# Python3 program for demonstration
# of list index() method
# Random list having sublist and tuple also
list1 = [1, 2, 3, [9, 8, 7], ('cat', 'bat')]
# Will print the index of sublist [9, 8, 7]
print(list1.index([9, 8, 7]))
# Will print the index of tuple
# ('cat', 'bat') inside list
print(list1.index(('cat', 'bat')))
Answer:
1. Write python program to print list item by passing index number.
example list - this below list is just for example, you can make your own list
lst = [12, 23, 34]
To get a list item by passing index number I am making a function that will have an argument, index number
def func(index_number):
return lst[index_number]
Use example
print(func(index_number=0))
Output --> 12
2. Write a program that show following arithmetic operators.
Sorry, the question isn't clear, because you haven't mentioned any arithmetic operators.