diff --git a/.gitignore b/.gitignore index 5a21347f..605a96c9 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,6 @@ res.py hello.py backup.md .DS_Store -__pycache__ \ No newline at end of file +__pycache__ +playground/ +playground \ No newline at end of file diff --git a/mypackage/__init__.py b/mypackage/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/mypackage/arithmetics.py b/mypackage/arithmetics.py new file mode 100644 index 00000000..2bca4930 --- /dev/null +++ b/mypackage/arithmetics.py @@ -0,0 +1,28 @@ +def add_numbers(*args): + total = 0 + for num in args: + total += num + return total + + +def subtract(a, b): + return (a - b) + + +def multiple(a, b): + return a * b + + +def division(a, b): + return a / b + + +def remainder(a, b): + return a % b + + +def power(a, b): + return a ** b + + + diff --git a/mypackage/greet.py b/mypackage/greet.py new file mode 100644 index 00000000..21636916 --- /dev/null +++ b/mypackage/greet.py @@ -0,0 +1,2 @@ +def greet_person(firstname, lastname): + return f'{firstname} {lastname}, welcome to 30DaysOfPython Challenge!' diff --git a/readme19-21.md b/readme19-21.md index 7581b7e1..1428ea47 100644 --- a/readme19-21.md +++ b/readme19-21.md @@ -6,7 +6,7 @@ 🧳 [Part 4: Day 10 - 12](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme10-12.md) 🧳 [Part 5: Day 13 - 15](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme13-15.md) 🧳 [Part 6: Day 16 - 18](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme16-18.md) -🧳 [Part 7: Day 19 - 21](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme19-21.md) +🧳 [Part 7: Day 19 - 21](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme19-21.md) 🧳 [Part 8: Day 22 - 24](#) πŸ”’ 🧳 [Part 9: Day 25 - 27](#) πŸ”’ 🧳 [Part 10: Day 28 - 30](#) πŸ”’ @@ -36,6 +36,7 @@ - [Show package](#show-package) - [PIP freeze](#pip-freeze) - [Reading from URL](#reading-from-url) + - [Creating a package](#creating-a-package) - [Further information about packages](#further-information-about-packages) - [Exercises: Day 20](#exercises-day-20) - [πŸ“˜ Day 21](#%f0%9f%93%98-day-21) @@ -49,29 +50,40 @@ - [Inheritance](#inheritance) - [Overriding parent method](#overriding-parent-method) - [πŸ’» Exercises: Day 20](#%f0%9f%92%bb-exercises-day-20) + # πŸ“˜ Day 19 + ## File handling -So far we have seen different python data types. We usually store our data in a different file format. In addition to handling file, we will also see different file formats(.txt, .json, .xml, .csv, .tsv, .excel) file formats in this section. First, let's get familiar with handling file with common file format(.txt). -File handling is an import part of programming which allows us to create, read, update and delete files. In python to handle data we use *open()* builtin function. +So far we have seen different python data types. We usually store our data in a different file format. In addition to handling file, we will also see different file formats(.txt, .json, .xml, .csv, .tsv, .excel) file formats in this section. First, let's get familiar with handling file with common file format(.txt). + +File handling is an import part of programming which allows us to create, read, update and delete files. In python to handle data we use _open()_ builtin function. + ```py # Syntax open('filename', mode) # mode(r, a, w, x, t,b, could be to read, write, update ``` -* "r" - Read - Default value. Opens a file for reading, error if the file does not exist -* "a" - Append - Opens a file for appending, creates the file if it does not exist -* "w" - Write - Opens a file for writing, creates the file if it does not exist -* "x" - Create - Creates the specified file, returns an error if the file exists -* "t" - Text - Default value. Text mode -* "b" - Binary - Binary mode (e.g. images) + +- "r" - Read - Default value. Opens a file for reading, error if the file does not exist +- "a" - Append - Opens a file for appending, creates the file if it does not exist +- "w" - Write - Opens a file for writing, creates the file if it does not exist +- "x" - Create - Creates the specified file, returns an error if the file exists +- "t" - Text - Default value. Text mode +- "b" - Binary - Binary mode (e.g. images) + ### Opening File for reading -The default mode of *open* is reading, so we do not have to specify 'r' or 'rt'. I have created and saved a file named reading_file_example.txt in the files directory. Let see read this file. + +The default mode of _open_ is reading, so we do not have to specify 'r' or 'rt'. I have created and saved a file named reading_file_example.txt in the files directory. Let see read this file. + ```py f = open('./files/reading_file_example.txt') print(f) # <_io.TextIOWrapper name='./files/reading_file_example.txt' mode='r' encoding='UTF-8'> ``` -As you can see in the above example, I printed the opened file and it gave me some information about the opened file. Opened file has different reading methods: *read()*, *readline*, *readlines*. An opened file has to be closed with *close()* method. -* *read()*: read the whole text as string. If we want to limit the number of characters we read, we can limit it by passing int value to the methods. + +As you can see in the above example, I printed the opened file and it gave me some information about the opened file. Opened file has different reading methods: _read()_, _readline_, _readlines_. An opened file has to be closed with _close()_ method. + +- _read()_: read the whole text as string. If we want to limit the number of characters we read, we can limit it by passing int value to the methods. + ```py f = open('./files/reading_file_example.txt') txt = f.read() @@ -79,13 +91,16 @@ print(type(txt)) print(txt) f.close() ``` + ```sh # output This is an example to show how to open a file and read. This is the second line of the text. ``` + Instead of printing all the text, let see by printing the first 10 characters of the text in the file. + ```py f = open('./files/reading_file_example.txt') txt = f.read(10) @@ -93,12 +108,15 @@ print(type(txt)) print(txt) f.close() ``` + ```sh # output This is an ``` -* *readline()*: read only the first line + +- _readline()_: read only the first line + ```py f = open('./files/reading_file_example.txt') line = f.readline() @@ -106,12 +124,15 @@ print(type(line)) print(line) f.close() ``` + ```sh # output This is an example to show how to open a file and read. ``` -* *readlines()*: read all the text line by line and returns a list of lines + +- _readlines()_: read all the text line by line and returns a list of lines + ```py f = open('./files/reading_file_example.txt') lines = f.readlines() @@ -119,12 +140,15 @@ print(type(lines)) print(lines) f.close() ``` + ```sh # output ['This is an example to show how to open a file and read.\n', 'This is the second line of the text.'] ``` -Another way to get all the lines a list is using *splitlines()*: + +Another way to get all the lines a list is using _splitlines()_: + ```py f = open('./files/reading_file_example.txt') lines = f.read().splitlines() @@ -132,47 +156,61 @@ print(type(lines)) print(lines) f.close() ``` + ```sh # output ['This is an example to show how to open a file and read.', 'This is the second line of the text.'] ``` -After we open a file, we should close the file but there is a high tendency to forget to close. There is a new way of opening method, *with* which closes by itself. Let's write the above code with *with*. + +After we open a file, we should close the file but there is a high tendency to forget to close. There is a new way of opening method, _with_ which closes by itself. Let's write the above code with _with_. + ```py with open('./files/reading_file_example.txt') as f: lines = f.read().splitlines() print(type(lines)) print(lines) ``` + ```sh # output ['This is an example to show how to open a file and read.', 'This is the second line of the text.'] ``` + ### Opening file for writing or updating -To write to an existing file, we must add a mode as parameter to the *open()* function: -* "a" - append - will append to the end of the file if the file does not exist it raise FileNotFoundError. -* "w" - write - will overwrite any existing content if the file does not exist it creates. + +To write to an existing file, we must add a mode as parameter to the _open()_ function: + +- "a" - append - will append to the end of the file if the file does not exist it raise FileNotFoundError. +- "w" - write - will overwrite any existing content if the file does not exist it creates. Let's append some text to the file, we have been reading + ```py with open('./files/reading_file_example.txt','a') as f: f.write('This text has to be appended at the end') ``` + This method below, create a new file if the file does not exist. + ```py with open('./files/writing_file_example.txt','w') as f: f.write('This text will be written in a newly created file') ``` -### Deleting file -We have seen in previous section, how to make and remove a directory using *os* module. Again now, if we want to remove a file we use *os* module. + +### Deleting file + +We have seen in previous section, how to make and remove a directory using _os_ module. Again now, if we want to remove a file we use _os_ module. ```py import os os.remove('./files/example.txt') ``` + If the file does not exist, the remove method will raise an error it is good to use condition. + ```py import os if os.path.exist('./files/example.txt): @@ -180,12 +218,18 @@ if os.path.exist('./files/example.txt): else: os.remove('The file does not exist') ``` + ## File Types + ### File with txt Extension -File with *txt* extension is a very common form data and we have covered it in the previous section. Let's move to the JSON file + +File with _txt_ extension is a very common form data and we have covered it in the previous section. Let's move to the JSON file + ### File with json Extension + JSON stands for JavaScript Object Notation. Actually, it a stringified JavaScript object. -*Example:* +_Example:_ + ```py # dictionary person_dct= { @@ -205,8 +249,10 @@ person_json = '''{ "skills":["JavaScrip", "React","Python"] }''' ``` + ### Changing JSON to dictionary -To change a JSON to a dictionary we use *loads* method. + +To change a JSON to a dictionary we use _loads_ method. ```py import json @@ -222,6 +268,7 @@ person_dct = json.loads(person_json) print(person_dct) print(person_dct['name']) ``` + ```sh # output {'name': 'Asabeneh', 'country': 'Finland', 'city': 'Helsinki', 'skills': ['JavaScrip', 'React', 'Python']} @@ -229,7 +276,9 @@ Asabeneh ``` ### Changing dictionary to JSON -To change a dictionary to a JSON we use *dumps* method. + +To change a dictionary to a JSON we use _dumps_ method. + ```py import json # python dictionary @@ -244,6 +293,7 @@ person_json = json.dumps(person, indent=4) # indent could be 2, 4, 8. It beautif print(type(person_json)) print(person_json) ``` + ```sh # output # when you it is printed it does not have the quote but actually it is a string @@ -260,8 +310,11 @@ print(person_json) ] } ``` + ### Saving as JSON file + We can also save our data as a json file. Let's save it as a json file using the following steps. + ```py import json # python dictionary @@ -274,9 +327,11 @@ person = { with open('./files/json_example.json', 'w', encoding='utf-8') as f: json.dump(person, f, ensure_ascii=False, indent=4) ``` + In the above code, we use encoding and indentation. Indentation makes the json file easy to read. ### File with csv Extension + CSV stands for comma separated values. CSV is a simple file format used to store tabular data, such as a spreadsheet or database. CSV is a very common data format in data science. **Example:** @@ -285,7 +340,9 @@ CSV stands for comma separated values. CSV is a simple file format used to store "name","country","city","skills" "Asabeneh","Finland","Helsinki","JavaScrip" ``` + **Example:** + ```py import csv with open('./files/csv_example.csv') as f: @@ -301,17 +358,23 @@ with open('./files/csv_example.csv') as f: line_count += 1 print(f'Number of lines: {line_count}') ``` + ```sh # output: Column names are :name, country, city, skills Asabeneh is a teachers. He lives in Finland, Helsinki. Number of lines: 2 ``` + ### File with xlsx Extension -To read excel we need to install *xlrd* package. We will cover this after we cover package installing using pip. + +To read excel we need to install _xlrd_ package. We will cover this after we cover package installing using pip. + ### File with xml Extension + XML is another structured data format which looks like HTML. In XML the tags are not predefined. The first line is an XML declaration. The person tag is the root of the XML. The person has a gender attribute. **Example:XML** + ```xml @@ -325,7 +388,9 @@ XML is another structured data format which looks like HTML. In XML the tags are ``` + How to read an XML file, for more information check the [documentation](https://docs.python.org/2/library/xml.etree.elementtree.html) + ```py import xml.etree.ElementTree as ET tree = ET.parse('./files/xml_example.xml') @@ -335,6 +400,7 @@ print('Attribute:', root.attrib) for child in root: print('field: ', child.tag) ``` + ```sh # output Root tag: person @@ -346,61 +412,67 @@ field: skills ``` ## πŸ’» Exercises: Day 19 + 1. Writ a function which count number of lines and number of words from a text. All the files are in data the folder: 1. Read obama_speech.txt file and count number of lines and now of words 2. Read michelle_obama_speech.txt file and count number of lines and now of words 3. Read donald_speech.txt file and count number of lines and now of words 4. Read melina_trump_speech.txt file and count number of lines and now of words 2. Read the countries_data.json data file in data directory, create a function which find the ten most spoken languages - ```py - # Your output should look like this - print(most_spoken_languages(filename='./data/countries_data.json', 10)) - [(91, 'English'), - (45, 'French'), - (25, 'Arabic'), - (24, 'Spanish'), - (9, 'Russian'), - (9, 'Portuguese'), - (8, 'Dutch'), - (7, 'German'), - (5, 'Chinese'), - (4, 'Swahili'), - (4, 'Serbian')] - # Your output should look like this - print(most_spoken_languages(filename='./data/countries_data.json', 3)) - [(91, 'English'), - (45, 'French'), - (25, 'Arabic')] - ``` + ```py + # Your output should look like this + print(most_spoken_languages(filename='./data/countries_data.json', 10)) + [(91, 'English'), + (45, 'French'), + (25, 'Arabic'), + (24, 'Spanish'), + (9, 'Russian'), + (9, 'Portuguese'), + (8, 'Dutch'), + (7, 'German'), + (5, 'Chinese'), + (4, 'Swahili'), + (4, 'Serbian')] + + # Your output should look like this + print(most_spoken_languages(filename='./data/countries_data.json', 3)) + [(91, 'English'), + (45, 'French'), + (25, 'Arabic')] + ``` + 3. Read the countries_data.json data file in data directory,create a function which create the ten most populated countries - ```py - # Your output should look like this - print(most_populated_countries(filename='./data/countries_data.json', 10)) - - [{'country': 'China', 'population': 1377422166}, - {'country': 'India', 'population': 1295210000}, - {'country': 'United States of America', 'population': 323947000}, - {'country': 'Indonesia', 'population': 258705000}, - {'country': 'Brazil', 'population': 206135893}, - {'country': 'Pakistan', 'population': 194125062}, - {'country': 'Nigeria', 'population': 186988000}, - {'country': 'Bangladesh', 'population': 161006790}, - {'country': 'Russian Federation', 'population': 146599183}, - {'country': 'Japan', 'population': 126960000}] - # Your output should look like this + ```py + # Your output should look like this + print(most_populated_countries(filename='./data/countries_data.json', 10)) - print(most_populated_countries(filename='./data/countries_data.json', 3)) - [{'country': 'China', 'population': 1377422166}, - {'country': 'India', 'population': 1295210000}, - {'country': 'United States of America', 'population': 323947000}] - ``` -4. Extract all incoming emails from the email_exchange_big.txt file. + [{'country': 'China', 'population': 1377422166}, + {'country': 'India', 'population': 1295210000}, + {'country': 'United States of America', 'population': 323947000}, + {'country': 'Indonesia', 'population': 258705000}, + {'country': 'Brazil', 'population': 206135893}, + {'country': 'Pakistan', 'population': 194125062}, + {'country': 'Nigeria', 'population': 186988000}, + {'country': 'Bangladesh', 'population': 161006790}, + {'country': 'Russian Federation', 'population': 146599183}, + {'country': 'Japan', 'population': 126960000}] + + # Your output should look like this + + print(most_populated_countries(filename='./data/countries_data.json', 3)) + [{'country': 'China', 'population': 1377422166}, + {'country': 'India', 'population': 1295210000}, + {'country': 'United States of America', 'population': 323947000}] + ``` + +4. Extract all incoming emails from the email_exchange_big.txt file. 5. Find the most common words in the English language. Call the name of your function find_most_common_words, it will take two parameters which are a string or a file and a positive integer. Your function will return an array of tuples in descending order. Check the output + ```py # Your output should look like this - + print(find_most_common_words('sample.txt', 10)) [(10, 'the'), (8, 'be'), @@ -422,6 +494,7 @@ field: skills (6, 'of'), (5, 'and')] ``` + 6. Use the function, find_most_frequent_words to find out: 1. The ten most frequent words used in [Obama's speech](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/data/obama_speech.txt) 2. The ten most frequent words used in [Michelle's speech](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/data/michelle_obama_speech.txt) @@ -435,42 +508,57 @@ field: skills 3. Count the number lines containing Java not JavaScript # πŸ“˜ Day 20 + ## Python PIP - Python Package Manager + ### What is PIP ? -PIP stands for Preferred installer program. We use *pip* to install different python packages. + +PIP stands for Preferred installer program. We use _pip_ to install different python packages. Package is a python module which can contain one or more modules or other packages. A module or modules which we can install to our application is a package. -In programming, we do not have to write every utility programs instead we install packages and import the package to our applications. +In programming, we do not have to write every utility programs instead we install packages and import the package to our applications. + ### Installing pip + If you did not install pip, lets install pip. Go to your terminal or command prompt and copy and past this: + ```sh asabeneh@Asabeneh:~$ pip install pip ``` + Check if it is installed by writing ```sh pip --version ``` + ```py asabeneh@Asabeneh:~$ pip --version pip 19.3.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7) ``` + As you can see, I am using pip version 19.3.1, if you see some number a bit below or above that mean you have pip installed. Let's some of the package used in the python community for different purposes. Just to let you know that there are lots of package which are available for use with different applications. + ### Installing packages using pip -Let's try to install *numpy*, which is called a numeric python. It is one of the most popular package in machine learning and data science community. -* NumPy is the fundamental package for scientific computing with Python. It contains among other things: - * a powerful N-dimensional array object - * sophisticated (broadcasting) functions - * tools for integrating C/C++ and Fortran code - * useful linear algebra, Fourier transform, and random number capabilities + +Let's try to install _numpy_, which is called a numeric python. It is one of the most popular package in machine learning and data science community. + +- NumPy is the fundamental package for scientific computing with Python. It contains among other things: + - a powerful N-dimensional array object + - sophisticated (broadcasting) functions + - tools for integrating C/C++ and Fortran code + - useful linear algebra, Fourier transform, and random number capabilities + ```sh asabeneh@Asabeneh:~$ pip install numpy ``` + Lets start using numpy. Open your python interactive shell, write python and then import numpy as follows: + ```py asabeneh@Asabeneh:~$ python -Python 3.7.5 (default, Nov 1 2019, 02:16:32) +Python 3.7.5 (default, Nov 1 2019, 02:16:32) [Clang 11.0.0 (clang-1100.0.33.8)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import numpy @@ -486,35 +574,49 @@ array([1, 2, 3, 4, 5]) array([ 2, 4, 6, 8, 10]) >>> np_arr + 2 array([3, 4, 5, 6, 7]) ->>> +>>> ``` -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. Lets install big brother of numpy *pandas* as we did for *numpy*. + +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. Lets install big brother of numpy _pandas_ as we did for _numpy_. + ```sh asabeneh@Asabeneh:~$ pip install pandas ``` + ```py asabeneh@Asabeneh:~$ python -Python 3.7.5 (default, Nov 1 2019, 02:16:32) +Python 3.7.5 (default, Nov 1 2019, 02:16:32) [Clang 11.0.0 (clang-1100.0.33.8)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import pandas ``` + This section is not 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. + ### Uninstall packages + If you do not like to keep the installed packages, you can remove them. + ```sh pip uninstall packagename ``` + ### List of packages + To see the installed packages on our machine. We can use pip followed by lis. + ```sh pip list ``` + ### Show package + To show information about a package + ```sh pip show packagename ``` + ```sh asabeneh@Asabeneh:~$ pip show pandas Name: pandas @@ -526,9 +628,11 @@ Author-email: None License: BSD Location: /usr/local/lib/python3.7/site-packages Requires: python-dateutil, pytz, numpy -Required-by: +Required-by: ``` + If we want even more detail than the above, just add --verbose + ```sh asabeneh@Asabeneh:~$ pip show --verbose pandas Name: pandas @@ -540,7 +644,7 @@ Author-email: None License: BSD Location: /usr/local/lib/python3.7/site-packages Requires: numpy, pytz, python-dateutil -Required-by: +Required-by: Metadata-Version: 2.1 Installer: pip Classifiers: @@ -560,8 +664,11 @@ Entry-points: [pandas_plotting_backends] matplotlib = pandas:plotting._matplotlib ``` + ### PIP freeze + Generate output suitable for a requirements file. + ```sh asabeneh@Asabeneh:~$ pip freeze docutils==0.11 @@ -570,22 +677,27 @@ MarkupSafe==0.19 Pygments==1.6 Sphinx==1.2.2 ``` + The pip freeze gave us the packages use installed and their version. We use with requirements.txt file for deployment. -### Reading from URL -By now you are familiar with how to read or write on a file which is located in you local machine. Sometimes, we may like to read from a website using url or from an API. -API stands for Application Program Interface. It is a means to exchange structure data between servers primarily a json data. To open network, we need a package called *requests* which allows to open network and to implement CRUD(create, read, update and delete) operation. In this section, we will cover only reading part of a CRUD. -Let's install *requests* +### Reading from URL + +By now you are familiar with how to read or write on a file which is located in you local machine. Sometimes, we may like to read from a website using url or from an API. +API stands for Application Program Interface. It is a means to exchange structure data between servers primarily a json data. To open network, we need a package called _requests_ which allows to open network and to implement CRUD(create, read, update and delete) operation. In this section, we will cover only reading part of a CRUD. + +Let's install _requests_ + ```py asabeneh@Asabeneh:~$ pip install requests ``` -We will see *get*, *status_code*, *headers*, *text* and *json* methods from *requests* module - * get(): to open a network and fetch data from url and it returns a response object - * status_code: After we fetched, we check the status(succes, error, etc) - * headers: To check the header types - * text: to extract the text from the fetched response object - * json: to extract json data + +We will see _get_, _status_code_, _headers_, _text_ and _json_ methods from _requests_ module +_ get(): to open a network and fetch data from url and it returns a response object +_ status_code: After we fetched, we check the status(succes, error, etc) +_ headers: To check the header types +_ text: to extract the text from the fetched response object \* json: to extract json data Let's read a txt file form this website, https://www.w3.org/TR/PNG/iso_8859-1.txt. + ```py import requests # importing the request module @@ -597,12 +709,15 @@ print(response.status_code) # status code, success:200 print(response.headers) # headers information print(response.text) # gives all the text from the page ``` + ```sh 200 {'date': 'Sun, 08 Dec 2019 18:00:31 GMT', 'last-modified': 'Fri, 07 Nov 2003 05:51:11 GMT', 'etag': '"17e9-3cb82080711c0;50c0b26855880-gzip"', 'accept-ranges': 'bytes', 'cache-control': 'max-age=31536000', 'expires': 'Mon, 07 Dec 2020 18:00:31 GMT', 'vary': 'Accept-Encoding', 'content-encoding': 'gzip', 'access-control-allow-origin': '*', 'content-length': '1616', 'content-type': 'text/plain', 'strict-transport-security': 'max-age=15552000; includeSubdomains; preload', 'content-security-policy': 'upgrade-insecure-requests'} ``` -* Lets 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 api:https://restcountries.eu/rest/v2/all. Let's read this API using *requests* module. + +- Lets 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 api:https://restcountries.eu/rest/v2/all. Let's read this API using _requests_ module. + ```py import requests url = 'https://restcountries.eu/rest/v2/all' # countries api @@ -612,6 +727,7 @@ print(response.status_code) # status code, success:200 countries = response.json() print(countries[:1]) # we sliced only the first country, remove the slicing to see all countries ``` + ```sh 200 @@ -663,55 +779,140 @@ print(countries[:1]) # we sliced only the first country, remove the slicing to 'nl': 'Afghanistan', 'pt': 'AfeganistΓ£o'}}] ``` -We use *json()* method from response object, if the we are fetching JSON data. For txt, html, xml and other file formats we can use *text*. + +We use _json()_ method from response object, if the we are fetching JSON data. For txt, html, xml and other file formats we can use _text_. + +### Creating a package + +We organize a large number of files in different folders and subfolders 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: + +Create a new folder named mypacakge inside 30DaysOfPython folder +Create an empty **init**.py file in the mypackage folder. +Create modules arithmetic.py and greet.py with following code: + +```py +# mypackage/arithmetics.py +# arithmetics.py +def add_numbers(*args): + total = 0 + for num in args: + total += num + return total + + +def subtract(a, b): + return (a - b) + + +def multiple(a, b): + return a * b + + +def division(a, b): + return a / b + + +def remainder(a, b): + return a % b + + +def power(a, b): + return a ** b +``` +```py +# mypackage/greet.py +# greet.py +def greet_person(firstname, lastname): + return f'{firstname} {lastname}, welcome to 30DaysOfPython Challenge!' +``` +The folder structure of your package should look like this: +```sh +─ mypackage + β”œβ”€β”€ __init__.py + β”œβ”€β”€ arithmetic.py + └── greet.py +``` +Now let's open the python interactive shell and try the package we have created: +```sh +asabeneh@Asabeneh:~/Desktop/30DaysOfPython$ python +Python 3.7.5 (default, Nov 1 2019, 02:16:32) +[Clang 11.0.0 (clang-1100.0.33.8)] on darwin +Type "help", "copyright", "credits" or "license" for more information. +>>> from mypackage import arithmetics +>>> arithmetics.add_numbers(1,2,3,5) +11 +>>> arithmetics.subtract(5, 3) +2 +>>> arithmetics.multiple(5, 3) +15 +>>> arithmetics.division(5, 3) +1.6666666666666667 +>>> arithmetics.remainder(5, 3) +2 +>>> arithmetics.power(5, 3) +125 +>>> from mypackage import greet +>>> greet.greet_person('Asabeneh', 'Yetayeh') +'Asabeneh Yetayeh, welcome to 30DaysOfPython Challenge!' +>>> +``` +As you can see our package works perfect. The package folder contains a special file called __init__.py which stores the package's content. If we put __init__.py in the package folder, python start recognizes it as a package. +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. + ### Further information about packages -* Database - * SQLAlchemy or SQLObject - Object oriented access to several different database systems - * *pip install SQLAlchemy* -* Web Development - * Django - High-level web framework. - * *pip install django* - * Flask - microframework for Python based on Werkzeug, Jinja 2. (It's BSD licensed) - * *pip install flask* -* HTML Parser - * [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. - * *pip install beautifulsoup4* - * PyQuery - implements jQuery in Python; faster than BeautifulSoup, apparently. - -* XML Processing - * ElementTree - The Element type is a simple but flexible container object, designed to store hierarchical data structures, such as simplified XML infosets, in memory. --Note: Python 2.5 and up has ElementTree in the Standard Library -* GUI - * PyQt - Bindings for the cross-platform Qt framework. - * TkInter - The traditional Python user interface toolkit. -* Data Analysis, Data Science and Machine learning - * Numpy: Numpy(numeric python) is known as one of the most popular machine learning library in Python. - * Pandas: is a machine learning library in Python that provides data structures of high-level and a wide variety of tools for analysis. - * 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. - * Scikit-Learn: It is NumPy and SciPy. It is considered as one of the best libraries for working with complex data. - * TensorFlow: is a machine learning library built by Google. - * Keras: is considered as one of the coolest machine learning libraries in Python. It provides an easier mechanism to express neural networks. Keras also provides some of the best utilities for compiling models, processing data-sets, visualization of graphs, and much more. -* Network: - * requests: is a package which we can use to send requests to a server(GET, POST, DELETE, PUT) - * *pip install requests* + +- Database + - SQLAlchemy or SQLObject - Object oriented access to several different database systems + - _pip install SQLAlchemy_ +- Web Development + - Django - High-level web framework. + - _pip install django_ + - Flask - microframework for Python based on Werkzeug, Jinja 2. (It's BSD licensed) + - _pip install flask_ +- HTML Parser + + - [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. + - _pip install beautifulsoup4_ + - PyQuery - implements jQuery in Python; faster than BeautifulSoup, apparently. + +- XML Processing + - ElementTree - The Element type is a simple but flexible container object, designed to store hierarchical data structures, such as simplified XML infosets, in memory. --Note: Python 2.5 and up has ElementTree in the Standard Library +- GUI + - PyQt - Bindings for the cross-platform Qt framework. + - TkInter - The traditional Python user interface toolkit. +- Data Analysis, Data Science and Machine learning + - Numpy: Numpy(numeric python) is known as one of the most popular machine learning library in Python. + - Pandas: is a machine learning library in Python that provides data structures of high-level and a wide variety of tools for analysis. + - 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. + - Scikit-Learn: It is NumPy and SciPy. It is considered as one of the best libraries for working with complex data. + - TensorFlow: is a machine learning library built by Google. + - Keras: is considered as one of the coolest machine learning libraries in Python. It provides an easier mechanism to express neural networks. Keras also provides some of the best utilities for compiling models, processing data-sets, visualization of graphs, and much more. +- Network: + - requests: is a package which we can use to send requests to a server(GET, POST, DELETE, PUT) + - _pip install requests_ ## Exercises: Day 20 + 1. Read this url and find out the 10 most frequent words.romeo_and_juliet = 'http://www.gutenberg.org/files/1112/1112.txt' 1. Read the cats api and cats_api = 'https://api.thecatapi.com/v1/breeds' and find the avarage weight of cat in metric unit. 1. Read the countries api and find out the 10 largest countries -2. UCI is one the most common place for get data set for data science and machine learning. Read the content of UCL(http://mlr.cs.umass.edu/ml/datasets.html). Without library it will be difficult, you may try it with BeautifulSoup4 +1. UCI is one the most common place for get data set for data science and machine learning. Read the content of UCL(http://mlr.cs.umass.edu/ml/datasets.html). Without library it will be difficult, you may try it with BeautifulSoup4 # πŸ“˜ Day 21 + ## Classes and Objects -Python is an object oriented programming language. Everything in Python is an object, with its properties and methods. A number, string, list, dictionary,tuple, set etc. used in a program is an object of a corresponding built-in class. We create class to create an object. A Class is like an object constructor, or a "blueprint" for creating objects. We instantiate a class to create an object. The class defines attributes and the behavior of the object, while the object, on the other hand, represents the class. -We have been working with classes and objects right from the beginning of these challenge unknowingly. Every element in a Python program is an object of a class. +Python is an object oriented programming language. Everything in Python is an object, with its properties and methods. A number, string, list, dictionary,tuple, set etc. used in a program is an object of a corresponding built-in class. We create class to create an object. A Class is like an object constructor, or a "blueprint" for creating objects. We instantiate a class to create an object. The class defines attributes and the behavior of the object, while the object, on the other hand, represents the class. + +We have been working with classes and objects right from the beginning of these challenge unknowingly. Every element in a Python program is an object of a class. Let's check if everything in python is class: + ```py Last login: Tue Dec 10 09:35:28 on console asabeneh@Asabeneh:~$ pyhton -bash: pyhton: command not found asabeneh@Asabeneh:~$ python -Python 3.7.5 (default, Nov 1 2019, 02:16:32) +Python 3.7.5 (default, Nov 1 2019, 02:16:32) [Clang 11.0.0 (clang-1100.0.33.8)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> num = 10 @@ -736,44 +937,59 @@ Type "help", "copyright", "credits" or "license" for more information. >>> type(dct) ``` + ### Creating a Class + To create a class we need the key word **class** followed by colon. Class name should be **CamelCase**. + ```sh # syntax class ClassName: code goes here ``` + **Example:** + ```py class Person: pass ``` + ```sh <__main__.Person object at 0x10804e510> ``` + ### Creating an Object + We can create an object by calling the class. + ```py p = Person() print(p) ``` + ### Class Constructor -In the above examples, we have created an object from the Person class. However, Class without a constructor is not really useful in real applications. Let's use constructor function to make our class more useful. Like the constructor function in Java or JavaScript, python has also a builtin *__init__()* constructor function. The *__init__* constructor function has self parameter which is a reference to the current instance of the class + +In the above examples, we have created an object from the Person class. However, Class without a constructor is not really useful in real applications. Let's use constructor function to make our class more useful. Like the constructor function in Java or JavaScript, python has also a builtin _**init**()_ constructor function. The _**init**_ constructor function has self parameter which is a reference to the current instance of the class **Examples:** + ```py class Person: def __init__ (self, name): self.name =name - + p = Person('Asabeneh') print(p.name) print(p) ``` + ```sh # output Asabeneh ``` + Let's add more parameter to the constructor function. + ```py class Person: def __init__(self, firstname, lastname, age, country, city): @@ -791,6 +1007,7 @@ print(p.age) print(p.country) print(p.city) ``` + ```sh # output Asabeneh @@ -799,9 +1016,12 @@ Yetayeh Finland Helsinki ``` + ### Object Methods + Objects can have methods. The methods are functions which are belongs to the object. **Example:** + ```py class Person: def __init__(self, firstname, lastname, age, country, city): @@ -818,14 +1038,18 @@ class Person: p = Person('Asabeneh', 'Yetayeh', 250, 'Finland', 'Helsinki') print(p.person_info()) ``` + ```sh # output Asabeneh Yetayeh is 250 year old. He lives in Helsinki, Finland ``` + ### Object default methods + Sometimes, you may want to have a default values for you object methods. If we give a default values for the parameters in the constructor, we can avoid error when we call or instantiate our class without parameters. Let's see how it looks using example. **Example:** + ```py class Person: def __init__(self, firstname='Asabeneh', lastname='Yetayeh', age=250, country='Finland', city='Helsinki'): @@ -843,13 +1067,17 @@ print(p1.person_info()) p2 = Person('John', 'Doe', 30, 'Nomanland', 'Noman city') print(p2.person_info()) ``` + ```sh # output Asabeneh Yetayeh is 250 year old. He lives in Helsinki, Finland. John Doe is 30 year old. He lives in Noman city, Nomanland. ``` + ### Method to modify class default values + In the example below, the person class, all the constructor parameters have default values and in addition to that we have a skills default value which we can access it using method. Let's create add_skill method to add skill to the skills list. + ```py class Person: def __init__(self, firstname='Asabeneh', lastname='Yetayeh', age=250, country='Finland', city='Helsinki'): @@ -875,6 +1103,7 @@ print(p2.person_info()) print(p1.skills) print(p2.skills) ``` + ```sh # output Asabeneh Yetayeh is 250 year old. He lives in Helsinki, Finland. @@ -882,10 +1111,12 @@ John Doe is 30 year old. He lives in Noman city, Nomanland. ['HTML', 'CSS', 'JavaScript'] [] ``` + ### Inheritance Using inheritance we can reuse parent class code. Inheritance allows us to define a class that inherits all the methods and properties from another class. The parent class or super or base class is the class which gives all the methods and properties. Child class is the class the inherits from another class. Let's see create a student class by inheriting from person class. + ```py class Student(Person): pass @@ -906,6 +1137,7 @@ s2.add_skill('Digital Marketing') print(s2.skills) ``` + ```sh output Eyob Yetayeh is 30 year old. He lives in Helsinki, Finland. @@ -914,10 +1146,11 @@ Lidiya Teklemariam is 28 year old. He lives in Espoo, Finland. ['Organizing', 'Marketing', 'Digital Marketing'] ``` -We didn't call the *__init__()* constructor in the child class. If we didn't call it we can access all the properties but if we call it once we access the parent properties by calling *super*. -We can write add a new method to the child or we can overwrite the parent class by creating the same method name in the child class. When we add the __init__() function, the child class will no longer inherit the parent's __init__() function. +We didn't call the _**init**()_ constructor in the child class. If we didn't call it we can access all the properties but if we call it once we access the parent properties by calling _super_. +We can write add a new method to the child or we can overwrite the parent class by creating the same method name in the child class. When we add the **init**() function, the child class will no longer inherit the parent's **init**() function. ### Overriding parent method + ```py class Student(Person): def __init__ (self, firstname='Asabeneh', lastname='Yetayeh',age=250, country='Finland', city='Helsinki', gender='male'): @@ -941,16 +1174,20 @@ s2.add_skill('Marketing') s2.add_skill('Digital Marketing') print(s2.skills) ``` + ```sh Eyob Yetayeh is 30 year old. He lives in Helsinki, Finland. ['JavaScript', 'React', 'Python'] Lidiya Teklemariam is 28 year old. She lives in Espoo, Finland. ['Organizing', 'Marketing', 'Digital Marketing'] ``` + We can use super() function or the parent name Person to automatically inherit the methods and properties from its parent. In the above example, we override the parant method. The child method has a different feature, it can identify if the gender is male or female and assign the proper pronoun(He/She). -## πŸ’» Exercises: Day 20 -1. Develop a program which calculate the measure of central tendency of a sample(mean, median, mode) and measure of variability(range, variance, standard deviation). In addition to those measure, find the min, max, count and frequency distribution of the sample. Check the output below. +## πŸ’» Exercises: Day 20 + +1. Python has the module called _statistics_ and we can use this module to do all the statistical caluculations. Hower to challlenge ourselves, let's try to develop a program which calculate measure of central tendency of a sample(mean, median, mode) and measure of variability(range, variance, standard deviation). In addition to those measure, find the min, max, count and frequency distribution of the sample. Check the output below. + ```py ages = [31, 26, 34, 37, 27, 26, 32, 32, 26, 27, 27, 24, 32, 33, 27, 25, 26, 38, 37, 31, 34, 24, 33, 29, 26] @@ -966,7 +1203,10 @@ print('Variance: ',data.var()) # 17.5 print('Standard Deviation: ', data.std()) # 4.2 print('Variance: ',data.var()) # 17.5 print('Frequency Distribution: ',data.freq_dist()) # [(20.0, 26), (16.0, 27), (12.0, 32), (8.0, 37), (8.0, 34), (8.0, 33), (8.0, 31), (8.0, 24), (4.0, 38), (4.0, 29), (4.0, 25)] +``` +```sh +# you output should look like this print(data.describe()) Count: 25 Sum: 744 @@ -980,9 +1220,3 @@ Variance: 17.5 Standard Deviation: 4.2 Frequency Distribution: [(20.0, 26), (16.0, 27), (12.0, 32), (8.0, 37), (8.0, 34), (8.0, 33), (8.0, 31), (8.0, 24), (4.0, 38), (4.0, 29), (4.0, 25)] ``` - - - - - -