Python To Remember
Python To Remember
* 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-6: Dictionaries
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.
* __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 "