Header Ads Widget

Responsive Advertisement

List in python important topic for class 11


 Hello friends in this article we will discussed the important topic of the python is List which is Ordered and mutable. We will discussed all methods and other attributes in this article. We just share code for each method you have to run that code and check out the output of all this methods and analysis this output. 

List  : Lists are used to store multiple items in a single variable.
  • Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
  • List is a collection which is ordered and changeable. 
  • Allows duplicate members.
  • Lists are created using square brackets:

Example: 
list1 = ["apple", "banana", "cherry"] 
print(list1)

Ordered: it means that the items have a defined order, and that order will not change. If you add new items to a list, the new items will be placed at the end of the list.

Changeable: The list is changeable, meaning that we can change, add, and remove items in a list after it has been created.

Allow Duplicates Since lists are indexed, lists can have items with the same value

list1 = ["apple", "banana", "cherry", "apple", "cherry"] 
print(list1)

list = ["apple", "banana", "cherry"] 
print(len(list))

list1 = ["apple", "banana", "cherry"] 
list2 = [1, 5, 7, 9, 3] 
list3 = [True, False, False]
print(list1)
print(list2)
print(list3)

A list with strings, integers and boolean values: 
list1 = ["abc", 34, True, 40, "male"]

list1 = ["apple", "banana", "cherry"] 
print(type(list1))

list1 = list(("apple", "banana", "cherry")) # note the double round-brackets 
print(list1)


Access Items
Print the second item of the list: 
list1 = ["apple", "banana", "cherry"] 
print(list1[1])

Negative Indexing 
Negative indexing means start from the end -1 refers to the last item, -2 refers to the second last item etc.
list1 = ["apple", "banana", "cherry"] 
print(list1[-1])

Return the third, fourth, and fifth item: 
list1 = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] print(list1[2:5])



list1 = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] print(list1[:4])


list1 = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] print(list1[2:])


list1 = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] print(list1[-4:-1])

list1 = ["apple", "banana", "cherry"] 
if "apple" in list1: 
    print("Yes, 'apple' is in the fruits list")


Using the append() method to append an item: 
list1 = ["apple", "banana", "cherry"] 
list1.append("orange") 
print(list1)


Insert an item as the second position: 
list1 = ["apple", "banana", "cherry"] 
list1.insert(1, "orange") 
print(list1)


list1 = ["apple", "banana", "cherry"] 
tropical = ["mango", "pineapple", "papaya"] 
list1.extend(tropical) 
print(list1)


Add elements of a tuple to a list: 
list1 = ["apple", "banana", "cherry"] 
tuple1 = ("kiwi", "orange") 
list1.extend(tuple1) print(list1)


Change the second item: 
list1 = ["apple", "banana", "cherry"] 
list1[1] = "blackcurrant" 
print(list1)


list1 = ["apple", "banana", "cherry", "orange", "kiwi", "mango"] 
list1[1:3] = ["blackcurrant", "watermelon"] 
print(list1)

list1 = ["apple", "banana", "cherry"] 
list1[1:2] = ["blackcurrant", "watermelon"] 
print(list1)


Change the second and third value by replacing it with one value: 
list1 = ["apple", "banana", "cherry"] 
list1[1:3] = ["watermelon"] print(list1)


list1 = ["apple", "banana", "cherry"] 
list1.insert(2, "watermelon") 
print(list1)


list1 = ["apple", "banana", "cherry"] 
list1.remove("banana") 
print(list1)


Remove the second item: 
list1 = ["apple", "banana", "cherry"] 
list1.pop(1) 
print(list1)


Remove the last item: 
list1 = ["apple", "banana", "cherry"] 
list1.pop() print(list1)


list1 = ["apple", "banana", "cherry"] 
del list1[0] 
print(list1)









list in python example list in python class 11 list in python class 11 notes list in python is mutable or immutable list in python are immutable list in python append list in python add list in python addition list in python and its functions a list in python is immutable a list in python is mutable reverse a list in python sort a list in python list in python example unordered list in python empty list in python list in python how to create list in python python list function how to add to a list in python list in python is mutable or immutable


Post a Comment

0 Comments