Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
177 views

Python Scripting

The document provides an overview of a 4-day Python Scripting training taking place from May 20th-23rd. It includes details about using the Python interactive shell via IDLE and Jupyter notebooks. It then demonstrates various string operations in Python like concatenation, indexing, slicing, and string methods. It also covers list operations, dictionaries, file handling, modules, classes, inheritance and exception handling in Python.

Uploaded by

Nikita Agrawal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
177 views

Python Scripting

The document provides an overview of a 4-day Python Scripting training taking place from May 20th-23rd. It includes details about using the Python interactive shell via IDLE and Jupyter notebooks. It then demonstrates various string operations in Python like concatenation, indexing, slicing, and string methods. It also covers list operations, dictionaries, file handling, modules, classes, inheritance and exception handling in Python.

Uploaded by

Nikita Agrawal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 108

Python Scripting-training - 20th May – 23rd May

Webex link - https://thinkwright.webex.com/thinkwright/j.php?MTID=mafb0692d7ea0a8f56f7eb59b03fb26e2


Python interactive shell –

- 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

>>> Address="Mahavir Ornate, Sector11, Kperkhairane, Navi Mumbai"


>>> print (Address)
Mahavir Ornate, Sector11, Kperkhairane, Navi Mumbai
>>> str_list=Address.split()
>>>for wd in str_list:
print(wd)
Mahavir
Ornate,
Sector11,
Kperkhairane,
Navi
Mumbai
>>> print(fullname[0])
N
>>>print(fullname[-1])
L
Slicing can also be done via : operator
>>> print(Address[0:5]) #total five character will be printed
Mahav
>>> print(Address[1:5])
ahav
>>> print(fullname[0:6])
Nikita
>>> print(fullname[0:5:2]) #display values by difference of 2
Nkt
>>> print(fullname[0:10:2])
Nkt g
>>> print(fullname[::1])
Nikita agrawal
>>> print(fullname[::-1])
lawarga atikiN
>>> print(fullname[::2])
Nkt gaa
>>> name="NAMAN"
>>> if name[::1]==name[::-1]:
print("yes")
else:
print("no")

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.

To change this behavior –


Use list2=list1[:] instead of list2=list1

If element are also list then will take it as 2D array


List2=deepcopy(list1)
>>> import deepcopy

Traceback (most recent call last):

File "<pyshell#134>", line 1, in <module>

import deepcopy

ModuleNotFoundError: No module named 'deepcopy'

>>> from copy 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

Use of pass in class


Pass in try catch exceptions

Function – def function name


Passing the perimeter to function

Passing by reference or passing by value.

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.

So we can make our perimeter using keywords.


We saw, required variable, Keyword, default value and now we will see variable number of arguments.

Here arg is not a reserved word.

By giving star, we can pass any parameters as we want.


Usage of this variable number of parameters

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.

Arguments that are passed, Converted it to a list

Then check which is the max number.

Another way - below we are using directly args in for loop like we use list or tuple.

Print all dictionary values -


Another way of printing –

Lambda function

Here g and square has same reference so we can use g also instead of square.
Similarly we can use lambda

We can pass as many as parameter as needed.

We do not same return here

Lambda evaluated as expression.

This is majorly used with map and filter


Lambda function can have only single line of definition.

Using filter use functional programming.

Using map – transforming a list from one form to another by applying a process.
Now we can use lambda in case of functions.

This will work in same way.

Filter always returs as True/False, its evaluating expression, if expression is true then that number will
be passed.
Scope of variable

Global and Local


Iterable is ajn object.

Use yield to convert function as a iterrable. Its used in place of return.


Here we have used sum function to sum the numbers in list. Findsum is just sending the list.

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.

Above is input and output required.


------------\

Decorator function to change the functions.

Decorator improve the output of the function.


Decorator function should be above the main function.

Using @ to call-

File handling –
For r – reading – file should be present

For w= if file is not present then it will get created

For A – append

To open file there are 3 days to open

r filepath – this r is for raw string

or use \\ as mentioned in below program comment

or use linux file path

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.

Seek is used for getting cursor position, take two parameters.


This will not give sany output as fh.read() – put the cursor at end of the file.

Using seek we can reach out to beginning of the function


Module

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 module is independent script where it has print command as well


If we call the function now then this print statement will also get printed.

To solve this problem

If namespace is hello then mail will be executed else it won’t run.

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 –

If there are multiple files in a directory then we can make it as package.


CREATED director called mypackage

Created file ___init__.py

eExample1.py

example2.py

all these functions are mentioned in init function.

We are using. before the file inside this package as .example1 and then import the function.
Install other package –

Day 3

Class

___init___ = is the constructor.


If we want to print { in our program
Passing argument to a constructor

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.

Instance variable here x and y are not visible to other object.

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

In case we want ot call parent class inside child class.


If we use A.display instead of super.().display() then gets error –

In case of class method this can be done by using @

Multi class inheritance.


Now in this case it will print from A, there is display method in both A and C but not in B

Mro = Method resolution order

Search order-
Now if A also has parent then if its not present in A then it will go to D.

It takes order in which its inherited


Changing the order of inheritance - Now doing class B(C,A)

Operator overloading
If we want to do obj1+obj2 it till give error.

So we can use predefined __add__ to define it.


We can refer the documentation to get information about all the built in function(in idle)  using help
-->> python doc

We can define methods


Exception handling –

If we give input as string then int function can not to type cast and throw error.

To catch this we can use try


So error message can be suppressed. Here we can use pass or any print statement anything.

Sys.exc_info() is giving error details here.

Single try block can have multiple except block.


Try has else class, if try is not having error then it will be executed.
Finally, is specially used for cleaning up the resources, even if its ended with exception.

Regular expression

Import regular expression modules using import re


Checking blank lines

Here expression search is checking


Re.I will ignore the case.
It will print

mydomain
Greedy match (. Trying to match as much as possible)

Using ? it says as least as possible/


() shows the group.
Here first we are searching with blank line, if its true then continue checking next line.

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

Multiprocessing – use all CPU s in your order


Now will create 5 thread, we are sending argulment as tuple so we have to give , to show it as tuple (1,)
Will create Custom Thread class –

This is asynchronous right now.


To make it sequential-

Using lock acquire and release command to lock the resources and release that.
----------------

CSV file –

Read CSV file via csv.reader function.

Use CSV to make dictionary, using DictReader function.


Here we are printing only 2 column from CSV, but using this header we can print many columns.

Now reading Jason 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.

Its not load, its loads

We can convert a string in 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

See the difference of both the programs –

From file object we load but from string we use loads.


In dictionary find the key using recursive function
Log information –

Depending upon the situation, you can message it


Cutom logger name can be given example sample logger

This is directly coming to terminal, we can save it to the files as well.

Now to move these logs to file –


Rest of the code will remain same.

Just give filename in basic config


There are many things to work on log, like appending , rotating log, change colors of logs ..many options
are there.

Panda
Automatic index will be generated.

If we do want to give our own index then we can do so.


We can read into csv file and write to excel files.

Df variable taken for data frame


Now if we want to get particular range of data say from row 10-15

We want it only for few columns

Row, column
Now for example we need row number 10,18, 27 then we can write like this -

Now we need specific column as well as row

If we want to filter with some column name


Now if we have 2 Excel sheet

department file - -

Merge column wise using common column name.

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

We can do for different sheet wise also from same excel


We also have some matplot library
Sorting data frame
Numpy

You might also like