What is List in Python | EP-16 List in Python | How to Create List in Python | Lists Functions

preview_player
Показать описание
A list is a collection of things, enclosed in [ ] and separated by commas.
The list is a sequence data type that is used to store the collection of data.
The list is mutable elements. It contains heterogenous value.
Tuples and String are other types of sequence data types.

1. using []
2. using constructor
"""
Creating a list in Python
empty list
list1 = []
print(list1)

list2 = [1,2,3]
print(list2)

list3 = ["a",1,2.5]
print(list3)

type of list
print(type(list3))

length of list
print(len(list3))
print(len(list1))

2. using constructor
list4 = list("a")
print(list4)

list5 = list(("a","b","c"))
print(list5)

my_tuple =(1,2,3)
my_list = list(my_tuple)
print(my_list)

Accessing elements from the List
"""
To access the list items refer to the index number.
Use the index operator [ ] to access an item in a list. The index must be an
integer. Nested lists are accessed using nested indexing.
"""
list6 = [1,2,3,4,5,6]
print(list6[5])
print(list6[0])

Accessing elements from a multi-dimensional list
"""
In Python, multi-dimensional lists, also known as nested lists, are lists that contain
other lists as elements.Since a multi-dimensional list is essentially a list of lists,
you access elements by using square brackets [] with nested indexes.
The first index refers to the position of the inner list (like a row in a table),
and the second index refers to the element's position within that inner list
(like a column).
"""
list7 =[[1,2,3],["a","b"]]
print(list7[0][1])
print(list7[0][2])
print(list7[1][1])
print(list7[1][0])

Negative indexing
"""
In Python, negative sequence indexes represent positions from the end of the List. Negative indexing means beginning from the end,-1 refers to the last item, -2 refers to the second-last item, etc.
"""
list8= ["c","b",1,5]
print(list8[-1])
print(list8[-2])
print(list8[-3])

Taking Input of a Python List
"""
We can take the input of a list of elements as string, integer, float, etc.
But the default one is a string.
Note:
The split() method in Python is a versatile tool for splitting a string into a
list of substrings based on a specified separator.
"""
company = input("enter the company name")
print(list9)

#Python #PythonLists #PythonTutorials #LearnPython #CodingForBeginners #Programming #TechEducation #PythonFunctions #ProgrammingForBeginners #TechTutorials #pythonprogramming
#datatypes #beginnerpython #programmingtutorial

Connect With Us:
—————————————
Рекомендации по теме