python list object code example

Example 1: python list

# example of list in python

myList = [9, 'hello', 2, 'python']

print(myList[0]) # output --> 9
print(myList[-3]) # output --> hello
print(myList[:3]) # output --> [9, 'hello', 2]
print(myList) # output --> [9, 'hello', 2, 'python']

Example 2: making lists in python

# list with numbers
list = [10, 20, 30]
# list with strings
list = ["john", "kai", "albert"]
# mixed data
list = ["john",1 ,True ]

Example 3: how to call object of a list python

L = [1, 2, 3, 4]
print(L[1]) ## Calls the index position of the list "L"

Example 4: python list of objects

# Create a new class
class MyClass(object):
    def __init__(self, number):
        self.number = number


# Create a array to store your objects
my_objects = []

# Add 100 objects to array
for i in range(100):
    my_objects.append(MyClass(i))

Example 5: lists in python

[0,'',True,5.2,False,[1,2]]