You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<small> First Edition: Nov 22 - Dec 22, 2019</small>
12
+
<small>Second Edition: July, 2021</small>
13
13
</sub>
14
-
15
-
</div>
16
14
</div>
17
15
18
16
[<< Day 19](../19_Day_File_handling/19_file_handling.md) | [Day 21 >>](../21_Day_Classes_and_objects/21_classes_and_objects.md)
@@ -39,36 +37,36 @@
39
37
40
38
### What is PIP ?
41
39
42
-
PIP stands for Preferred installer program. We use _pip_ to install different python packages.
43
-
Package is a python module that can contain one or more modules or other packages. A module or modules that we can install to our application is a package.
40
+
PIP stands for Preferred installer program. We use _pip_ to install different Python packages.
41
+
Package is a Python module that can contain one or more modules or other packages. A module or modules that we can install to our application is a package.
44
42
In programming, we do not have to write every utility program, instead we install packages and import them to our applications.
45
43
46
44
### Installing PIP
47
45
48
-
If you did not install pip, let us do it now. Go to your terminal or command prompt and copy and paste this:
46
+
If you did not install pip, let us install it now. Go to your terminal or command prompt and copy and paste this:
As you can see, I am using pip version 19.3.1, if you see some number a bit below or above that, means you have pip installed.
63
+
As you can see, I am using pip version 21.1.3, if you see some number a bit below or above that, means you have pip installed.
66
64
67
-
Let's check some of the packages used in the python community for different purposes. Just to let you know that there are lots of packages available for use with different applications.
65
+
Let us check some of the packages used in the Python community for different purposes. Just to let you know that there are lots of packages available for use with different applications.
68
66
69
67
### Installing packages using pip
70
68
71
-
Let's try to install _numpy_, called numeric python. It is one of the most popular packages in machine learning and data science community.
69
+
Let us try to install _numpy_, called numeric python. It is one of the most popular packages in machine learning and data science community.
72
70
73
71
- NumPy is the fundamental package for scientific computing with Python. It contains among other things:
74
72
- a powerful N-dimensional array object
@@ -80,16 +78,16 @@ Let's try to install _numpy_, called numeric python. It is one of the most popul
80
78
asabeneh@Asabeneh:~$ pip install numpy
81
79
```
82
80
83
-
Lets start using numpy. Open your python interactive shell, write python and then import numpy as follows:
81
+
Let us start using numpy. Open your python interactive shell, write python and then import numpy as follows:
84
82
85
83
```py
86
84
asabeneh@Asabeneh:~$ python
87
-
Python 3.7.5 (default, Nov 12019, 02:16:32)
85
+
Python 3.9.6 (default, Jun 282021, 15:26:21)
88
86
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
89
87
Type "help", "copyright", "credits"or"license"for more information.
90
88
>>>import numpy
91
89
>>> numpy.version.version
92
-
'1.17.3'
90
+
'1.20.1'
93
91
>>> lst = [1, 2, 3,4, 5]
94
92
>>> np_arr = numpy.array(lst)
95
93
>>> np_arr
@@ -103,23 +101,23 @@ array([3, 4, 5, 6, 7])
103
101
>>>
104
102
```
105
103
106
-
Pandas is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language. Let's install the big brother of numpy, _pandas_:
104
+
Pandas is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language. Let us install the big brother of numpy, _pandas_:
107
105
108
106
```sh
109
107
asabeneh@Asabeneh:~$ pip install pandas
110
108
```
111
109
112
110
```py
113
111
asabeneh@Asabeneh:~$ python
114
-
Python 3.7.5 (default, Nov 12019, 02:16:32)
112
+
Python 3.9.6 (default, Jun 282021, 15:26:21)
115
113
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
116
114
Type "help", "copyright", "credits"or"license"for more information.
117
115
>>>import pandas
118
116
```
119
117
120
118
This section isnot about numpy nor pandas, here we are trying to learn how to install packages and how to import them. If it is needed, we will talk about different packages in other sections.
121
119
122
-
Let's import a web browser module, which can help us to open any website. We do not install this module, it is already installed by default with python 3. For instance if you like to open any number of websites at any time or if you like to schedule something, this _webbrowser_ module can be of use.
120
+
Let us import a web browser module, which can help us to openany website. We do notneed to install this module, it is already installed by default withPython3. For instance if you like to openany number of websites at any time orif you like to schedule something, this _webbrowser_ module can be used.
123
121
124
122
```py
125
123
import webbrowser # web browser module to open websites
@@ -128,7 +126,7 @@ import webbrowser # web browser module to open websites
128
126
url_lists = [
129
127
'http://www.python.org',
130
128
'https://www.linkedin.com/in/asabeneh/',
131
-
'https://twitter.com/Asabeneh',
129
+
'https://github.com/Asabeneh',
132
130
'https://twitter.com/Asabeneh',
133
131
]
134
132
@@ -139,7 +137,7 @@ for url in url_lists:
139
137
140
138
### Uninstalling Packages
141
139
142
-
If you do not like to keep the installed packages, you can remove them.
140
+
If you do not like to keep the installed packages, you can remove them using the following command.
143
141
144
142
```sh
145
143
pip uninstall packagename
@@ -164,7 +162,7 @@ pip show packagename
164
162
```sh
165
163
asabeneh@Asabeneh:~$ pip show pandas
166
164
Name: pandas
167
-
Version: 0.25.3
165
+
Version: 1.2.3
168
166
Summary: Powerful data structures for data analysis, time series, and statistics
169
167
Home-page: http://pandas.pydata.org
170
168
Author: None
@@ -180,7 +178,7 @@ If we want even more details, just add --verbose
180
178
```sh
181
179
asabeneh@Asabeneh:~$ pip show --verbose pandas
182
180
Name: pandas
183
-
Version: 0.25.3
181
+
Version: 1.2.3
184
182
Summary: Powerful data structures for data analysis, time series, and statistics
185
183
Home-page: http://pandas.pydata.org
186
184
Author: None
@@ -211,7 +209,7 @@ Entry-points:
211
209
212
210
### PIP Freeze
213
211
214
-
Generate output suitable fora requirements file.
212
+
Generate installed Python packages with their version and the output issuitable to use it ina requirements file. A requirements.txt fileis a file that should contain all the installed Python packages in a Python project.
215
213
216
214
```sh
217
215
asabeneh@Asabeneh:~$ pip freeze
@@ -227,21 +225,21 @@ The pip freeze gave us the packages used, installed and their version. We use it
227
225
### Reading from URL
228
226
229
227
By now you are familiar with how to read or write on a file located on you local machine. Sometimes, we would like to read from a website using url orfrom an API.
230
-
API stands for Application Program Interface. It is a means to exchange structured data between servers primarily as json data. To open a network connection, we need a package called _requests_ - it allows to open a network connection and to implement CRUD(create, read, update and delete) operations. In this section, we will cover only reading part of a CRUD.
228
+
API stands for Application Program Interface. It is a means to exchange structured data between servers primarily as json data. To open a network connection, we need a package called _requests_ - it allows to open a network connection and to implement CRUD(create, read, update and delete) operations. In this section, we will cover only reading ore getting part of a CRUD.
231
229
232
-
Let's install _requests_:
230
+
Let us install _requests_:
233
231
234
232
```py
235
233
asabeneh@Asabeneh:~$ pip install requests
236
234
```
237
235
238
236
We will see _get_, _status_code_, _headers_, _text_ and _json_ methods in _requests_ module:
239
237
- _get()_: to open a network and fetch data from url - it returns a response object
240
-
- _status_code_: After we fetched data, we can check the status of the operation (succes, error, etc)
238
+
- _status_code_: After we fetched data, we can check the status of the operation (success, error, etc)
241
239
- _headers_: To check the header types
242
240
- _text_: to extract the text from the fetched response object
243
241
- _json_: to extract json data
244
-
Let's read a txt file form this website, https://www.w3.org/TR/PNG/iso_8859-1.txt.
242
+
Let's read a txt file from this website, https://www.w3.org/TR/PNG/iso_8859-1.txt.
245
243
246
244
```py
247
245
import requests # importing the request module
@@ -261,7 +259,7 @@ print(response.text) # gives all the text from the page
- Let's read from an api. API stands for Application Program Interface. It is a means to exchange structure data between servers primarily a json data. An example of an api:https://restcountries.eu/rest/v2/all. Let's read this API using _requests_ module.
262
+
- Let us read from an API. API stands for Application Program Interface. It is a means to exchange structure data between servers primarily a json data. An example of an API:https://restcountries.eu/rest/v2/all. Let us read this API using _requests_ module.
265
263
266
264
```py
267
265
import requests
@@ -329,10 +327,10 @@ We use _json()_ method from response object, if the we are fetching JSON data. F
329
327
330
328
### Creating a Package
331
329
332
-
We organize a large number of files in different folders andsubfolders based on some criteria, so that we can find and manage them easily. As you know, a module can contain multiple objects, such as classes, functions, etc. A package can contain one or more relevant modules. A package is actually a folder containing one or more module files. Let's create a package named mypackage, using the following steps:
330
+
We organize a large number of files in different folders andsub-folders based on some criteria, so that we can find and manage them easily. As you know, a module can contain multiple objects, such as classes, functions, etc. A package can contain one or more relevant modules. A package is actually a folder containing one or more module files. Let us create a package named mypackage, using the following steps:
333
331
334
332
Create a new folder named mypacakge inside 30DaysOfPython folder
335
-
Create an empty **init**.py filein the mypackage folder.
333
+
Create an empty **__init__**.py filein the mypackage folder.
336
334
Create modules arithmetic.py and greet.py with following code:
337
335
338
336
```py
@@ -385,11 +383,11 @@ Now let's open the python interactive shell and try the package we have created:
Type "help", "copyright", "credits"or"license"for more information.
391
389
>>>from mypackage import arithmetics
392
-
>>> arithmetics.add_numbers(1,2,3,5)
390
+
>>> arithmetics.add_numbers(1,2, 3, 5)
393
391
11
394
392
>>> arithmetics.subtract(5, 3)
395
393
2
@@ -407,8 +405,8 @@ Type "help", "copyright", "credits" or "license" for more information.
407
405
>>>
408
406
```
409
407
410
-
As you can see our package works perfectly. The package folder contains a special file called **init**.py - it stores the package's content. If we put **init**.py in the package folder, python start recognizes it as a package.
411
-
The **init**.py exposes specified resources from its modules to be imported to other python files. An empty **init**.py file makes all functions available when a package is imported. The **init**.py is essential for the folder to be recognized by Python as a package.
408
+
As you can see our package works perfectly. The package folder contains a special file called **__init__**.py - it stores the package's content. If we put **__init__**.py in the package folder, python start recognizes it as a package.
409
+
The **__init__**.py exposes specified resources from its modules to be imported to other python files. An empty **__init__**.py file makes all functions available when a package is imported. The **__init__**.py is essential for the folder to be recognized by Python as a package.
412
410
413
411
### Further Information About Packages
414
412
@@ -418,10 +416,9 @@ The **init**.py exposes specified resources from its modules to be imported to o
418
416
- Web Development
419
417
- Django - High-level web framework.
420
418
- _pip install django_
421
-
- Flask -microframeworkfor Python based on Werkzeug, Jinja 2. (It's BSD licensed)
419
+
- Flask -micro frameworkfor Python based on Werkzeug, Jinja 2. (It's BSD licensed)
422
420
- _pip install flask_
423
421
-HTML Parser
424
-
425
422
- [Beautiful Soup](https://www.crummy.com/software/BeautifulSoup/bs4/doc/) -HTML/XML parser designed for quick turnaround projects like screen-scraping, will accept bad markup.
426
423
- _pip install beautifulsoup4_
427
424
- PyQuery - implements jQuery in Python; faster than BeautifulSoup, apparently.
@@ -433,7 +430,7 @@ The **init**.py exposes specified resources from its modules to be imported to o
433
430
- TkInter - The traditional Python user interface toolkit.
434
431
- Data Analysis, Data Science and Machine learning
435
432
- Numpy: Numpy(numeric python) is known as one of the most popular machine learning library in Python.
436
-
- Pandas: is a machine learning library in Python that provides data structures of high-level and a wide variety of tools for analysis.
433
+
- Pandas: is a data analysis, data science and a machine learning library in Python that provides data structures of high-level and a wide variety of tools for analysis.
437
434
- SciPy: SciPy is a machine learning library for application developers and engineers. SciPy library contains modules for optimization, linear algebra, integration, image processing, and statistics.
438
435
- Scikit-Learn: It is NumPy and SciPy. It is considered as one of the best libraries for working withcomplex data.
439
436
- TensorFlow: is a machine learning library built by Google.
@@ -442,15 +439,17 @@ The **init**.py exposes specified resources from its modules to be imported to o
442
439
- requests: is a package which we can use to send requests to a server(GET, POST, DELETE, PUT)
443
440
- _pip install requests_
444
441
445
-
446
-
🌕 You are always progressing and you are a head of 20 steps to your way to greatness.
442
+
🌕 You are always progressing and you are a head of 20 steps to your way to greatness. Now do some exercises for your brain and muscles.
447
443
448
444
## Exercises: Day 20
449
445
450
446
1. Read this url and find the 10 most frequent words. Romeo_and_juliet = 'http://www.gutenberg.org/files/1112/1112.txt'
451
-
2. Read the cats api and cats_api = 'https://api.thecatapi.com/v1/breeds'and find the avarage weight of a cat in metric units.
452
-
3. Read the countries api and find the 10 largest countries
453
-
4. UCIis one the most common places to get data sets for data science and machine learning. Read the content of UCL (http://mlr.cs.umass.edu/ml/datasets.html). Without additional libraries it will be difficult, so you may try it with BeautifulSoup4
447
+
2. Read the cats APIand cats_api = 'https://api.thecatapi.com/v1/breeds'and find
448
+
1. the min, max, mean, median, standard deviation of cats' weight in metric units.
449
+
2. the min, max, mean, median, standard deviation of cats' lifespan in years.
450
+
3. Create a frequency table of country and breed of cats
451
+
3. Read the countries APIand find the 10 largest countries
452
+
4. UCIis one of the most common places to get data sets for data science and machine learning. Read the content of UCL (https://archive.ics.uci.edu/ml/datasets.php). Without additional libraries it will be difficult, so you may try it with BeautifulSoup4
0 commit comments