Python for Data Science - part 2
- Creating functions in Python
- Understand build-in functions
- Keywords Argument
- Accepting input
- Nested Functions
- Files in Python
- Lambda Functions
- Object-oriented programming in Python
- Creating Class
- Global Variables
- Super keyword
- Modules in Python
- Introduction to Python libraries in Machine learning
- pip - Package Manager used in Python
Let's get started.
Functions are reusable code that we need to execute repeatably. Python has some of the build in functions like:
- print - To print result. For e.g: print('Hello World');
- len - To find out the length of the variable. For e.g:
var1 = [1, 2, 3, 4]; print(len(var1))
Some other useful built-in functions that we usually use to solve data related problems are:
- Sorted - It is used to sort the object in order. It takes two arguments as shown below:
arr = [11, 23, 45, 9, 45.8, 9, 13] print(sorted(arr, reverse = False)) # Output [9, 9, 11, 13, 23, 45, 45.8] print(sorted(arr, reverse = True)) # Output [45.8, 45, 23, 13, 11, 9, 9]
- upper and count string functions: These are the function that we can perform on the string. To know the complete list of string function you can type help(str)
string = "learning python" print('String to upper case: ' +string.upper()) print(string.count('n')) #output: String to upper case: LEARNING PYTHON 3
Apart from that, we can create our own functions like we can in almost all programming languages.
To declare a function we use a def keyword. An example is shown below:
def sum_number(x,y): z = x + y return z
And we can call a function like:
result = sum_number(4,6) print(result)
We can even pass the default value for the variable. Refer the code shown below:
def sum_number(x,y=0): z = x + y return z
So now we can call a function as:
result = sum_number(4) print(result) # 4 result = sum_number(4, 6) print(result) # 10
Another way to call sum_number function is as shown below:
result = sum_number(x=4, y=6)
This will make more clear and is good practice while coding.
What if we don't know how many arguments does the function have?
def employee(name, *args): print(name) print(args)
Now we can call above function as
employee("Mark", 123, 3.14, "Rohit") #or employee("Mark", 123)
As we have explored about tuples in the previous post, (If not I highly recommend to go through Python for Data Science - part 1) let's see how we can use them in functions. Here for demonstrating function, I will be writing a function that returns a tuple. Below is the code for that:
def sayHi(name, message): fName = name.capitalize() fmessage = message + '!!!' tupple_result = (fName, fmessage) return tupple_result name, message = sayHi('python', 'Welcome to python course') print(name) # Python print(message) # Welcome to python course!!!
In the code shown above, you would have noticed that we are creating a tuple `tupple_result`. Which we are returning from the function. The when we are calling a function we are storing the result in separate variables: name and message.
Keywords arguments are used when we are not sure about the number of arguments that a function may require. To understand it consider an example shown below:
def employee(name, **kwargs): print(name) print(kwargs["id"], kwagrs["name"])
And we can call a function as :
employee("Mark", id=123, name="Rohit")
We can accept input simply by using Python build in Input function as shown below:
name = input('Enter employee name') print(name)
We can even nest a function within the existing function. The nested function has access to variables defined in the nested function. Please refer to the code shown below to declare nested functions:
def declare(): name= 'Python' def getName(): print(name)
Files in python
f = open('employee.txt', a);
where a stands for append. There are some other access mode in Python. These are:
- w - write
- r - read
- rb - read in binary mode
- wb - write in binary mode
These are an anonymous function that doesn't have any name. It doesn't have a def keyword.
result = lambda x: x+10
which is equal to:
def result(x): x = x+ 10
We can call lambda function as:
Object-Oriented Programming in Python
You must know the power of Object-oriented programming if you have ever worked with object-oriented languages like Java, C# and lot more. Python also supports object-oriented programming and we can define a class in python. Let's explore more how to achieve this in Python.
Creating class in Python has a very similar structure as any other programming language. Below is the syntax to create a class in Python.
class Employee: def EmployeeName(self, name): print(name) emp = Employee() emp.EmployeeName('Tavish')
NOTE: Here we are passing self argument to function, which has to be a first argument in python. Don't be confuse try to think it as this keyword as any other programming language.
Constructors method in python is defined as '__init__()'. Let's try to replace the above function method as constructor method.
class Employee: def __init__(self, name): print(name) emp = Employee("Tavish")
Let's explore more about 'self' keyword. 'Self' keyword is a keyword which is aware of all the properties in the class. Confused? Let's understand with the help of an example:
class Employee: def setName(self, name): self.name = name def getName(self): print(self.name) emp = Employee() emp.setName('Tavish') emp.getName()
In the example shown above, I am using setters and getters to set employee name. And employee name is set in self.name. So this means that now Employee class is aware that name property exists in the class and can be used anywhere in the class. As I am using here in a getName method.
We can also define variables that do not change with the instances. There are the global variables for the class. Refer the example shown below:
class Employee: company = "Test co.in" def __init__(self, name): print(name +' ' + self.company) emp = Employee("Tavish") emp = Employee("Employee") print(Employee.company) #Output Tavish Test co.in Employee Test co.in Test co.in
In the code shown above, we have created two instances of the Employee class, but you will notice that in output company remains the same.
Also, you might have noticed that we can even directly able to access company without even creating the instance of the class.
When we are learning object-oriented programming it is very important to know how inheritance works. Let's dig into it to find out how inheritance works in Python.
Suppose we have Employee class that we have defined earlier. But now we need to have EmployeeCompany class to find out where the employee is working.
That means we need to inherit Employee class in EmployeeCompany class.
class Employee: company = "Test co.in" def __init__(self, name): print(name +' ' + self.company) def get_company(self): print(self.company) class EmployeeCompany(Employee): company = "Example co.in" def get_company(self): print(self.company) emp = EmployeeCompany("Tavish") emp.get_company() emp = Employee("Tavish") emp.get_company()
NOTE: Here in above example Employee is parent class and EmployeeCompany is derived class.
A lot of code!!! Let's understand it chunk by chunk.
1. In the code shown above, I am able to call Employee constructor despite creating an instance of EmployeeCompany. The reason is clear that class is inherited.
2. I am also able to overwrite the methods in two class. You can notice that I have get_company method defined in both the class. Now if I call get_company method by creating an instance of EmployeeCompany class the EmployeeCompany class get_company method will be executed.
And if I call get_company method by creating an instance of Employee class the Employee class get_company method will be executed.
We also have a super keyword with which we can call methods of a parent class. Let's find out how with the help of example shown below:
class Employee: company = "Test co.in" def __init__(self, name): print(name +' ' + self.company) def get_company(self): print(self.company) class EmployeeCompany(Employee): company = "Example co.in" def get_company(self): super().get_company() print(self.company + " From EmployeeCompany class") emp = EmployeeCompany("Tavish") emp.get_company()
In the example shown above, you will notice that in EmployeeCompany class get_company method we have a super keyword. It will call the get_company method of Employee class. So the output from above code will be:
#output Tavish Example co.in Example co.in Example co.in From EmployeeCompany class
Modules in Python
We can break our code among various files.
- Our Employee class is in employee.py file
- Our EmployeeCompany class is in employeeCompany.py file
And we have our main.py file having the following lines of code:
emp = EmployeeCompany("Tavish") emp.get_company()
If we run the code we will land into error because `main.py` file doesn't know about EmployeeCompany class. We can fix this by using an import statement as shown below:
from employeeCompany import EmployeeCompany
Adding the above line at top of the `main.py` file will fix the issue.
Python Libraries for Machine Learning
As we have grasped a good knowledge of Python. It's time to dive into and look at the packages that Python has provided for machine learning. These are as shown below:
- numpy - Used for scientific computing
- pandas - Make it easy to analyze the data
- matplotlib - Used for 2D plotting
- scikit - Used for performing many machine learning operations like pre-processing of the data.
We will be using these libraries when we will be diving into creating models using machine learning.
How to convert python code into an executable file?
The answer is pyinstaller.
To install pyinstaller run below command:
pip3 install pyinstaller
NOTE: pip or pip3 is a package manager for python.
To create an exe file:
That's it. It will generate an exe file. But you will notice that there will be a lot of files that will be generated in dist folder.
To generate a single executable file we can pass --onefile flag to pyinstaller as shown below:
pyinstaller --onefile <rootfile.py>
Now we will find only one executable file in dist folder.
We don't ship our code as exe file. Right? We need users to install our application in order to use it. We need setup wizard using which the application will be installed on pc. Let's look into it how we can generate setup wizard for users to install the application.
innosetup is used for windows to generate setup manager. Innosetup is itself self-explanatory as it walks us through various steps with which we can generate setup file. Please go through documentation if you have any questions.
Hope that you have some good knowledge about basics of Python.