Python Scripting
Python Scripting
- Using IDLE
- Ipython notebook (Jupiter)
String Operation
>>> fname="Nikita"
>>> lname="agrawal"
print(fname+" "+lname)
Nikita agrawal
>>> fullname=fname+" "+lname
>>> print(fullname)
Nikita agrawal
>>> print("Hello ",fullname)
Hello Nikita agrawal
>>> print(fullname.upper())
NIKITA AGRAWAL
>>> print (fullname.lower())
nikita agrawal
>>>print(len(fullname))
14
>>> print(len(fname))
6
>>> print(len(lname))
7
>>> for ch in fullname:
print (ch, " ", end="")
N i k i t a a g r a w a l
yes
>>> name="Nikita"
>>> if name[::1]==name[::-1]:
print("yes")
else:
print("no")
String operations
List
Enumerate function return 2 values that’s why we have use x,y and first value is index of list and second
value will be value of that index.
File operation
Multiple value in dictionary
This will print list2 3 times
Reference is not modified
re
Changing list 1 will change list2 as well.
import deepcopy
>>>
Appen take only one perimeter and it behave the multiple values as another list as single element.
Insert take 2 parameters, its similar to append. First where you want to insert/
You will get index and value
Dictionary
Read CSV file and make dictionary-
Operator
If/else
For loop –
Prime number
In case of above code where we have sent mylist, we have reference same and the value changed inside
the function will retain outside function as well.
But in case of below program, in case of string, changes done inside the program is not reflected outside
of the program.
Argument of function –
Below will give error if we do not pass the value.
Below is valid –
Multiple Argument -
We can change the parameter order but it will not work as we wanted or expected.
For example- we are using from excel sheet, we are not sure how many rows or columns are there in a
file so number of parameters is unknown initial
Another example, we want to find the maximum number from the tuple or list.
Another way - below we are using directly args in for loop like we use list or tuple.
Lambda function
Here g and square has same reference so we can use g also instead of square.
Similarly we can use lambda
Using map – transforming a list from one form to another by applying a process.
Now we can use lambda in case of functions.
Filter always returs as True/False, its evaluating expression, if expression is true then that number will
be passed.
Scope of variable
Instead of returning list we are using yield, in case of large number list will throw out of memory error.
But yield will process ..as it will send one value one by one.
Using @ to call-
File handling –
For r – reading – file should be present
For A – append
Reading a file.
Write into file
Binary content
In order to read write image file then we can do it but we have import some library.
Create a module, update the path variable to provide location of your module and then you can import
it from other program.
Hello as module –
Importing hello
Or
Now this can run as independent module as well as can be imported as module.
When we call hello.addnum then namespace will be passed as hello not as main so that will not go to
mail function.
Package –
eExample1.py
example2.py
We are using. before the file inside this package as .example1 and then import the function.
Install other package –
Day 3
Class
Repr function - Repr is function automatically called, when we say print object else it will show object
reference in hexadecimal form.
Repr is built in function.
Class variable –
Class variables are shared across the object. Example here is number_of_points, its similar to static
variable.
Destructor is another built in method. Its called when scope of object goes off or its got deleted.
In case of Linux this destructor is not required to call explicitly …it will implicitly called.
Get attribute
Inheritance
Method overwriting
Search order-
Now if A also has parent then if its not present in A then it will go to D.
Operator overloading
If we want to do obj1+obj2 it till give error.
If we give input as string then int function can not to type cast and throw error.
Regular expression
mydomain
Greedy match (. Trying to match as much as possible)
Second check if line starts with # followed by any character and then anything which we put in d2
dictionary that is temporary dictionary, copy the content to new dict.
Here set default is used to create new person if its not already present. At position count and copy
content of d2 to d1.
We are using copy instead of assigning other wise it will be assigned as refrence, and when we do
d3.clear then it will clear d1 as well.
In else part we are splitting he line using : as delimiter and then d1 key valyue will be first part and value
is second part.
Database-
DB operations – Insert, select, delete …display…
Trading
Using lock acquire and release command to lock the resources and release that.
----------------
CSV file –
Import Json
We are writing dictionary in json file as a string, here d1.json is filename. Do not confuse it with di
dictionary variable.
Using dump function, d1 dictionary is dumped into file.
Reading the Jason file in dictionary, using load function to loads the data of file to dictionary.
Here we have not pull any file, we are using string as input and converting this to load to dictionary.
Now convert from string to dictionary, use dumps function, with one input argument as dictionary and
output is string. Here we are using dumps, not dump
Panda
Automatic index will be generated.
Row, column
Now for example we need row number 10,18, 27 then we can write like this -
department file - -
Now if we want to merge. If decode is not available, then it will put NaN.
Similarly we can do using right, here age is coming as NaN