list commands python code example

Example 1: list methods python

list.append(x) # append x to end of list
list.extend(iterable) # append all elements of iterable to list
list.insert(i, x) # insert x at index i
list.remove(x) # remove first occurance of x from list
list.pop([i]) # pop element at index i (defaults to end of list)
list.clear() # delete all elements from the list
list.index(x[, start[, end]]) # return index of element x
list.count(x) # return number of occurances of x in list
list.reverse() # reverse elements of list in-place (no return)
list.sort(key=None, reverse=False) # sort list in-place
list.copy() # return a shallow copy of the list

Example 2: python commands

simple python commands

print("your text here")

-----------------------------

name = input("what is your name")
print("nice to meet you " + name)

-----------------------------

A = int(input("give me the first num "))
B = int(input("give me the second num "))
f = A + B
print(f)

Example 3: 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 4: list in python

thislist = ["apple", "banana", "cherry"]
print(thislist[1])

Example 5: list in python

#list is data structure 
#used to store different types of data at same place
list = ['this is str', 12, 12.2, True]

Tags:

Cpp Example