0% found this document useful (0 votes)
3 views2 pages

Python To Remember

The document covers various programming concepts including lists, dictionaries, and object-oriented programming in Python. It explains list operations such as appending, deleting, and sorting, as well as the use of loops and slicing. Additionally, it introduces classes, attributes, access specifiers, and the __str__ method for custom string representation of objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Python To Remember

The document covers various programming concepts including lists, dictionaries, and object-oriented programming in Python. It explains list operations such as appending, deleting, and sorting, as well as the use of loops and slicing. Additionally, it introduces classes, attributes, access specifiers, and the __str__ method for custom string representation of objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Chapter-3: Lists

* Lists is a collection of items in a particular order


* printing whole and individual
* index and reverse index
* .append()
* del lst[ind]
* x=lst.pop(pos)
* .sort() and .sort(reverse=true) and sorted(lst)--> gives temporary sorted list
*.reverse()
* .remove(val_in_the_list) ---> removes first occurrence in the list if not there
gives value error
* len(lst) --> gives length of list lst

chapter-4: working with lists

* for x in lst:
print(x) ## for evey element in lst we are storing in variable x and operating
on it
* range(st,end+1) -->gives no's from st to end with default step size of 1
* for x in range(st,end+1):
print(x) or print (lst[x]) # etc operations can be done
* lst=list(range(2,10,2)) //2,4,6,8
* min(lst) , max(lst), sum(lst) -->gives max, min, sum
* SLICING A LIST --> lst[st:end:step_size] --> if no st assumes from beginning, if
no end assumes till end ,default step=1
* for copying a list do it as lst_copy=lst[:]----->CORRECT and lst_copy=lst----
>WRONG_WRONG
* tuples are nothing but immutable lists
* In tuples you cant modify the it -->tup[0]=20 --->ERROR

chapter-5 : conditions --> SKIP

chapter-6: Dictionaries

* key value pairs storing


* looping through a dictionary : for k,v in mp.items():
* looping through only keys : for k in mp:
* looping through values only : for v in mp.values():
* If you need something sorted just add sorted before mp
* Nested datastructures same syntax but will be nested

chapter-7:OOPS
* class keyword---> to make class and variables in class are attributes and can be
accessed by . operator
* An object consists of:
* State: It is represented by the attributes and reflects the properties of an
object.
* Behavior: It is represented by the methods of an object and reflects the
response of an object to other objects.
* Identity: It gives a unique name to an object and enables one object to
interact with other objects.

* Access specifiers: self.name=name ---> public attribute


self._age=age ---> protected attribute
self.__color=color ---> private attribute
* Use getter and setter for private attributes

* __str__ Method
__str__ method in Python allows us to define a custom string representation of an
object. By default, when we print an object or
convert it to a string using str(), Python uses the default implementation, which
returns a string like
<__main__.ClassName object at 0x00000123>

def __str__(self):
return "//write something here "

You might also like