PYTHON
Get Shubham Nagalwade’s stories in your inbox
Join Medium for free to get updates from this writer.
Python is a highlevel programming language. Widely used in Data Science / Machine Learning algorithm. The “Python” language invented by greatest researcher by Guido Van Rossum at CWI in Netherland. He is member of the “ National Research Institute of Mathematics and Computer Science”. What is mean by CWI? CWI is “ Centrum Wiskunde & Informatica ” is the national research institute for mathematics and computer science in the Netherlands. Founded in 1946. Python programming language originates from research institute “Centrum Wiskunde & Informatica (CWI)” in Amsterdam, Netherlands. He started working at CWI, fresh out of university, as a programmer in the ABC group. Due to the lack of obvious success he is moved to the Amoeba group. But ABC gave me the key inspiration for Python, Amoeba the immediate motivation. The name Python came from………… One of famous comedy troupes name as “ Monty Python’s Flying Circus ”. The reference of these show and the word “ Python ”. Some people thinking that name came from animal “Snake”. It was an O’Reilly tradition to use animal pictures, and if it had to be an animal, it might as well be a snake. Finally, when O’Reilly wanted to put a snake on the front of their first Python book “Programming Python”. Python was originally foundation in the late 1980s. Final implementation of this language in “ December 1989 ” and finished the first working some months after that in 1990. The first public release of version 0.9.0 on 20 February 1991 van Rossum published the code (labeled version 0.9.0) to alt.sources. In summer 1994, Python 1.0 was released with new features like : ( lambda, map, filter, and reduce ). In 2000, Python 2.0 was released. This version of Python included list comprehensions, a full garbage collector, and it supported Unicode.Then after long time duration python language was fully developed, on December 3, 2008. Python 3.0 (also called “Py3K”) was released. Difference Between python 2 and 3 Python 2 and 3 are similar there are subtle differences. The way the print statement works, as in Python 3.0 the print statement has been replaced with a print () function. Why Python? Python is a generalpurpose language,Which means it can be used to build just about anything. Which is designed to be simple to read and write. Which makes it very easier to work with, even for nonprogramming backgorund or developers. Professionally, Python is great for artificial intelligence, machine learning, data analysis, backend web development, and scientific computing. Many developers have also used Python to build productivity tools, games, and desktop apps. Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc). 1) Easy to Understand Python handles a lot of complexity for you, so it is very beginnerfriendly in that it allows beginners to focus on learning programming concepts. 2) Very Flexible This means there are no hard rules on how to build features. 3) Has a Amazing Libraries and Frameworks It has hundreds of different libraries and frameworks that can be used by developers. There are many libraries, some are useful for Machine Learning and Artificial Intelligence. For Example : a) Keras b) TensorFlow c) Scikitlearn 4) Is a Reliable and Efficient You can work across several domains including, but not limited to web development, desktop applications, mobile applications, hardware and more. 5) Open Source It’s free of Cost. It can be easy to set up the environment. 7) Has a Community Because of this support / Community, Python learners can easily improve their Machine Learning knowledge, which only leads to increasing popularity. Some of largest community basically for Python. a) 5th Largest StackOverflow Community b) 4th MostUsed Language at GitHub c) 3rd Largest Meetup Community Introduction to Python It was designed with an emphasis on code readability, and its syntax allows programmers to express their concepts in fewer lines of code. It is used for : 1) Web development 2) Software development 3) Mathematics 4) Mobile Application 5) Machine Learning There are two major Python version : Python2 and Python3. 1) Finding an Interpreter: Before we start Python programming, we need to have an interpreter to interpret and run our programs. Number of interpreter freely available in the market such as Spider, Jupyter Notebook etc. Same interpreter install on different OS (operating system). Following procedure how to install the Python Interpreter on different OS. Install the Anaconda : 1) Windows : There are many interpreters available freely to run Python scripts like IDLE (Integrated Development Environment) that comes bundled with the Python software. i. Download the Anconda Installer https://www.anaconda.com/download/#windows ii. Double click the installer to launch. Then you can show this dialog box. iii. Click Next. iv. Read the licensing terms and at the last click “I Agree”. v. Select an install for “Just Me” unless you are installing for all users (which requires Windows Administrator privileges) and click Next. There is two option one is “Public User” and another is “Just Me”. vi. Select a destination folder to install Anaconda and click the Next button. See below diagram. vii. Choose whether to add Anaconda to your PATH environment variable. We recommend not adding Anaconda to the PATH environment variable, since this can interfere with other software. Instead, use Anaconda software by opening Anaconda Navigator or the Anaconda Prompt from the Start Menu. Better way choose the default path. viii. Choose whether to register Anaconda as your default Python. Unless you plan on installing and running multiple versions of Anaconda or multiple versions of Python, accept the default and leave this box checked. ix. Hit the Install button. x. Click the Next button. xi. After a successful installation you will see the “Thanks for installing Anaconda” dialog box: xii. Hit the Finish button. Enjoy. 2) LINUX / UNIX : i. for x86 systems. ii. In your browser, download the Anaconda installer for Linux. Choose latest version. iii. RECOMMENDED: Verify data integrity with SHA256. For more information on hashes, see What about cryptographic hash verification? iv. Open a terminal (short cut key : ctrl + alt + T) and run the following : sha256sum /path/filename v. Enter the following command to install Anaconda for Python 3.7: bash ~/Downloads/Anaconda32019.10Linuxx86_64.sh vi. Enter the following command to install Anaconda for Python 2.7 : bash ~/Downloads/Anaconda22019.10Linuxx86_64.sh Note : If you did not download to your Downloads directory, replace ~/Downloads/ with the path to the file you downloaded. vii. The installer prompts “In order to continue the installation process, please review the license agreement”. Click “Enter” to view license terms. viii. Scroll to the bottom of the license terms and write “Yes” to agree. ix. You accept the default install location, the installer displays “PREFIX=/home//anaconda” and continues the installation. It may take a few minutes to complete. x. The installer prompts “Do you wish the installer to initialize Anaconda3 by running conda init?” We recommend “yes”. xi. The installer finishes and displays “Thank you for installing Anaconda!” Enjoy. 3) Installing on macOS You can install Anaconda using either the graphical installer (“wizard”) or the command line (“manual”) instructions below. If you are unsure, choose the graphical install. i. Download the graphical macOS installer for your version of Python. ii. RECOMMENDED: Verify data integrity with SHA256. For more information on hashes, see What about cryptographic hash verification? iii. Doubleclick the downloaded file and click continue to start the installation. iv. Answer the prompts on the Introduction, Read Me, and License screens. v. Click the Install button to install Anaconda in your ~/opt directory (recommended): vi. OR, click the Change Install Location button to install in another location . On the Destination Select screen, select Install for me only. Click the continue button. Enjoy. After installation open the “Anaconda Navigator” and wait a few minute. Open new window like following picture, Then launch the Jupyter notebook for python. It is a IDE for python. Pacakages / Library : After launching you can see the notebook like structure. For machine learning required some library for run the mathematics as well statistical function. How to install it? Follow the procedure. i. If you use jupyter notebook simply write ( !pip install pandas ) instead of pandas used whatever you required the packages. Difference is !pip and pip is used jupyter notebook and linux terminal respectively. ii. Some of library by default install. So just check the packages by “!pip list”. Don’t waste time for installing process. Just check it in a list and install it using pip. What is mean by PIP? PIP is a package manager for Python packages or modules if you like. A package contains all the files you need for a module. Install PIP If you do not have PIP installed, you can download and install it from this page: https://pypi.org/project/pip/ Download a Package High performance of package available for machine learning tool they also install using pip. Example : !pip install DecisionTree for jupyter notebook Example : pip install DecisionTree for linux terminal sometime new/old packages didn’t working properly so basic thing is update and upgrade your pytohn. Following command used for update / upgrading. !pip install upgrade pip !pip install update pip both for python2 if used python3 then small changes is over i.e. python3 m pip install –upgrade pip python3 m pip install –update pip Using a Package Once the package is installed, it is ready to use. Import the “pandas” package into your project. “import pandas” Enjoy…… but some time pacakage name as much as large as its difficult to write every time so, then use one shortcut method like, “import pandas as pd” above line shows the pd as a shortcut for pandas. it’s fine… Remove a Package Use the uninstall command to remove a package: Example : !pip install pandas Setting up Virtual Environment why? When you are working on different project, every project required different types of packages. So install the all packages in a single environment sometime happen is error in that given pacakages. Because of single environment used for all project. When install the new packages some time automatically update the all python that’s why happening this error. The solution for this problem is to create a virtual environment. This means it may not be possible for one Python installation to meet the requirements of every application. If application A needs version 1.0 of a particular module but application B needs version 2.0, then the requirements are in conflict and installing either version 1.0 or 2.0 will leave one application unable to run. If you have multiple versions of Python on your system, you can select a specific Python version by running “python3”. So i am recommended used the virtual environment for every project or problem statement. Notice that create the virtual environment on same folder location whatever you save the your dataset and file. Following steps how to create the environment. i. create and manage virtual environments is called “venv”. ii. pip install virtualenv iii. Now check your installation, virtualenv version iv. Create a virtual environment now, virtualenv virtualenv_name v. After this command, a folder named virtualenv_name will be created. You can name anything to it. If you want to create a virtualenv for specific python version, type $ virtualenv p /usr/bin/python3 virtualenv_name vi. Activate the virtual environment for Mac OS / Linux source mypython/bin/activate for Windows mypthon\Scripts\activate vii. You can deactivate using deactivate Example : when you can activate venv, your terminal look like (tutorialenv) $ Sample Program It’s a very simple to write a code in python. If you print the ‘hello word!’ see the following code. source code : 1. Print hello word. print(‘Hello word!’) 2. Add two numbers #this program adds two number number_1 = 2 number_2 = 3 #add two numbers add = number_1 + number_2 print(add) output = 5 Notice : “ # ” this special character (hashtag) used for comment. Data Structures : What is mean by Data Structures? Data structures are basically just that they are structures which can hold some data together. It means that, they are used to store a collection of related data. Data structures can be divided into two categories in computer science : 1. Primitive data structures. 2. NonPrimitive data structures. Types of Primitive Data Structures 1. Integers 2. Float 3. Strings 4. Boolean Types of NonPrimitive Data Structures 1. List 2. Tuple 3. Dictionary 4. Set Quick introduction 1. List : A LIST is a data structure that holds an ordered collection of items i.e. you can store a SEQUENCE of items in a list. The list of items should be enclosed in square brackets ( ‘[ ]’ ) so that Python understands that you are specifying a list. Once you have created a list, you can add, remove or search for items in the list. A list is a mutable data type. i.e. editable. figure : List ( how to create a list in python ) 2. Tuple : Tuples are used to hold / combine / collect together multiple objects. Think of them as similar to lists, but without the extensive functionality that the list class gives you. One major feature of tuples is that they are immutable like strings i.e. you cannot modify / edit the tuples. Tuples are defined by specifying items separated by commas (,)within an optional pair of parentheses. Figure : Tuple 3. Dictionary : A dictionary is like an addressbook where you can find the address or contact details of a person by knowing only his/her name i.e. we associate keys (name) with values (details). Note that the key must be unique just like you cannot find out the correct information if you have two persons with the exact same name. Note that you can use only immutable objects (like strings) for the keys of a dictionary but you can use either immutable or mutable objects for the values of the dictionary. Pairs of keys and values are specified in a dictionary by using the notation d = {key1 : value1, key2 : value2 }. Notice :- The key-value pairs are separated by a colon (:) and the pairs are separated themselves by commas (,) and all this is enclosed in a pair of curly bracket ({ }). Figure : Dictionary 4. Set : Sets are a collection of distinct (unique) objects. These are useful to create lists that only hold unique values in the dataset. It is an unordered collection but a mutable. Example : bri = set([‘brazil’, ‘russia’, ‘india’]) Python Variables Creating Variables : 1. Variables are containers for storing data values. 2. Unlike other programming languages, Python has no command for declaring a variable. 3. A variable is created the moment you first assign a value to it. Example : x = 5 y = “FireBlaze AI School” 4. Variables do not need to be declared with any particular type and can even change type after they have been set. 5. String variables can be declared either by using single or double quotes: Example : x = “FireBlaze AI School” # is the same as x = ‘FireBlaze AI School’ Variable Names : A variable can have a short name (like x , y and z) or a more descriptive name (age, sex, total_income, salary). Rules for Python variables: • A variable name must start with a letter or the underscore ( _ )character • A variable name cannot start with a number (integer or float) • A variable name can only contain alphanumeric characters and underscores (Az, 09 and _) • Variable names are casesensitive (age, Age and AGE are three different variables) Assign the Multiple Variable : Python allows you to assign values to multiple variables in one line. Example : x, y, z = “FireBlaze”, “AI”, “School” Conditional Statement : Alternatively referred to as a conditional expression and conditional processing, a conditional statement is a set of rules performed if a certain condition is met. Python supports the usual logical conditions from mathematics: i. Equals: a == b ii. Not Equals: a != b iii. Less than: a b vi. Greater than or equal to: a >= b These conditions can be used in several ways, most commonly in “if statements” and loops. An “if statement” is written by using the if keyword. 1. If : if : 2. if….else : The else keyword catches anything which isn’t caught by the preceding conditions. Syntax : if ( some condition ): print(“ if condition satisfied ”) else : print(“ if condition not satisfied”) 3. Elif : The elif keyword is pythons way of saying “if the previous conditions were not true, then try this or next condition”. Syntax : if ( some condition ): print(“ if condition satisfied ”) elif ( another condition ): print(“if satified”) 4. ifelifelse : Syntax : if( some condition): print(“ if condition satisfied”) elif(another condition): print(“if satisfied”) else: print(“ ”) Loops : A loop is a sequence of instruction’s that is continually repeated until a certain condition is reached. Python has two primitive loop commands: • while loops • for loops 1. while loop : with the while loop we can excute a set of statements when condition is true. Syntax : while expression: statement (i) 2. The Break Statement : With the break statement we can stop the loop even if the while condition is true: Syntax : while expression: statement (i) if (condition): break 3. for loop : A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). With the for loop we can execute a set of statements, once for each item in a list, tuple, set , etc. Syntax : for val in (sequence): Body of for Matrix : DataFrame.as_matrix(self, columns=None) Matrix is a Two dimensional array (2D) where each data element is same size. Numpy: “NumPy” derives from an old library called Numeric, which was the first array object built for Python. Is a library for the Python programming language, adding support for large, multidimensional arrays and matrices, along with a large collection of highlevel mathematical functions to operate on these array. Pandas: What is mean by Pandas ? “Python Data Analysis Library”. What is Pandas? Pandas is an opensource library that allows to you perform data manipulation and data wrangling in Python. Pandas library is built on top of Numpy, meaning Pandas needs Numpy to operate. Pandas is also an elegant solution for time series data. How to Install Pandas? Pandas is installed by default. You can install pandas using In Jupyter Notebook : !pip install pandas Pandas as Three data structures : 1. Series 2. DataFrame 3. Panels 1. Series : Is a 1D labeled array. holding data of any type, ex: (integer, string, float, python objects, etc.). Here, we create a series using “Series” function. For ex, ser1 create a series of [1,2,3,4] using Series function and check output is in column as well as by default indexing values. Suppose any one want change the index value i.e. replace by int or string it is possible in python with help of idex function. Example : ser3 = Series([1,2,3], index=[‘a’,’b’,’c’]) 2. DataFrame : Is a 2D labeled array,with labeled axes (rows and columns). You can convert a numpy array to a pandas data frame with pd.Data frame(). Some of basic methodlogies in DataFrame such as indexing, Reindexing, add column, drop the column or row, filling the missing values, fffill, bfill, and many more opertaion possible in DataFrame in pandas. Data Visualization : Data does not make sense until you can look at in a visual form, such as with plotting technique like barplot, boxplot, histogram, pie chart, etc. Data visualization is an skill in applied statistics and machine learning. Data visualization is a part of descreptive statistics. Overview here are a few popular plotting libraries : • Matplotlib : low level, provides lots of freedom with various plotting technique. 1. Line Plot : A line chart or line graph is a type of chart which displays information as a series of data points called ‘markers’ connected by straight line segments. 2. Bar Plot :- A bar chart or bar graph is a chart or graph that presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent. 3. Histogram Plot :- A histogram shows the frequency on the vertical axis and the horizontal axis is another dimension. 4. Box Plot :- Make a box and whisker plot for each column of x or each vector in sequence x . The box extends from the lower to upper quartile values of the data, with a line at the median. 5. Pie chart : A pie chart is a circular statistical graphic, which is divided into slices to illustrate numerical proportion. In a pie chart, the arc length of each slice, is proportional to the quantity it represents. • Seaborn : high level interface, built on Matplotlib • Joint Plot : Seaborn’s joinplot displays a relationship between 2 variables (bivariate) as well as 1D profiles (univariate) in the margins. Library : all method already define with function and every function has different operation. What is mean by Library ? It like normal library has a store lots of knowledge and share any one of them without any cost. Same as concept used in that function. Once function is ready just save the function one file and another file just pass the input and get output. Another advanced level is this input call in a single line called as “Library”. Few Important Libraries : 1. pandas 2. math 3. numpy 4. matplotlib 5. sklearn : Father of Libraries. All machine learning algorithm libraries are develped. 6. Kears : It is used for deep learning algorithm. 7. Tensorflow : mostly used for image processing and texttotext and texttospeech engine. 8. Seaborn





