Python Prep
Python Prep
Access Modifier
_ _ variableName : private –Class properties and methods with private access
modifier can only be accessed within the class where they are defined and cannot
be accessed outside the class. In Python private properties and methods are
declared by adding a prefix with two underscores(‘__’) before their declaration.
One is public : By default the member variables and methods are public which
means they can be accessed from anywhere outside or inside the class. No public
keyword is required to make the class or methods and properties public.Here is an
example of Public access modifier
Using copy() method we can avoid the issue facing with Mutable object
With copy() method new ID get assing to the variable if we change one variable
thenit will not affect any other one
Escape Sequence (To avoid its special meaning we write ‘/’ 2 times)
If you want the string to print exact what we have written then you should write
this in triple quote
Or To print the 3 line of string in only one line then you can use / after ending of
each line
Both will print same output We can use r to print it as raw string
String
o/p=P
String interpolation
Need only one % or you can write multiple % for individual variable
Formatting of String
There is not need write anything inside {} if we don’t want to format it
{0:2} //It uses 2 character space and 1st value with right align default one
{1:3} //It uses 3 character space and 2nd value with right align
{0:<2} //It uses 2 character space and 1st value with leftalign
{1:<3} //It uses 3 character space and 2nd value with left align
{0:^2} //It uses 2 character space and 1st value with middle align
{1:^3} //It uses 3 character space and 2nd value with middle align
FString in Python
We can add any variable instead of 22/7
Total 100 character and 70 decimal value as python is able to calculate upto 51
digit so remaining 70-51=19 digits are filled with 0
Slicing
Loops
For loops
print(sum([int(val) for val in value])) #will print the sum of all the digits in the string
Else in Loop
If whole for loop get executed then else get executed
If whole loop is not completed and occurred any interruption only break not
contine then else loop will not get executed
Built In Function
Enumerate() Function in Python (To get index and value it same time in
the form of tuple)
List in Python
After every deletion the index value gets changed and the new index is assign
to the variable
Here we have removed all the value greater than 400 and lesser than 200
We can create tuple with single string with comma , should be there at last
Dictionary in Python
You can create the Dict using dict constructor but keys should be string and
values can be anything like int or string
len(dict2) #2
To copy you can use copy() method or dict(dict_reference) method
Ex. newdict1=dict(dict2)
Nested Dictionary
Keyword Arguments
Value are getting passed w.r.t. Value by reference/and in python we dont have
pass by pointer So list[0] value get changed to 0
As we are assigning new value i.e. new memory therefore list can not change its
value
To change the value of default argument we have to pass it with variable name otherwise
is take it as part of optional argument
The variable argument which is not part of argument in function then it should pass at
last and it store as dictionary
OOPs
Class Variable
We can access class variable using class name but in python we can access same variable
using object, if we change the value using object reference then it act as instance variable for
that particular object
split() method
Join method (Only work with String)
sort() vs sorted()
sort() It modify the existing String
sorted() It sort and returned the String Sorting based on the ASCII value
sorted() It sort and returned the String Sorting based on the ASCII value to avoid this you can
use key=str.casfold as following similarly you can use this for sorted() function as well
reversed() function
reversed function is returning index value as 0,1,2 but value is in reverse order
Exception Handling
Finally block will always execute the code irrespective of the return statement in try block
After execution of finally block it will return value form try block
If the try and finally block having the return statement then it will ignore the return statement
from the try block and execute the return statement from the finally block
_ _ init_ _ ( ) Method are special method which influence the value of variable
Its also called as Dunder Method / Instance Method