Apply for Zend Framework Certification Training

Python




< First Getting Started With CodeIgniter URL Routing >



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.

Python collections(array)

There are four collection data types in the Python programming language: 
List is a collection which is ordered and changeable. Allows duplicate members.
Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate members.
Dictionary is a collection which is ordered** and changeable. No duplicate members.

Eg:-
#How to create an List
mybooks = ['english', 'math', 'python', 'science']
#How to print an List
print(mybooks);
# How to get the type of an Lists
print(type(mybooks));
# How to access items of an Lists
print(mybooks[1])
print(len(mybooks))
mtdatatypelist =[1,2,3,4,5,6,7,8,9]
print(type(mtdatatypelist))
mydatalistofdifferenttypes = [1,'hello',3,True,4,5,6,7,8]
print(mydatalistofdifferenttypes)
print(mybooks[0])
# Negative indexing Negative indexing means start from the end(count position)
print(mybooks[-1])
print(mybooks[-3])
# Range of index [index,position]
print(mybooks[1:3])
mybooks1 = ['english', 'math', 'python', 'science','x','y','z']
print(mybooks1[:5])
print(mybooks1[2:5])
print(mybooks1[2:])
# Range of index Negative values
print(mybooks1[-4:-1])
print(mybooks1[-4:-2])
# to check whether a perticular element is present ot not
if 'english' in mybooks1:
    print('yes it existes')
#How to change the elements of an lists
mybooks1[0]= "social";
print(mybooks1)
# How to add an element in an list
mybooks1[1:3] = ['physics', 'chemistry'];
print(mybooks1)
['social', 'physics', 'chemistry', 'science', 'x', 'y', 'z']
mybooks1[1:0] = ['physics1', 'chemistry1'];

print(mybooks1)
['social', 'physics1', 'chemistry1', 'physics', 'chemistry', 'science', 'x', 'y', 'z']

< First Getting Started With CodeIgniter URL Routing >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top