Monday, April 22, 2019

Python Bible

Data Structures in Python

List 


list.append(x)
Add an item to the end of the list.
list.extend(iterable)
Extend the list by appending all the items from the iterable. 
list.insert(ix)
Insert an item at a given position. 
a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
list.remove(x)
Remove the first item from the list whose value is equal to x.
list.pop([i])
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. 
list.clear()
Remove all items from the list.
list.index(x[start[end]])
Return zero-based index in the list of the first item whose value is equal to x 
list.count(x)
Return the number of times x appears in the list.
list.sort(key=Nonereverse=False)
Sort the items of the list in place
list.reverse()
Reverse the elements of the list in place.
list.copy()
     Return a shallow copy of the list


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

Tuples 

A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists, tuples use parentheses, whereas lists use square brackets.

eg
tup1 = ('physics', 'chemistry', 1997);
tup2 = (1, 2, 3 );
tup3 = "a", "b",;

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

Dictionary 

A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values.

thisdict = {
  "brand""Ford",
  "model""Mustang",
  "year"1964
}
print(thisdict)

Share:

0 comments:

Post a Comment

Total Pageviews

About

Niranjan Adhikari is currently pursuing Computer Engineering in WRC IOE,Pokhara. Resident of Butwal,Niranjan's interests include Poetry, Design and Programming
Powered by Blogger.