Python交互式Shell适用于尝试和测试小型脚本代码,但不适用于大型项目。在实际工作环境中,开发人员使用不同的代码编辑器来编写代码。在这30天的Python编程挑战中,我们将使用Visual Studio Code。Visual Studio Code是一个非常流行的开源文本编辑器。我是vscode的粉丝,我建议下载Visual Studio Code,但如果您更喜欢其他编辑器,请随意使用您拥有的编辑器。
print('1 is 1',1is1)# True - 因为数据值相同
+print('1 is not 2',1isnot2)# True - 因为1不等于2
+print('A in Asabeneh','A'in'Asabeneh')# True - A在字符串中找到
+print('B in Asabeneh','B'in'Asabeneh')# False - 没有大写B
+print('coding'in'coding for all')# True - 因为coding for all包含单词coding
+print('a in an:','a'in'an')# True
+print('4 is 2 ** 2:',4is2**2)# True
+
letter='P'# 一个字符串可以是一个单字符或一堆文本
+print(letter)# P
+print(len(letter))# 1
+greeting='Hello, World!'# 字符串可以使用单引号或双引号创建,"Hello, World!"
+print(greeting)# Hello, World!
+print(len(greeting))# 13
+sentence="I hope you are enjoying 30 days of Python Challenge"
+print(sentence)
+
+
多行字符串可以使用三重单引号''''或三重双引号""""创建。请参考以下示例。
+
multiline_string='''I am a teacher and enjoy teaching.
+I didn't find anything as rewarding as empowering people.
+That is why I created 30 days of python.'''
+print(multiline_string)
+
+# 还有另一种做同样事情的方法
+multiline_string="""I am a teacher and enjoy teaching.
+I didn't find anything as rewarding as empowering people.
+That is why I created 30 days of python."""
+print(multiline_string)
+
# 仅包含字符串
+first_name='Asabeneh'
+last_name='Yetayeh'
+language='Python'
+formated_string='I am %s%s. I teach %s'%(first_name,last_name,language)
+print(formated_string)
+
+# 包含字符串和数字
+radius=10
+pi=3.14
+area=pi*radius**2
+formated_string='The area of circle with a radius %d is %.2f.'%(radius,area)# 2表示小数点后的2个有效数字
+python_libraries=['Django','Flask','NumPy','Matplotlib','Pandas']
+formated_string='The following are python libraries:%s'%(python_libraries)
+print(formated_string)# "The following are python libraries:['Django', 'Flask', 'NumPy', 'Matplotlib','Pandas']"
+
challenge='thirty days of python'
+print(challenge.count('y'))# 3
+print(challenge.count('y',7,14))# 1
+print(challenge.count('th'))# 2
+
+
+
endswith(): 检查字符串是否以指定的后缀结束
+
+
challenge='thirty days of python'
+print(challenge.endswith('on'))# True
+print(challenge.endswith('tion'))# False
+
+
+
expandtabs(): 用空格替换制表符字符,默认制表符大小为8。它接受制表符大小参数。
+
+
challenge='thirty\tdays\tof\tpython'
+print(challenge.expandtabs())# 'thirty days of python'
+print(challenge.expandtabs(10))# 'thirty days of python'
+
+
+
find(): 返回子字符串的第一次出现的索引,如果未找到则返回-1
+
+
challenge='thirty days of python'
+print(challenge.find('y'))# 5
+print(challenge.find('th'))# 0
+
+
+
rfind(): 返回子字符串的最后一次出现的索引,如果未找到则返回-1
+
+
challenge='thirty days of python'
+print(challenge.rfind('y'))# 16
+print(challenge.rfind('th'))# 17
+
first_name='Asabeneh'
+last_name='Yetayeh'
+age=250
+job='teacher'
+country='Finland'
+sentence='I am {}{}. I am a {}. I am {} years old. I live in {}.'.format(first_name,last_name,age,job,country)
+print(sentence)# I am Asabeneh Yetayeh. I am 250 years old. I am a teacher. I live in Finland.
+
+radius=10
+pi=3.14
+area=pi*radius**2
+result='The area of a circle with radius {} is {}'.format(str(radius),str(area))
+print(result)# The area of a circle with radius 10 is 314
+
challenge='thirty days of pythoonnn'
+print(challenge.strip('noth'))# 'irty days of py'
+
+
+
replace(): 用给定的字符串替换子字符串
+
+
challenge='thirty days of python'
+print(challenge.replace('python','coding'))# 'thirty days of coding'
+
+
+
split(): 使用给定的字符串或空格作为分隔符拆分字符串
+
+
challenge='thirty days of python'
+print(challenge.split())# ['thirty', 'days', 'of', 'python']
+challenge='thirty, days, of, python'
+print(challenge.split(', '))# ['thirty', 'days', 'of', 'python']
+
+
+
title(): 返回以标题格式的字符串
+
+
challenge='thirty days of python'
+print(challenge.title())# Thirty Days Of Python
+
+
+
swapcase(): 将所有大写字符转换为小写字符,将所有小写字符转换为大写字符
+
+
challenge='thirty days of python'
+print(challenge.swapcase())# THIRTY DAYS OF PYTHON
+challenge='Thirty Days Of Python'
+print(challenge.swapcase())# tHIRTY dAYS oF pYTHON
+
+
+
startswith(): 检查字符串是否以指定的字符串开头
+
+
challenge='thirty days of python'
+print(challenge.startswith('thirty'))# True
+
+challenge='30 days of python'
+print(challenge.startswith('thirty'))# False
+
+
+[<< Day 10](../10_Day_Loops/10_loops.md) | [Day 12 >>](../12_Day_Modules/12_modules.md)
+
+![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
+
+- [📘 Day 11](#-day-11)
+ - [Functions](#functions)
+ - [Defining a Function](#defining-a-function)
+ - [Declaring and Calling a Function](#declaring-and-calling-a-function)
+ - [Function without Parameters](#function-without-parameters)
+ - [Function Returning a Value - Part 1](#function-returning-a-value---part-1)
+ - [Function with Parameters](#function-with-parameters)
+ - [Passing Arguments with Key and Value](#passing-arguments-with-key-and-value)
+ - [Function Returning a Value - Part 2](#function-returning-a-value---part-2)
+ - [Function with Default Parameters](#function-with-default-parameters)
+ - [Arbitrary Number of Arguments](#arbitrary-number-of-arguments)
+ - [Default and Arbitrary Number of Parameters in Functions](#default-and-arbitrary-number-of-parameters-in-functions)
+ - [Function as a Parameter of Another Function](#function-as-a-parameter-of-another-function)
+ - [💻 Exercises: Day 11](#-exercises-day-11)
+ - [Exercises: Level 1](#exercises-level-1)
+ - [Exercises: Level 2](#exercises-level-2)
+ - [Exercises: Level 3](#exercises-level-3)
+
+# 📘 Day 11
+
+## Functions
+
+So far we have seen many built-in Python functions. In this section, we will focus on custom functions. What is a function? Before we start making functions, let us learn what a function is and why we need them?
+
+### Defining a Function
+
+A function is a reusable block of code or programming statements designed to perform a certain task. To define or declare a function, Python provides the _def_ keyword. The following is the syntax for defining a function. The function block of code is executed only if the function is called or invoked.
+
+### Declaring and Calling a Function
+
+When we make a function, we call it declaring a function. When we start using the it, we call it *calling* or *invoking* a function. Function can be declared with or without parameters.
+
+```py
+# syntax
+# Declaring a function
+def function_name():
+ codes
+ codes
+# Calling a function
+function_name()
+```
+
+### Function without Parameters
+
+Function can be declared without parameters.
+
+**Example:**
+
+```py
+def generate_full_name ():
+ first_name = 'Asabeneh'
+ last_name = 'Yetayeh'
+ space = ' '
+ full_name = first_name + space + last_name
+ print(full_name)
+generate_full_name () # calling a function
+
+def add_two_numbers ():
+ num_one = 2
+ num_two = 3
+ total = num_one + num_two
+ print(total)
+add_two_numbers()
+```
+
+### Function Returning a Value - Part 1
+
+Function can also return values, if a function does not have a return statement, the value of the function is None. Let us rewrite the above functions using return. From now on, we get a value from a function when we call the function and print it.
+
+```py
+def generate_full_name ():
+ first_name = 'Asabeneh'
+ last_name = 'Yetayeh'
+ space = ' '
+ full_name = first_name + space + last_name
+ return full_name
+print(generate_full_name())
+
+def add_two_numbers ():
+ num_one = 2
+ num_two = 3
+ total = num_one + num_two
+ return total
+print(add_two_numbers())
+```
+
+### Function with Parameters
+
+In a function we can pass different data types(number, string, boolean, list, tuple, dictionary or set) as a parameter
+
+- Single Parameter: If our function takes a parameter we should call our function with an argument
+
+```py
+ # syntax
+ # Declaring a function
+ def function_name(parameter):
+ codes
+ codes
+ # Calling function
+ print(function_name(argument))
+```
+
+**Example:**
+
+```py
+def greetings (name):
+ message = name + ', welcome to Python for Everyone!'
+ return message
+
+print(greetings('Asabeneh'))
+
+def add_ten(num):
+ ten = 10
+ return num + ten
+print(add_ten(90))
+
+def square_number(x):
+ return x * x
+print(square_number(2))
+
+def area_of_circle (r):
+ PI = 3.14
+ area = PI * r ** 2
+ return area
+print(area_of_circle(10))
+
+def sum_of_numbers(n):
+ total = 0
+ for i in range(n+1):
+ total+=i
+ print(total)
+print(sum_of_numbers(10)) # 55
+print(sum_of_numbers(100)) # 5050
+```
+
+- Two Parameter: A function may or may not have a parameter or parameters. A function may also have two or more parameters. If our function takes parameters we should call it with arguments. Let us check a function with two parameters:
+
+```py
+ # syntax
+ # Declaring a function
+ def function_name(para1, para2):
+ codes
+ codes
+ # Calling function
+ print(function_name(arg1, arg2))
+```
+
+**Example:**
+
+```py
+def generate_full_name (first_name, last_name):
+ space = ' '
+ full_name = first_name + space + last_name
+ return full_name
+print('Full Name: ', generate_full_name('Asabeneh','Yetayeh'))
+
+def sum_two_numbers (num_one, num_two):
+ sum = num_one + num_two
+ return sum
+print('Sum of two numbers: ', sum_two_numbers(1, 9))
+
+def calculate_age (current_year, birth_year):
+ age = current_year - birth_year
+ return age;
+
+print('Age: ', calculate_age(2021, 1819))
+
+def weight_of_object (mass, gravity):
+ weight = str(mass * gravity)+ ' N' # the value has to be changed to a string first
+ return weight
+print('Weight of an object in Newtons: ', weight_of_object(100, 9.81))
+```
+
+### Passing Arguments with Key and Value
+
+If we pass the arguments with key and value, the order of the arguments does not matter.
+
+```py
+# syntax
+# Declaring a function
+def function_name(para1, para2):
+ codes
+ codes
+# Calling function
+print(function_name(para1 = 'John', para2 = 'Doe')) # the order of arguments does not matter here
+```
+
+**Example:**
+
+```py
+def print_fullname(firstname, lastname):
+ space = ' '
+ full_name = firstname + space + lastname
+ print(full_name)
+print(print_fullname(firstname = 'Asabeneh', lastname = 'Yetayeh'))
+
+def add_two_numbers (num1, num2):
+ total = num1 + num2
+ print(total)
+print(add_two_numbers(num2 = 3, num1 = 2)) # Order does not matter
+```
+
+### Function Returning a Value - Part 2
+
+If we do not return a value with a function, then our function is returning _None_ by default. To return a value with a function we use the keyword _return_ followed by the variable we are returning. We can return any kind of data types from a function.
+
+- Returning a string:
+**Example:**
+
+```py
+def print_name(firstname):
+ return firstname
+print_name('Asabeneh') # Asabeneh
+
+def print_full_name(firstname, lastname):
+ space = ' '
+ full_name = firstname + space + lastname
+ return full_name
+print_full_name(firstname='Asabeneh', lastname='Yetayeh')
+```
+
+- Returning a number:
+
+**Example:**
+
+```py
+def add_two_numbers (num1, num2):
+ total = num1 + num2
+ return total
+print(add_two_numbers(2, 3))
+
+def calculate_age (current_year, birth_year):
+ age = current_year - birth_year
+ return age;
+print('Age: ', calculate_age(2019, 1819))
+```
+
+- Returning a boolean:
+ **Example:**
+
+```py
+def is_even (n):
+ if n % 2 == 0:
+ print('even')
+ return True # return stops further execution of the function, similar to break
+ return False
+print(is_even(10)) # True
+print(is_even(7)) # False
+```
+
+- Returning a list:
+ **Example:**
+
+```py
+def find_even_numbers(n):
+ evens = []
+ for i in range(n + 1):
+ if i % 2 == 0:
+ evens.append(i)
+ return evens
+print(find_even_numbers(10))
+```
+
+### Function with Default Parameters
+
+Sometimes we pass default values to parameters, when we invoke the function. If we do not pass arguments when calling the function, their default values will be used.
+
+```py
+# syntax
+# Declaring a function
+def function_name(param = value):
+ codes
+ codes
+# Calling function
+function_name()
+function_name(arg)
+```
+
+**Example:**
+
+```py
+def greetings (name = 'Peter'):
+ message = name + ', welcome to Python for Everyone!'
+ return message
+print(greetings())
+print(greetings('Asabeneh'))
+
+def generate_full_name (first_name = 'Asabeneh', last_name = 'Yetayeh'):
+ space = ' '
+ full_name = first_name + space + last_name
+ return full_name
+
+print(generate_full_name())
+print(generate_full_name('David','Smith'))
+
+def calculate_age (birth_year,current_year = 2021):
+ age = current_year - birth_year
+ return age;
+print('Age: ', calculate_age(1821))
+
+def weight_of_object (mass, gravity = 9.81):
+ weight = str(mass * gravity)+ ' N' # the value has to be changed to string first
+ return weight
+print('Weight of an object in Newtons: ', weight_of_object(100)) # 9.81 - average gravity on Earth's surface
+print('Weight of an object in Newtons: ', weight_of_object(100, 1.62)) # gravity on the surface of the Moon
+```
+
+### Arbitrary Number of Arguments
+
+If we do not know the number of arguments we pass to our function, we can create a function which can take arbitrary number of arguments by adding \* before the parameter name.
+
+```py
+# syntax
+# Declaring a function
+def function_name(*args):
+ codes
+ codes
+# Calling function
+function_name(param1, param2, param3,..)
+```
+
+**Example:**
+
+```py
+def sum_all_nums(*nums):
+ total = 0
+ for num in nums:
+ total += num # same as total = total + num
+ return total
+print(sum_all_nums(2, 3, 5)) # 10
+```
+
+### Default and Arbitrary Number of Parameters in Functions
+
+```py
+def generate_groups (team,*args):
+ print(team)
+ for i in args:
+ print(i)
+print(generate_groups('Team-1','Asabeneh','Brook','David','Eyob'))
+```
+
+### Function as a Parameter of Another Function
+
+```py
+#You can pass functions around as parameters
+def square_number (n):
+ return n * n
+def do_something(f, x):
+ return f(x)
+print(do_something(square_number, 3)) # 27
+```
+
+🌕 You achieved quite a lot so far. Keep going! You have just completed day 11 challenges and you are 11 steps a head in to your way to greatness. Now do some exercises for your brain and muscles.
+
+## Testimony
+Now it is time to express your thoughts about the Author and 30DaysOfPython. You can leave your testimonial on this [link](https://testimonify.herokuapp.com/)
+
+## 💻 Exercises: Day 11
+
+### Exercises: Level 1
+
+1. Declare a function _add_two_numbers_. It takes two parameters and it returns a sum.
+2. Area of a circle is calculated as follows: area = π x r x r. Write a function that calculates _area_of_circle_.
+3. Write a function called add_all_nums which takes arbitrary number of arguments and sums all the arguments. Check if all the list items are number types. If not do give a reasonable feedback.
+4. Temperature in °C can be converted to °F using this formula: °F = (°C x 9/5) + 32. Write a function which converts °C to °F, _convert_celsius_to-fahrenheit_.
+5. Write a function called check-season, it takes a month parameter and returns the season: Autumn, Winter, Spring or Summer.
+6. Write a function called calculate_slope which return the slope of a linear equation
+7. Quadratic equation is calculated as follows: ax² + bx + c = 0. Write a function which calculates solution set of a quadratic equation, _solve_quadratic_eqn_.
+8. Declare a function named print_list. It takes a list as a parameter and it prints out each element of the list.
+9. Declare a function named reverse_list. It takes an array as a parameter and it returns the reverse of the array (use loops).
+
+```py
+print(reverse_list([1, 2, 3, 4, 5]))
+# [5, 4, 3, 2, 1]
+print(reverse_list1(["A", "B", "C"]))
+# ["C", "B", "A"]
+```
+
+10. Declare a function named capitalize_list_items. It takes a list as a parameter and it returns a capitalized list of items
+11. Declare a function named add_item. It takes a list and an item parameters. It returns a list with the item added at the end.
+
+```py
+food_staff = ['Potato', 'Tomato', 'Mango', 'Milk'];
+print(add_item(food_staff, 'Meat')) # ['Potato', 'Tomato', 'Mango', 'Milk','Meat'];
+numbers = [2, 3, 7, 9];
+print(add_item(numbers, 5)) [2, 3, 7, 9, 5]
+```
+
+12. Declare a function named remove_item. It takes a list and an item parameters. It returns a list with the item removed from it.
+
+```py
+food_staff = ['Potato', 'Tomato', 'Mango', 'Milk'];
+print(remove_item(food_staff, 'Mango')) # ['Potato', 'Tomato', 'Milk'];
+numbers = [2, 3, 7, 9];
+print(remove_item(numbers, 3)) # [2, 7, 9]
+```
+
+13. Declare a function named sum_of_numbers. It takes a number parameter and it adds all the numbers in that range.
+
+```py
+print(sum_of_numbers(5)) # 15
+print(sum_all_numbers(10)) # 55
+print(sum_all_numbers(100)) # 5050
+```
+
+14. Declare a function named sum_of_odds. It takes a number parameter and it adds all the odd numbers in that range.
+15. Declare a function named sum_of_even. It takes a number parameter and it adds all the even numbers in that - range.
+
+### Exercises: Level 2
+
+1. Declare a function named evens_and_odds . It takes a positive integer as parameter and it counts number of evens and odds in the number.
+
+```py
+ print(evens_and_odds(100))
+ # The number of odds are 50.
+ # The number of evens are 51.
+```
+
+1. Call your function factorial, it takes a whole number as a parameter and it return a factorial of the number
+1. Call your function _is_empty_, it takes a parameter and it checks if it is empty or not
+1. Write different functions which take lists. They should calculate_mean, calculate_median, calculate_mode, calculate_range, calculate_variance, calculate_std (standard deviation).
+
+### Exercises: Level 3
+
+1. Write a function called is_prime, which checks if a number is prime.
+1. Write a functions which checks if all items are unique in the list.
+1. Write a function which checks if all the items of the list are of the same data type.
+1. Write a function which check if provided variable is a valid python variable
+1. Go to the data folder and access the countries-data.py file.
+
+- Create a function called the most_spoken_languages in the world. It should return 10 or 20 most spoken languages in the world in descending order
+- Create a function called the most_populated_countries. It should return 10 or 20 most populated countries in descending order.
+
+🎉 CONGRATULATIONS ! 🎉
+
+[<< Day 10](../10_Day_Loops/10_loops.md) | [Day 12 >>](../12_Day_Modules/12_modules.md)
diff --git a/11_functions/index.html b/11_functions/index.html
new file mode 100644
index 0000000..4429e6a
--- /dev/null
+++ b/11_functions/index.html
@@ -0,0 +1,1857 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 11 functions - 30天入门Python全栈开发
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
So far we have seen many built-in Python functions. In this section, we will focus on custom functions. What is a function? Before we start making functions, let us learn what a function is and why we need them?
A function is a reusable block of code or programming statements designed to perform a certain task. To define or declare a function, Python provides the def keyword. The following is the syntax for defining a function. The function block of code is executed only if the function is called or invoked.
When we make a function, we call it declaring a function. When we start using the it, we call it calling or invoking a function. Function can be declared with or without parameters.
+
# syntax
+# Declaring a function
+deffunction_name():
+ codes
+ codes
+# Calling a function
+function_name()
+
Function can also return values, if a function does not have a return statement, the value of the function is None. Let us rewrite the above functions using return. From now on, we get a value from a function when we call the function and print it.
Two Parameter: A function may or may not have a parameter or parameters. A function may also have two or more parameters. If our function takes parameters we should call it with arguments. Let us check a function with two parameters:
+
+
# syntax
+ # Declaring a function
+ deffunction_name(para1,para2):
+ codes
+ codes
+ # Calling function
+ print(function_name(arg1,arg2))
+
+
Example:
+
defgenerate_full_name(first_name,last_name):
+ space=' '
+ full_name=first_name+space+last_name
+ returnfull_name
+print('Full Name: ',generate_full_name('Asabeneh','Yetayeh'))
+
+defsum_two_numbers(num_one,num_two):
+ sum=num_one+num_two
+ returnsum
+print('Sum of two numbers: ',sum_two_numbers(1,9))
+
+defcalculate_age(current_year,birth_year):
+ age=current_year-birth_year
+ returnage;
+
+print('Age: ',calculate_age(2021,1819))
+
+defweight_of_object(mass,gravity):
+ weight=str(mass*gravity)+' N'# the value has to be changed to a string first
+ returnweight
+print('Weight of an object in Newtons: ',weight_of_object(100,9.81))
+
If we pass the arguments with key and value, the order of the arguments does not matter.
+
# syntax
+# Declaring a function
+deffunction_name(para1,para2):
+ codes
+ codes
+# Calling function
+print(function_name(para1='John',para2='Doe'))# the order of arguments does not matter here
+
+
Example:
+
defprint_fullname(firstname,lastname):
+ space=' '
+ full_name=firstname+space+lastname
+ print(full_name)
+print(print_fullname(firstname='Asabeneh',lastname='Yetayeh'))
+
+defadd_two_numbers(num1,num2):
+ total=num1+num2
+ print(total)
+print(add_two_numbers(num2=3,num1=2))# Order does not matter
+
If we do not return a value with a function, then our function is returning None by default. To return a value with a function we use the keyword return followed by the variable we are returning. We can return any kind of data types from a function.
defis_even(n):
+ ifn%2==0:
+ print('even')
+ returnTrue# return stops further execution of the function, similar to break
+ returnFalse
+print(is_even(10))# True
+print(is_even(7))# False
+
Sometimes we pass default values to parameters, when we invoke the function. If we do not pass arguments when calling the function, their default values will be used.
+
# syntax
+# Declaring a function
+deffunction_name(param=value):
+ codes
+ codes
+# Calling function
+function_name()
+function_name(arg)
+
+
Example:
+
defgreetings(name='Peter'):
+ message=name+', welcome to Python for Everyone!'
+ returnmessage
+print(greetings())
+print(greetings('Asabeneh'))
+
+defgenerate_full_name(first_name='Asabeneh',last_name='Yetayeh'):
+ space=' '
+ full_name=first_name+space+last_name
+ returnfull_name
+
+print(generate_full_name())
+print(generate_full_name('David','Smith'))
+
+defcalculate_age(birth_year,current_year=2021):
+ age=current_year-birth_year
+ returnage;
+print('Age: ',calculate_age(1821))
+
+defweight_of_object(mass,gravity=9.81):
+ weight=str(mass*gravity)+' N'# the value has to be changed to string first
+ returnweight
+print('Weight of an object in Newtons: ',weight_of_object(100))# 9.81 - average gravity on Earth's surface
+print('Weight of an object in Newtons: ',weight_of_object(100,1.62))# gravity on the surface of the Moon
+
If we do not know the number of arguments we pass to our function, we can create a function which can take arbitrary number of arguments by adding * before the parameter name.
+
# syntax
+# Declaring a function
+deffunction_name(*args):
+ codes
+ codes
+# Calling function
+function_name(param1,param2,param3,..)
+
+
Example:
+
defsum_all_nums(*nums):
+ total=0
+ fornuminnums:
+ total+=num# same as total = total + num
+ returntotal
+print(sum_all_nums(2,3,5))# 10
+
+
Default and Arbitrary Number of Parameters in Functions⚓︎
#You can pass functions around as parameters
+defsquare_number(n):
+ returnn*n
+defdo_something(f,x):
+ returnf(x)
+print(do_something(square_number,3))# 27
+
+
🌕 You achieved quite a lot so far. Keep going! You have just completed day 11 challenges and you are 11 steps a head in to your way to greatness. Now do some exercises for your brain and muscles.
Declare a function add_two_numbers. It takes two parameters and it returns a sum.
+
Area of a circle is calculated as follows: area = π x r x r. Write a function that calculates area_of_circle.
+
Write a function called add_all_nums which takes arbitrary number of arguments and sums all the arguments. Check if all the list items are number types. If not do give a reasonable feedback.
+
Temperature in °C can be converted to °F using this formula: °F = (°C x 9/5) + 32. Write a function which converts °C to °F, convert_celsius_to-fahrenheit.
+
Write a function called check-season, it takes a month parameter and returns the season: Autumn, Winter, Spring or Summer.
+
Write a function called calculate_slope which return the slope of a linear equation
+
Quadratic equation is calculated as follows: ax² + bx + c = 0. Write a function which calculates solution set of a quadratic equation, solve_quadratic_eqn.
+
Declare a function named print_list. It takes a list as a parameter and it prints out each element of the list.
+
Declare a function named reverse_list. It takes an array as a parameter and it returns the reverse of the array (use loops).
Declare a function named evens_and_odds . It takes a positive integer as parameter and it counts number of evens and odds in the number.
+
+
print(evens_and_odds(100))
+ # The number of odds are 50.
+ # The number of evens are 51.
+
+
+
Call your function factorial, it takes a whole number as a parameter and it return a factorial of the number
+
Call your function is_empty, it takes a parameter and it checks if it is empty or not
+
Write different functions which take lists. They should calculate_mean, calculate_median, calculate_mode, calculate_range, calculate_variance, calculate_std (standard deviation).
+
+
+[<< Day 11](../11_Day_Functions/11_functions.md) | [Day 13>>](../13_Day_List_comprehension/13_list_comprehension.md)
+
+![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
+
+- [📘 Day 12](#-day-12)
+ - [Modules](#modules)
+ - [What is a Module](#what-is-a-module)
+ - [Creating a Module](#creating-a-module)
+ - [Importing a Module](#importing-a-module)
+ - [Import Functions from a Module](#import-functions-from-a-module)
+ - [Import Functions from a Module and Renaming](#import-functions-from-a-module-and-renaming)
+ - [Import Built-in Modules](#import-built-in-modules)
+ - [OS Module](#os-module)
+ - [Sys Module](#sys-module)
+ - [Statistics Module](#statistics-module)
+ - [Math Module](#math-module)
+ - [String Module](#string-module)
+ - [Random Module](#random-module)
+ - [💻 Exercises: Day 12](#-exercises-day-12)
+ - [Exercises: Level 1](#exercises-level-1)
+ - [Exercises: Level 2](#exercises-level-2)
+ - [Exercises: Level 3](#exercises-level-3)
+
+# 📘 Day 12
+
+## Modules
+
+### What is a Module
+
+A module is a file containing a set of codes or a set of functions which can be included to an application. A module could be a file containing a single variable, a function or a big code base.
+
+### Creating a Module
+
+To create a module we write our codes in a python script and we save it as a .py file. Create a file named mymodule.py inside your project folder. Let us write some code in this file.
+
+```py
+# mymodule.py file
+def generate_full_name(firstname, lastname):
+ return firstname + ' ' + lastname
+```
+
+Create main.py file in your project directory and import the mymodule.py file.
+
+### Importing a Module
+
+To import the file we use the _import_ keyword and the name of the file only.
+
+```py
+# main.py file
+import mymodule
+print(mymodule.generate_full_name('Asabeneh', 'Yetayeh')) # Asabeneh Yetayeh
+```
+
+### Import Functions from a Module
+
+We can have many functions in a file and we can import all the functions differently.
+
+```py
+# main.py file
+from mymodule import generate_full_name, sum_two_nums, person, gravity
+print(generate_full_name('Asabneh','Yetayeh'))
+print(sum_two_nums(1,9))
+mass = 100;
+weight = mass * gravity
+print(weight)
+print(person['firstname'])
+```
+
+### Import Functions from a Module and Renaming
+
+During importing we can rename the name of the module.
+
+```py
+# main.py file
+from mymodule import generate_full_name as fullname, sum_two_nums as total, person as p, gravity as g
+print(fullname('Asabneh','Yetayeh'))
+print(total(1, 9))
+mass = 100;
+weight = mass * g
+print(weight)
+print(p)
+print(p['firstname'])
+```
+
+## Import Built-in Modules
+
+Like other programming languages we can also import modules by importing the file/function using the key word _import_. Let's import the common module we will use most of the time. Some of the common built-in modules: _math_, _datetime_, _os_,_sys_, _random_, _statistics_, _collections_, _json_,_re_
+
+### OS Module
+
+Using python _os_ module it is possible to automatically perform many operating system tasks. The OS module in Python provides functions for creating, changing current working directory, and removing a directory (folder), fetching its contents, changing and identifying the current directory.
+
+```py
+# import the module
+import os
+# Creating a directory
+os.mkdir('directory_name')
+# Changing the current directory
+os.chdir('path')
+# Getting current working directory
+os.getcwd()
+# Removing directory
+os.rmdir()
+```
+
+### Sys Module
+
+The sys module provides functions and variables used to manipulate different parts of the Python runtime environment. Function sys.argv returns a list of command line arguments passed to a Python script. The item at index 0 in this list is always the name of the script, at index 1 is the argument passed from the command line.
+
+Example of a script.py file:
+
+```py
+import sys
+#print(sys.argv[0], argv[1],sys.argv[2]) # this line would print out: filename argument1 argument2
+print('Welcome {}. Enjoy {} challenge!'.format(sys.argv[1], sys.argv[2]))
+```
+
+Now to check how this script works I wrote in command line:
+
+```sh
+python script.py Asabeneh 30DaysOfPython
+```
+
+The result:
+
+```sh
+Welcome Asabeneh. Enjoy 30DayOfPython challenge!
+```
+
+Some useful sys commands:
+
+```py
+# to exit sys
+sys.exit()
+# To know the largest integer variable it takes
+sys.maxsize
+# To know environment path
+sys.path
+# To know the version of python you are using
+sys.version
+```
+
+### Statistics Module
+
+The statistics module provides functions for mathematical statistics of numeric data. The popular statistical functions which are defined in this module: _mean_, _median_, _mode_, _stdev_ etc.
+
+```py
+from statistics import * # importing all the statistics modules
+ages = [20, 20, 4, 24, 25, 22, 26, 20, 23, 22, 26]
+print(mean(ages)) # ~22.9
+print(median(ages)) # 23
+print(mode(ages)) # 20
+print(stdev(ages)) # ~2.3
+```
+
+### Math Module
+
+Module containing many mathematical operations and constants.
+
+```py
+import math
+print(math.pi) # 3.141592653589793, pi constant
+print(math.sqrt(2)) # 1.4142135623730951, square root
+print(math.pow(2, 3)) # 8.0, exponential function
+print(math.floor(9.81)) # 9, rounding to the lowest
+print(math.ceil(9.81)) # 10, rounding to the highest
+print(math.log10(100)) # 2, logarithm with 10 as base
+```
+
+Now, we have imported the *math* module which contains lots of function which can help us to perform mathematical calculations. To check what functions the module has got, we can use _help(math)_, or _dir(math)_. This will display the available functions in the module. If we want to import only a specific function from the module we import it as follows:
+
+```py
+from math import pi
+print(pi)
+```
+
+It is also possible to import multiple functions at once
+
+```py
+
+from math import pi, sqrt, pow, floor, ceil, log10
+print(pi) # 3.141592653589793
+print(sqrt(2)) # 1.4142135623730951
+print(pow(2, 3)) # 8.0
+print(floor(9.81)) # 9
+print(ceil(9.81)) # 10
+print(math.log10(100)) # 2
+
+```
+
+But if we want to import all the function in math module we can use \* .
+
+```py
+from math import *
+print(pi) # 3.141592653589793, pi constant
+print(sqrt(2)) # 1.4142135623730951, square root
+print(pow(2, 3)) # 8.0, exponential
+print(floor(9.81)) # 9, rounding to the lowest
+print(ceil(9.81)) # 10, rounding to the highest
+print(math.log10(100)) # 2
+```
+
+When we import we can also rename the name of the function.
+
+```py
+from math import pi as PI
+print(PI) # 3.141592653589793
+```
+
+### String Module
+
+A string module is a useful module for many purposes. The example below shows some use of the string module.
+
+```py
+import string
+print(string.ascii_letters) # abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
+print(string.digits) # 0123456789
+print(string.punctuation) # !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
+```
+
+### Random Module
+
+By now you are familiar with importing modules. Let us do one more import to get very familiar with it. Let us import _random_ module which gives us a random number between 0 and 0.9999.... The _random_ module has lots of functions but in this section we will only use _random_ and _randint_.
+
+```py
+from random import random, randint
+print(random()) # it doesn't take any arguments; it returns a value between 0 and 0.9999
+print(randint(5, 20)) # it returns a random integer number between [5, 20] inclusive
+```
+
+🌕 You are going far. Keep going! You have just completed day 12 challenges and you are 12 steps a head in to your way to greatness. Now do some exercises for your brain and muscles.
+
+## 💻 Exercises: Day 12
+
+### Exercises: Level 1
+
+1. Writ a function which generates a six digit/character random_user_id.
+ ```py
+ print(random_user_id());
+ '1ee33d'
+ ```
+2. Modify the previous task. Declare a function named user_id_gen_by_user. It doesn’t take any parameters but it takes two inputs using input(). One of the inputs is the number of characters and the second input is the number of IDs which are supposed to be generated.
+
+```py
+print(user_id_gen_by_user()) # user input: 5 5
+#output:
+#kcsy2
+#SMFYb
+#bWmeq
+#ZXOYh
+#2Rgxf
+
+print(user_id_gen_by_user()) # 16 5
+#1GCSgPLMaBAVQZ26
+#YD7eFwNQKNs7qXaT
+#ycArC5yrRupyG00S
+#UbGxOFI7UXSWAyKN
+#dIV0SSUTgAdKwStr
+```
+
+3. Write a function named rgb_color_gen. It will generate rgb colors (3 values ranging from 0 to 255 each).
+
+```py
+print(rgb_color_gen())
+# rgb(125,244,255) - the output should be in this form
+```
+
+### Exercises: Level 2
+
+1. Write a function list_of_hexa_colors which returns any number of hexadecimal colors in an array (six hexadecimal numbers written after #. Hexadecimal numeral system is made out of 16 symbols, 0-9 and first 6 letters of the alphabet, a-f. Check the task 6 for output examples).
+1. Write a function list_of_rgb_colors which returns any number of RGB colors in an array.
+1. Write a function generate_colors which can generate any number of hexa or rgb colors.
+
+```py
+ generate_colors('hexa', 3) # ['#a3e12f','#03ed55','#eb3d2b']
+ generate_colors('hexa', 1) # ['#b334ef']
+ generate_colors('rgb', 3) # ['rgb(5, 55, 175','rgb(50, 105, 100','rgb(15, 26, 80']
+ generate_colors('rgb', 1) # ['rgb(33,79, 176)']
+ ```
+
+### Exercises: Level 3
+
+1. Call your function shuffle_list, it takes a list as a parameter and it returns a shuffled list
+1. Write a function which returns an array of seven random numbers in a range of 0-9. All the numbers must be unique.
+
+🎉 CONGRATULATIONS ! 🎉
+
+[<< Day 11](../11_Day_Functions/11_functions.md) | [Day 13>>](../13_Day_List_comprehension/13_list_comprehension.md)
diff --git a/12_modules/index.html b/12_modules/index.html
new file mode 100644
index 0000000..8743896
--- /dev/null
+++ b/12_modules/index.html
@@ -0,0 +1,1706 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 12 modules - 30天入门Python全栈开发
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
A module is a file containing a set of codes or a set of functions which can be included to an application. A module could be a file containing a single variable, a function or a big code base.
To create a module we write our codes in a python script and we save it as a .py file. Create a file named mymodule.py inside your project folder. Let us write some code in this file.
Like other programming languages we can also import modules by importing the file/function using the key word import. Let's import the common module we will use most of the time. Some of the common built-in modules: math, datetime, os,sys, random, statistics, collections, json,re
Using python os module it is possible to automatically perform many operating system tasks. The OS module in Python provides functions for creating, changing current working directory, and removing a directory (folder), fetching its contents, changing and identifying the current directory.
+
# import the module
+importos
+# Creating a directory
+os.mkdir('directory_name')
+# Changing the current directory
+os.chdir('path')
+# Getting current working directory
+os.getcwd()
+# Removing directory
+os.rmdir()
+
The sys module provides functions and variables used to manipulate different parts of the Python runtime environment. Function sys.argv returns a list of command line arguments passed to a Python script. The item at index 0 in this list is always the name of the script, at index 1 is the argument passed from the command line.
+
Example of a script.py file:
+
importsys
+#print(sys.argv[0], argv[1],sys.argv[2]) # this line would print out: filename argument1 argument2
+print('Welcome {}. Enjoy {} challenge!'.format(sys.argv[1],sys.argv[2]))
+
+
Now to check how this script works I wrote in command line:
+
pythonscript.pyAsabeneh30DaysOfPython
+
+
The result:
+
WelcomeAsabeneh.Enjoy30DayOfPythonchallenge!
+
+
Some useful sys commands:
+
# to exit sys
+sys.exit()
+# To know the largest integer variable it takes
+sys.maxsize
+# To know environment path
+sys.path
+# To know the version of python you are using
+sys.version
+
The statistics module provides functions for mathematical statistics of numeric data. The popular statistical functions which are defined in this module: mean, median, mode, stdev etc.
+
fromstatisticsimport*# importing all the statistics modules
+ages=[20,20,4,24,25,22,26,20,23,22,26]
+print(mean(ages))# ~22.9
+print(median(ages))# 23
+print(mode(ages))# 20
+print(stdev(ages))# ~2.3
+
Module containing many mathematical operations and constants.
+
importmath
+print(math.pi)# 3.141592653589793, pi constant
+print(math.sqrt(2))# 1.4142135623730951, square root
+print(math.pow(2,3))# 8.0, exponential function
+print(math.floor(9.81))# 9, rounding to the lowest
+print(math.ceil(9.81))# 10, rounding to the highest
+print(math.log10(100))# 2, logarithm with 10 as base
+
+
Now, we have imported the math module which contains lots of function which can help us to perform mathematical calculations. To check what functions the module has got, we can use help(math), or dir(math). This will display the available functions in the module. If we want to import only a specific function from the module we import it as follows:
+
frommathimportpi
+print(pi)
+
+
It is also possible to import multiple functions at once
By now you are familiar with importing modules. Let us do one more import to get very familiar with it. Let us import random module which gives us a random number between 0 and 0.9999.... The random module has lots of functions but in this section we will only use random and randint.
+
fromrandomimportrandom,randint
+print(random())# it doesn't take any arguments; it returns a value between 0 and 0.9999
+print(randint(5,20))# it returns a random integer number between [5, 20] inclusive
+
+
🌕 You are going far. Keep going! You have just completed day 12 challenges and you are 12 steps a head in to your way to greatness. Now do some exercises for your brain and muscles.
Writ a function which generates a six digit/character random_user_id.
+
print(random_user_id());
+ '1ee33d'
+
+
Modify the previous task. Declare a function named user_id_gen_by_user. It doesn’t take any parameters but it takes two inputs using input(). One of the inputs is the number of characters and the second input is the number of IDs which are supposed to be generated.
Write a function list_of_hexa_colors which returns any number of hexadecimal colors in an array (six hexadecimal numbers written after #. Hexadecimal numeral system is made out of 16 symbols, 0-9 and first 6 letters of the alphabet, a-f. Check the task 6 for output examples).
+
Write a function list_of_rgb_colors which returns any number of RGB colors in an array.
+
Write a function generate_colors which can generate any number of hexa or rgb colors.
+
+
+[<< Day 12](../12_Day_Modules/12_modules.md) | [Day 14>>](../14_Day_Higher_order_functions/14_higher_order_functions.md)
+
+![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
+
+- [📘 Day 13](#-day-13)
+ - [List Comprehension](#list-comprehension)
+ - [Lambda Function](#lambda-function)
+ - [Creating a Lambda Function](#creating-a-lambda-function)
+ - [Lambda Function Inside Another Function](#lambda-function-inside-another-function)
+ - [💻 Exercises: Day 13](#-exercises-day-13)
+
+# 📘 Day 13
+
+## List Comprehension
+
+List comprehension in Python is a compact way of creating a list from a sequence. It is a short way to create a new list. List comprehension is considerably faster than processing a list using the _for_ loop.
+
+```py
+# syntax
+[i for i in iterable if expression]
+```
+
+**Example:1**
+
+For instance if you want to change a string to a list of characters. You can use a couple of methods. Let's see some of them:
+
+```py
+# One way
+language = 'Python'
+lst = list(language) # changing the string to list
+print(type(lst)) # list
+print(lst) # ['P', 'y', 't', 'h', 'o', 'n']
+
+# Second way: list comprehension
+lst = [i for i in language]
+print(type(lst)) # list
+print(lst) # ['P', 'y', 't', 'h', 'o', 'n']
+
+```
+
+**Example:2**
+
+For instance if you want to generate a list of numbers
+
+```py
+# Generating numbers
+numbers = [i for i in range(11)] # to generate numbers from 0 to 10
+print(numbers) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+
+# It is possible to do mathematical operations during iteration
+squares = [i * i for i in range(11)]
+print(squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
+
+# It is also possible to make a list of tuples
+numbers = [(i, i * i) for i in range(11)]
+print(numbers) # [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
+
+```
+
+**Example:2**
+
+List comprehension can be combined with if expression
+
+
+```py
+# Generating even numbers
+even_numbers = [i for i in range(21) if i % 2 == 0] # to generate even numbers list in range 0 to 21
+print(even_numbers) # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
+
+# Generating odd numbers
+odd_numbers = [i for i in range(21) if i % 2 != 0] # to generate odd numbers in range 0 to 21
+print(odd_numbers) # [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
+# Filter numbers: let's filter out positive even numbers from the list below
+numbers = [-8, -7, -3, -1, 0, 1, 3, 4, 5, 7, 6, 8, 10]
+positive_even_numbers = [i for i in range(21) if i % 2 == 0 and i > 0]
+print(positive_even_numbers) # [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
+
+# Flattening a three dimensional array
+list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
+flattened_list = [ number for row in list_of_lists for number in row]
+print(flattened_list) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
+```
+
+## Lambda Function
+
+Lambda function is a small anonymous function without a name. It can take any number of arguments, but can only have one expression. Lambda function is similar to anonymous functions in JavaScript. We need it when we want to write an anonymous function inside another function.
+
+### Creating a Lambda Function
+
+To create a lambda function we use _lambda_ keyword followed by a parameter(s), followed by an expression. See the syntax and the example below. Lambda function does not use return but it explicitly returns the expression.
+
+```py
+# syntax
+x = lambda param1, param2, param3: param1 + param2 + param2
+print(x(arg1, arg2, arg3))
+```
+
+**Example:**
+
+```py
+# Named function
+def add_two_nums(a, b):
+ return a + b
+
+print(add_two_nums(2, 3)) # 5
+# Lets change the above function to a lambda function
+add_two_nums = lambda a, b: a + b
+print(add_two_nums(2,3)) # 5
+
+# Self invoking lambda function
+(lambda a, b: a + b)(2,3) # 5 - need to encapsulate it in print() to see the result in the console
+
+square = lambda x : x ** 2
+print(square(3)) # 9
+cube = lambda x : x ** 3
+print(cube(3)) # 27
+
+# Multiple variables
+multiple_variable = lambda a, b, c: a ** 2 - 3 * b + 4 * c
+print(multiple_variable(5, 5, 3)) # 22
+```
+
+### Lambda Function Inside Another Function
+
+Using a lambda function inside another function.
+
+```py
+def power(x):
+ return lambda n : x ** n
+
+cube = power(2)(3) # function power now need 2 arguments to run, in separate rounded brackets
+print(cube) # 8
+two_power_of_five = power(2)(5)
+print(two_power_of_five) # 32
+```
+
+🌕 Keep up the good work. Keep the momentum going, the sky is the limit! You have just completed day 13 challenges and you are 13 steps a head in to your way to greatness. Now do some exercises for your brain and muscles.
+
+## 💻 Exercises: Day 13
+
+1. Filter only negative and zero in the list using list comprehension
+ ```py
+ numbers = [-4, -3, -2, -1, 0, 2, 4, 6]
+ ```
+2. Flatten the following list of lists of lists to a one dimensional list :
+
+ ```py
+ list_of_lists =[[[1, 2, 3]], [[4, 5, 6]], [[7, 8, 9]]]
+
+ output
+ [1, 2, 3, 4, 5, 6, 7, 8, 9]
+ ```
+
+3. Using list comprehension create the following list of tuples:
+ ```py
+ [(0, 1, 0, 0, 0, 0, 0),
+ (1, 1, 1, 1, 1, 1, 1),
+ (2, 1, 2, 4, 8, 16, 32),
+ (3, 1, 3, 9, 27, 81, 243),
+ (4, 1, 4, 16, 64, 256, 1024),
+ (5, 1, 5, 25, 125, 625, 3125),
+ (6, 1, 6, 36, 216, 1296, 7776),
+ (7, 1, 7, 49, 343, 2401, 16807),
+ (8, 1, 8, 64, 512, 4096, 32768),
+ (9, 1, 9, 81, 729, 6561, 59049),
+ (10, 1, 10, 100, 1000, 10000, 100000)]
+ ```
+4. Flatten the following list to a new list:
+ ```py
+ countries = [[('Finland', 'Helsinki')], [('Sweden', 'Stockholm')], [('Norway', 'Oslo')]]
+ output:
+ [['FINLAND','FIN', 'HELSINKI'], ['SWEDEN', 'SWE', 'STOCKHOLM'], ['NORWAY', 'NOR', 'OSLO']]
+ ```
+5. Change the following list to a list of dictionaries:
+ ```py
+ countries = [[('Finland', 'Helsinki')], [('Sweden', 'Stockholm')], [('Norway', 'Oslo')]]
+ output:
+ [{'country': 'FINLAND', 'city': 'HELSINKI'},
+ {'country': 'SWEDEN', 'city': 'STOCKHOLM'},
+ {'country': 'NORWAY', 'city': 'OSLO'}]
+ ```
+6. Change the following list of lists to a list of concatenated strings:
+ ```py
+ names = [[('Asabeneh', 'Yetayeh')], [('David', 'Smith')], [('Donald', 'Trump')], [('Bill', 'Gates')]]
+ output
+ ['Asabeneh Yetaeyeh', 'David Smith', 'Donald Trump', 'Bill Gates']
+ ```
+7. Write a lambda function which can solve a slope or y-intercept of linear functions.
+
+🎉 CONGRATULATIONS ! 🎉
+
+[<< Day 12](../12_Day_Modules/12_modules.md) | [Day 14>>](../14_Day_Higher_order_functions/14_higher_order_functions.md)
diff --git a/13_list_comprehension/index.html b/13_list_comprehension/index.html
new file mode 100644
index 0000000..d55fc40
--- /dev/null
+++ b/13_list_comprehension/index.html
@@ -0,0 +1,1405 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 13 list comprehension - 30天入门Python全栈开发
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
List comprehension in Python is a compact way of creating a list from a sequence. It is a short way to create a new list. List comprehension is considerably faster than processing a list using the for loop.
+
# syntax
+[iforiiniterableifexpression]
+
+
Example:1
+
For instance if you want to change a string to a list of characters. You can use a couple of methods. Let's see some of them:
+
# One way
+language='Python'
+lst=list(language)# changing the string to list
+print(type(lst))# list
+print(lst)# ['P', 'y', 't', 'h', 'o', 'n']
+
+# Second way: list comprehension
+lst=[iforiinlanguage]
+print(type(lst))# list
+print(lst)# ['P', 'y', 't', 'h', 'o', 'n']
+
+
Example:2
+
For instance if you want to generate a list of numbers
+
# Generating numbers
+numbers=[iforiinrange(11)]# to generate numbers from 0 to 10
+print(numbers)# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+
+# It is possible to do mathematical operations during iteration
+squares=[i*iforiinrange(11)]
+print(squares)# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
+
+# It is also possible to make a list of tuples
+numbers=[(i,i*i)foriinrange(11)]
+print(numbers)# [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
+
+
Example:2
+
List comprehension can be combined with if expression
+
# Generating even numbers
+even_numbers=[iforiinrange(21)ifi%2==0]# to generate even numbers list in range 0 to 21
+print(even_numbers)# [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
+
+# Generating odd numbers
+odd_numbers=[iforiinrange(21)ifi%2!=0]# to generate odd numbers in range 0 to 21
+print(odd_numbers)# [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
+# Filter numbers: let's filter out positive even numbers from the list below
+numbers=[-8,-7,-3,-1,0,1,3,4,5,7,6,8,10]
+positive_even_numbers=[iforiinrange(21)ifi%2==0andi>0]
+print(positive_even_numbers)# [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
+
+# Flattening a three dimensional array
+list_of_lists=[[1,2,3],[4,5,6],[7,8,9]]
+flattened_list=[numberforrowinlist_of_listsfornumberinrow]
+print(flattened_list)# [1, 2, 3, 4, 5, 6, 7, 8, 9]
+
Lambda function is a small anonymous function without a name. It can take any number of arguments, but can only have one expression. Lambda function is similar to anonymous functions in JavaScript. We need it when we want to write an anonymous function inside another function.
To create a lambda function we use lambda keyword followed by a parameter(s), followed by an expression. See the syntax and the example below. Lambda function does not use return but it explicitly returns the expression.
# Named function
+defadd_two_nums(a,b):
+ returna+b
+
+print(add_two_nums(2,3))# 5
+# Lets change the above function to a lambda function
+add_two_nums=lambdaa,b:a+b
+print(add_two_nums(2,3))# 5
+
+# Self invoking lambda function
+(lambdaa,b:a+b)(2,3)# 5 - need to encapsulate it in print() to see the result in the console
+
+square=lambdax:x**2
+print(square(3))# 9
+cube=lambdax:x**3
+print(cube(3))# 27
+
+# Multiple variables
+multiple_variable=lambdaa,b,c:a**2-3*b+4*c
+print(multiple_variable(5,5,3))# 22
+
defpower(x):
+ returnlambdan:x**n
+
+cube=power(2)(3)# function power now need 2 arguments to run, in separate rounded brackets
+print(cube)# 8
+two_power_of_five=power(2)(5)
+print(two_power_of_five)# 32
+
+
🌕 Keep up the good work. Keep the momentum going, the sky is the limit! You have just completed day 13 challenges and you are 13 steps a head in to your way to greatness. Now do some exercises for your brain and muscles.
+
+
+[<< Day 13](../13_Day_List_comprehension/13_list_comprehension.md) | [Day 15>>](../15_Day_Python_type_errors/15_python_type_errors.md)
+
+![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
+- [📘 Day 14](#-day-14)
+ - [Higher Order Functions](#higher-order-functions)
+ - [Function as a Parameter](#function-as-a-parameter)
+ - [Function as a Return Value](#function-as-a-return-value)
+ - [Python Closures](#python-closures)
+ - [Python Decorators](#python-decorators)
+ - [Creating Decorators](#creating-decorators)
+ - [Applying Multiple Decorators to a Single Function](#applying-multiple-decorators-to-a-single-function)
+ - [Accepting Parameters in Decorator Functions](#accepting-parameters-in-decorator-functions)
+ - [Built-in Higher Order Functions](#built-in-higher-order-functions)
+ - [Python - Map Function](#python---map-function)
+ - [Python - Filter Function](#python---filter-function)
+ - [Python - Reduce Function](#python---reduce-function)
+ - [💻 Exercises: Day 14](#-exercises-day-14)
+ - [Exercises: Level 1](#exercises-level-1)
+ - [Exercises: Level 2](#exercises-level-2)
+ - [Exercises: Level 3](#exercises-level-3)
+
+# 📘 Day 14
+
+## Higher Order Functions
+
+In Python functions are treated as first class citizens, allowing you to perform the following operations on functions:
+
+- A function can take one or more functions as parameters
+- A function can be returned as a result of another function
+- A function can be modified
+- A function can be assigned to a variable
+
+In this section, we will cover:
+
+1. Handling functions as parameters
+2. Returning functions as return value from another functions
+3. Using Python closures and decorators
+
+### Function as a Parameter
+
+```py
+def sum_numbers(nums): # normal function
+ return sum(nums) # a sad function abusing the built-in sum function :<
+
+def higher_order_function(f, lst): # function as a parameter
+ summation = f(lst)
+ return summation
+result = higher_order_function(sum_numbers, [1, 2, 3, 4, 5])
+print(result) # 15
+```
+
+### Function as a Return Value
+
+```py
+def square(x): # a square function
+ return x ** 2
+
+def cube(x): # a cube function
+ return x ** 3
+
+def absolute(x): # an absolute value function
+ if x >= 0:
+ return x
+ else:
+ return -(x)
+
+def higher_order_function(type): # a higher order function returning a function
+ if type == 'square':
+ return square
+ elif type == 'cube':
+ return cube
+ elif type == 'absolute':
+ return absolute
+
+result = higher_order_function('square')
+print(result(3)) # 9
+result = higher_order_function('cube')
+print(result(3)) # 27
+result = higher_order_function('absolute')
+print(result(-3)) # 3
+```
+
+You can see from the above example that the higher order function is returning different functions depending on the passed parameter
+
+## Python Closures
+
+Python allows a nested function to access the outer scope of the enclosing function. This is is known as a Closure. Let us have a look at how closures work in Python. In Python, closure is created by nesting a function inside another encapsulating function and then returning the inner function. See the example below.
+
+**Example:**
+
+```py
+def add_ten():
+ ten = 10
+ def add(num):
+ return num + ten
+ return add
+
+closure_result = add_ten()
+print(closure_result(5)) # 15
+print(closure_result(10)) # 20
+```
+
+## Python Decorators
+
+A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure. Decorators are usually called before the definition of a function you want to decorate.
+
+### Creating Decorators
+
+To create a decorator function, we need an outer function with an inner wrapper function.
+
+**Example:**
+
+```py
+# Normal function
+def greeting():
+ return 'Welcome to Python'
+def uppercase_decorator(function):
+ def wrapper():
+ func = function()
+ make_uppercase = func.upper()
+ return make_uppercase
+ return wrapper
+g = uppercase_decorator(greeting)
+print(g()) # WELCOME TO PYTHON
+
+## Let us implement the example above with a decorator
+
+'''This decorator function is a higher order function
+that takes a function as a parameter'''
+def uppercase_decorator(function):
+ def wrapper():
+ func = function()
+ make_uppercase = func.upper()
+ return make_uppercase
+ return wrapper
+@uppercase_decorator
+def greeting():
+ return 'Welcome to Python'
+print(greeting()) # WELCOME TO PYTHON
+
+```
+
+### Applying Multiple Decorators to a Single Function
+
+```py
+
+'''These decorator functions are higher order functions
+that take functions as parameters'''
+
+# First Decorator
+def uppercase_decorator(function):
+ def wrapper():
+ func = function()
+ make_uppercase = func.upper()
+ return make_uppercase
+ return wrapper
+
+# Second decorator
+def split_string_decorator(function):
+ def wrapper():
+ func = function()
+ splitted_string = func.split()
+ return splitted_string
+
+ return wrapper
+
+@split_string_decorator
+@uppercase_decorator # order with decorators is important in this case - .upper() function does not work with lists
+def greeting():
+ return 'Welcome to Python'
+print(greeting()) # WELCOME TO PYTHON
+```
+
+### Accepting Parameters in Decorator Functions
+
+Most of the time we need our functions to take parameters, so we might need to define a decorator that accepts parameters.
+
+```py
+def decorator_with_parameters(function):
+ def wrapper_accepting_parameters(para1, para2, para3):
+ function(para1, para2, para3)
+ print("I live in {}".format(para3))
+ return wrapper_accepting_parameters
+
+@decorator_with_parameters
+def print_full_name(first_name, last_name, country):
+ print("I am {} {}. I love to teach.".format(
+ first_name, last_name, country))
+
+print_full_name("Asabeneh", "Yetayeh",'Finland')
+```
+
+## Built-in Higher Order Functions
+
+Some of the built-in higher order functions that we cover in this part are _map()_, _filter_, and _reduce_.
+Lambda function can be passed as a parameter and the best use case of lambda functions is in functions like map, filter and reduce.
+
+### Python - Map Function
+
+The map() function is a built-in function that takes a function and iterable as parameters.
+
+```py
+ # syntax
+ map(function, iterable)
+```
+
+**Example:1**
+
+```py
+numbers = [1, 2, 3, 4, 5] # iterable
+def square(x):
+ return x ** 2
+numbers_squared = map(square, numbers)
+print(list(numbers_squared)) # [1, 4, 9, 16, 25]
+# Lets apply it with a lambda function
+numbers_squared = map(lambda x : x ** 2, numbers)
+print(list(numbers_squared)) # [1, 4, 9, 16, 25]
+```
+
+**Example:2**
+
+```py
+numbers_str = ['1', '2', '3', '4', '5'] # iterable
+numbers_int = map(int, numbers_str)
+print(list(numbers_int)) # [1, 2, 3, 4, 5]
+```
+
+**Example:3**
+
+```py
+names = ['Asabeneh', 'Lidiya', 'Ermias', 'Abraham'] # iterable
+
+def change_to_upper(name):
+ return name.upper()
+
+names_upper_cased = map(change_to_upper, names)
+print(list(names_upper_cased)) # ['ASABENEH', 'LIDIYA', 'ERMIAS', 'ABRAHAM']
+
+# Let us apply it with a lambda function
+names_upper_cased = map(lambda name: name.upper(), names)
+print(list(names_upper_cased)) # ['ASABENEH', 'LIDIYA', 'ERMIAS', 'ABRAHAM']
+```
+
+What actually map does is iterating over a list. For instance, it changes the names to upper case and returns a new list.
+
+### Python - Filter Function
+
+The filter() function calls the specified function which returns boolean for each item of the specified iterable (list). It filters the items that satisfy the filtering criteria.
+
+```py
+ # syntax
+ filter(function, iterable)
+```
+
+**Example:1**
+
+```py
+# Lets filter only even nubers
+numbers = [1, 2, 3, 4, 5] # iterable
+
+def is_even(num):
+ if num % 2 == 0:
+ return True
+ return False
+
+even_numbers = filter(is_even, numbers)
+print(list(even_numbers)) # [2, 4]
+```
+
+**Example:2**
+
+```py
+numbers = [1, 2, 3, 4, 5] # iterable
+
+def is_odd(num):
+ if num % 2 != 0:
+ return True
+ return False
+
+odd_numbers = filter(is_odd, numbers)
+print(list(odd_numbers)) # [1, 3, 5]
+```
+
+```py
+# Filter long name
+names = ['Asabeneh', 'Lidiya', 'Ermias', 'Abraham'] # iterable
+def is_name_long(name):
+ if len(name) > 7:
+ return True
+ return False
+
+long_names = filter(is_name_long, names)
+print(list(long_names)) # ['Asabeneh']
+```
+
+### Python - Reduce Function
+
+The _reduce()_ function is defined in the functools module and we should import it from this module. Like map and filter it takes two parameters, a function and an iterable. However, it does not return another iterable, instead it returns a single value.
+**Example:1**
+
+```py
+numbers_str = ['1', '2', '3', '4', '5'] # iterable
+def add_two_nums(x, y):
+ return int(x) + int(y)
+
+total = reduce(add_two_nums, numbers_str)
+print(total) # 15
+```
+
+## 💻 Exercises: Day 14
+
+```py
+countries = ['Estonia', 'Finland', 'Sweden', 'Denmark', 'Norway', 'Iceland']
+names = ['Asabeneh', 'Lidiya', 'Ermias', 'Abraham']
+numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+```
+
+### Exercises: Level 1
+
+1. Explain the difference between map, filter, and reduce.
+2. Explain the difference between higher order function, closure and decorator
+3. Define a call function before map, filter or reduce, see examples.
+4. Use for loop to print each country in the countries list.
+5. Use for to print each name in the names list.
+6. Use for to print each number in the numbers list.
+
+### Exercises: Level 2
+
+1. Use map to create a new list by changing each country to uppercase in the countries list
+1. Use map to create a new list by changing each number to its square in the numbers list
+1. Use map to change each name to uppercase in the names list
+1. Use filter to filter out countries containing 'land'.
+1. Use filter to filter out countries having exactly six characters.
+1. Use filter to filter out countries containing six letters and more in the country list.
+1. Use filter to filter out countries starting with an 'E'
+1. Chain two or more list iterators (eg. arr.map(callback).filter(callback).reduce(callback))
+1. Declare a function called get_string_lists which takes a list as a parameter and then returns a list containing only string items.
+1. Use reduce to sum all the numbers in the numbers list.
+1. Use reduce to concatenate all the countries and to produce this sentence: Estonia, Finland, Sweden, Denmark, Norway, and Iceland are north European countries
+1. Declare a function called categorize_countries that returns a list of countries with some common pattern (you can find the [countries list](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/data/countries.py) in this repository as countries.js(eg 'land', 'ia', 'island', 'stan')).
+1. Create a function returning a dictionary, where keys stand for starting letters of countries and values are the number of country names starting with that letter.
+2. Declare a get_first_ten_countries function - it returns a list of first ten countries from the countries.js list in the data folder.
+1. Declare a get_last_ten_countries function that returns the last ten countries in the countries list.
+
+### Exercises: Level 3
+
+1. Use the countries_data.py (https://github.com/Asabeneh/30-Days-Of-Python/blob/master/data/countries-data.py) file and follow the tasks below:
+ - Sort countries by name, by capital, by population
+ - Sort out the ten most spoken languages by location.
+ - Sort out the ten most populated countries.
+
+🎉 CONGRATULATIONS ! 🎉
+
+[<< Day 13](../13_Day_List_comprehension/13_list_comprehension.md) | [Day 15>>](../15_Day_Python_type_errors/15_python_type_errors.md)
\ No newline at end of file
diff --git a/14_higher_order_functions/index.html b/14_higher_order_functions/index.html
new file mode 100644
index 0000000..826249d
--- /dev/null
+++ b/14_higher_order_functions/index.html
@@ -0,0 +1,1772 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 14 higher order functions - 30天入门Python全栈开发
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
defsum_numbers(nums):# normal function
+ returnsum(nums)# a sad function abusing the built-in sum function :<
+
+defhigher_order_function(f,lst):# function as a parameter
+ summation=f(lst)
+ returnsummation
+result=higher_order_function(sum_numbers,[1,2,3,4,5])
+print(result)# 15
+
defsquare(x):# a square function
+ returnx**2
+
+defcube(x):# a cube function
+ returnx**3
+
+defabsolute(x):# an absolute value function
+ ifx>=0:
+ returnx
+ else:
+ return-(x)
+
+defhigher_order_function(type):# a higher order function returning a function
+ iftype=='square':
+ returnsquare
+ eliftype=='cube':
+ returncube
+ eliftype=='absolute':
+ returnabsolute
+
+result=higher_order_function('square')
+print(result(3))# 9
+result=higher_order_function('cube')
+print(result(3))# 27
+result=higher_order_function('absolute')
+print(result(-3))# 3
+
+
You can see from the above example that the higher order function is returning different functions depending on the passed parameter
Python allows a nested function to access the outer scope of the enclosing function. This is is known as a Closure. Let us have a look at how closures work in Python. In Python, closure is created by nesting a function inside another encapsulating function and then returning the inner function. See the example below.
A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure. Decorators are usually called before the definition of a function you want to decorate.
To create a decorator function, we need an outer function with an inner wrapper function.
+
Example:
+
# Normal function
+defgreeting():
+ return'Welcome to Python'
+defuppercase_decorator(function):
+ defwrapper():
+ func=function()
+ make_uppercase=func.upper()
+ returnmake_uppercase
+ returnwrapper
+g=uppercase_decorator(greeting)
+print(g())# WELCOME TO PYTHON
+
+## Let us implement the example above with a decorator
+
+'''This decorator function is a higher order function
+that takes a function as a parameter'''
+defuppercase_decorator(function):
+ defwrapper():
+ func=function()
+ make_uppercase=func.upper()
+ returnmake_uppercase
+ returnwrapper
+@uppercase_decorator
+defgreeting():
+ return'Welcome to Python'
+print(greeting())# WELCOME TO PYTHON
+
+
Applying Multiple Decorators to a Single Function⚓︎
+
'''These decorator functions are higher order functions
+that take functions as parameters'''
+
+# First Decorator
+defuppercase_decorator(function):
+ defwrapper():
+ func=function()
+ make_uppercase=func.upper()
+ returnmake_uppercase
+ returnwrapper
+
+# Second decorator
+defsplit_string_decorator(function):
+ defwrapper():
+ func=function()
+ splitted_string=func.split()
+ returnsplitted_string
+
+ returnwrapper
+
+@split_string_decorator
+@uppercase_decorator# order with decorators is important in this case - .upper() function does not work with lists
+defgreeting():
+ return'Welcome to Python'
+print(greeting())# WELCOME TO PYTHON
+
Most of the time we need our functions to take parameters, so we might need to define a decorator that accepts parameters.
+
defdecorator_with_parameters(function):
+ defwrapper_accepting_parameters(para1,para2,para3):
+ function(para1,para2,para3)
+ print("I live in {}".format(para3))
+ returnwrapper_accepting_parameters
+
+@decorator_with_parameters
+defprint_full_name(first_name,last_name,country):
+ print("I am {}{}. I love to teach.".format(
+ first_name,last_name,country))
+
+print_full_name("Asabeneh","Yetayeh",'Finland')
+
Some of the built-in higher order functions that we cover in this part are map(), filter, and reduce.
+Lambda function can be passed as a parameter and the best use case of lambda functions is in functions like map, filter and reduce.
names=['Asabeneh','Lidiya','Ermias','Abraham']# iterable
+
+defchange_to_upper(name):
+ returnname.upper()
+
+names_upper_cased=map(change_to_upper,names)
+print(list(names_upper_cased))# ['ASABENEH', 'LIDIYA', 'ERMIAS', 'ABRAHAM']
+
+# Let us apply it with a lambda function
+names_upper_cased=map(lambdaname:name.upper(),names)
+print(list(names_upper_cased))# ['ASABENEH', 'LIDIYA', 'ERMIAS', 'ABRAHAM']
+
+
What actually map does is iterating over a list. For instance, it changes the names to upper case and returns a new list.
The filter() function calls the specified function which returns boolean for each item of the specified iterable (list). It filters the items that satisfy the filtering criteria.
The reduce() function is defined in the functools module and we should import it from this module. Like map and filter it takes two parameters, a function and an iterable. However, it does not return another iterable, instead it returns a single value.
+Example:1
Use map to create a new list by changing each country to uppercase in the countries list
+
Use map to create a new list by changing each number to its square in the numbers list
+
Use map to change each name to uppercase in the names list
+
Use filter to filter out countries containing 'land'.
+
Use filter to filter out countries having exactly six characters.
+
Use filter to filter out countries containing six letters and more in the country list.
+
Use filter to filter out countries starting with an 'E'
+
Chain two or more list iterators (eg. arr.map(callback).filter(callback).reduce(callback))
+
Declare a function called get_string_lists which takes a list as a parameter and then returns a list containing only string items.
+
Use reduce to sum all the numbers in the numbers list.
+
Use reduce to concatenate all the countries and to produce this sentence: Estonia, Finland, Sweden, Denmark, Norway, and Iceland are north European countries
+
Declare a function called categorize_countries that returns a list of countries with some common pattern (you can find the countries list in this repository as countries.js(eg 'land', 'ia', 'island', 'stan')).
+
Create a function returning a dictionary, where keys stand for starting letters of countries and values are the number of country names starting with that letter.
+
Declare a get_first_ten_countries function - it returns a list of first ten countries from the countries.js list in the data folder.
+
Declare a get_last_ten_countries function that returns the last ten countries in the countries list.
+
+
+[<< Day 14](../14_Day_Higher_order_functions/14_higher_order_functions.md) | [Day 16 >>](../16_Day_Python_date_time/16_python_datetime.md)
+
+![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
+- [📘 Day 15](#-day-15)
+ - [Python Error Types](#python-error-types)
+ - [SyntaxError](#syntaxerror)
+ - [NameError](#nameerror)
+ - [IndexError](#indexerror)
+ - [ModuleNotFoundError](#modulenotfounderror)
+ - [AttributeError](#attributeerror)
+ - [KeyError](#keyerror)
+ - [TypeError](#typeerror)
+ - [ImportError](#importerror)
+ - [ValueError](#valueerror)
+ - [ZeroDivisionError](#zerodivisionerror)
+ - [💻 Exercises: Day 15](#-exercises-day-15)
+
+# 📘 Day 15
+
+## Python Error Types
+
+When we write code it is common that we make a typo or some other common error. If our code fails to run, the Python interpreter will display a message, containing feedback with information on where the problem occurs and the type of an error. It will also sometimes gives us suggestions on a possible fix. Understanding different types of errors in programming languages will help us to debug our code quickly and also it makes us better at what we do.
+
+Let us see the most common error types one by one. First let us open our Python interactive shell. Go to your you computer terminal and write 'python'. The python interactive shell will be opened.
+
+### SyntaxError
+
+**Example 1: SyntaxError**
+
+```py
+asabeneh@Asabeneh:~$ python
+Python 3.9.6 (default, Jun 28 2021, 15:26:21)
+[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
+Type "help", "copyright", "credits" or "license" for more information.
+>>> print 'hello world'
+ File "", line 1
+ print 'hello world'
+ ^
+SyntaxError: Missing parentheses in call to 'print'. Did you mean print('hello world')?
+>>>
+```
+
+As you can see we made a syntax error because we forgot to enclose the string with parenthesis and Python already suggests the solution. Let us fix it.
+
+```py
+asabeneh@Asabeneh:~$ python
+Python 3.9.6 (default, Jun 28 2021, 15:26:21)
+[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
+Type "help", "copyright", "credits" or "license" for more information.
+>>> print 'hello world'
+ File "", line 1
+ print 'hello world'
+ ^
+SyntaxError: Missing parentheses in call to 'print'. Did you mean print('hello world')?
+>>> print('hello world')
+hello world
+>>>
+```
+
+The error was a _SyntaxError_. After the fix our code was executed without a hitch. Let see more error types.
+
+### NameError
+
+**Example 1: NameError**
+
+```py
+asabeneh@Asabeneh:~$ python
+Python 3.9.6 (default, Jun 28 2021, 15:26:21)
+[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
+Type "help", "copyright", "credits" or "license" for more information.
+>>> print(age)
+Traceback (most recent call last):
+ File "", line 1, in
+NameError: name 'age' is not defined
+>>>
+```
+
+As you can see from the message above, name age is not defined. Yes, it is true that we did not define an age variable but we were trying to print it out as if we had had declared it. Now, lets fix this by declaring it and assigning with a value.
+
+```py
+asabeneh@Asabeneh:~$ python
+Python 3.9.6 (default, Jun 28 2021, 15:26:21)
+[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
+Type "help", "copyright", "credits" or "license" for more information.
+>>> print(age)
+Traceback (most recent call last):
+ File "", line 1, in
+NameError: name 'age' is not defined
+>>> age = 25
+>>> print(age)
+25
+>>>
+```
+
+The type of error was a _NameError_. We debugged the error by defining the variable name.
+
+### IndexError
+
+**Example 1: IndexError**
+
+```py
+asabeneh@Asabeneh:~$ python
+Python 3.9.6 (default, Jun 28 2021, 15:26:21)
+[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
+Type "help", "copyright", "credits" or "license" for more information.
+>>> numbers = [1, 2, 3, 4, 5]
+>>> numbers[5]
+Traceback (most recent call last):
+ File "", line 1, in
+IndexError: list index out of range
+>>>
+```
+
+In the example above, Python raised an _IndexError_, because the list has only indexes from 0 to 4 , so it was out of range.
+
+### ModuleNotFoundError
+
+**Example 1: ModuleNotFoundError**
+
+```py
+asabeneh@Asabeneh:~$ python
+Python 3.9.6 (default, Jun 28 2021, 15:26:21)
+[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
+Type "help", "copyright", "credits" or "license" for more information.
+>>> import maths
+Traceback (most recent call last):
+ File "", line 1, in
+ModuleNotFoundError: No module named 'maths'
+>>>
+```
+
+In the example above, I added an extra s to math deliberately and _ModuleNotFoundError_ was raised. Lets fix it by removing the extra s from math.
+
+```py
+asabeneh@Asabeneh:~$ python
+Python 3.9.6 (default, Jun 28 2021, 15:26:21)
+[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
+Type "help", "copyright", "credits" or "license" for more information.
+>>> import maths
+Traceback (most recent call last):
+ File "", line 1, in
+ModuleNotFoundError: No module named 'maths'
+>>> import math
+>>>
+```
+
+We fixed it, so let's use some of the functions from the math module.
+
+### AttributeError
+
+**Example 1: AttributeError**
+
+```py
+asabeneh@Asabeneh:~$ python
+Python 3.9.6 (default, Jun 28 2021, 15:26:21)
+[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
+Type "help", "copyright", "credits" or "license" for more information.
+>>> import maths
+Traceback (most recent call last):
+ File "", line 1, in
+ModuleNotFoundError: No module named 'maths'
+>>> import math
+>>> math.PI
+Traceback (most recent call last):
+ File "", line 1, in
+AttributeError: module 'math' has no attribute 'PI'
+>>>
+```
+
+As you can see, I made a mistake again! Instead of pi, I tried to call a PI function from maths module. It raised an attribute error, it means, that the function does not exist in the module. Lets fix it by changing from PI to pi.
+
+```py
+asabeneh@Asabeneh:~$ python
+Python 3.9.6 (default, Jun 28 2021, 15:26:21)
+[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
+Type "help", "copyright", "credits" or "license" for more information.
+>>> import maths
+Traceback (most recent call last):
+ File "", line 1, in
+ModuleNotFoundError: No module named 'maths'
+>>> import math
+>>> math.PI
+Traceback (most recent call last):
+ File "", line 1, in
+AttributeError: module 'math' has no attribute 'PI'
+>>> math.pi
+3.141592653589793
+>>>
+```
+
+Now, when we call pi from the math module we got the result.
+
+### KeyError
+
+**Example 1: KeyError**
+
+```py
+asabeneh@Asabeneh:~$ python
+Python 3.9.6 (default, Jun 28 2021, 15:26:21)
+[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
+Type "help", "copyright", "credits" or "license" for more information.
+>>> users = {'name':'Asab', 'age':250, 'country':'Finland'}
+>>> users['name']
+'Asab'
+>>> users['county']
+Traceback (most recent call last):
+ File "", line 1, in
+KeyError: 'county'
+>>>
+```
+
+As you can see, there was a typo in the key used to get the dictionary value. so, this is a key error and the fix is quite straight forward. Let's do this!
+
+```py
+asabeneh@Asabeneh:~$ python
+Python 3.9.6 (default, Jun 28 2021, 15:26:21)
+[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
+Type "help", "copyright", "credits" or "license" for more information.
+>>> user = {'name':'Asab', 'age':250, 'country':'Finland'}
+>>> user['name']
+'Asab'
+>>> user['county']
+Traceback (most recent call last):
+ File "", line 1, in
+KeyError: 'county'
+>>> user['country']
+'Finland'
+>>>
+```
+
+We debugged the error, our code ran and we got the value.
+
+### TypeError
+
+**Example 1: TypeError**
+
+```py
+asabeneh@Asabeneh:~$ python
+Python 3.9.6 (default, Jun 28 2021, 15:26:21)
+[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
+Type "help", "copyright", "credits" or "license" for more information.
+>>> 4 + '3'
+Traceback (most recent call last):
+ File "", line 1, in
+TypeError: unsupported operand type(s) for +: 'int' and 'str'
+>>>
+```
+
+In the example above, a TypeError is raised because we cannot add a number to a string. First solution would be to convert the string to int or float. Another solution would be converting the number to a string (the result then would be '43'). Let us follow the first fix.
+
+```py
+asabeneh@Asabeneh:~$ python
+Python 3.9.6 (default, Jun 28 2021, 15:26:21)
+[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
+Type "help", "copyright", "credits" or "license" for more information.
+>>> 4 + '3'
+Traceback (most recent call last):
+ File "", line 1, in
+TypeError: unsupported operand type(s) for +: 'int' and 'str'
+>>> 4 + int('3')
+7
+>>> 4 + float('3')
+7.0
+>>>
+```
+
+Error removed and we got the result we expected.
+
+### ImportError
+
+**Example 1: TypeError**
+
+```py
+asabeneh@Asabeneh:~$ python
+Python 3.9.6 (default, Jun 28 2021, 15:26:21)
+[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
+Type "help", "copyright", "credits" or "license" for more information.
+>>> from math import power
+Traceback (most recent call last):
+ File "", line 1, in
+ImportError: cannot import name 'power' from 'math'
+>>>
+```
+
+There is no function called power in the math module, it goes with a different name: _pow_. Let's correct it:
+
+```py
+asabeneh@Asabeneh:~$ python
+Python 3.9.6 (default, Jun 28 2021, 15:26:21)
+[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
+Type "help", "copyright", "credits" or "license" for more information.
+>>> from math import power
+Traceback (most recent call last):
+ File "", line 1, in
+ImportError: cannot import name 'power' from 'math'
+>>> from math import pow
+>>> pow(2,3)
+8.0
+>>>
+```
+
+### ValueError
+
+```py
+asabeneh@Asabeneh:~$ python
+Python 3.9.6 (default, Jun 28 2021, 15:26:21)
+[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
+Type "help", "copyright", "credits" or "license" for more information.
+>>> int('12a')
+Traceback (most recent call last):
+ File "", line 1, in
+ValueError: invalid literal for int() with base 10: '12a'
+>>>
+```
+
+In this case we cannot change the given string to a number, because of the 'a' letter in it.
+
+### ZeroDivisionError
+
+```py
+asabeneh@Asabeneh:~$ python
+Python 3.9.6 (default, Jun 28 2021, 15:26:21)
+[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
+Type "help", "copyright", "credits" or "license" for more information.
+>>> 1/0
+Traceback (most recent call last):
+ File "", line 1, in
+ZeroDivisionError: division by zero
+>>>
+```
+
+We cannot divide a number by zero.
+
+We have covered some of the python error types, if you want to check more about it check the python documentation about python error types.
+If you are good at reading the error types then you will be able to fix your bugs fast and you will also become a better programmer.
+
+🌕 You are excelling. You made it to half way to your way to greatness. Now do some exercises for your brain and for your muscle.
+
+## 💻 Exercises: Day 15
+
+1. Open you python interactive shell and try all the examples covered in this section.
+
+🎉 CONGRATULATIONS ! 🎉
+
+[<< Day 14](../14_Day_Higher_order_functions/14_higher_order_functions.md) | [Day 16 >>](../16_Day_Python_date_time/16_python_datetime.md)
\ No newline at end of file
diff --git a/15_python_type_errors/index.html b/15_python_type_errors/index.html
new file mode 100644
index 0000000..1d25d77
--- /dev/null
+++ b/15_python_type_errors/index.html
@@ -0,0 +1,1646 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 15 python type errors - 30天入门Python全栈开发
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
When we write code it is common that we make a typo or some other common error. If our code fails to run, the Python interpreter will display a message, containing feedback with information on where the problem occurs and the type of an error. It will also sometimes gives us suggestions on a possible fix. Understanding different types of errors in programming languages will help us to debug our code quickly and also it makes us better at what we do.
+
Let us see the most common error types one by one. First let us open our Python interactive shell. Go to your you computer terminal and write 'python'. The python interactive shell will be opened.
As you can see we made a syntax error because we forgot to enclose the string with parenthesis and Python already suggests the solution. Let us fix it.
As you can see from the message above, name age is not defined. Yes, it is true that we did not define an age variable but we were trying to print it out as if we had had declared it. Now, lets fix this by declaring it and assigning with a value.
As you can see, I made a mistake again! Instead of pi, I tried to call a PI function from maths module. It raised an attribute error, it means, that the function does not exist in the module. Lets fix it by changing from PI to pi.
As you can see, there was a typo in the key used to get the dictionary value. so, this is a key error and the fix is quite straight forward. Let's do this!
In the example above, a TypeError is raised because we cannot add a number to a string. First solution would be to convert the string to int or float. Another solution would be converting the number to a string (the result then would be '43'). Let us follow the first fix.
We have covered some of the python error types, if you want to check more about it check the python documentation about python error types.
+If you are good at reading the error types then you will be able to fix your bugs fast and you will also become a better programmer.
+
🌕 You are excelling. You made it to half way to your way to greatness. Now do some exercises for your brain and for your muscle.
+
+[<< Day 15](../15_Day_Python_type_errors/15_python_type_errors.md) | [Day 17 >>](../17_Day_Exception_handling/17_exception_handling.md)
+
+![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
+- [📘 Day 16](#-day-16)
+ - [Python *datetime*](#python-datetime)
+ - [Getting *datetime* Information](#getting-datetime-information)
+ - [Formatting Date Output Using *strftime*](#formatting-date-output-using-strftime)
+ - [String to Time Using *strptime*](#string-to-time-using-strptime)
+ - [Using *date* from *datetime*](#using-date-from-datetime)
+ - [Time Objects to Represent Time](#time-objects-to-represent-time)
+ - [Difference Between Two Points in Time Using](#difference-between-two-points-in-time-using)
+ - [Difference Between Two Points in Time Using *timedelata*](#difference-between-two-points-in-time-using-timedelata)
+ - [💻 Exercises: Day 16](#-exercises-day-16)
+# 📘 Day 16
+
+## Python *datetime*
+
+Python has got _datetime_ module to handle date and time.
+
+```py
+import datetime
+print(dir(datetime))
+['MAXYEAR', 'MINYEAR', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'date', 'datetime', 'datetime_CAPI', 'sys', 'time', 'timedelta', 'timezone', 'tzinfo']
+```
+
+With dir or help built-in commands it is possible to know the available functions in a certain module. As you can see, in the datetime module there are many functions, but we will focus on _date_, _datetime_, _time_ and _timedelta_. Let se see them one by one.
+
+### Getting *datetime* Information
+
+```py
+from datetime import datetime
+now = datetime.now()
+print(now) # 2021-07-08 07:34:46.549883
+day = now.day # 8
+month = now.month # 7
+year = now.year # 2021
+hour = now.hour # 7
+minute = now.minute # 38
+second = now.second
+timestamp = now.timestamp()
+print(day, month, year, hour, minute)
+print('timestamp', timestamp)
+print(f'{day}/{month}/{year}, {hour}:{minute}') # 8/7/2021, 7:38
+```
+
+Timestamp or Unix timestamp is the number of seconds elapsed from 1st of January 1970 UTC.
+
+### Formatting Date Output Using *strftime*
+
+```py
+from datetime import datetime
+new_year = datetime(2020, 1, 1)
+print(new_year) # 2020-01-01 00:00:00
+day = new_year.day
+month = new_year.month
+year = new_year.year
+hour = new_year.hour
+minute = new_year.minute
+second = new_year.second
+print(day, month, year, hour, minute) #1 1 2020 0 0
+print(f'{day}/{month}/{year}, {hour}:{minute}') # 1/1/2020, 0:0
+
+```
+
+Formatting date time using *strftime* method and the documentation can be found [here](https://strftime.org/).
+
+```py
+from datetime import datetime
+# current date and time
+now = datetime.now()
+t = now.strftime("%H:%M:%S")
+print("time:", t)
+time_one = now.strftime("%m/%d/%Y, %H:%M:%S")
+# mm/dd/YY H:M:S format
+print("time one:", time_one)
+time_two = now.strftime("%d/%m/%Y, %H:%M:%S")
+# dd/mm/YY H:M:S format
+print("time two:", time_two)
+```
+
+```sh
+time: 01:05:01
+time one: 12/05/2019, 01:05:01
+time two: 05/12/2019, 01:05:01
+```
+
+Here are all the _strftime_ symbols we use to format time. An example of all the formats for this module.
+
+![strftime](../images/strftime.png)
+
+### String to Time Using *strptime*
+Here is a [documentation](https://www.programiz.com/python-programming/datetime/strptimet) hat helps to understand the format.
+
+```py
+from datetime import datetime
+date_string = "5 December, 2019"
+print("date_string =", date_string)
+date_object = datetime.strptime(date_string, "%d %B, %Y")
+print("date_object =", date_object)
+```
+
+```sh
+date_string = 5 December, 2019
+date_object = 2019-12-05 00:00:00
+```
+
+### Using *date* from *datetime*
+
+```py
+from datetime import date
+d = date(2020, 1, 1)
+print(d)
+print('Current date:', d.today()) # 2019-12-05
+# date object of today's date
+today = date.today()
+print("Current year:", today.year) # 2019
+print("Current month:", today.month) # 12
+print("Current day:", today.day) # 5
+```
+
+### Time Objects to Represent Time
+
+```py
+from datetime import time
+# time(hour = 0, minute = 0, second = 0)
+a = time()
+print("a =", a)
+# time(hour, minute and second)
+b = time(10, 30, 50)
+print("b =", b)
+# time(hour, minute and second)
+c = time(hour=10, minute=30, second=50)
+print("c =", c)
+# time(hour, minute, second, microsecond)
+d = time(10, 30, 50, 200555)
+print("d =", d)
+```
+
+output
+a = 00:00:00
+b = 10:30:50
+c = 10:30:50
+d = 10:30:50.200555
+
+### Difference Between Two Points in Time Using
+
+```py
+today = date(year=2019, month=12, day=5)
+new_year = date(year=2020, month=1, day=1)
+time_left_for_newyear = new_year - today
+# Time left for new year: 27 days, 0:00:00
+print('Time left for new year: ', time_left_for_newyear)
+
+t1 = datetime(year = 2019, month = 12, day = 5, hour = 0, minute = 59, second = 0)
+t2 = datetime(year = 2020, month = 1, day = 1, hour = 0, minute = 0, second = 0)
+diff = t2 - t1
+print('Time left for new year:', diff) # Time left for new year: 26 days, 23: 01: 00
+```
+
+### Difference Between Two Points in Time Using *timedelata*
+
+```py
+from datetime import timedelta
+t1 = timedelta(weeks=12, days=10, hours=4, seconds=20)
+t2 = timedelta(days=7, hours=5, minutes=3, seconds=30)
+t3 = t1 - t2
+print("t3 =", t3)
+```
+
+```sh
+ date_string = 5 December, 2019
+ date_object = 2019-12-05 00:00:00
+ t3 = 86 days, 22:56:50
+```
+
+🌕 You are an extraordinary. You are 16 steps a head to your way to greatness. Now do some exercises for your brain and muscles.
+
+## 💻 Exercises: Day 16
+
+1. Get the current day, month, year, hour, minute and timestamp from datetime module
+1. Format the current date using this format: "%m/%d/%Y, %H:%M:%S")
+1. Today is 5 December, 2019. Change this time string to time.
+1. Calculate the time difference between now and new year.
+1. Calculate the time difference between 1 January 1970 and now.
+1. Think, what can you use the datetime module for? Examples:
+ - Time series analysis
+ - To get a timestamp of any activities in an application
+ - Adding posts on a blog
+
+🎉 CONGRATULATIONS ! 🎉
+
+[<< Day 15](../15_Day_Python_type_errors/15_python_type_errors.md) | [Day 17 >>](../17_Day_Exception_handling/17_exception_handling.md)
\ No newline at end of file
diff --git a/16_python_datetime/index.html b/16_python_datetime/index.html
new file mode 100644
index 0000000..60b7192
--- /dev/null
+++ b/16_python_datetime/index.html
@@ -0,0 +1,1473 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 16 python datetime - 30天入门Python全栈开发
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
With dir or help built-in commands it is possible to know the available functions in a certain module. As you can see, in the datetime module there are many functions, but we will focus on date, datetime, time and timedelta. Let se see them one by one.
Formatting date time using strftime method and the documentation can be found here.
+
fromdatetimeimportdatetime
+# current date and time
+now=datetime.now()
+t=now.strftime("%H:%M:%S")
+print("time:",t)
+time_one=now.strftime("%m/%d/%Y, %H:%M:%S")
+# mm/dd/YY H:M:S format
+print("time one:",time_one)
+time_two=now.strftime("%d/%m/%Y, %H:%M:%S")
+# dd/mm/YY H:M:S format
+print("time two:",time_two)
+
today=date(year=2019,month=12,day=5)
+new_year=date(year=2020,month=1,day=1)
+time_left_for_newyear=new_year-today
+# Time left for new year: 27 days, 0:00:00
+print('Time left for new year: ',time_left_for_newyear)
+
+t1=datetime(year=2019,month=12,day=5,hour=0,minute=59,second=0)
+t2=datetime(year=2020,month=1,day=1,hour=0,minute=0,second=0)
+diff=t2-t1
+print('Time left for new year:',diff)# Time left for new year: 26 days, 23: 01: 00
+
+
Difference Between Two Points in Time Using timedelata⚓︎
+
+[<< Day 16](../16_Day_Python_date_time/16_python_datetime.md) | [Day 18 >>](../18_Day_Regular_expressions/18_regular_expressions.md)
+
+![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
+
+- [📘 Day 17](#-day-17)
+ - [Exception Handling](#exception-handling)
+ - [Packing and Unpacking Arguments in Python](#packing-and-unpacking-arguments-in-python)
+ - [Unpacking](#unpacking)
+ - [Unpacking Lists](#unpacking-lists)
+ - [Unpacking Dictionaries](#unpacking-dictionaries)
+ - [Packing](#packing)
+ - [Packing Lists](#packing-lists)
+ - [Packing Dictionaries](#packing-dictionaries)
+ - [Spreading in Python](#spreading-in-python)
+ - [Enumerate](#enumerate)
+ - [Zip](#zip)
+ - [Exercises: Day 17](#exercises-day-17)
+
+# 📘 Day 17
+
+## Exception Handling
+
+Python uses _try_ and _except_ to handle errors gracefully. A graceful exit (or graceful handling) of errors is a simple programming idiom - a program detects a serious error condition and "exits gracefully", in a controlled manner as a result. Often the program prints a descriptive error message to a terminal or log as part of the graceful exit, this makes our application more robust. The cause of an exception is often external to the program itself. An example of exceptions could be an incorrect input, wrong file name, unable to find a file, a malfunctioning IO device. Graceful handling of errors prevents our applications from crashing.
+
+We have covered the different Python _error_ types in the previous section. If we use _try_ and _except_ in our program, then it will not raise errors in those blocks.
+
+![Try and Except](../images/try_except.png)
+
+```py
+try:
+ code in this block if things go well
+except:
+ code in this block run if things go wrong
+```
+
+**Example:**
+
+```py
+try:
+ print(10 + '5')
+except:
+ print('Something went wrong')
+```
+
+In the example above the second operand is a string. We could change it to float or int to add it with the number to make it work. But without any changes, the second block, _except_, will be executed.
+
+**Example:**
+
+```py
+try:
+ name = input('Enter your name:')
+ year_born = input('Year you were born:')
+ age = 2019 - year_born
+ print(f'You are {name}. And your age is {age}.')
+except:
+ print('Something went wrong')
+```
+
+```sh
+Something went wrong
+```
+
+In the above example, the exception block will run and we do not know exactly the problem. To analyze the problem, we can use the different error types with except.
+
+In the following example, it will handle the error and will also tell us the kind of error raised.
+
+```py
+try:
+ name = input('Enter your name:')
+ year_born = input('Year you were born:')
+ age = 2019 - year_born
+ print(f'You are {name}. And your age is {age}.')
+except TypeError:
+ print('Type error occured')
+except ValueError:
+ print('Value error occured')
+except ZeroDivisionError:
+ print('zero division error occured')
+```
+
+```sh
+Enter your name:Asabeneh
+Year you born:1920
+Type error occured
+```
+
+In the code above the output is going to be _TypeError_.
+Now, let's add an additional block:
+
+```py
+try:
+ name = input('Enter your name:')
+ year_born = input('Year you born:')
+ age = 2019 - int(year_born)
+ print('You are {name}. And your age is {age}.')
+except TypeError:
+ print('Type error occur')
+except ValueError:
+ print('Value error occur')
+except ZeroDivisionError:
+ print('zero division error occur')
+else:
+ print('I usually run with the try block')
+finally:
+ print('I alway run.')
+```
+
+```sh
+Enter your name:Asabeneh
+Year you born:1920
+You are Asabeneh. And your age is 99.
+I usually run with the try block
+I alway run.
+```
+
+It is also shorten the above code as follows:
+
+```py
+try:
+ name = input('Enter your name:')
+ year_born = input('Year you born:')
+ age = 2019 - int(year_born)
+ print('You are {name}. And your age is {age}.')
+except Exception as e:
+ print(e)
+
+```
+
+## Packing and Unpacking Arguments in Python
+
+We use two operators:
+
+- \* for tuples
+- \*\* for dictionaries
+
+Let us take as an example below. It takes only arguments but we have list. We can unpack the list and changes to argument.
+
+### Unpacking
+
+#### Unpacking Lists
+
+```py
+def sum_of_five_nums(a, b, c, d, e):
+ return a + b + c + d + e
+
+lst = [1, 2, 3, 4, 5]
+print(sum_of_five_nums(lst)) # TypeError: sum_of_five_nums() missing 4 required positional arguments: 'b', 'c', 'd', and 'e'
+```
+
+When we run the this code, it raises an error, because this function takes numbers (not a list) as arguments. Let us unpack/destructure the list.
+
+```py
+def sum_of_five_nums(a, b, c, d, e):
+ return a + b + c + d + e
+
+lst = [1, 2, 3, 4, 5]
+print(sum_of_five_nums(*lst)) # 15
+```
+
+We can also use unpacking in the range built-in function that expects a start and an end.
+
+```py
+numbers = range(2, 7) # normal call with separate arguments
+print(list(numbers)) # [2, 3, 4, 5, 6]
+args = [2, 7]
+numbers = range(*args) # call with arguments unpacked from a list
+print(numbers) # [2, 3, 4, 5,6]
+
+```
+
+A list or a tuple can also be unpacked like this:
+
+```py
+countries = ['Finland', 'Sweden', 'Norway', 'Denmark', 'Iceland']
+fin, sw, nor, *rest = countries
+print(fin, sw, nor, rest) # Finland Sweden Norway ['Denmark', 'Iceland']
+numbers = [1, 2, 3, 4, 5, 6, 7]
+one, *middle, last = numbers
+print(one, middle, last) # 1 [2, 3, 4, 5, 6] 7
+```
+
+#### Unpacking Dictionaries
+
+```py
+def unpacking_person_info(name, country, city, age):
+ return f'{name} lives in {country}, {city}. He is {age} year old.'
+dct = {'name':'Asabeneh', 'country':'Finland', 'city':'Helsinki', 'age':250}
+print(unpacking_person_info(**dct)) # Asabeneh lives in Finland, Helsinki. He is 250 years old.
+```
+
+### Packing
+
+Sometimes we never know how many arguments need to be passed to a python function. We can use the packing method to allow our function to take unlimited number or arbitrary number of arguments.
+
+### Packing Lists
+
+```py
+def sum_all(*args):
+ s = 0
+ for i in args:
+ s += i
+ return s
+print(sum_all(1, 2, 3)) # 6
+print(sum_all(1, 2, 3, 4, 5, 6, 7)) # 28
+```
+
+#### Packing Dictionaries
+
+```py
+def packing_person_info(**kwargs):
+ # check the type of kwargs and it is a dict type
+ # print(type(kwargs))
+ # Printing dictionary items
+ for key in kwargs:
+ print(f"{key} = {kwargs[key]}")
+ return kwargs
+
+print(packing_person_info(name="Asabeneh",
+ country="Finland", city="Helsinki", age=250))
+```
+
+```sh
+name = Asabeneh
+country = Finland
+city = Helsinki
+age = 250
+{'name': 'Asabeneh', 'country': 'Finland', 'city': 'Helsinki', 'age': 250}
+```
+
+## Spreading in Python
+
+Like in JavaScript, spreading is possible in Python. Let us check it in an example below:
+
+```py
+lst_one = [1, 2, 3]
+lst_two = [4, 5, 6, 7]
+lst = [0, *lst_one, *lst_two]
+print(lst) # [0, 1, 2, 3, 4, 5, 6, 7]
+country_lst_one = ['Finland', 'Sweden', 'Norway']
+country_lst_two = ['Denmark', 'Iceland']
+nordic_countries = [*country_lst_one, *country_lst_two]
+print(nordic_countries) # ['Finland', 'Sweden', 'Norway', 'Denmark', 'Iceland']
+```
+
+## Enumerate
+
+If we are interested in an index of a list, we use _enumerate_ built-in function to get the index of each item in the list.
+
+```py
+for index, item in enumerate([20, 30, 40]):
+ print(index, item)
+```
+
+```py
+for index, i in enumerate(countries):
+ print('hi')
+ if i == 'Finland':
+ print('The country {i} has been found at index {index}')
+```
+
+```sh
+The country Finland has been found at index 1.
+```
+
+## Zip
+
+Sometimes we would like to combine lists when looping through them. See the example below:
+
+```py
+fruits = ['banana', 'orange', 'mango', 'lemon', 'lime']
+vegetables = ['Tomato', 'Potato', 'Cabbage','Onion', 'Carrot']
+fruits_and_veges = []
+for f, v in zip(fruits, vegetables):
+ fruits_and_veges.append({'fruit':f, 'veg':v})
+
+print(fruits_and_veges)
+```
+
+```sh
+[{'fruit': 'banana', 'veg': 'Tomato'}, {'fruit': 'orange', 'veg': 'Potato'}, {'fruit': 'mango', 'veg': 'Cabbage'}, {'fruit': 'lemon', 'veg': 'Onion'}, {'fruit': 'lime', 'veg': 'Carrot'}]
+```
+
+🌕 You are determined. You are 17 steps a head to your way to greatness. Now do some exercises for your brain and muscles.
+
+## Exercises: Day 17
+
+1. names = ['Finland', 'Sweden', 'Norway','Denmark','Iceland', 'Estonia','Russia']. Unpack the first five countries and store them in a variable nordic_countries, store Estonia and Russia in es, and ru respectively.
+
+🎉 CONGRATULATIONS ! 🎉
+
+[<< Day 16](../16_Day_Python_date_time/16_python_datetime.md) | [Day 18 >>](../18_Day_Regular_expressions/18_regular_expressions.md)
diff --git a/17_exception_handling/index.html b/17_exception_handling/index.html
new file mode 100644
index 0000000..eac6ea5
--- /dev/null
+++ b/17_exception_handling/index.html
@@ -0,0 +1,1614 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 17 exception handling - 30天入门Python全栈开发
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Python uses try and except to handle errors gracefully. A graceful exit (or graceful handling) of errors is a simple programming idiom - a program detects a serious error condition and "exits gracefully", in a controlled manner as a result. Often the program prints a descriptive error message to a terminal or log as part of the graceful exit, this makes our application more robust. The cause of an exception is often external to the program itself. An example of exceptions could be an incorrect input, wrong file name, unable to find a file, a malfunctioning IO device. Graceful handling of errors prevents our applications from crashing.
+
We have covered the different Python error types in the previous section. If we use try and except in our program, then it will not raise errors in those blocks.
try:
+ print(10+'5')
+except:
+ print('Something went wrong')
+
+
In the example above the second operand is a string. We could change it to float or int to add it with the number to make it work. But without any changes, the second block, except, will be executed.
+
Example:
+
try:
+ name=input('Enter your name:')
+ year_born=input('Year you were born:')
+ age=2019-year_born
+ print(f'You are {name}. And your age is {age}.')
+except:
+ print('Something went wrong')
+
+
Somethingwentwrong
+
+
In the above example, the exception block will run and we do not know exactly the problem. To analyze the problem, we can use the different error types with except.
+
In the following example, it will handle the error and will also tell us the kind of error raised.
+
try:
+ name=input('Enter your name:')
+ year_born=input('Year you were born:')
+ age=2019-year_born
+ print(f'You are {name}. And your age is {age}.')
+exceptTypeError:
+ print('Type error occured')
+exceptValueError:
+ print('Value error occured')
+exceptZeroDivisionError:
+ print('zero division error occured')
+
In the code above the output is going to be TypeError.
+Now, let's add an additional block:
+
try:
+ name=input('Enter your name:')
+ year_born=input('Year you born:')
+ age=2019-int(year_born)
+ print('You are {name}. And your age is {age}.')
+exceptTypeError:
+ print('Type error occur')
+exceptValueError:
+ print('Value error occur')
+exceptZeroDivisionError:
+ print('zero division error occur')
+else:
+ print('I usually run with the try block')
+finally:
+ print('I alway run.')
+
try:
+ name=input('Enter your name:')
+ year_born=input('Year you born:')
+ age=2019-int(year_born)
+ print('You are {name}. And your age is {age}.')
+exceptExceptionase:
+ print(e)
+
We can also use unpacking in the range built-in function that expects a start and an end.
+
numbers=range(2,7)# normal call with separate arguments
+print(list(numbers))# [2, 3, 4, 5, 6]
+args=[2,7]
+numbers=range(*args)# call with arguments unpacked from a list
+print(numbers)# [2, 3, 4, 5,6]
+
+
A list or a tuple can also be unpacked like this:
+
countries=['Finland','Sweden','Norway','Denmark','Iceland']
+fin,sw,nor,*rest=countries
+print(fin,sw,nor,rest)# Finland Sweden Norway ['Denmark', 'Iceland']
+numbers=[1,2,3,4,5,6,7]
+one,*middle,last=numbers
+print(one,middle,last)# 1 [2, 3, 4, 5, 6] 7
+
defunpacking_person_info(name,country,city,age):
+ returnf'{name} lives in {country}, {city}. He is {age} year old.'
+dct={'name':'Asabeneh','country':'Finland','city':'Helsinki','age':250}
+print(unpacking_person_info(**dct))# Asabeneh lives in Finland, Helsinki. He is 250 years old.
+
Sometimes we never know how many arguments need to be passed to a python function. We can use the packing method to allow our function to take unlimited number or arbitrary number of arguments.
defpacking_person_info(**kwargs):
+ # check the type of kwargs and it is a dict type
+ # print(type(kwargs))
+ # Printing dictionary items
+ forkeyinkwargs:
+ print(f"{key} = {kwargs[key]}")
+ returnkwargs
+
+print(packing_person_info(name="Asabeneh",
+ country="Finland",city="Helsinki",age=250))
+
names = ['Finland', 'Sweden', 'Norway','Denmark','Iceland', 'Estonia','Russia']. Unpack the first five countries and store them in a variable nordic_countries, store Estonia and Russia in es, and ru respectively.
+
+
+[<< Day 17](../17_Day_Exception_handling/17_exception_handling.md) | [Day 19>>](../19_Day_File_handling/19_file_handling.md)
+
+![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
+
+- [📘 Day 18](#-day-18)
+ - [Regular Expressions](#regular-expressions)
+ - [The *re* Module](#the-re-module)
+ - [Methods in *re* Module](#methods-in-re-module)
+ - [Match](#match)
+ - [Search](#search)
+ - [Searching for All Matches Using *findall*](#searching-for-all-matches-using-findall)
+ - [Replacing a Substring](#replacing-a-substring)
+ - [Splitting Text Using RegEx Split](#splitting-text-using-regex-split)
+ - [Writing RegEx Patterns](#writing-regex-patterns)
+ - [Square Bracket](#square-bracket)
+ - [Escape character(\\) in RegEx](#escape-character-in-regex)
+ - [One or more times(+)](#one-or-more-times)
+ - [Period(.)](#period)
+ - [Zero or more times(\*)](#zero-or-more-times)
+ - [Zero or one time(?)](#zero-or-one-time)
+ - [Quantifier in RegEx](#quantifier-in-regex)
+ - [Cart ^](#cart-)
+ - [💻 Exercises: Day 18](#-exercises-day-18)
+ - [Exercises: Level 1](#exercises-level-1)
+ - [Exercises: Level 2](#exercises-level-2)
+ - [Exercises: Level 3](#exercises-level-3)
+
+# 📘 Day 18
+
+## Regular Expressions
+
+A regular expression or RegEx is a special text string that helps to find patterns in data. A RegEx can be used to check if some pattern exists in a different data type. To use RegEx in python first we should import the RegEx module which is called *re*.
+
+### The *re* Module
+
+After importing the module we can use it to detect or find patterns.
+
+```py
+import re
+```
+
+### Methods in *re* Module
+
+To find a pattern we use different set of *re* character sets that allows to search for a match in a string.
+
+- *re.match()*: searches only in the beginning of the first line of the string and returns matched objects if found, else returns None.
+- *re.search*: Returns a match object if there is one anywhere in the string, including multiline strings.
+- *re.findall*: Returns a list containing all matches
+- *re.split*: Takes a string, splits it at the match points, returns a list
+- *re.sub*: Replaces one or many matches within a string
+
+#### Match
+
+```py
+# syntac
+re.match(substring, string, re.I)
+# substring is a string or a pattern, string is the text we look for a pattern , re.I is case ignore
+```
+
+```py
+import re
+
+txt = 'I love to teach python and javaScript'
+# It returns an object with span, and match
+match = re.match('I love to teach', txt, re.I)
+print(match) #
+# We can get the starting and ending position of the match as tuple using span
+span = match.span()
+print(span) # (0, 15)
+# Lets find the start and stop position from the span
+start, end = span
+print(start, end) # 0, 15
+substring = txt[start:end]
+print(substring) # I love to teach
+```
+
+As you can see from the example above, the pattern we are looking for (or the substring we are looking for) is *I love to teach*. The match function returns an object **only** if the text starts with the pattern.
+
+```py
+import re
+
+txt = 'I love to teach python and javaScript'
+match = re.match('I like to teach', txt, re.I)
+print(match) # None
+```
+
+The string does not string with *I like to teach*, therefore there was no match and the match method returned None.
+
+#### Search
+
+```py
+# syntax
+re.match(substring, string, re.I)
+# substring is a pattern, string is the text we look for a pattern , re.I is case ignore flag
+```
+
+```py
+import re
+
+txt = '''Python is the most beautiful language that a human being has ever created.
+I recommend python for a first programming language'''
+
+# It returns an object with span and match
+match = re.search('first', txt, re.I)
+print(match) #
+# We can get the starting and ending position of the match as tuple using span
+span = match.span()
+print(span) # (100, 105)
+# Lets find the start and stop position from the span
+start, end = span
+print(start, end) # 100 105
+substring = txt[start:end]
+print(substring) # first
+```
+
+As you can see, search is much better than match because it can look for the pattern throughout the text. Search returns a match object with a first match that was found, otherwise it returns *None*. A much better *re* function is *findall*. This function checks for the pattern through the whole string and returns all the matches as a list.
+
+#### Searching for All Matches Using *findall*
+
+*findall()* returns all the matches as a list
+
+```py
+txt = '''Python is the most beautiful language that a human being has ever created.
+I recommend python for a first programming language'''
+
+# It return a list
+matches = re.findall('language', txt, re.I)
+print(matches) # ['language', 'language']
+```
+
+As you can see, the word *language* was found two times in the string. Let us practice some more.
+Now we will look for both Python and python words in the string:
+
+```py
+txt = '''Python is the most beautiful language that a human being has ever created.
+I recommend python for a first programming language'''
+
+# It returns list
+matches = re.findall('python', txt, re.I)
+print(matches) # ['Python', 'python']
+
+```
+
+Since we are using *re.I* both lowercase and uppercase letters are included. If we do not have the re.I flag, then we will have to write our pattern differently. Let us check it out:
+
+```py
+txt = '''Python is the most beautiful language that a human being has ever created.
+I recommend python for a first programming language'''
+
+matches = re.findall('Python|python', txt)
+print(matches) # ['Python', 'python']
+
+#
+matches = re.findall('[Pp]ython', txt)
+print(matches) # ['Python', 'python']
+
+```
+
+#### Replacing a Substring
+
+```py
+txt = '''Python is the most beautiful language that a human being has ever created.
+I recommend python for a first programming language'''
+
+match_replaced = re.sub('Python|python', 'JavaScript', txt, re.I)
+print(match_replaced) # JavaScript is the most beautiful language that a human being has ever created.
+# OR
+match_replaced = re.sub('[Pp]ython', 'JavaScript', txt, re.I)
+print(match_replaced) # JavaScript is the most beautiful language that a human being has ever created.
+```
+
+Let us add one more example. The following string is really hard to read unless we remove the % symbol. Replacing the % with an empty string will clean the text.
+
+```py
+
+txt = '''%I a%m te%%a%%che%r% a%n%d %% I l%o%ve te%ach%ing.
+T%he%re i%s n%o%th%ing as r%ewarding a%s e%duc%at%i%ng a%n%d e%m%p%ow%er%ing p%e%o%ple.
+I fo%und te%a%ching m%ore i%n%t%er%%es%ting t%h%an any other %jobs.
+D%o%es thi%s m%ot%iv%a%te %y%o%u to b%e a t%e%a%cher?'''
+
+matches = re.sub('%', '', txt)
+print(matches)
+```
+
+```sh
+I am teacher and I love teaching.
+There is nothing as rewarding as educating and empowering people.
+I found teaching more interesting than any other jobs. Does this motivate you to be a teacher?
+```
+
+## Splitting Text Using RegEx Split
+
+```py
+txt = '''I am teacher and I love teaching.
+There is nothing as rewarding as educating and empowering people.
+I found teaching more interesting than any other jobs.
+Does this motivate you to be a teacher?'''
+print(re.split('\n', txt)) # splitting using \n - end of line symbol
+```
+
+```sh
+['I am teacher and I love teaching.', 'There is nothing as rewarding as educating and empowering people.', 'I found teaching more interesting than any other jobs.', 'Does this motivate you to be a teacher?']
+```
+
+## Writing RegEx Patterns
+
+To declare a string variable we use a single or double quote. To declare RegEx variable *r''*.
+The following pattern only identifies apple with lowercase, to make it case insensitive either we should rewrite our pattern or we should add a flag.
+
+```py
+import re
+
+regex_pattern = r'apple'
+txt = 'Apple and banana are fruits. An old cliche says an apple a day a doctor way has been replaced by a banana a day keeps the doctor far far away. '
+matches = re.findall(regex_pattern, txt)
+print(matches) # ['apple']
+
+# To make case insensitive adding flag '
+matches = re.findall(regex_pattern, txt, re.I)
+print(matches) # ['Apple', 'apple']
+# or we can use a set of characters method
+regex_pattern = r'[Aa]pple' # this mean the first letter could be Apple or apple
+matches = re.findall(regex_pattern, txt)
+print(matches) # ['Apple', 'apple']
+
+```
+
+* []: A set of characters
+ - [a-c] means, a or b or c
+ - [a-z] means, any letter from a to z
+ - [A-Z] means, any character from A to Z
+ - [0-3] means, 0 or 1 or 2 or 3
+ - [0-9] means any number from 0 to 9
+ - [A-Za-z0-9] any single character, that is a to z, A to Z or 0 to 9
+- \\: uses to escape special characters
+ - \d means: match where the string contains digits (numbers from 0-9)
+ - \D means: match where the string does not contain digits
+- . : any character except new line character(\n)
+- ^: starts with
+ - r'^substring' eg r'^love', a sentence that starts with a word love
+ - r'[^abc] means not a, not b, not c.
+- $: ends with
+ - r'substring$' eg r'love$', sentence that ends with a word love
+- *: zero or more times
+ - r'[a]*' means a optional or it can occur many times.
+- +: one or more times
+ - r'[a]+' means at least once (or more)
+- ?: zero or one time
+ - r'[a]?' means zero times or once
+- {3}: Exactly 3 characters
+- {3,}: At least 3 characters
+- {3,8}: 3 to 8 characters
+- |: Either or
+ - r'apple|banana' means either apple or a banana
+- (): Capture and group
+
+![Regular Expression cheat sheet](../images/regex.png)
+
+Let us use examples to clarify the meta characters above
+
+### Square Bracket
+
+Let us use square bracket to include lower and upper case
+
+```py
+regex_pattern = r'[Aa]pple' # this square bracket mean either A or a
+txt = 'Apple and banana are fruits. An old cliche says an apple a day a doctor way has been replaced by a banana a day keeps the doctor far far away.'
+matches = re.findall(regex_pattern, txt)
+print(matches) # ['Apple', 'apple']
+```
+
+If we want to look for the banana, we write the pattern as follows:
+
+```py
+regex_pattern = r'[Aa]pple|[Bb]anana' # this square bracket means either A or a
+txt = 'Apple and banana are fruits. An old cliche says an apple a day a doctor way has been replaced by a banana a day keeps the doctor far far away.'
+matches = re.findall(regex_pattern, txt)
+print(matches) # ['Apple', 'banana', 'apple', 'banana']
+```
+
+Using the square bracket and or operator , we manage to extract Apple, apple, Banana and banana.
+
+### Escape character(\\) in RegEx
+
+```py
+regex_pattern = r'\d' # d is a special character which means digits
+txt = 'This regular expression example was made on December 6, 2019 and revised on July 8, 2021'
+matches = re.findall(regex_pattern, txt)
+print(matches) # ['6', '2', '0', '1', '9', '8', '2', '0', '2', '1'], this is not what we want
+```
+
+### One or more times(+)
+
+```py
+regex_pattern = r'\d+' # d is a special character which means digits, + mean one or more times
+txt = 'This regular expression example was made on December 6, 2019 and revised on July 8, 2021'
+matches = re.findall(regex_pattern, txt)
+print(matches) # ['6', '2019', '8', '2021'] - now, this is better!
+```
+
+### Period(.)
+
+```py
+regex_pattern = r'[a].' # this square bracket means a and . means any character except new line
+txt = '''Apple and banana are fruits'''
+matches = re.findall(regex_pattern, txt)
+print(matches) # ['an', 'an', 'an', 'a ', 'ar']
+
+regex_pattern = r'[a].+' # . any character, + any character one or more times
+matches = re.findall(regex_pattern, txt)
+print(matches) # ['and banana are fruits']
+```
+
+### Zero or more times(\*)
+
+Zero or many times. The pattern could may not occur or it can occur many times.
+
+```py
+regex_pattern = r'[a].*' # . any character, * any character zero or more times
+txt = '''Apple and banana are fruits'''
+matches = re.findall(regex_pattern, txt)
+print(matches) # ['and banana are fruits']
+```
+
+### Zero or one time(?)
+
+Zero or one time. The pattern may not occur or it may occur once.
+
+```py
+txt = '''I am not sure if there is a convention how to write the word e-mail.
+Some people write it as email others may write it as Email or E-mail.'''
+regex_pattern = r'[Ee]-?mail' # ? means here that '-' is optional
+matches = re.findall(regex_pattern, txt)
+print(matches) # ['e-mail', 'email', 'Email', 'E-mail']
+```
+
+### Quantifier in RegEx
+
+We can specify the length of the substring we are looking for in a text, using a curly bracket. Let us imagine, we are interested in a substring with a length of 4 characters:
+
+```py
+txt = 'This regular expression example was made on December 6, 2019 and revised on July 8, 2021'
+regex_pattern = r'\d{4}' # exactly four times
+matches = re.findall(regex_pattern, txt)
+print(matches) # ['2019', '2021']
+
+txt = 'This regular expression example was made on December 6, 2019 and revised on July 8, 2021'
+regex_pattern = r'\d{1, 4}' # 1 to 4
+matches = re.findall(regex_pattern, txt)
+print(matches) # ['6', '2019', '8', '2021']
+```
+
+### Cart ^
+
+- Starts with
+
+```py
+txt = 'This regular expression example was made on December 6, 2019 and revised on July 8, 2021'
+regex_pattern = r'^This' # ^ means starts with
+matches = re.findall(regex_pattern, txt)
+print(matches) # ['This']
+```
+
+- Negation
+
+```py
+txt = 'This regular expression example was made on December 6, 2019 and revised on July 8, 2021'
+regex_pattern = r'[^A-Za-z ]+' # ^ in set character means negation, not A to Z, not a to z, no space
+matches = re.findall(regex_pattern, txt)
+print(matches) # ['6,', '2019', '8', '2021']
+```
+
+## 💻 Exercises: Day 18
+
+### Exercises: Level 1
+
+ 1. What is the most frequent word in the following paragraph?
+
+```py
+ paragraph = 'I love teaching. If you do not love teaching what else can you love. I love Python if you do not love something which can give you all the capabilities to develop an application what else can you love.
+```
+
+```sh
+ [
+ (6, 'love'),
+ (5, 'you'),
+ (3, 'can'),
+ (2, 'what'),
+ (2, 'teaching'),
+ (2, 'not'),
+ (2, 'else'),
+ (2, 'do'),
+ (2, 'I'),
+ (1, 'which'),
+ (1, 'to'),
+ (1, 'the'),
+ (1, 'something'),
+ (1, 'if'),
+ (1, 'give'),
+ (1, 'develop'),
+ (1, 'capabilities'),
+ (1, 'application'),
+ (1, 'an'),
+ (1, 'all'),
+ (1, 'Python'),
+ (1, 'If')
+ ]
+```
+
+2. The position of some particles on the horizontal x-axis are -12, -4, -3 and -1 in the negative direction, 0 at origin, 4 and 8 in the positive direction. Extract these numbers from this whole text and find the distance between the two furthest particles.
+
+```py
+points = ['-12', '-4', '-3', '-1', '0', '4', '8']
+sorted_points = [-12, -4, -3, -1, -1, 0, 2, 4, 8]
+distance = 8 -(-12) # 20
+```
+
+### Exercises: Level 2
+
+1. Write a pattern which identifies if a string is a valid python variable
+
+ ```sh
+ is_valid_variable('first_name') # True
+ is_valid_variable('first-name') # False
+ is_valid_variable('1first_name') # False
+ is_valid_variable('firstname') # True
+ ```
+
+### Exercises: Level 3
+
+1. Clean the following text. After cleaning, count three most frequent words in the string.
+
+ ```py
+ sentence = '''%I $am@% a %tea@cher%, &and& I lo%#ve %tea@ching%;. There $is nothing; &as& mo@re rewarding as educa@ting &and& @emp%o@wering peo@ple. ;I found tea@ching m%o@re interesting tha@n any other %jo@bs. %Do@es thi%s mo@tivate yo@u to be a tea@cher!?'''
+
+ print(clean_text(sentence));
+ I am a teacher and I love teaching There is nothing as more rewarding as educating and empowering people I found teaching more interesting than any other jobs Does this motivate you to be a teacher
+ print(most_frequent_words(cleaned_text)) # [(3, 'I'), (2, 'teaching'), (2, 'teacher')]
+ ```
+
+🎉 CONGRATULATIONS ! 🎉
+
+[<< Day 17](../17_Day_Exception_handling/17_exception_handling.md) | [Day 19>>](../19_Day_File_handling/19_file_handling.md)
diff --git a/18_regular_expressions/index.html b/18_regular_expressions/index.html
new file mode 100644
index 0000000..65436b4
--- /dev/null
+++ b/18_regular_expressions/index.html
@@ -0,0 +1,1930 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 18 regular expressions - 30天入门Python全栈开发
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
A regular expression or RegEx is a special text string that helps to find patterns in data. A RegEx can be used to check if some pattern exists in a different data type. To use RegEx in python first we should import the RegEx module which is called re.
# syntac
+re.match(substring,string,re.I)
+# substring is a string or a pattern, string is the text we look for a pattern , re.I is case ignore
+
+
importre
+
+txt='I love to teach python and javaScript'
+# It returns an object with span, and match
+match=re.match('I love to teach',txt,re.I)
+print(match)# <re.Match object; span=(0, 15), match='I love to teach'>
+# We can get the starting and ending position of the match as tuple using span
+span=match.span()
+print(span)# (0, 15)
+# Lets find the start and stop position from the span
+start,end=span
+print(start,end)# 0, 15
+substring=txt[start:end]
+print(substring)# I love to teach
+
+
As you can see from the example above, the pattern we are looking for (or the substring we are looking for) is I love to teach. The match function returns an object only if the text starts with the pattern.
+
importre
+
+txt='I love to teach python and javaScript'
+match=re.match('I like to teach',txt,re.I)
+print(match)# None
+
+
The string does not string with I like to teach, therefore there was no match and the match method returned None.
# syntax
+re.match(substring,string,re.I)
+# substring is a pattern, string is the text we look for a pattern , re.I is case ignore flag
+
+
importre
+
+txt='''Python is the most beautiful language that a human being has ever created.
+I recommend python for a first programming language'''
+
+# It returns an object with span and match
+match=re.search('first',txt,re.I)
+print(match)# <re.Match object; span=(100, 105), match='first'>
+# We can get the starting and ending position of the match as tuple using span
+span=match.span()
+print(span)# (100, 105)
+# Lets find the start and stop position from the span
+start,end=span
+print(start,end)# 100 105
+substring=txt[start:end]
+print(substring)# first
+
+
As you can see, search is much better than match because it can look for the pattern throughout the text. Search returns a match object with a first match that was found, otherwise it returns None. A much better re function is findall. This function checks for the pattern through the whole string and returns all the matches as a list.
txt='''Python is the most beautiful language that a human being has ever created.
+I recommend python for a first programming language'''
+
+# It return a list
+matches=re.findall('language',txt,re.I)
+print(matches)# ['language', 'language']
+
+
As you can see, the word language was found two times in the string. Let us practice some more.
+Now we will look for both Python and python words in the string:
+
txt='''Python is the most beautiful language that a human being has ever created.
+I recommend python for a first programming language'''
+
+# It returns list
+matches=re.findall('python',txt,re.I)
+print(matches)# ['Python', 'python']
+
+
Since we are using re.I both lowercase and uppercase letters are included. If we do not have the re.I flag, then we will have to write our pattern differently. Let us check it out:
+
txt='''Python is the most beautiful language that a human being has ever created.
+I recommend python for a first programming language'''
+
+matches=re.findall('Python|python',txt)
+print(matches)# ['Python', 'python']
+
+#
+matches=re.findall('[Pp]ython',txt)
+print(matches)# ['Python', 'python']
+
txt='''Python is the most beautiful language that a human being has ever created.
+I recommend python for a first programming language'''
+
+match_replaced=re.sub('Python|python','JavaScript',txt,re.I)
+print(match_replaced)# JavaScript is the most beautiful language that a human being has ever created.
+# OR
+match_replaced=re.sub('[Pp]ython','JavaScript',txt,re.I)
+print(match_replaced)# JavaScript is the most beautiful language that a human being has ever created.
+
+
Let us add one more example. The following string is really hard to read unless we remove the % symbol. Replacing the % with an empty string will clean the text.
+
txt='''%I a%m te%%a%%che%r% a%n%d%% I l%o%ve te%ach%ing.
+T%he%re i%s n%o%th%ing as r%ewarding a%s e%duc%at%i%ng a%n%d e%m%p%ow%er%ing p%e%o%ple.
+I fo%und te%a%ching m%ore i%n%t%er%%es%ting t%h%an any other %jobs.
+D%o%es thi%s m%ot%iv%a%te %y%o%u to b%e a t%e%a%cher?'''
+
+matches=re.sub('%','',txt)
+print(matches)
+
txt='''I am teacher and I love teaching.
+There is nothing as rewarding as educating and empowering people.
+I found teaching more interesting than any other jobs.
+Does this motivate you to be a teacher?'''
+print(re.split('\n',txt))# splitting using \n - end of line symbol
+
+
['I am teacher and I love teaching.','There is nothing as rewarding as educating and empowering people.','I found teaching more interesting than any other jobs.','Does this motivate you to be a teacher?']
+
To declare a string variable we use a single or double quote. To declare RegEx variable r''.
+The following pattern only identifies apple with lowercase, to make it case insensitive either we should rewrite our pattern or we should add a flag.
+
importre
+
+regex_pattern=r'apple'
+txt='Apple and banana are fruits. An old cliche says an apple a day a doctor way has been replaced by a banana a day keeps the doctor far far away. '
+matches=re.findall(regex_pattern,txt)
+print(matches)# ['apple']
+
+# To make case insensitive adding flag '
+matches=re.findall(regex_pattern,txt,re.I)
+print(matches)# ['Apple', 'apple']
+# or we can use a set of characters method
+regex_pattern=r'[Aa]pple'# this mean the first letter could be Apple or apple
+matches=re.findall(regex_pattern,txt)
+print(matches)# ['Apple', 'apple']
+
+
+
[]: A set of characters
+
[a-c] means, a or b or c
+
[a-z] means, any letter from a to z
+
[A-Z] means, any character from A to Z
+
[0-3] means, 0 or 1 or 2 or 3
+
[0-9] means any number from 0 to 9
+
[A-Za-z0-9] any single character, that is a to z, A to Z or 0 to 9
+
\: uses to escape special characters
+
\d means: match where the string contains digits (numbers from 0-9)
+
\D means: match where the string does not contain digits
+
. : any character except new line character(\n)
+
^: starts with
+
r'^substring' eg r'^love', a sentence that starts with a word love
+
r'[^abc] means not a, not b, not c.
+
$: ends with
+
r'substring\(' eg r'love\)', sentence that ends with a word love
+
*: zero or more times
+
r'[a]*' means a optional or it can occur many times.
+
+: one or more times
+
r'[a]+' means at least once (or more)
+
?: zero or one time
+
r'[a]?' means zero times or once
+
{3}: Exactly 3 characters
+
{3,}: At least 3 characters
+
{3,8}: 3 to 8 characters
+
|: Either or
+
r'apple|banana' means either apple or a banana
+
(): Capture and group
+
+
+
Let us use examples to clarify the meta characters above
Let us use square bracket to include lower and upper case
+
regex_pattern=r'[Aa]pple'# this square bracket mean either A or a
+txt='Apple and banana are fruits. An old cliche says an apple a day a doctor way has been replaced by a banana a day keeps the doctor far far away.'
+matches=re.findall(regex_pattern,txt)
+print(matches)# ['Apple', 'apple']
+
+
If we want to look for the banana, we write the pattern as follows:
+
regex_pattern=r'[Aa]pple|[Bb]anana'# this square bracket means either A or a
+txt='Apple and banana are fruits. An old cliche says an apple a day a doctor way has been replaced by a banana a day keeps the doctor far far away.'
+matches=re.findall(regex_pattern,txt)
+print(matches)# ['Apple', 'banana', 'apple', 'banana']
+
+
Using the square bracket and or operator , we manage to extract Apple, apple, Banana and banana.
regex_pattern=r'\d'# d is a special character which means digits
+txt='This regular expression example was made on December 6, 2019 and revised on July 8, 2021'
+matches=re.findall(regex_pattern,txt)
+print(matches)# ['6', '2', '0', '1', '9', '8', '2', '0', '2', '1'], this is not what we want
+
regex_pattern=r'\d+'# d is a special character which means digits, + mean one or more times
+txt='This regular expression example was made on December 6, 2019 and revised on July 8, 2021'
+matches=re.findall(regex_pattern,txt)
+print(matches)# ['6', '2019', '8', '2021'] - now, this is better!
+
regex_pattern=r'[a].'# this square bracket means a and . means any character except new line
+txt='''Apple and banana are fruits'''
+matches=re.findall(regex_pattern,txt)
+print(matches)# ['an', 'an', 'an', 'a ', 'ar']
+
+regex_pattern=r'[a].+'# . any character, + any character one or more times
+matches=re.findall(regex_pattern,txt)
+print(matches)# ['and banana are fruits']
+
Zero or many times. The pattern could may not occur or it can occur many times.
+
regex_pattern=r'[a].*'# . any character, * any character zero or more times
+txt='''Apple and banana are fruits'''
+matches=re.findall(regex_pattern,txt)
+print(matches)# ['and banana are fruits']
+
Zero or one time. The pattern may not occur or it may occur once.
+
txt='''I am not sure if there is a convention how to write the word e-mail.
+Some people write it as email others may write it as Email or E-mail.'''
+regex_pattern=r'[Ee]-?mail'# ? means here that '-' is optional
+matches=re.findall(regex_pattern,txt)
+print(matches)# ['e-mail', 'email', 'Email', 'E-mail']
+
We can specify the length of the substring we are looking for in a text, using a curly bracket. Let us imagine, we are interested in a substring with a length of 4 characters:
+
txt='This regular expression example was made on December 6, 2019 and revised on July 8, 2021'
+regex_pattern=r'\d{4}'# exactly four times
+matches=re.findall(regex_pattern,txt)
+print(matches)# ['2019', '2021']
+
+txt='This regular expression example was made on December 6, 2019 and revised on July 8, 2021'
+regex_pattern=r'\d{1, 4}'# 1 to 4
+matches=re.findall(regex_pattern,txt)
+print(matches)# ['6', '2019', '8', '2021']
+
txt='This regular expression example was made on December 6, 2019 and revised on July 8, 2021'
+regex_pattern=r'^This'# ^ means starts with
+matches=re.findall(regex_pattern,txt)
+print(matches)# ['This']
+
+
+
Negation
+
+
txt='This regular expression example was made on December 6, 2019 and revised on July 8, 2021'
+regex_pattern=r'[^A-Za-z ]+'# ^ in set character means negation, not A to Z, not a to z, no space
+matches=re.findall(regex_pattern,txt)
+print(matches)# ['6,', '2019', '8', '2021']
+
What is the most frequent word in the following paragraph?
+
+
paragraph='I love teaching. If you do not love teaching what else can you love. I love Python if you do not love something which can give you all the capabilities to develop an application what else can you love.
+
The position of some particles on the horizontal x-axis are -12, -4, -3 and -1 in the negative direction, 0 at origin, 4 and 8 in the positive direction. Extract these numbers from this whole text and find the distance between the two furthest particles.
Clean the following text. After cleaning, count three most frequent words in the string.
+
sentence='''%I $am@% a %tea@cher%, &and& I lo%#ve %tea@ching%;. There $is nothing; &as& mo@re rewarding as educa@ting &and& @emp%o@wering peo@ple. ;I found tea@ching m%o@re interesting tha@n any other %jo@bs. %Do@es thi%s mo@tivate yo@u to be a tea@cher!?'''
+
+print(clean_text(sentence));
+IamateacherandIloveteachingThereisnothingasmorerewardingaseducatingandempoweringpeopleIfoundteachingmoreinterestingthananyotherjobsDoesthismotivateyoutobeateacher
+print(most_frequent_words(cleaned_text))# [(3, 'I'), (2, 'teaching'), (2, 'teacher')]
+
+
+[<< Day 18](../18_Day_Regular_expressions/18_regular_expressions.md) | [Day 20 >>](../20_Day_Python_package_manager/20_python_package_manager.md)
+
+![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
+
+- [📘 Day 19](#-day-19)
+ - [File Handling](#file-handling)
+ - [Opening Files for Reading](#opening-files-for-reading)
+ - [Opening Files for Writing and Updating](#opening-files-for-writing-and-updating)
+ - [Deleting Files](#deleting-files)
+ - [File Types](#file-types)
+ - [File with txt Extension](#file-with-txt-extension)
+ - [File with json Extension](#file-with-json-extension)
+ - [Changing JSON to Dictionary](#changing-json-to-dictionary)
+ - [Changing Dictionary to JSON](#changing-dictionary-to-json)
+ - [Saving as JSON File](#saving-as-json-file)
+ - [File with csv Extension](#file-with-csv-extension)
+ - [File with xlsx Extension](#file-with-xlsx-extension)
+ - [File with xml Extension](#file-with-xml-extension)
+ - [💻 Exercises: Day 19](#-exercises-day-19)
+ - [Exercises: Level 1](#exercises-level-1)
+ - [Exercises: Level 2](#exercises-level-2)
+ - [Exercises: Level 3](#exercises-level-3)
+
+# 📘 Day 19
+
+## File Handling
+
+So far we have seen different Python data types. We usually store our data in different file formats. In addition to handling files, we will also see different file formats(.txt, .json, .xml, .csv, .tsv, .excel) in this section. First, let us get familiar with handling files 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()_ built-in 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, it returns an 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 Files 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 us see how it is done:
+
+```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 example above, I printed the opened file and it gave some information about it. 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 want to read, we can limit it by passing int value to the *read(number)* method.
+
+```py
+f = open('./files/reading_file_example.txt')
+txt = f.read()
+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 us print the first 10 characters of the text file.
+
+```py
+f = open('./files/reading_file_example.txt')
+txt = f.read(10)
+print(type(txt))
+print(txt)
+f.close()
+```
+
+```sh
+# output
+
+This is an
+```
+
+- _readline()_: read only the first line
+
+```py
+f = open('./files/reading_file_example.txt')
+line = f.readline()
+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
+
+```py
+f = open('./files/reading_file_example.txt')
+lines = f.readlines()
+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 as a list is using _splitlines()_:
+
+```py
+f = open('./files/reading_file_example.txt')
+lines = f.read().splitlines()
+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 it. There is a high tendency of forgetting to close them. There is a new way of opening files using _with_ - closes the files by itself. Let us rewrite the the previous example with the _with_ method:
+
+```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 Files for Writing and 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 it creates a new file.
+- "w" - write - will overwrite any existing content, if the file does not exist it creates.
+
+Let us 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')
+```
+
+The method below creates 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 Files
+
+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, so it is good to use a condition like this:
+
+```py
+import os
+if os.path.exists('./files/example.txt'):
+ os.remove('./files/example.txt')
+else:
+ print('The file does not exist')
+```
+
+## File Types
+
+### File with txt Extension
+
+File with _txt_ extension is a very common form of data and we have covered it in the previous section. Let us move to the JSON file
+
+### File with json Extension
+
+JSON stands for JavaScript Object Notation. Actually, it is a stringified JavaScript object or Python dictionary.
+
+_Example:_
+
+```py
+# dictionary
+person_dct= {
+ "name":"Asabeneh",
+ "country":"Finland",
+ "city":"Helsinki",
+ "skills":["JavaScrip", "React","Python"]
+}
+# JSON: A string form a dictionary
+person_json = "{'name': 'Asabeneh', 'country': 'Finland', 'city': 'Helsinki', 'skills': ['JavaScrip', 'React', 'Python']}"
+
+# we use three quotes and make it multiple line to make it more readable
+person_json = '''{
+ "name":"Asabeneh",
+ "country":"Finland",
+ "city":"Helsinki",
+ "skills":["JavaScrip", "React","Python"]
+}'''
+```
+
+### Changing JSON to Dictionary
+
+To change a JSON to a dictionary, first we import the json module and then we use _loads_ method.
+
+```py
+import json
+# JSON
+person_json = '''{
+ "name": "Asabeneh",
+ "country": "Finland",
+ "city": "Helsinki",
+ "skills": ["JavaScrip", "React", "Python"]
+}'''
+# let's change JSON to dictionary
+person_dct = json.loads(person_json)
+print(type(person_dct))
+print(person_dct)
+print(person_dct['name'])
+```
+
+```sh
+# output
+
+{'name': 'Asabeneh', 'country': 'Finland', 'city': 'Helsinki', 'skills': ['JavaScrip', 'React', 'Python']}
+Asabeneh
+```
+
+### Changing Dictionary to JSON
+
+To change a dictionary to a JSON we use _dumps_ method from the json module.
+
+```py
+import json
+# python dictionary
+person = {
+ "name": "Asabeneh",
+ "country": "Finland",
+ "city": "Helsinki",
+ "skills": ["JavaScrip", "React", "Python"]
+}
+# let's convert it to json
+person_json = json.dumps(person, indent=4) # indent could be 2, 4, 8. It beautifies the json
+print(type(person_json))
+print(person_json)
+```
+
+```sh
+# output
+# when you print it, it does not have the quote, but actually it is a string
+# JSON does not have type, it is a string type.
+
+{
+ "name": "Asabeneh",
+ "country": "Finland",
+ "city": "Helsinki",
+ "skills": [
+ "JavaScrip",
+ "React",
+ "Python"
+ ]
+}
+```
+
+### Saving as JSON File
+
+We can also save our data as a json file. Let us save it as a json file using the following steps. For writing a json file, we use the json.dump() method, it can take dictionary, output file, ensure_ascii and indent.
+
+```py
+import json
+# python dictionary
+person = {
+ "name": "Asabeneh",
+ "country": "Finland",
+ "city": "Helsinki",
+ "skills": ["JavaScrip", "React", "Python"]
+}
+with open('./files/json_example.json', 'w', encoding='utf-8') as f:
+ json.dump(person, f, ensure_ascii=False, indent=4)
+```
+
+In the code above, 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:**
+
+```csv
+"name","country","city","skills"
+"Asabeneh","Finland","Helsinki","JavaScript"
+```
+
+**Example:**
+
+```py
+import csv
+with open('./files/csv_example.csv') as f:
+ csv_reader = csv.reader(f, delimiter=',') # w use, reader method to read csv
+ line_count = 0
+ for row in csv_reader:
+ if line_count == 0:
+ print(f'Column names are :{", ".join(row)}')
+ line_count += 1
+ else:
+ print(
+ f'\t{row[0]} is a teachers. He lives in {row[1]}, {row[2]}.')
+ line_count += 1
+ print(f'Number of lines: {line_count}')
+```
+
+```sh
+# output:
+Column names are :name, country, city, skills
+ Asabeneh is a teacher. He lives in Finland, Helsinki.
+Number of lines: 2
+```
+
+### File with xlsx Extension
+
+To read excel files we need to install _xlrd_ package. We will cover this after we cover package installing using pip.
+
+```py
+import xlrd
+excel_book = xlrd.open_workbook('sample.xls)
+print(excel_book.nsheets)
+print(excel_book.sheet_names)
+```
+
+### 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
+
+
+ Asabeneh
+ Finland
+ Helsinki
+
+ JavaScrip
+ React
+ Python
+
+
+```
+
+For more information on how to read an XML file 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')
+root = tree.getroot()
+print('Root tag:', root.tag)
+print('Attribute:', root.attrib)
+for child in root:
+ print('field: ', child.tag)
+```
+
+```sh
+# output
+Root tag: person
+Attribute: {'gender': 'male'}
+field: name
+field: country
+field: city
+field: skills
+```
+
+🌕 You are making a big progress. Maintain your momentum, keep the good work. Now do some exercises for your brain and muscles.
+
+## 💻 Exercises: Day 19
+
+### Exercises: Level 1
+
+1. Write a function which count number of lines and number of words in a text. All the files are in the data the folder:
+ a) Read obama_speech.txt file and count number of lines and words
+ b) Read michelle_obama_speech.txt file and count number of lines and words
+ c) Read donald_speech.txt file and count number of lines and words
+ d) Read melina_trump_speech.txt file and count number of lines and words
+2. Read the countries_data.json data file in data directory, create a function that finds 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')]
+ ```
+
+3. Read the countries_data.json data file in data directory, create a function that creates a list of 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
+
+ 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}
+ ]
+ ```
+
+### Exercises: Level 2
+
+4. Extract all incoming email addresses as a list 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 - a string or a file and a positive integer, indicating the number of words. 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'),
+ (6, 'to'),
+ (6, 'of'),
+ (5, 'and'),
+ (4, 'a'),
+ (4, 'in'),
+ (3, 'that'),
+ (2, 'have'),
+ (2, 'I')]
+
+ # Your output should look like this
+ print(find_most_common_words('sample.txt', 5))
+
+ [(10, 'the'),
+ (8, 'be'),
+ (6, 'to'),
+ (6, 'of'),
+ (5, 'and')]
+```
+
+6. Use the function, find_most_frequent_words to find:
+ a) The ten most frequent words used in [Obama's speech](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/data/obama_speech.txt)
+ b) 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)
+ c) The ten most frequent words used in [Trump's speech](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/data/donald_speech.txt)
+ d) The ten most frequent words used in [Melina's speech](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/data/melina_trump_speech.txt)
+7. Write a python application that checks similarity between two texts. It takes a file or a string as a parameter and it will evaluate the similarity of the two texts. For instance check the similarity between the transcripts of [Michelle's](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/data/michelle_obama_speech.txt) and [Melina's](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/data/melina_trump_speech.txt) speech. You may need a couple of functions, function to clean the text(clean_text), function to remove support words(remove_support_words) and finally to check the similarity(check_text_similarity). List of [stop words](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/data/stop_words.py) are in the data directory
+8. Find the 10 most repeated words in the romeo_and_juliet.txt
+9. Read the [hacker news csv](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/data/hacker_news.csv) file and find out:
+ a) Count the number of lines containing python or Python
+ b) Count the number lines containing JavaScript, javascript or Javascript
+ c) Count the number lines containing Java and not JavaScript
+
+### Exercises: Level 3
+
+🎉 CONGRATULATIONS ! 🎉
+
+[<< Day 18](../18_Day_Regular_expressions/18_regular_expressions.md) | [Day 20 >>](../20_Day_Python_package_manager/20_python_package_manager.md)
diff --git a/19_file_handling/index.html b/19_file_handling/index.html
new file mode 100644
index 0000000..0fc97df
--- /dev/null
+++ b/19_file_handling/index.html
@@ -0,0 +1,1894 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 19 file handling - 30天入门Python全栈开发
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
So far we have seen different Python data types. We usually store our data in different file formats. In addition to handling files, we will also see different file formats(.txt, .json, .xml, .csv, .tsv, .excel) in this section. First, let us get familiar with handling files 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() built-in function.
+
# 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, it returns an 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
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 us see how it is done:
As you can see in the example above, I printed the opened file and it gave some information about it. 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 want to read, we can limit it by passing int value to the read(number) method.
# output
+<class'list'>
+['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 it. There is a high tendency of forgetting to close them. There is a new way of opening files using with - closes the files by itself. Let us rewrite the the previous example with the with method:
JSON stands for JavaScript Object Notation. Actually, it is a stringified JavaScript object or Python dictionary.
+
Example:
+
# dictionary
+person_dct={
+ "name":"Asabeneh",
+ "country":"Finland",
+ "city":"Helsinki",
+ "skills":["JavaScrip","React","Python"]
+}
+# JSON: A string form a dictionary
+person_json="{'name': 'Asabeneh', 'country': 'Finland', 'city': 'Helsinki', 'skills': ['JavaScrip', 'React', 'Python']}"
+
+# we use three quotes and make it multiple line to make it more readable
+person_json='''{
+ "name":"Asabeneh",
+ "country":"Finland",
+ "city":"Helsinki",
+ "skills":["JavaScrip", "React","Python"]
+}'''
+
To change a dictionary to a JSON we use dumps method from the json module.
+
importjson
+# python dictionary
+person={
+ "name":"Asabeneh",
+ "country":"Finland",
+ "city":"Helsinki",
+ "skills":["JavaScrip","React","Python"]
+}
+# let's convert it to json
+person_json=json.dumps(person,indent=4)# indent could be 2, 4, 8. It beautifies the json
+print(type(person_json))
+print(person_json)
+
+
# output
+# when you print it, it does not have the quote, but actually it is a string
+# JSON does not have type, it is a string type.
+<class'str'>
+{
+"name":"Asabeneh",
+"country":"Finland",
+"city":"Helsinki",
+"skills":[
+"JavaScrip",
+"React",
+"Python"
+]
+}
+
We can also save our data as a json file. Let us save it as a json file using the following steps. For writing a json file, we use the json.dump() method, it can take dictionary, output file, ensure_ascii and indent.
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.
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
Write a function which count number of lines and number of words in a text. All the files are in the data the folder:
+ a) Read obama_speech.txt file and count number of lines and words
+ b) Read michelle_obama_speech.txt file and count number of lines and words
+ c) Read donald_speech.txt file and count number of lines and words
+ d) Read melina_trump_speech.txt file and count number of lines and words
+
Read the countries_data.json data file in data directory, create a function that finds the ten most spoken languages
+
+
# 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')]
+
+
+
Read the countries_data.json data file in data directory, create a function that creates a list of the ten most populated countries
+
+
# 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
+
+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}
+]
+
Extract all incoming email addresses as a list from the email_exchange_big.txt file.
+
Find the most common words in the English language. Call the name of your function find_most_common_words, it will take two parameters - a string or a file and a positive integer, indicating the number of words. Your function will return an array of tuples in descending order. Check the output
+
+
# Your output should look like this
+ print(find_most_common_words('sample.txt',10))
+ [(10,'the'),
+ (8,'be'),
+ (6,'to'),
+ (6,'of'),
+ (5,'and'),
+ (4,'a'),
+ (4,'in'),
+ (3,'that'),
+ (2,'have'),
+ (2,'I')]
+
+ # Your output should look like this
+ print(find_most_common_words('sample.txt',5))
+
+ [(10,'the'),
+ (8,'be'),
+ (6,'to'),
+ (6,'of'),
+ (5,'and')]
+
+
+
Use the function, find_most_frequent_words to find:
+ a) The ten most frequent words used in Obama's speech
+ b) The ten most frequent words used in Michelle's speech
+ c) The ten most frequent words used in Trump's speech
+ d) The ten most frequent words used in Melina's speech
+
Write a python application that checks similarity between two texts. It takes a file or a string as a parameter and it will evaluate the similarity of the two texts. For instance check the similarity between the transcripts of Michelle's and Melina's speech. You may need a couple of functions, function to clean the text(clean_text), function to remove support words(remove_support_words) and finally to check the similarity(check_text_similarity). List of stop words are in the data directory
+
Find the 10 most repeated words in the romeo_and_juliet.txt
+
Read the hacker news csv file and find out:
+ a) Count the number of lines containing python or Python
+ b) Count the number lines containing JavaScript, javascript or Javascript
+ c) Count the number lines containing Java and not JavaScript
+
+[<< Day 19](../19_Day_File_handling/19_file_handling.md) | [Day 21 >>](../21_Day_Classes_and_objects/21_classes_and_objects.md)
+
+![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
+
+- [📘 Day 20](#-day-20)
+ - [Python PIP - Python Package Manager](#python-pip---python-package-manager)
+ - [What is PIP ?](#what-is-pip-)
+ - [Installing PIP](#installing-pip)
+ - [Installing packages using pip](#installing-packages-using-pip)
+ - [Uninstalling Packages](#uninstalling-packages)
+ - [List of Packages](#list-of-packages)
+ - [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 20
+
+## Python PIP - Python Package Manager
+
+### What is PIP ?
+
+PIP stands for Preferred installer program. We use _pip_ to install different Python packages.
+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.
+In programming, we do not have to write every utility program, instead we install packages and import them to our applications.
+
+### Installing PIP
+
+If you did not install pip, let us install it now. Go to your terminal or command prompt and copy and paste this:
+
+```sh
+asabeneh@Asabeneh:~$ pip install pip
+```
+
+Check if pip is installed by writing
+
+```sh
+pip --version
+```
+
+```py
+asabeneh@Asabeneh:~$ pip --version
+pip 21.1.3 from /usr/local/lib/python3.7/site-packages/pip (python 3.9.6)
+```
+
+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.
+
+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.
+
+### Installing packages using pip
+
+Let us try to install _numpy_, called numeric python. It is one of the most popular packages 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
+```
+
+Let us start using numpy. Open your python interactive shell, write python and then import numpy as follows:
+
+```py
+asabeneh@Asabeneh:~$ python
+Python 3.9.6 (default, Jun 28 2021, 15:26:21)
+[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
+Type "help", "copyright", "credits" or "license" for more information.
+>>> import numpy
+>>> numpy.version.version
+'1.20.1'
+>>> lst = [1, 2, 3,4, 5]
+>>> np_arr = numpy.array(lst)
+>>> np_arr
+array([1, 2, 3, 4, 5])
+>>> len(np_arr)
+5
+>>> np_arr * 2
+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. Let us install the big brother of numpy, _pandas_:
+
+```sh
+asabeneh@Asabeneh:~$ pip install pandas
+```
+
+```py
+asabeneh@Asabeneh:~$ python
+Python 3.9.6 (default, Jun 28 2021, 15:26:21)
+[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.
+
+Let us import a web browser module, which can help us to open any website. We do not need to 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 used.
+
+```py
+import webbrowser # web browser module to open websites
+
+# list of urls: python
+url_lists = [
+ 'http://www.python.org',
+ 'https://www.linkedin.com/in/asabeneh/',
+ 'https://github.com/Asabeneh',
+ 'https://twitter.com/Asabeneh',
+]
+
+# opens the above list of websites in a different tab
+for url in url_lists:
+ webbrowser.open_new_tab(url)
+```
+
+### Uninstalling Packages
+
+If you do not like to keep the installed packages, you can remove them using the following command.
+
+```sh
+pip uninstall packagename
+```
+
+### List of Packages
+
+To see the installed packages on our machine. We can use pip followed by list.
+
+```sh
+pip list
+```
+
+### Show Package
+
+To show information about a package
+
+```sh
+pip show packagename
+```
+
+```sh
+asabeneh@Asabeneh:~$ pip show pandas
+Name: pandas
+Version: 1.2.3
+Summary: Powerful data structures for data analysis, time series, and statistics
+Home-page: http://pandas.pydata.org
+Author: None
+Author-email: None
+License: BSD
+Location: /usr/local/lib/python3.7/site-packages
+Requires: python-dateutil, pytz, numpy
+Required-by:
+```
+
+If we want even more details, just add --verbose
+
+```sh
+asabeneh@Asabeneh:~$ pip show --verbose pandas
+Name: pandas
+Version: 1.2.3
+Summary: Powerful data structures for data analysis, time series, and statistics
+Home-page: http://pandas.pydata.org
+Author: None
+Author-email: None
+License: BSD
+Location: /usr/local/lib/python3.7/site-packages
+Requires: numpy, pytz, python-dateutil
+Required-by:
+Metadata-Version: 2.1
+Installer: pip
+Classifiers:
+ Development Status :: 5 - Production/Stable
+ Environment :: Console
+ Operating System :: OS Independent
+ Intended Audience :: Science/Research
+ Programming Language :: Python
+ Programming Language :: Python :: 3
+ Programming Language :: Python :: 3.5
+ Programming Language :: Python :: 3.6
+ Programming Language :: Python :: 3.7
+ Programming Language :: Python :: 3.8
+ Programming Language :: Cython
+ Topic :: Scientific/Engineering
+Entry-points:
+ [pandas_plotting_backends]
+ matplotlib = pandas:plotting._matplotlib
+```
+
+### PIP Freeze
+
+Generate installed Python packages with their version and the output is suitable to use it in a requirements file. A requirements.txt file is a file that should contain all the installed Python packages in a Python project.
+
+```sh
+asabeneh@Asabeneh:~$ pip freeze
+docutils==0.11
+Jinja2==2.7.2
+MarkupSafe==0.19
+Pygments==1.6
+Sphinx==1.2.2
+```
+
+The pip freeze gave us the packages used, installed and their version. We use it with requirements.txt file for deployment.
+
+### Reading from URL
+
+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 or from an API.
+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.
+
+Let us install _requests_:
+
+```py
+asabeneh@Asabeneh:~$ pip install requests
+```
+
+We will see _get_, _status_code_, _headers_, _text_ and _json_ methods in _requests_ module:
+ - _get()_: to open a network and fetch data from url - it returns a response object
+ - _status_code_: After we fetched data, we can check the status of the operation (success, 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 from this website, https://www.w3.org/TR/PNG/iso_8859-1.txt.
+
+```py
+import requests # importing the request module
+
+url = 'https://www.w3.org/TR/PNG/iso_8859-1.txt' # text from a website
+
+response = requests.get(url) # opening a network and fetching a data
+print(response)
+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'}
+```
+
+- 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.
+
+```py
+import requests
+url = 'https://restcountries.eu/rest/v2/all' # countries api
+response = requests.get(url) # opening a network and fetching a data
+print(response) # response object
+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
+[{'alpha2Code': 'AF',
+ 'alpha3Code': 'AFG',
+ 'altSpellings': ['AF', 'Afġānistān'],
+ 'area': 652230.0,
+ 'borders': ['IRN', 'PAK', 'TKM', 'UZB', 'TJK', 'CHN'],
+ 'callingCodes': ['93'],
+ 'capital': 'Kabul',
+ 'cioc': 'AFG',
+ 'currencies': [{'code': 'AFN', 'name': 'Afghan afghani', 'symbol': '؋'}],
+ 'demonym': 'Afghan',
+ 'flag': 'https://restcountries.eu/data/afg.svg',
+ 'gini': 27.8,
+ 'languages': [{'iso639_1': 'ps',
+ 'iso639_2': 'pus',
+ 'name': 'Pashto',
+ 'nativeName': 'پښتو'},
+ {'iso639_1': 'uz',
+ 'iso639_2': 'uzb',
+ 'name': 'Uzbek',
+ 'nativeName': 'Oʻzbek'},
+ {'iso639_1': 'tk',
+ 'iso639_2': 'tuk',
+ 'name': 'Turkmen',
+ 'nativeName': 'Türkmen'}],
+ 'latlng': [33.0, 65.0],
+ 'name': 'Afghanistan',
+ 'nativeName': 'افغانستان',
+ 'numericCode': '004',
+ 'population': 27657145,
+ 'region': 'Asia',
+ 'regionalBlocs': [{'acronym': 'SAARC',
+ 'name': 'South Asian Association for Regional Cooperation',
+ 'otherAcronyms': [],
+ 'otherNames': []}],
+ 'subregion': 'Southern Asia',
+ 'timezones': ['UTC+04:30'],
+ 'topLevelDomain': ['.af'],
+ 'translations': {'br': 'Afeganistão',
+ 'de': 'Afghanistan',
+ 'es': 'Afganistán',
+ 'fa': 'افغانستان',
+ 'fr': 'Afghanistan',
+ 'hr': 'Afganistan',
+ 'it': 'Afghanistan',
+ 'ja': 'アフガニスタン',
+ '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_.
+
+### Creating a Package
+
+We organize a large number of files in different folders and sub-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:
+
+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.9.6 (default, Jun 28 2021, 15:26:21)
+[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 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.
+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 - micro framework 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 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.
+ - 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_
+
+🌕 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.
+
+## Exercises: Day 20
+
+1. Read this url and find the 10 most frequent words. romeo_and_juliet = 'http://www.gutenberg.org/files/1112/1112.txt'
+2. Read the cats API and cats_api = 'https://api.thecatapi.com/v1/breeds' and find :
+ 1. the min, max, mean, median, standard deviation of cats' weight in metric units.
+ 2. the min, max, mean, median, standard deviation of cats' lifespan in years.
+ 3. Create a frequency table of country and breed of cats
+3. Read the [countries API](https://restcountries.eu/rest/v2/all) and find
+ 1. the 10 largest countries
+ 2. the 10 most spoken languages
+ 3. the total number of languages in the countries API
+4. UCI is 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
+
+🎉 CONGRATULATIONS ! 🎉
+
+[<< Day 19](../19_Day_File_handling/19_file_handling.md) | [Day 21 >>](../21_Day_Classes_and_objects/21_classes_and_objects.md)
diff --git a/20_python_package_manager/index.html b/20_python_package_manager/index.html
new file mode 100644
index 0000000..963e0db
--- /dev/null
+++ b/20_python_package_manager/index.html
@@ -0,0 +1,1754 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 20 python package manager - 30天入门Python全栈开发
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
PIP stands for Preferred installer program. We use pip to install different Python packages.
+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.
+In programming, we do not have to write every utility program, instead we install packages and import them to our applications.
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.
+
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.
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:
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.
+
Let us import a web browser module, which can help us to open any website. We do not need to 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 used.
+
importwebbrowser# web browser module to open websites
+
+# list of urls: python
+url_lists=[
+ 'http://www.python.org',
+ 'https://www.linkedin.com/in/asabeneh/',
+ 'https://github.com/Asabeneh',
+ 'https://twitter.com/Asabeneh',
+]
+
+# opens the above list of websites in a different tab
+forurlinurl_lists:
+ webbrowser.open_new_tab(url)
+
Generate installed Python packages with their version and the output is suitable to use it in a requirements file. A requirements.txt file is a file that should contain all the installed Python packages in a Python project.
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 or from an API.
+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.
+
Let us install requests:
+
asabeneh@Asabeneh:~$pipinstallrequests
+
+
We will see get, status_code, headers, text and json methods in requests module:
+ - get(): to open a network and fetch data from url - it returns a response object
+ - status_code: After we fetched data, we can check the status of the operation (success, 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 from this website, https://www.w3.org/TR/PNG/iso_8859-1.txt.
+
importrequests# importing the request module
+
+url='https://www.w3.org/TR/PNG/iso_8859-1.txt'# text from a website
+
+response=requests.get(url)# opening a network and fetching a data
+print(response)
+print(response.status_code)# status code, success:200
+print(response.headers)# headers information
+print(response.text)# gives all the text from the page
+
+
<Response[200]>
+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'}
+
+
+
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.
+
+
importrequests
+url='https://restcountries.eu/rest/v2/all'# countries api
+response=requests.get(url)# opening a network and fetching a data
+print(response)# response object
+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
+
We organize a large number of files in different folders and sub-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:
+
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:
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.
+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.
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 - micro framework for Python based on Werkzeug, Jinja 2. (It's BSD licensed)
+
pip install flask
+
+
+
HTML Parser
+
Beautiful Soup - 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 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.
+
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
+
+
+
+
🌕 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.
the total number of languages in the countries API
+
UCI is 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
+
+[<< Day 20](../20_Day_Python_package_manager/20_python_package_manager.md) | [Day 22 >>](../22_Day_Web_scraping/22_web_scraping.md)
+
+![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
+
+- [📘 Day 21](#-day-21)
+ - [Classes and Objects](#classes-and-objects)
+ - [Creating a Class](#creating-a-class)
+ - [Creating an Object](#creating-an-object)
+ - [Class Constructor](#class-constructor)
+ - [Object Methods](#object-methods)
+ - [Object Default Methods](#object-default-methods)
+ - [Method to Modify Class Default Values](#method-to-modify-class-default-values)
+ - [Inheritance](#inheritance)
+ - [Overriding parent method](#overriding-parent-method)
+ - [💻 Exercises: Day 21](#-exercises-day-21)
+ - [Exercises: Level 1](#exercises-level-1)
+ - [Exercises: Level 2](#exercises-level-2)
+ - [Exercises: Level 3](#exercises-level-3)
+
+# 📘 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 this challenge unknowingly. Every element in a Python program is an object of a class.
+Let us check if everything in python is a class:
+
+```py
+asabeneh@Asabeneh:~$ python
+Python 3.9.6 (default, Jun 28 2021, 15:26:21)
+[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
+Type "help", "copyright", "credits" or "license" for more information.
+>>> num = 10
+>>> type(num)
+
+>>> string = 'string'
+>>> type(string)
+
+>>> boolean = True
+>>> type(boolean)
+
+>>> lst = []
+>>> type(lst)
+
+>>> tpl = ()
+>>> type(tpl)
+
+>>> set1 = set()
+>>> type(set1)
+
+>>> dct = {}
+>>> type(dct)
+
+```
+
+### Creating a Class
+
+To create a class we need the key word **class** followed by the name and colon. Class name should be **CamelCase**.
+
+```sh
+# syntax
+class ClassName:
+ code goes here
+```
+
+**Example:**
+
+```py
+class Person:
+ pass
+print(Person)
+```
+
+```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 examples above, we have created an object from the Person class. However, a class without a constructor is not really useful in real applications. Let us use constructor function to make our class more useful. Like the constructor function in Java or JavaScript, Python has also a built-in **__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 allows to attach parameter to the class
+ self.name =name
+
+p = Person('Asabeneh')
+print(p.name)
+print(p)
+```
+
+```sh
+# output
+Asabeneh
+<__main__.Person object at 0x2abf46907e80>
+```
+
+Let us add more parameters to the constructor function.
+
+```py
+class Person:
+ def __init__(self, firstname, lastname, age, country, city):
+ self.firstname = firstname
+ self.lastname = lastname
+ self.age = age
+ self.country = country
+ self.city = city
+
+
+p = Person('Asabeneh', 'Yetayeh', 250, 'Finland', 'Helsinki')
+print(p.firstname)
+print(p.lastname)
+print(p.age)
+print(p.country)
+print(p.city)
+```
+
+```sh
+# output
+Asabeneh
+Yetayeh
+250
+Finland
+Helsinki
+```
+
+### Object Methods
+
+Objects can have methods. The methods are functions which belong to the object.
+
+**Example:**
+
+```py
+class Person:
+ def __init__(self, firstname, lastname, age, country, city):
+ self.firstname = firstname
+ self.lastname = lastname
+ self.age = age
+ self.country = country
+ self.city = city
+ def person_info(self):
+ return f'{self.firstname} {self.lastname} is {self.age} years old. He lives in {self.city}, {self.country}'
+
+p = Person('Asabeneh', 'Yetayeh', 250, 'Finland', 'Helsinki')
+print(p.person_info())
+```
+
+```sh
+# output
+Asabeneh Yetayeh is 250 years old. He lives in Helsinki, Finland
+```
+
+### Object Default Methods
+
+Sometimes, you may want to have a default values for your object methods. If we give default values for the parameters in the constructor, we can avoid errors when we call or instantiate our class without parameters. Let's see how it looks:
+
+**Example:**
+
+```py
+class Person:
+ def __init__(self, firstname='Asabeneh', lastname='Yetayeh', age=250, country='Finland', city='Helsinki'):
+ self.firstname = firstname
+ self.lastname = lastname
+ self.age = age
+ self.country = country
+ self.city = city
+
+ def person_info(self):
+ return f'{self.firstname} {self.lastname} is {self.age} years old. He lives in {self.city}, {self.country}.'
+
+p1 = Person()
+print(p1.person_info())
+p2 = Person('John', 'Doe', 30, 'Nomanland', 'Noman city')
+print(p2.person_info())
+```
+
+```sh
+# output
+Asabeneh Yetayeh is 250 years old. He lives in Helsinki, Finland.
+John Doe is 30 years 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. In addition to that, we have skills parameter, which we can access using a method. Let us create add_skill method to add skills to the skills list.
+
+```py
+class Person:
+ def __init__(self, firstname='Asabeneh', lastname='Yetayeh', age=250, country='Finland', city='Helsinki'):
+ self.firstname = firstname
+ self.lastname = lastname
+ self.age = age
+ self.country = country
+ self.city = city
+ self.skills = []
+
+ def person_info(self):
+ return f'{self.firstname} {self.lastname} is {self.age} years old. He lives in {self.city}, {self.country}.'
+ def add_skill(self, skill):
+ self.skills.append(skill)
+
+p1 = Person()
+print(p1.person_info())
+p1.add_skill('HTML')
+p1.add_skill('CSS')
+p1.add_skill('JavaScript')
+p2 = Person('John', 'Doe', 30, 'Nomanland', 'Noman city')
+print(p2.person_info())
+print(p1.skills)
+print(p2.skills)
+```
+
+```sh
+# output
+Asabeneh Yetayeh is 250 years old. He lives in Helsinki, Finland.
+John Doe is 30 years 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 parent class. The parent class or super or base class is the class which gives all the methods and properties. Child class is the class that inherits from another or parent class.
+Let us create a student class by inheriting from person class.
+
+```py
+class Student(Person):
+ pass
+
+
+s1 = Student('Eyob', 'Yetayeh', 30, 'Finland', 'Helsinki')
+s2 = Student('Lidiya', 'Teklemariam', 28, 'Finland', 'Espoo')
+print(s1.person_info())
+s1.add_skill('JavaScript')
+s1.add_skill('React')
+s1.add_skill('Python')
+print(s1.skills)
+
+print(s2.person_info())
+s2.add_skill('Organizing')
+s2.add_skill('Marketing')
+s2.add_skill('Digital Marketing')
+print(s2.skills)
+
+```
+
+```sh
+output
+Eyob Yetayeh is 30 years old. He lives in Helsinki, Finland.
+['JavaScript', 'React', 'Python']
+Lidiya Teklemariam is 28 years old. He lives in Espoo, Finland.
+['Organizing', 'Marketing', 'Digital Marketing']
+```
+
+We did not call the **__init__**() constructor in the child class. If we didn't call it then we can still access all the properties from the parent. But if we do call the constructor we can access the parent properties by calling _super_.
+We can add a new method to the child or we can override the parent class methods 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'):
+ self.gender = gender
+ super().__init__(firstname, lastname,age, country, city)
+ def person_info(self):
+ gender = 'He' if self.gender =='male' else 'She'
+ return f'{self.firstname} {self.lastname} is {self.age} years old. {gender} lives in {self.city}, {self.country}.'
+
+s1 = Student('Eyob', 'Yetayeh', 30, 'Finland', 'Helsinki','male')
+s2 = Student('Lidiya', 'Teklemariam', 28, 'Finland', 'Espoo', 'female')
+print(s1.person_info())
+s1.add_skill('JavaScript')
+s1.add_skill('React')
+s1.add_skill('Python')
+print(s1.skills)
+
+print(s2.person_info())
+s2.add_skill('Organizing')
+s2.add_skill('Marketing')
+s2.add_skill('Digital Marketing')
+print(s2.skills)
+```
+
+```sh
+Eyob Yetayeh is 30 years old. He lives in Helsinki, Finland.
+['JavaScript', 'React', 'Python']
+Lidiya Teklemariam is 28 years old. She lives in Espoo, Finland.
+['Organizing', 'Marketing', 'Digital Marketing']
+```
+
+We can use super() built-in function or the parent name Person to automatically inherit the methods and properties from its parent. In the example above we override the parent 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).
+
+🌕 Now, you are fully charged with a super power of programming. Now do some exercises for your brain and muscles.
+
+## 💻 Exercises: Day 21
+
+### Exercises: Level 1
+
+1. Python has the module called _statistics_ and we can use this module to do all the statistical calculations. However, to learn how to make function and reuse function let us try to develop a program, which calculates the measure of central tendency of a sample (mean, median, mode) and measure of variability (range, variance, standard deviation). In addition to those measures, find the min, max, count, percentile, and frequency distribution of the sample. You can create a class called Statistics and create all the functions that do statistical calculations as methods for the Statistics class. 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]
+
+print('Count:', data.count()) # 25
+print('Sum: ', data.sum()) # 744
+print('Min: ', data.min()) # 24
+print('Max: ', data.max()) # 38
+print('Range: ', data.range() # 14
+print('Mean: ', data.mean()) # 30
+print('Median: ', data.median()) # 29
+print('Mode: ', data.mode()) # {'mode': 26, 'count': 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
+Min: 24
+Max: 38
+Range: 14
+Mean: 30
+Median: 29
+Mode: (26, 5)
+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)]
+```
+
+### Exercises: Level 2
+
+1. Create a class called PersonAccount. It has firstname, lastname, incomes, expenses properties and it has total_income, total_expense, account_info, add_income, add_expense and account_balance methods. Incomes is a set of incomes and its description. The same goes for expenses.
+
+### Exercises: Level 3
+
+
+🎉 CONGRATULATIONS ! 🎉
+
+[<< Day 20](../20_Day_Python_package_manager/20_python_package_manager.md) | [Day 22 >>](../22_Day_Web_scraping/22_web_scraping.md)
diff --git a/21_classes_and_objects/index.html b/21_classes_and_objects/index.html
new file mode 100644
index 0000000..24eadef
--- /dev/null
+++ b/21_classes_and_objects/index.html
@@ -0,0 +1,1693 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 21 classes and objects - 30天入门Python全栈开发
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
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 this challenge unknowingly. Every element in a Python program is an object of a class.
+Let us check if everything in python is a class:
In the examples above, we have created an object from the Person class. However, a class without a constructor is not really useful in real applications. Let us use constructor function to make our class more useful. Like the constructor function in Java or JavaScript, Python has also a built-in init() constructor function. The init constructor function has self parameter which is a reference to the current instance of the class
+Examples:
+
classPerson:
+ def__init__(self,name):
+ # self allows to attach parameter to the class
+ self.name=name
+
+p=Person('Asabeneh')
+print(p.name)
+print(p)
+
Sometimes, you may want to have a default values for your object methods. If we give default values for the parameters in the constructor, we can avoid errors when we call or instantiate our class without parameters. Let's see how it looks:
+
Example:
+
classPerson:
+ def__init__(self,firstname='Asabeneh',lastname='Yetayeh',age=250,country='Finland',city='Helsinki'):
+ self.firstname=firstname
+ self.lastname=lastname
+ self.age=age
+ self.country=country
+ self.city=city
+
+ defperson_info(self):
+ returnf'{self.firstname}{self.lastname} is {self.age} years old. He lives in {self.city}, {self.country}.'
+
+p1=Person()
+print(p1.person_info())
+p2=Person('John','Doe',30,'Nomanland','Noman city')
+print(p2.person_info())
+
In the example below, the person class, all the constructor parameters have default values. In addition to that, we have skills parameter, which we can access using a method. Let us create add_skill method to add skills to the skills list.
Using inheritance we can reuse parent class code. Inheritance allows us to define a class that inherits all the methods and properties from parent class. The parent class or super or base class is the class which gives all the methods and properties. Child class is the class that inherits from another or parent class.
+Let us create a student class by inheriting from person class.
We did not call the init() constructor in the child class. If we didn't call it then we can still access all the properties from the parent. But if we do call the constructor we can access the parent properties by calling super.
+We can add a new method to the child or we can override the parent class methods 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 can use super() built-in function or the parent name Person to automatically inherit the methods and properties from its parent. In the example above we override the parent 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).
+
🌕 Now, you are fully charged with a super power of programming. Now do some exercises for your brain and muscles.
Python has the module called statistics and we can use this module to do all the statistical calculations. However, to learn how to make function and reuse function let us try to develop a program, which calculates the measure of central tendency of a sample (mean, median, mode) and measure of variability (range, variance, standard deviation). In addition to those measures, find the min, max, count, percentile, and frequency distribution of the sample. You can create a class called Statistics and create all the functions that do statistical calculations as methods for the Statistics class. Check the output below.
# you output should look like this
+print(data.describe())
+Count:25
+Sum:744
+Min:24
+Max:38
+Range:14
+Mean:30
+Median:29
+Mode:(26,5)
+Variance:17.5
+StandardDeviation:4.2
+FrequencyDistribution:[(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)]
+
Create a class called PersonAccount. It has firstname, lastname, incomes, expenses properties and it has total_income, total_expense, account_info, add_income, add_expense and account_balance methods. Incomes is a set of incomes and its description. The same goes for expenses.
+
+[<< Day 21](../21_Day_Classes_and_objects/21_classes_and_objects.md) | [Day 23 >>](../23_Day_Virtual_environment/23_virtual_environment.md)
+
+![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
+
+- [📘 Day 22](#-day-22)
+ - [Python Web Scraping](#python-web-scraping)
+ - [What is Web Scrapping](#what-is-web-scrapping)
+ - [💻 Exercises: Day 22](#-exercises-day-22)
+
+# 📘 Day 22
+
+## Python Web Scraping
+
+### What is Web Scrapping
+
+The internet is full of huge amount of data which can be used for different purposes. To collect this data we need to know how to scrape data from a website.
+
+Web scraping is the process of extracting and collecting data from websites and storing it on a local machine or in a database.
+
+In this section, we will use beautifulsoup and requests package to scrape data. The package version we are using is beautifulsoup 4.
+
+To start scraping websites you need _requests_, _beautifoulSoup4_ and a _website_.
+
+```sh
+pip install requests
+pip install beautifulsoup4
+```
+
+To scrape data from websites, basic understanding of HTML tags and CSS selectors is needed. We target content from a website using HTML tags, classes or/and ids.
+Let us import the requests and BeautifulSoup module
+
+```py
+import requests
+from bs4 import BeautifulSoup
+```
+
+Let us declare url variable for the website which we are going to scrape.
+
+```py
+
+import requests
+from bs4 import BeautifulSoup
+url = 'https://archive.ics.uci.edu/ml/datasets.php'
+
+# Lets use the requests get method to fetch the data from url
+
+response = requests.get(url)
+# lets check the status
+status = response.status_code
+print(status) # 200 means the fetching was successful
+```
+
+```sh
+200
+```
+
+Using beautifulSoup to parse content from the page
+
+```py
+import requests
+from bs4 import BeautifulSoup
+url = 'https://archive.ics.uci.edu/ml/datasets.php'
+
+response = requests.get(url)
+content = response.content # we get all the content from the website
+soup = BeautifulSoup(content, 'html.parser') # beautiful soup will give a chance to parse
+print(soup.title) # UCI Machine Learning Repository: Data Sets
+print(soup.title.get_text()) # UCI Machine Learning Repository: Data Sets
+print(soup.body) # gives the whole page on the website
+print(response.status_code)
+
+tables = soup.find_all('table', {'cellpadding':'3'})
+# We are targeting the table with cellpadding attribute with the value of 3
+# We can select using id, class or HTML tag , for more information check the beautifulsoup doc
+table = tables[0] # the result is a list, we are taking out data from it
+for td in table.find('tr').find_all('td'):
+ print(td.text)
+```
+
+If you run this code, you can see that the extraction is half done. You can continue doing it because it is part of exercise 1.
+For reference check the [beautifulsoup documentation](https://www.crummy.com/software/BeautifulSoup/bs4/doc/#quick-start)
+
+🌕 You are so special, you are progressing everyday. You are left with only eight days to your way to greatness. Now do some exercises for your brain and muscles.
+
+## 💻 Exercises: Day 22
+
+1. Scrape the following website and store the data as json file(url = 'http://www.bu.edu/president/boston-university-facts-stats/').
+1. Extract the table in this url (https://archive.ics.uci.edu/ml/datasets.php) and change it to a json file
+2. Scrape the presidents table and store the data as json(https://en.wikipedia.org/wiki/List_of_presidents_of_the_United_States). The table is not very structured and the scrapping may take very long time.
+
+🎉 CONGRATULATIONS ! 🎉
+
+[<< Day 21](../21_Day_Web_scraping/21_class_and_object.md) | [Day 23 >>](../23_Day_Virtual_environment/23_virtual_environment.md)
diff --git a/22_web_scraping/index.html b/22_web_scraping/index.html
new file mode 100644
index 0000000..3ea7fb4
--- /dev/null
+++ b/22_web_scraping/index.html
@@ -0,0 +1,1286 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 22 web scraping - 30天入门Python全栈开发
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
The internet is full of huge amount of data which can be used for different purposes. To collect this data we need to know how to scrape data from a website.
+
Web scraping is the process of extracting and collecting data from websites and storing it on a local machine or in a database.
+
In this section, we will use beautifulsoup and requests package to scrape data. The package version we are using is beautifulsoup 4.
+
To start scraping websites you need requests, beautifoulSoup4 and a website.
+
pipinstallrequests
+pipinstallbeautifulsoup4
+
+
To scrape data from websites, basic understanding of HTML tags and CSS selectors is needed. We target content from a website using HTML tags, classes or/and ids.
+Let us import the requests and BeautifulSoup module
+
importrequests
+frombs4importBeautifulSoup
+
+
Let us declare url variable for the website which we are going to scrape.
+
importrequests
+frombs4importBeautifulSoup
+url='https://archive.ics.uci.edu/ml/datasets.php'
+
+# Lets use the requests get method to fetch the data from url
+
+response=requests.get(url)
+# lets check the status
+status=response.status_code
+print(status)# 200 means the fetching was successful
+
+
200
+
+
Using beautifulSoup to parse content from the page
+
importrequests
+frombs4importBeautifulSoup
+url='https://archive.ics.uci.edu/ml/datasets.php'
+
+response=requests.get(url)
+content=response.content# we get all the content from the website
+soup=BeautifulSoup(content,'html.parser')# beautiful soup will give a chance to parse
+print(soup.title)# <title>UCI Machine Learning Repository: Data Sets</title>
+print(soup.title.get_text())# UCI Machine Learning Repository: Data Sets
+print(soup.body)# gives the whole page on the website
+print(response.status_code)
+
+tables=soup.find_all('table',{'cellpadding':'3'})
+# We are targeting the table with cellpadding attribute with the value of 3
+# We can select using id, class or HTML tag , for more information check the beautifulsoup doc
+table=tables[0]# the result is a list, we are taking out data from it
+fortdintable.find('tr').find_all('td'):
+ print(td.text)
+
+
If you run this code, you can see that the extraction is half done. You can continue doing it because it is part of exercise 1.
+For reference check the beautifulsoup documentation
+
🌕 You are so special, you are progressing everyday. You are left with only eight days to your way to greatness. Now do some exercises for your brain and muscles.
Scrape the following website and store the data as json file(url = 'http://www.bu.edu/president/boston-university-facts-stats/').
+
Extract the table in this url (https://archive.ics.uci.edu/ml/datasets.php) and change it to a json file
+
Scrape the presidents table and store the data as json(https://en.wikipedia.org/wiki/List_of_presidents_of_the_United_States). The table is not very structured and the scrapping may take very long time.
+
+[<< Day 22](../22_Day_Web_scraping/22_web_scraping.md) | [Day 24 >>](../24_Day_Statistics/24_statistics.md)
+
+![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
+
+- [📘 Day 23](#-day-23)
+ - [Setting up Virtual Environments](#setting-up-virtual-environments)
+ - [💻 Exercises: Day 23](#-exercises-day-23)
+
+# 📘 Day 23
+
+## Setting up Virtual Environments
+
+To start with project, it would be better to have a virtual environment. Virtual environment can help us to create an isolated or separate environment. This will help us to avoid conflicts in dependencies across projects. If you write pip freeze on your terminal you will see all the installed packages on your computer. If we use virtualenv, we will access only packages which are specific for that project. Open your terminal and install virtualenv
+
+```sh
+asabeneh@Asabeneh:~$ pip install virtualenv
+```
+
+Inside the 30DaysOfPython folder create a flask_project folder.
+
+After installing the virtualenv package go to your project folder and create a virtual env by writing:
+
+For Mac/Linux:
+```sh
+asabeneh@Asabeneh:~/Desktop/30DaysOfPython/flask_project\$ virtualenv venv
+
+```
+
+For Windows:
+```sh
+C:\Users\User\Documents\30DaysOfPython\flask_project>python -m venv venv
+```
+
+I prefer to call the new project venv, but feel free to name it differently. Let us check if the the venv was created by using ls (or dir for windows command prompt) command.
+
+```sh
+asabeneh@Asabeneh:~/Desktop/30DaysOfPython/flask_project$ ls
+venv/
+```
+
+Let us activate the virtual environment by writing the following command at our project folder.
+
+For Mac/Linux:
+```sh
+asabeneh@Asabeneh:~/Desktop/30DaysOfPython/flask_project$ source venv/bin/activate
+```
+Activation of the virtual environment in Windows may very on Windows Power shell and git bash.
+
+For Windows Power Shell:
+```sh
+C:\Users\User\Documents\30DaysOfPython\flask_project> venv\Scripts\activate
+```
+
+For Windows Git bash:
+```sh
+C:\Users\User\Documents\30DaysOfPython\flask_project> venv\Scripts\. activate
+```
+
+After you write the activation command, your project directory will start with venv. See the example below.
+
+```sh
+(venv) asabeneh@Asabeneh:~/Desktop/30DaysOfPython/flask_project$
+```
+
+Now, lets check the available packages in this project by writing pip freeze. You will not see any packages.
+
+We are going to do a small flask project so let us install flask package to this project.
+
+```sh
+(venv) asabeneh@Asabeneh:~/Desktop/30DaysOfPython/flask_project$ pip install Flask
+```
+
+Now, let us write pip freeze to see a list of installed packages in the project:
+
+```sh
+(venv) asabeneh@Asabeneh:~/Desktop/30DaysOfPython/flask_project$ pip freeze
+Click==7.0
+Flask==1.1.1
+itsdangerous==1.1.0
+Jinja2==2.10.3
+MarkupSafe==1.1.1
+Werkzeug==0.16.0
+```
+
+When you finish you should dactivate active project using _deactivate_.
+
+```sh
+(venv) asabeneh@Asabeneh:~/Desktop/30DaysOfPython$ deactivate
+```
+
+The necessary modules to work with flask are installed. Now, your project directory is ready for a flask project. You should include the venv to your .gitignore file not to push it to github.
+
+## 💻 Exercises: Day 23
+
+1. Create a project directory with a virtual environment based on the example given above.
+
+🎉 CONGRATULATIONS ! 🎉
+
+[<< Day 22](../22_Day_Web_scraping/22_web_scraping.md) | [Day 24 >>](../24_Day_Statistics/24_statistics.md)
diff --git a/23_virtual_environment/index.html b/23_virtual_environment/index.html
new file mode 100644
index 0000000..37038a0
--- /dev/null
+++ b/23_virtual_environment/index.html
@@ -0,0 +1,1247 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 23 virtual environment - 30天入门Python全栈开发
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
To start with project, it would be better to have a virtual environment. Virtual environment can help us to create an isolated or separate environment. This will help us to avoid conflicts in dependencies across projects. If you write pip freeze on your terminal you will see all the installed packages on your computer. If we use virtualenv, we will access only packages which are specific for that project. Open your terminal and install virtualenv
+
asabeneh@Asabeneh:~$pipinstallvirtualenv
+
+
Inside the 30DaysOfPython folder create a flask_project folder.
+
After installing the virtualenv package go to your project folder and create a virtual env by writing:
I prefer to call the new project venv, but feel free to name it differently. Let us check if the the venv was created by using ls (or dir for windows command prompt) command.
The necessary modules to work with flask are installed. Now, your project directory is ready for a flask project. You should include the venv to your .gitignore file not to push it to github.
Statistics is the discipline that studies the collection, organization, displaying, analysing, interpretation and presentation of data.
+Statistics is a branch of Mathematics that is recommended to be a prerequisite for data science and machine learning. Statistics is a very broad field but we will focus in this section only on the most relevant part.
+After completing this challenge, you may go onto the web development, data analysis, machine learning and data science path. Whatever path you may follow, at some point in your career you will get data which you may work on. Having some statistical knowledge will help you to make decisions based on data, data tells as they say.
What is data? Data is any set of characters that is gathered and translated for some purpose, usually analysis. It can be any character, including text and numbers, pictures, sound, or video. If data is not put in a context, it doesn't make any sense to a human or computer. To make sense from data we need to work on the data using different tools.
+
The work flow of data analysis, data science or machine learning starts from data. Data can be provided from some data source or it can be created. There are structured and unstructured data.
+
Data can be found in small or big format. Most of the data types we will get have been covered in the file handling section.
The Python statistics module provides functions for calculating mathematical statistics of numerical data. The module is not intended to be a competitor to third-party libraries such as NumPy, SciPy, or proprietary full-featured statistics packages aimed at professional statisticians such as Minitab, SAS and Matlab. It is aimed at the level of graphing and scientific calculators.
In the first section we defined Python as a great general-purpose programming language on its own, but with the help of other popular libraries as(numpy, scipy, matplotlib, pandas etc) it becomes a powerful environment for scientific computing.
+
NumPy is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with arrays.
+
So far, we have been using vscode but from now on I would recommend using Jupyter Notebook. To access jupyter notebook let's install anaconda. If you are using anaconda most of the common packages are included and you don't have install packages if you installed anaconda.
Jupyter notebook is available if your are in favor of jupyter notebook
+
# How to import numpy
+ importnumpyasnp
+ # How to check the version of the numpy package
+ print('numpy:',np.__version__)
+ # Checking the available methods
+ print(dir(np))
+
# We can always convert an array back to a python list using tolist().
+np_to_list=numpy_array_from_list.tolist()
+print(type(np_to_list))
+print('one dimensional array:',np_to_list)
+print('two dimensional array: ',numpy_two_dimensional_list.tolist())
+
The shape method provide the shape of the array as a tuple. The first is the row and the second is the column. If the array is just one dimensional it returns the size of the array.
NumPy array is not like exactly like python list. To do mathematical operation in Python list we have to loop through the items but numpy can allow to do any mathematical operation without looping.
+Mathematical Operation:
# Floor division: the division result without the remainder
+numpy_array_from_list=np.array([1,2,3,4,5])
+print('original array: ',numpy_array_from_list)
+ten_times_original=numpy_array_from_list//10
+print(ten_times_original)
+
# Exponential is finding some number the power of another:
+numpy_array_from_list=np.array([1,2,3,4,5])
+print('original array: ',numpy_array_from_list)
+ten_times_original=numpy_array_from_list**2
+print(ten_times_original)
+
Sometimes, you want to create values that are evenly spaced within a defined interval. For instance, you want to create values from 1 to 10; you can use numpy.arange() function
+
# creating list using range(starting, stop, step)
+lst=range(0,11,2)
+lst
+
+
range(0,11,2)
+
+
forlinlst:
+ print(l)
+
+
```sh 0
+ 2
+ 4
+ 6
+ 8
+ 10
+
```py
+# Similar to range arange numpy.arange(start, stop, step)
+whole_numbers = np.arange(0, 20, 1)
+whole_numbers
+
# numpy.linspace()
+# numpy.logspace() in Python with Example
+# For instance, it can be used to create 10 values from 1 to 5 evenly spaced.
+np.linspace(1.0,5.0,num=10)
+
# not to include the last value in the interval
+np.linspace(1.0,5.0,num=5,endpoint=False)
+
+
array([1. , 1.8, 2.6, 3.4, 4.2])
+
+
# LogSpace
+# LogSpace returns even spaced numbers on a log scale. Logspace has the same parameters as np.linspace.
+
+# Syntax:
+
+# numpy.logspace(start, stop, num, endpoint)
+
+np.logspace(2,4.0,num=4)
+
+
array([100.,464.15888336,2154.43469003,10000.])
+
+
# to check the size of an array
+x=np.array([1,2,3],dtype=np.complex128)
+
+
x
+
+
array([1.+0.j,2.+0.j,3.+0.j])
+
+
x.itemsize
+
+
16
+
+
# indexing and Slicing NumPy Arrays in Python
+np_list=np.array([(1,2,3),(4,5,6)])
+np_list
+
NumPy has quite useful statistical functions for finding minimum, maximum, mean, median, percentile,standard deviation and variance, etc from the given elements in the array.
+The functions are explained as follows −
+Statistical function
+Numpy is equipped with the robust statistical function as listed below
a=[1,2,3]
+
+# Repeat whole of 'a' two times
+print('Tile: ',np.tile(a,2))
+
+# Repeat each element of 'a' two times
+print('Repeat: ',np.repeat(a,2))
+
# numpy.dot(): Dot Product in Python using Numpy
+# Dot Product
+# Numpy is powerful library for matrices computation. For instance, you can compute the dot product with np.dot
+
+# Syntax
+
+# numpy.dot(x, y, out=None)
+
plt.plot(temp,pressure)
+plt.xlabel('Temperature in oC')
+plt.ylabel('Pressure in atm')
+plt.title('Temperature vs Pressure')
+plt.xticks(np.arange(0,6,step=0.5))
+plt.show()
+
+
+
To draw the Gaussian normal distribution using numpy. As you can see below, the numpy can generate random numbers. To create random sample, we need the mean(mu), sigma(standard deviation), mumber of data points.
+
+[<< Day 24](../24_Day_Statistics/24_statistics.md) | [Day 26 >>](../26_Day_Python_web/26_python_web.md)
+
+![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
+
+- [📘 Day 25](#-day-25)
+ - [Pandas](#pandas)
+ - [Installing Pandas](#installing-pandas)
+ - [Importing Pandas](#importing-pandas)
+ - [Creating Pandas Series with Default Index](#creating-pandas-series-with-default-index)
+ - [Creating Pandas Series with custom index](#creating--pandas-series-with-custom-index)
+ - [Creating Pandas Series from a Dictionary](#creating-pandas-series-from-a-dictionary)
+ - [Creating a Constant Pandas Series](#creating-a-constant-pandas-series)
+ - [Creating a Pandas Series Using Linspace](#creating-a--pandas-series-using-linspace)
+ - [DataFrames](#dataframes)
+ - [Creating DataFrames from List of Lists](#creating-dataframes-from-list-of-lists)
+ - [Creating DataFrame Using Dictionary](#creating-dataframe-using-dictionary)
+ - [Creating DataFrames from a List of Dictionaries](#creating-dataframes-from-a-list-of-dictionaries)
+ - [Reading CSV File Using Pandas](#reading-csv-file-using-pandas)
+ - [Data Exploration](#data-exploration)
+ - [Modifying a DataFrame](#modifying-a-dataframe)
+ - [Creating a DataFrame](#creating-a-dataframe)
+ - [Adding a New Column](#adding-a-new-column)
+ - [Modifying column values](#modifying-column-values)
+ - [Formating DataFrame columns](#formating-dataframe-columns)
+ - [Checking data types of Column values](#checking-data-types-of-column-values)
+ - [Boolean Indexing](#boolean-indexing)
+ - [Exercises: Day 25](#exercises-day-25)
+
+# 📘 Day 25
+
+## Pandas
+
+Pandas is an open source, high-performance, easy-to-use data structures and data analysis tools for the Python programming language.
+Pandas adds data structures and tools designed to work with table-like data which is *Series* and *Data Frames*.
+Pandas provides tools for data manipulation:
+
+- reshaping
+- merging
+- sorting
+- slicing
+- aggregation
+- imputation.
+If you are using anaconda, you do not have install pandas.
+
+### Installing Pandas
+
+For Mac:
+```py
+pip install conda
+conda install pandas
+```
+
+For Windows:
+```py
+pip install conda
+pip install pandas
+```
+
+Pandas data structure is based on *Series* and *DataFrames*.
+
+A *series* is a *column* and a DataFrame is a *multidimensional table* made up of collection of *series*. In order to create a pandas series we should use numpy to create a one dimensional arrays or a python list.
+Let us see an example of a series:
+
+Names Pandas Series
+
+![pandas series](../images/pandas-series-1.png)
+
+Countries Series
+
+![pandas series](../images/pandas-series-2.png)
+
+Cities Series
+
+![pandas series](../images/pandas-series-3.png)
+
+As you can see, pandas series is just one column of data. If we want to have multiple columns we use data frames. The example below shows pandas DataFrames.
+
+Let us see, an example of a pandas data frame:
+
+![Pandas data frame](../images/pandas-dataframe-1.png)
+
+Data frame is a collection of rows and columns. Look at the table below; it has many more columns than the example above:
+
+![Pandas data frame](../images/pandas-dataframe-2.png)
+
+Next, we will see how to import pandas and how to create Series and DataFrames using pandas
+
+### Importing Pandas
+
+```python
+import pandas as pd # importing pandas as pd
+import numpy as np # importing numpy as np
+```
+
+### Creating Pandas Series with Default Index
+
+```python
+nums = [1, 2, 3, 4,5]
+s = pd.Series(nums)
+print(s)
+```
+
+```sh
+ 0 1
+ 1 2
+ 2 3
+ 3 4
+ 4 5
+ dtype: int64
+```
+
+### Creating Pandas Series with custom index
+
+```python
+nums = [1, 2, 3, 4, 5]
+s = pd.Series(nums, index=[1, 2, 3, 4, 5])
+print(s)
+```
+
+```sh
+ 1 1
+ 2 2
+ 3 3
+ 4 4
+ 5 5
+ dtype: int64
+```
+
+```python
+fruits = ['Orange','Banana','Mango']
+fruits = pd.Series(fruits, index=[1, 2, 3])
+print(fruits)
+```
+
+```sh
+ 1 Orange
+ 2 Banana
+ 3 Mango
+ dtype: object
+```
+
+### Creating Pandas Series from a Dictionary
+
+```python
+dct = {'name':'Asabeneh','country':'Finland','city':'Helsinki'}
+```
+
+```python
+s = pd.Series(dct)
+print(s)
+```
+
+```sh
+ name Asabeneh
+ country Finland
+ city Helsinki
+ dtype: object
+```
+
+### Creating a Constant Pandas Series
+
+```python
+s = pd.Series(10, index = [1, 2, 3])
+print(s)
+```
+
+```sh
+ 1 10
+ 2 10
+ 3 10
+ dtype: int64
+```
+
+### Creating a Pandas Series Using Linspace
+
+```python
+s = pd.Series(np.linspace(5, 20, 10)) # linspace(starting, end, items)
+print(s)
+```
+
+```sh
+ 0 5.000000
+ 1 6.666667
+ 2 8.333333
+ 3 10.000000
+ 4 11.666667
+ 5 13.333333
+ 6 15.000000
+ 7 16.666667
+ 8 18.333333
+ 9 20.000000
+ dtype: float64
+```
+
+## DataFrames
+
+Pandas data frames can be created in different ways.
+
+### Creating DataFrames from List of Lists
+
+```python
+data = [
+ ['Asabeneh', 'Finland', 'Helsink'],
+ ['David', 'UK', 'London'],
+ ['John', 'Sweden', 'Stockholm']
+]
+df = pd.DataFrame(data, columns=['Names','Country','City'])
+print(df)
+```
+
+
+
+## Reading CSV File Using Pandas
+
+To download the CSV file, what is needed in this example, console/command line is enough:
+
+```sh
+curl -O https://raw.githubusercontent.com/Asabeneh/30-Days-Of-Python/master/data/weight-height.csv
+```
+
+Put the downloaded file in your working directory.
+
+```python
+import pandas as pd
+
+df = pd.read_csv('weight-height.csv')
+print(df)
+```
+
+### Data Exploration
+
+Let us read only the first 5 rows using head()
+
+```python
+print(df.head()) # give five rows we can increase the number of rows by passing argument to the head() method
+```
+
+
+
+
+
+
+
Gender
+
Height
+
Weight
+
+
+
+
+
0
+
Male
+
73.847017
+
241.893563
+
+
+
1
+
Male
+
68.781904
+
162.310473
+
+
+
2
+
Male
+
74.110105
+
212.740856
+
+
+
3
+
Male
+
71.730978
+
220.042470
+
+
+
4
+
Male
+
69.881796
+
206.349801
+
+
+
+
+Let us also explore the last recordings of the dataframe using the tail() methods.
+
+```python
+print(df.tail()) # tails give the last five rows, we can increase the rows by passing argument to tail method
+```
+
+
+
+
+
+
Gender
+
Height
+
Weight
+
+
+
+
+
9995
+
Female
+
66.172652
+
136.777454
+
+
+
9996
+
Female
+
67.067155
+
170.867906
+
+
+
9997
+
Female
+
63.867992
+
128.475319
+
+
+
9998
+
Female
+
69.034243
+
163.852461
+
+
+
9999
+
Female
+
61.944246
+
113.649103
+
+
+
+
+As you can see the csv file has three rows: Gender, Height and Weight. If the DataFrame would have a long rows, it would be hard to know all the columns. Therefore, we should use a method to know the colums. we do not know the number of rows. Let's use shape meathod.
+
+```python
+print(df.shape) # as you can see 10000 rows and three columns
+```
+
+ (10000, 3)
+
+Let us get all the columns using columns.
+
+```python
+print(df.columns)
+```
+
+ Index(['Gender', 'Height', 'Weight'], dtype='object')
+
+Now, let us get a specific column using the column key
+
+```python
+heights = df['Height'] # this is now a series
+```
+
+```python
+print(heights)
+```
+
+```sh
+ 0 73.847017
+ 1 68.781904
+ 2 74.110105
+ 3 71.730978
+ 4 69.881796
+ ...
+ 9995 66.172652
+ 9996 67.067155
+ 9997 63.867992
+ 9998 69.034243
+ 9999 61.944246
+ Name: Height, Length: 10000, dtype: float64
+```
+
+```python
+weights = df['Weight'] # this is now a series
+```
+
+```python
+print(weights)
+```
+
+```sh
+ 0 241.893563
+ 1 162.310473
+ 2 212.740856
+ 3 220.042470
+ 4 206.349801
+ ...
+ 9995 136.777454
+ 9996 170.867906
+ 9997 128.475319
+ 9998 163.852461
+ 9999 113.649103
+ Name: Weight, Length: 10000, dtype: float64
+```
+
+```python
+print(len(heights) == len(weights))
+```
+
+ True
+
+The describe() method provides a descriptive statistical values of a dataset.
+
+```python
+print(heights.describe()) # give statisical information about height data
+```
+
+```sh
+ count 10000.000000
+ mean 66.367560
+ std 3.847528
+ min 54.263133
+ 25% 63.505620
+ 50% 66.318070
+ 75% 69.174262
+ max 78.998742
+ Name: Height, dtype: float64
+```
+
+```python
+print(weights.describe())
+```
+
+```sh
+ count 10000.000000
+ mean 161.440357
+ std 32.108439
+ min 64.700127
+ 25% 135.818051
+ 50% 161.212928
+ 75% 187.169525
+ max 269.989699
+ Name: Weight, dtype: float64
+```
+
+```python
+print(df.describe()) # describe can also give statistical information from a dataFrame
+```
+
+
+
+
+
+
Height
+
Weight
+
+
+
+
+
count
+
10000.000000
+
10000.000000
+
+
+
mean
+
66.367560
+
161.440357
+
+
+
std
+
3.847528
+
32.108439
+
+
+
min
+
54.263133
+
64.700127
+
+
+
25%
+
63.505620
+
135.818051
+
+
+
50%
+
66.318070
+
161.212928
+
+
+
75%
+
69.174262
+
187.169525
+
+
+
max
+
78.998742
+
269.989699
+
+
+
+
+Similar to describe(), the info() method also give information about the dataset.
+
+## Modifying a DataFrame
+
+Modifying a DataFrame:
+ * We can create a new DataFrame
+ * We can create a new column and add it to the DataFrame,
+ * we can remove an existing column from a DataFrame,
+ * we can modify an existing column in a DataFrame,
+ * we can change the data type of column values in the DataFrame
+
+### Creating a DataFrame
+
+As always, first we import the necessary packages. Now, lets import pandas and numpy, two best friends ever.
+
+```python
+import pandas as pd
+import numpy as np
+data = [
+ {"Name": "Asabeneh", "Country":"Finland","City":"Helsinki"},
+ {"Name": "David", "Country":"UK","City":"London"},
+ {"Name": "John", "Country":"Sweden","City":"Stockholm"}]
+df = pd.DataFrame(data)
+print(df)
+```
+
+
+
+
+
+
Name
+
Country
+
City
+
+
+
+
+
0
+
Asabeneh
+
Finland
+
Helsinki
+
+
+
1
+
David
+
UK
+
London
+
+
+
2
+
John
+
Sweden
+
Stockholm
+
+
+
+
+Adding a column to a DataFrame is like adding a key to a dictionary.
+
+First let's use the previous example to create a DataFrame. After we create the DataFrame, we will start modifying the columns and column values.
+
+### Adding a New Column
+
+Let's add a weight column in the DataFrame
+
+```python
+weights = [74, 78, 69]
+df['Weight'] = weights
+df
+```
+
+
+
+
+
+
Name
+
Country
+
City
+
Weight
+
+
+
+
+
0
+
Asabeneh
+
Finland
+
Helsinki
+
74
+
+
+
1
+
David
+
UK
+
London
+
78
+
+
+
2
+
John
+
Sweden
+
Stockholm
+
69
+
+
+
+
+Let's add a height column into the DataFrame aswell
+
+```python
+heights = [173, 175, 169]
+df['Height'] = heights
+print(df)
+```
+
+
+
+
+
+
Name
+
Country
+
City
+
Weight
+
Height
+
+
+
+
+
0
+
Asabeneh
+
Finland
+
Helsinki
+
74
+
173
+
+
+
1
+
David
+
UK
+
London
+
78
+
175
+
+
+
2
+
John
+
Sweden
+
Stockholm
+
69
+
169
+
+
+
+
+As you can see in the DataFrame above, we did add new columns, Weight and Height. Let's add one additional column called BMI(Body Mass Index) by calculating their BMI using thier mass and height. BMI is mass divided by height squared (in meters) - Weight/Height * Height.
+
+As you can see, the height is in centimeters, so we shoud change it to meters. Let's modify the height row.
+
+### Modifying column values
+
+```python
+df['Height'] = df['Height'] * 0.01
+df
+```
+
+
+
+
+
+
Name
+
Country
+
City
+
Weight
+
Height
+
+
+
+
+
0
+
Asabeneh
+
Finland
+
Helsinki
+
74
+
1.73
+
+
+
1
+
David
+
UK
+
London
+
78
+
1.75
+
+
+
2
+
John
+
Sweden
+
Stockholm
+
69
+
1.69
+
+
+
+
+```python
+# Using functions makes our code clean, but you can calculate the bmi without one
+def calculate_bmi ():
+ weights = df['Weight']
+ heights = df['Height']
+ bmi = []
+ for w,h in zip(weights, heights):
+ b = w/(h*h)
+ bmi.append(b)
+ return bmi
+
+bmi = calculate_bmi()
+
+```
+
+
+```python
+df['BMI'] = bmi
+df
+```
+
+
+
+
+
+
Name
+
Country
+
City
+
Weight
+
Height
+
BMI
+
+
+
+
+
0
+
Asabeneh
+
Finland
+
Helsinki
+
74
+
1.73
+
24.725183
+
+
+
1
+
David
+
UK
+
London
+
78
+
1.75
+
25.469388
+
+
+
2
+
John
+
Sweden
+
Stockholm
+
69
+
1.69
+
24.158818
+
+
+
+
+### Formating DataFrame columns
+
+The BMI column values of the DataFrame are float with many significant digits after decimal. Let's change it to one significant digit after point.
+
+```python
+df['BMI'] = round(df['BMI'], 1)
+print(df)
+```
+
+
+
+
+
+
Name
+
Country
+
City
+
Weight
+
Height
+
BMI
+
+
+
+
+
0
+
Asabeneh
+
Finland
+
Helsinki
+
74
+
1.73
+
24.7
+
+
+
1
+
David
+
UK
+
London
+
78
+
1.75
+
25.5
+
+
+
2
+
John
+
Sweden
+
Stockholm
+
69
+
1.69
+
24.2
+
+
+
+
+The information in the DataFrame seems not yet complete, let's add birth year and current year columns.
+
+```python
+birth_year = ['1769', '1985', '1990']
+current_year = pd.Series(2020, index=[0, 1,2])
+df['Birth Year'] = birth_year
+df['Current Year'] = current_year
+df
+```
+
+
+
+
+
+
Name
+
Country
+
City
+
Weight
+
Height
+
BMI
+
Birth Year
+
Current Year
+
+
+
+
+
0
+
Asabeneh
+
Finland
+
Helsinki
+
74
+
1.73
+
24.7
+
1769
+
2020
+
+
+
1
+
David
+
UK
+
London
+
78
+
1.75
+
25.5
+
1985
+
2020
+
+
+
2
+
John
+
Sweden
+
Stockholm
+
69
+
1.69
+
24.2
+
1990
+
2020
+
+
+
+
+## Checking data types of Column values
+
+```python
+print(df.Weight.dtype)
+```
+
+```sh
+ dtype('int64')
+```
+
+```python
+df['Birth Year'].dtype # it gives string object , we should change this to number
+
+```
+
+```python
+df['Birth Year'] = df['Birth Year'].astype('int')
+print(df['Birth Year'].dtype) # let's check the data type now
+```
+
+```sh
+ dtype('int32')
+```
+
+Now same for the current year:
+
+```python
+df['Current Year'] = df['Current Year'].astype('int')
+df['Current Year'].dtype
+```
+
+```sh
+ dtype('int32')
+```
+
+Now, the column values of birth year and current year are integers. We can calculate the age.
+
+```python
+ages = df['Current Year'] - df['Birth Year']
+ages
+```
+
+ 0 251
+ 1 35
+ 2 30
+ dtype: int32
+
+```python
+df['Ages'] = ages
+print(df)
+```
+
+
+
+
+
+
Name
+
Country
+
City
+
Weight
+
Height
+
BMI
+
Birth Year
+
Current Year
+
Ages
+
+
+
+
+
0
+
Asabeneh
+
Finland
+
Helsinki
+
74
+
1.73
+
24.7
+
1769
+
2019
+
250
+
+
+
1
+
David
+
UK
+
London
+
78
+
1.75
+
25.5
+
1985
+
2019
+
34
+
+
+
2
+
John
+
Sweden
+
Stockholm
+
69
+
1.69
+
24.2
+
1990
+
2019
+
29
+
+
+
+
+The person in the first row lived so far for 251 years. It is unlikely for someone to live so long. Either it is a typo or the data is cooked. So lets fill that data with average of the columns without including outlier.
+
+mean = (35 + 30)/ 2
+
+```python
+mean = (35 + 30)/ 2
+print('Mean: ',mean) #it is good to add some description to the output, so we know what is what
+```
+
+```sh
+ Mean: 32.5
+```
+
+### Boolean Indexing
+
+```python
+print(df[df['Ages'] > 120])
+```
+
+
Pandas is an open source, high-performance, easy-to-use data structures and data analysis tools for the Python programming language.
+Pandas adds data structures and tools designed to work with table-like data which is Series and Data Frames.
+Pandas provides tools for data manipulation:
+
+
reshaping
+
merging
+
sorting
+
slicing
+
aggregation
+
imputation.
+If you are using anaconda, you do not have install pandas.
Pandas data structure is based on Series and DataFrames.
+
A series is a column and a DataFrame is a multidimensional table made up of collection of series. In order to create a pandas series we should use numpy to create a one dimensional arrays or a python list.
+Let us see an example of a series:
+
Names Pandas Series
+
+
Countries Series
+
+
Cities Series
+
+
As you can see, pandas series is just one column of data. If we want to have multiple columns we use data frames. The example below shows pandas DataFrames.
+
Let us see, an example of a pandas data frame:
+
+
Data frame is a collection of rows and columns. Look at the table below; it has many more columns than the example above:
+
+
Next, we will see how to import pandas and how to create Series and DataFrames using pandas
print(df.head())# give five rows we can increase the number of rows by passing argument to the head() method
+
+
+
+
+
+
Gender
+
Height
+
Weight
+
+
+
+
+
0
+
Male
+
73.847017
+
241.893563
+
+
+
1
+
Male
+
68.781904
+
162.310473
+
+
+
2
+
Male
+
74.110105
+
212.740856
+
+
+
3
+
Male
+
71.730978
+
220.042470
+
+
+
4
+
Male
+
69.881796
+
206.349801
+
+
+
+
+
Let us also explore the last recordings of the dataframe using the tail() methods.
+
print(df.tail())# tails give the last five rows, we can increase the rows by passing argument to tail method
+
+
+
+
+
+
Gender
+
Height
+
Weight
+
+
+
+
+
9995
+
Female
+
66.172652
+
136.777454
+
+
+
9996
+
Female
+
67.067155
+
170.867906
+
+
+
9997
+
Female
+
63.867992
+
128.475319
+
+
+
9998
+
Female
+
69.034243
+
163.852461
+
+
+
9999
+
Female
+
61.944246
+
113.649103
+
+
+
+
+
As you can see the csv file has three rows: Gender, Height and Weight. If the DataFrame would have a long rows, it would be hard to know all the columns. Therefore, we should use a method to know the colums. we do not know the number of rows. Let's use shape meathod.
+
print(df.shape)# as you can see 10000 rows and three columns
+
Modifying a DataFrame:
+ * We can create a new DataFrame
+ * We can create a new column and add it to the DataFrame,
+ * we can remove an existing column from a DataFrame,
+ * we can modify an existing column in a DataFrame,
+ * we can change the data type of column values in the DataFrame
As you can see in the DataFrame above, we did add new columns, Weight and Height. Let's add one additional column called BMI(Body Mass Index) by calculating their BMI using thier mass and height. BMI is mass divided by height squared (in meters) - Weight/Height * Height.
+
As you can see, the height is in centimeters, so we shoud change it to meters. Let's modify the height row.
# Using functions makes our code clean, but you can calculate the bmi without one
+defcalculate_bmi():
+ weights=df['Weight']
+ heights=df['Height']
+ bmi=[]
+ forw,hinzip(weights,heights):
+ b=w/(h*h)
+ bmi.append(b)
+ returnbmi
+
+bmi=calculate_bmi()
+
Now, the column values of birth year and current year are integers. We can calculate the age.
+
ages=df['Current Year']-df['Birth Year']
+ages
+
+
0 251
+1 35
+2 30
+dtype: int32
+
+
df['Ages']=ages
+print(df)
+
+
+
+
+
+
Name
+
Country
+
City
+
Weight
+
Height
+
BMI
+
Birth Year
+
Current Year
+
Ages
+
+
+
+
+
0
+
Asabeneh
+
Finland
+
Helsinki
+
74
+
1.73
+
24.7
+
1769
+
2019
+
250
+
+
+
1
+
David
+
UK
+
London
+
78
+
1.75
+
25.5
+
1985
+
2019
+
34
+
+
+
2
+
John
+
Sweden
+
Stockholm
+
69
+
1.69
+
24.2
+
1990
+
2019
+
29
+
+
+
+
+
The person in the first row lived so far for 251 years. It is unlikely for someone to live so long. Either it is a typo or the data is cooked. So lets fill that data with average of the columns without including outlier.
+
mean = (35 + 30)/ 2
+
mean=(35+30)/2
+print('Mean: ',mean)#it is good to add some description to the output, so we know what is what
+
+
+
+[<< Day 25 ](../25_Day_Pandas/25_pandas.md) | [Day 27 >>](../27_Day_Python_with_mongodb/27_python_with_mongodb.md)
+
+![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
+
+- [📘 Day 26](#-day-26)
+ - [Python for Web](#python-for-web)
+ - [Flask](#flask)
+ - [Folder structure](#folder-structure)
+ - [Setting up your project directory](#setting-up-your-project-directory)
+ - [Creating routes](#creating-routes)
+ - [Creating templates](#creating-templates)
+ - [Python Script](#python-script)
+ - [Navigation](#navigation)
+ - [Creating a layout](#creating-a-layout)
+ - [Serving Static File](#serving-static-file)
+ - [Deployment](#deployment)
+ - [Creating Heroku account](#creating-heroku-account)
+ - [Login to Heroku](#login-to-heroku)
+ - [Create requirements and Procfile](#create-requirements-and-procfile)
+ - [Pushing project to heroku](#pushing-project-to-heroku)
+ - [Exercises: Day 26](#exercises-day-26)
+
+# 📘 Day 26
+
+## Python for Web
+
+Python is a general purpose programming language and it can be used for many places. In this section, we will see how we use Python for the web. There are many Python web frame works. Django and Flask are the most popular ones. Today, we will see how to use Flask for web development.
+
+### Flask
+
+Flask is a web development framework written in Python. Flask uses Jinja2 template engine. Flask can be also used with other modern front libraries such as React.
+
+If you did not install the virtualenv package yet install it first. Virtual environment will allows to isolate project dependencies from the local machine dependencies.
+
+#### Folder structure
+
+After completing all the step, your project file structure should look like this:
+
+```sh
+
+├── Procfile
+├── app.py
+├── env
+│ ├── bin
+├── requirements.txt
+├── static
+│ └── css
+│ └── main.css
+└── templates
+ ├── about.html
+ ├── home.html
+ ├── layout.html
+ ├── post.html
+ └── result.html
+```
+
+### Setting up your project directory
+
+Follow the following steps to get started with Flask.
+
+Step 1: install virtualenv using the following command.
+
+```sh
+pip install virtualenv
+```
+
+Step 2:
+
+```sh
+asabeneh@Asabeneh:~/Desktop$ mkdir python_for_web
+asabeneh@Asabeneh:~/Desktop$ cd python_for_web/
+asabeneh@Asabeneh:~/Desktop/python_for_web$ virtualenv venv
+asabeneh@Asabeneh:~/Desktop/python_for_web$ source venv/bin/activate
+(env) asabeneh@Asabeneh:~/Desktop/python_for_web$ pip freeze
+(env) asabeneh@Asabeneh:~/Desktop/python_for_web$ pip install Flask
+(env) asabeneh@Asabeneh:~/Desktop/python_for_web$ pip freeze
+Click==7.0
+Flask==1.1.1
+itsdangerous==1.1.0
+Jinja2==2.10.3
+MarkupSafe==1.1.1
+Werkzeug==0.16.0
+(env) asabeneh@Asabeneh:~/Desktop/python_for_web$
+```
+
+We created a project director named python_for_web. Inside the project we created a virtual environment *venv* which could be any name but I prefer to call it _venv_. Then we activated the virtual environment. We used pip freeze to check the installed packages in the project directory. The result of pip freeze was empty because a package was not installed yet.
+
+Now, let's create app.py file in the project directory and write the following code. The app.py file will be the main file in the project. The following code has flask module, os module.
+
+### Creating routes
+
+The home route.
+
+```py
+# let's import the flask
+from flask import Flask
+import os # importing operating system module
+
+app = Flask(__name__)
+
+@app.route('/') # this decorator create the home route
+def home ():
+ return '
'
+
+
+if __name__ == '__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+To run the flask application, write python app.py in the main flask application directory.
+
+After you run _python app.py_ check local host 5000.
+
+Let us add additional route.
+Creating about route
+
+```py
+# let's import the flask
+from flask import Flask
+import os # importing operating system module
+
+app = Flask(__name__)
+
+@app.route('/') # this decorator create the home route
+def home ():
+ return '
'
+
+if __name__ == '__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+Now, we added the about route in the above code. How about if we want to render an HTML file instead of string? It is possible to render HTML file using the function *render_templae*. Let us create a folder called templates and create home.html and about.html in the project directory. Let us also import the *render_template* function from flask.
+
+### Creating templates
+
+Create the HTML files inside templates folder.
+
+home.html
+
+```html
+
+
+
+
+
+ Home
+
+
+
+
+
+
+```
+
+### Python Script
+
+app.py
+
+```py
+# let's import the flask
+from flask import Flask, render_template
+import os # importing operating system module
+
+app = Flask(__name__)
+
+@app.route('/') # this decorator create the home route
+def home ():
+ return render_template('home.html')
+
+@app.route('/about')
+def about():
+ return render_template('about.html')
+
+if __name__ == '__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+As you can see to go to different pages or to navigate we need a navigation. Let's add a link to each page or let's create a layout which we use to every page.
+
+### Navigation
+
+```html
+
+```
+
+Now, we can navigate between the pages using the above link. Let us create additional page which handle form data. You can call it any name, I like to call it post.html.
+
+We can inject data to the HTML files using Jinja2 template engine.
+
+```py
+# let's import the flask
+from flask import Flask, render_template, request, redirect, url_for
+import os # importing operating system module
+
+app = Flask(__name__)
+
+@app.route('/') # this decorator create the home route
+def home ():
+ techs = ['HTML', 'CSS', 'Flask', 'Python']
+ name = '30 Days Of Python Programming'
+ return render_template('home.html', techs=techs, name = name, title = 'Home')
+
+@app.route('/about')
+def about():
+ name = '30 Days Of Python Programming'
+ return render_template('about.html', name = name, title = 'About Us')
+
+@app.route('/post')
+def post():
+ name = 'Text Analyzer'
+ return render_template('post.html', name = name, title = name)
+
+
+if __name__ == '__main__':
+ # for deployment
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+Let's see the templates too:
+
+home.html
+
+```html
+
+
+
+
+
+ Home
+
+
+
+
+
+
+```
+
+### Creating a layout
+
+In the template files, there are lots of repeated codes, we can write a layout and we can remove the repetition. Let's create layout.html inside the templates folder.
+After we create the layout we will import to every file.
+
+#### Serving Static File
+
+Create a static folder in your project directory. Inside the static folder create CSS or styles folder and create a CSS stylesheet. We use the *url_for* module to serve the static file.
+
+layout.html
+
+```html
+
+
+
+
+
+
+
+ {% if title %}
+ 30 Days of Python - {{ title}}
+ {% else %}
+ 30 Days of Python
+ {% endif %}
+
+
+
+
+
+
+
+ {% block content %} {% endblock %}
+
+
+
+```
+
+Now, lets remove all the repeated code in the other template files and import the layout.html. The href is using _url_for_ function with the name of the route function to connect each navigation route.
+
+home.html
+
+```html
+{% extends 'layout.html' %} {% block content %}
+
+
Welcome to {{name}}
+
+ This application clean texts and analyse the number of word, characters and
+ most frequent words in the text. Check it out by click text analyzer at the
+ menu. You need the following technologies to build this web application:
+
+
+{% endblock %}
+```
+
+Request methods, there are different request methods(GET, POST, PUT, DELETE) are the common request methods which allow us to do CRUD(Create, Read, Update, Delete) operation.
+
+In the post, route we will use GET and POST method alternative depending on the type of request, check how it looks in the code below. The request method is a function to handle request methods and also to access form data.
+app.py
+
+```py
+# let's import the flask
+from flask import Flask, render_template, request, redirect, url_for
+import os # importing operating system module
+
+app = Flask(__name__)
+# to stop caching static file
+app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
+
+
+
+@app.route('/') # this decorator create the home route
+def home ():
+ techs = ['HTML', 'CSS', 'Flask', 'Python']
+ name = '30 Days Of Python Programming'
+ return render_template('home.html', techs=techs, name = name, title = 'Home')
+
+@app.route('/about')
+def about():
+ name = '30 Days Of Python Programming'
+ return render_template('about.html', name = name, title = 'About Us')
+
+@app.route('/result')
+def result():
+ return render_template('result.html')
+
+@app.route('/post', methods= ['GET','POST'])
+def post():
+ name = 'Text Analyzer'
+ if request.method == 'GET':
+ return render_template('post.html', name = name, title = name)
+ if request.method =='POST':
+ content = request.form['content']
+ print(content)
+ return redirect(url_for('result'))
+
+if __name__ == '__main__':
+ # for deployment
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+So far, we have seen how to use template and how to inject data to template, how to a common layout.
+Now, lets handle static file. Create a folder called static in the project director and create a folder called css. Inside css folder create main.css. Your main. css file will be linked to the layout.html.
+
+You don't have to write the css file, copy and use it. Let's move on to deployment.
+
+### Deployment
+
+#### Creating Heroku account
+
+Heroku provides a free deployment service for both front end and fullstack applications. Create an account on [heroku](https://www.heroku.com/) and install the heroku [CLI](https://devcenter.heroku.com/articles/heroku-cli) for you machine.
+After installing heroku write the following command
+
+#### Login to Heroku
+
+```sh
+asabeneh@Asabeneh:~$ heroku login
+heroku: Press any key to open up the browser to login or q to exit:
+```
+
+Let's see the result by clicking any key from the keyboard. When you press any key from you keyboard it will open the heroku login page and click the login page. Then you will local machine will be connected to the remote heroku server. If you are connected to remote server, you will see this.
+
+```sh
+asabeneh@Asabeneh:~$ heroku login
+heroku: Press any key to open up the browser to login or q to exit:
+Opening browser to https://cli-auth.heroku.com/auth/browser/be12987c-583a-4458-a2c2-ba2ce7f41610
+Logging in... done
+Logged in as asabeneh@gmail.com
+asabeneh@Asabeneh:~$
+```
+
+#### Create requirements and Procfile
+
+Before we push our code to remote server, we need requirements
+
+- requirements.txt
+- Procfile
+
+```sh
+(env) asabeneh@Asabeneh:~/Desktop/python_for_web$ pip freeze
+Click==7.0
+Flask==1.1.1
+itsdangerous==1.1.0
+Jinja2==2.10.3
+MarkupSafe==1.1.1
+Werkzeug==0.16.0
+(env) asabeneh@Asabeneh:~/Desktop/python_for_web$ touch requirements.txt
+(env) asabeneh@Asabeneh:~/Desktop/python_for_web$ pip freeze > requirements.txt
+(env) asabeneh@Asabeneh:~/Desktop/python_for_web$ cat requirements.txt
+Click==7.0
+Flask==1.1.1
+itsdangerous==1.1.0
+Jinja2==2.10.3
+MarkupSafe==1.1.1
+Werkzeug==0.16.0
+(env) asabeneh@Asabeneh:~/Desktop/python_for_web$ touch Procfile
+(env) asabeneh@Asabeneh:~/Desktop/python_for_web$ ls
+Procfile env/ static/
+app.py requirements.txt templates/
+(env) asabeneh@Asabeneh:~/Desktop/python_for_web$
+```
+
+The Procfile will have the command which run the application in the web server in our case on Heroku.
+
+```sh
+web: python app.py
+```
+
+#### Pushing project to heroku
+
+Now, it is ready to be deployed. Steps to deploy the application on heroku
+
+1. git init
+2. git add .
+3. git commit -m "commit message"
+4. heroku create 'name of the app as one word'
+5. git push heroku master
+6. heroku open(to launch the deployed application)
+
+After this step you will get an application like [this](http://thirdaysofpython-practice.herokuapp.com/)
+
+## Exercises: Day 26
+
+1. You will build [this application](https://thirtydaysofpython-v1-final.herokuapp.com/). Only the text analyser part is left
+
+
+🎉 CONGRATULATIONS ! 🎉
+
+[<< Day 25 ](../25_Day_Pandas/25_pandas.md) | [Day 27 >>](../27_Day_Python_with_mongodb/27_python_with_mongodb.md)
\ No newline at end of file
diff --git a/26_python_web/index.html b/26_python_web/index.html
new file mode 100644
index 0000000..d8e2475
--- /dev/null
+++ b/26_python_web/index.html
@@ -0,0 +1,1947 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 26 python web - 30天入门Python全栈开发
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Python is a general purpose programming language and it can be used for many places. In this section, we will see how we use Python for the web. There are many Python web frame works. Django and Flask are the most popular ones. Today, we will see how to use Flask for web development.
Flask is a web development framework written in Python. Flask uses Jinja2 template engine. Flask can be also used with other modern front libraries such as React.
+
If you did not install the virtualenv package yet install it first. Virtual environment will allows to isolate project dependencies from the local machine dependencies.
We created a project director named python_for_web. Inside the project we created a virtual environment venv which could be any name but I prefer to call it venv. Then we activated the virtual environment. We used pip freeze to check the installed packages in the project directory. The result of pip freeze was empty because a package was not installed yet.
+
Now, let's create app.py file in the project directory and write the following code. The app.py file will be the main file in the project. The following code has flask module, os module.
# let's import the flask
+fromflaskimportFlask
+importos# importing operating system module
+
+app=Flask(__name__)
+
+@app.route('/')# this decorator create the home route
+defhome():
+ return'<h1>Welcome</h1>'
+
+@app.route('/about')
+defabout():
+ return'<h1>About us</h1>'
+
+
+if__name__=='__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
+
To run the flask application, write python app.py in the main flask application directory.
+
After you run python app.py check local host 5000.
+
Let us add additional route.
+Creating about route
+
# let's import the flask
+fromflaskimportFlask
+importos# importing operating system module
+
+app=Flask(__name__)
+
+@app.route('/')# this decorator create the home route
+defhome():
+ return'<h1>Welcome</h1>'
+
+@app.route('/about')
+defabout():
+ return'<h1>About us</h1>'
+
+if__name__=='__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
+
Now, we added the about route in the above code. How about if we want to render an HTML file instead of string? It is possible to render HTML file using the function render_templae. Let us create a folder called templates and create home.html and about.html in the project directory. Let us also import the render_template function from flask.
# let's import the flask
+fromflaskimportFlask,render_template
+importos# importing operating system module
+
+app=Flask(__name__)
+
+@app.route('/')# this decorator create the home route
+defhome():
+ returnrender_template('home.html')
+
+@app.route('/about')
+defabout():
+ returnrender_template('about.html')
+
+if__name__=='__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
+
As you can see to go to different pages or to navigate we need a navigation. Let's add a link to each page or let's create a layout which we use to every page.
Now, we can navigate between the pages using the above link. Let us create additional page which handle form data. You can call it any name, I like to call it post.html.
+
We can inject data to the HTML files using Jinja2 template engine.
+
# let's import the flask
+fromflaskimportFlask,render_template,request,redirect,url_for
+importos# importing operating system module
+
+app=Flask(__name__)
+
+@app.route('/')# this decorator create the home route
+defhome():
+ techs=['HTML','CSS','Flask','Python']
+ name='30 Days Of Python Programming'
+ returnrender_template('home.html',techs=techs,name=name,title='Home')
+
+@app.route('/about')
+defabout():
+ name='30 Days Of Python Programming'
+ returnrender_template('about.html',name=name,title='About Us')
+
+@app.route('/post')
+defpost():
+ name='Text Analyzer'
+ returnrender_template('post.html',name=name,title=name)
+
+
+if__name__=='__main__':
+ # for deployment
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
In the template files, there are lots of repeated codes, we can write a layout and we can remove the repetition. Let's create layout.html inside the templates folder.
+After we create the layout we will import to every file.
Create a static folder in your project directory. Inside the static folder create CSS or styles folder and create a CSS stylesheet. We use the url_for module to serve the static file.
Now, lets remove all the repeated code in the other template files and import the layout.html. The href is using url_for function with the name of the route function to connect each navigation route.
+
home.html
+
{% extends 'layout.html' %} {% block content %}
+<divclass="container">
+ <h1>Welcome to {{name}}</h1>
+ <p>
+ This application clean texts and analyse the number of word, characters and
+ most frequent words in the text. Check it out by click text analyzer at the
+ menu. You need the following technologies to build this web application:
+ </p>
+ <ulclass="tech-lists">
+ {% for tech in techs %}
+ <liclass="tech">{{tech}}</li>
+
+ {% endfor %}
+ </ul>
+</div>
+
+{% endblock %}
+
+
about.html
+
{% extends 'layout.html' %} {% block content %}
+<divclass="container">
+ <h1>About {{name}}</h1>
+ <p>
+ This is a 30 days of python programming challenge. If you have been coding
+ this far, you are awesome. Congratulations for the job well done!
+ </p>
+</div>
+{% endblock %}
+
Request methods, there are different request methods(GET, POST, PUT, DELETE) are the common request methods which allow us to do CRUD(Create, Read, Update, Delete) operation.
+
In the post, route we will use GET and POST method alternative depending on the type of request, check how it looks in the code below. The request method is a function to handle request methods and also to access form data.
+app.py
+
# let's import the flask
+fromflaskimportFlask,render_template,request,redirect,url_for
+importos# importing operating system module
+
+app=Flask(__name__)
+# to stop caching static file
+app.config['SEND_FILE_MAX_AGE_DEFAULT']=0
+
+
+
+@app.route('/')# this decorator create the home route
+defhome():
+ techs=['HTML','CSS','Flask','Python']
+ name='30 Days Of Python Programming'
+ returnrender_template('home.html',techs=techs,name=name,title='Home')
+
+@app.route('/about')
+defabout():
+ name='30 Days Of Python Programming'
+ returnrender_template('about.html',name=name,title='About Us')
+
+@app.route('/result')
+defresult():
+ returnrender_template('result.html')
+
+@app.route('/post',methods=['GET','POST'])
+defpost():
+ name='Text Analyzer'
+ ifrequest.method=='GET':
+ returnrender_template('post.html',name=name,title=name)
+ ifrequest.method=='POST':
+ content=request.form['content']
+ print(content)
+ returnredirect(url_for('result'))
+
+if__name__=='__main__':
+ # for deployment
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
+
So far, we have seen how to use template and how to inject data to template, how to a common layout.
+Now, lets handle static file. Create a folder called static in the project director and create a folder called css. Inside css folder create main.css. Your main. css file will be linked to the layout.html.
+
You don't have to write the css file, copy and use it. Let's move on to deployment.
Heroku provides a free deployment service for both front end and fullstack applications. Create an account on heroku and install the heroku CLI for you machine.
+After installing heroku write the following command
Let's see the result by clicking any key from the keyboard. When you press any key from you keyboard it will open the heroku login page and click the login page. Then you will local machine will be connected to the remote heroku server. If you are connected to remote server, you will see this.
+
+[<< Day 26](../26_Day_Python_web/26_python_web.md) | [Day 28 >>](../28_Day_API/28_API.md)
+
+![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
+
+- [📘 Day 27](#-day-27)
+- [Python with MongoDB](#python-with-mongodb)
+ - [MongoDB](#mongodb)
+ - [SQL versus NoSQL](#sql-versus-nosql)
+ - [Getting Connection String(MongoDB URI)](#getting-connection-stringmongodb-uri)
+ - [Connecting Flask application to MongoDB Cluster](#connecting-flask-application-to-mongodb-cluster)
+ - [Creating a database and collection](#creating-a-database-and-collection)
+ - [Inserting many documents to collection](#inserting-many-documents-to-collection)
+ - [MongoDB Find](#mongodb-find)
+ - [Find with Query](#find-with-query)
+ - [Find query with modifier](#find-query-with-modifier)
+ - [Limiting documents](#limiting-documents)
+ - [Find with sort](#find-with-sort)
+ - [Update with query](#update-with-query)
+ - [Delete Document](#delete-document)
+ - [Drop a collection](#drop-a-collection)
+ - [💻 Exercises: Day 27](#-exercises-day-27)
+
+# 📘 Day 27
+
+# Python with MongoDB
+
+Python is a backend technology and it can be connected with different data base applications. It can be connected to both SQL and noSQL databases. In this section, we connect Python with MongoDB database which is noSQL database.
+
+## MongoDB
+
+MongoDB is a NoSQL database. MongoDB stores data in a JSON like document which make MongoDB very flexible and scalable. Let us see the different terminologies of SQL and NoSQL databases. The following table will make the difference between SQL versus NoSQL databases.
+
+### SQL versus NoSQL
+
+![SQL versus NoSQL](../images/mongoDB/sql-vs-nosql.png)
+
+In this section, we will focus on a NoSQL database MongoDB. Lets sign up on [mongoDB](https://www.mongodb.com/) by click on the sign in button then click register on the next page.
+
+![MongoDB Sign up pages](../images/mongoDB/mongodb-signup-page.png)
+
+Complete the fields and click continue
+
+![Mongodb register](../images/mongoDB/mongodb-register.png)
+
+Select the free plan
+
+![Mongodb free plan](../images/mongoDB/mongodb-free.png)
+
+Choose the proximate free region and give any name for you cluster.
+
+![Mongodb cluster name](../images/mongoDB/mongodb-cluster-name.png)
+
+Now, a free sandbox is created
+
+![Mongodb sandbox](../images/mongoDB/mongodb-sandbox.png)
+
+All local host access
+
+![Mongodb allow ip access](../images/mongoDB/mongodb-allow-ip-access.png)
+
+Add user and password
+
+![Mongodb add user](../images/mongoDB/mongodb-add-user.png)
+
+Create a mongoDB uri link
+
+![Mongodb create uri](../images/mongoDB/mongodb-create-uri.png)
+
+Select Python 3.6 or above driver
+
+![Mongodb python driver](../images/mongoDB/mongodb-python-driver.png)
+
+### Getting Connection String(MongoDB URI)
+
+Copy the connection string link and you will get something like this
+
+```sh
+mongodb+srv://asabeneh:@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority
+```
+
+Do not worry about the url, it is a means to connect your application with mongoDB.
+Let us replace the password placeholder with the password you used to add a user.
+
+**Example:**
+
+```sh
+mongodb+srv://asabeneh:123123123@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority
+```
+
+Now, I replaced everything and the password is 123123 and the name of the database is thirty_days_python. This is just an example, your password must be a bit stronger than this.
+
+Python needs a mongoDB driver to access mongoDB database. We will use _pymongo_ with _dnspython_ to connect our application with mongoDB base . Inside your project directory install pymongo and dnspython.
+
+```sh
+pip install pymongo dnspython
+```
+
+The "dnspython" module must be installed to use mongodb+srv:// URIs. The dnspython is a DNS toolkit for Python. It supports almost all record types.
+
+### Connecting Flask application to MongoDB Cluster
+
+```py
+# let's import the flask
+from flask import Flask, render_template
+import os # importing operating system module
+MONGODB_URI = 'mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+print(client.list_database_names())
+
+app = Flask(__name__)
+if __name__ == '__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+
+```
+
+When we run the above code we get the default mongoDB databases.
+
+```sh
+['admin', 'local']
+```
+
+### Creating a database and collection
+
+Let us create a database, database and collection in mongoDB will be created if it doesn't exist. Let's create a data base name _thirty_days_of_python_ and _students_ collection.
+To create a database
+
+```sh
+db = client.name_of_databse # we can create a database like this or the second way
+db = client['name_of_database']
+```
+
+```py
+# let's import the flask
+from flask import Flask, render_template
+import os # importing operating system module
+MONGODB_URI = 'mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+# Creating database
+db = client.thirty_days_of_python
+# Creating students collection and inserting a document
+db.students.insert_one({'name': 'Asabeneh', 'country': 'Finland', 'city': 'Helsinki', 'age': 250})
+print(client.list_database_names())
+
+app = Flask(__name__)
+if __name__ == '__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+After we create a database, we also created a students collection and we used *insert_one()* method to insert a document.
+Now, the database *thirty_days_of_python* and *students* collection have been created and the document has been inserted.
+Check your mongoDB cluster and you will see both the database and the collection. Inside the collection, there will be a document.
+
+```sh
+['thirty_days_of_python', 'admin', 'local']
+```
+
+If you see this on the mongoDB cluster, it means you have successfully created a database and a collection.
+
+![Creating database and collection](../images/mongoDB/mongodb-creating_database.png)
+
+If you have seen on the figure, the document has been created with a long id which acts as a primary key. Every time we create a document mongoDB create and unique id for it.
+
+### Inserting many documents to collection
+
+The *insert_one()* method inserts one item at a time if we want to insert many documents at once either we use *insert_many()* method or for loop.
+We can use for loop to inset many documents at once.
+
+```py
+# let's import the flask
+from flask import Flask, render_template
+import os # importing operating system module
+MONGODB_URI = 'mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+
+students = [
+ {'name':'David','country':'UK','city':'London','age':34},
+ {'name':'John','country':'Sweden','city':'Stockholm','age':28},
+ {'name':'Sami','country':'Finland','city':'Helsinki','age':25},
+ ]
+for student in students:
+ db.students.insert_one(student)
+
+
+app = Flask(__name__)
+if __name__ == '__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+### MongoDB Find
+
+The *find()* and *findOne()* methods are common method to find data in a collection in mongoDB database. It is similar to the SELECT statement in a MySQL database.
+Let us use the _find_one()_ method to get a document in a database collection.
+
+- \*find_one({"\_id": ObjectId("id"}): Gets the first occurrence if an id is not provided
+
+```py
+# let's import the flask
+from flask import Flask, render_template
+import os # importing operating system module
+MONGODB_URI = 'mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+db = client['thirty_days_of_python'] # accessing the database
+student = db.students.find_one()
+print(student)
+
+
+app = Flask(__name__)
+if __name__ == '__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+
+```
+
+```sh
+{'_id': ObjectId('5df68a21f106fe2d315bbc8b'), 'name': 'Asabeneh', 'country': 'Helsinki', 'city': 'Helsinki', 'age': 250}
+```
+
+The above query returns the first entry but we can target specific document using specific \_id. Let us do one example, use David's id to get David object.
+'\_id':ObjectId('5df68a23f106fe2d315bbc8c')
+
+```py
+# let's import the flask
+from flask import Flask, render_template
+import os # importing operating system module
+from bson.objectid import ObjectId # id object
+MONGODB_URI = 'mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+db = client['thirty_days_of_python'] # accessing the database
+student = db.students.find_one({'_id':ObjectId('5df68a23f106fe2d315bbc8c')})
+print(student)
+
+app = Flask(__name__)
+if __name__ == '__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+```sh
+{'_id': ObjectId('5df68a23f106fe2d315bbc8c'), 'name': 'David', 'country': 'UK', 'city': 'London', 'age': 34}
+```
+
+We have seen, how to use _find_one()_ using the above examples. Let's move one to _find()_
+
+- _find()_: returns all the occurrence from a collection if we don't pass a query object. The object is pymongo.cursor object.
+
+```py
+# let's import the flask
+from flask import Flask, render_template
+import os # importing operating system module
+
+MONGODB_URI = 'mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+db = client['thirty_days_of_python'] # accessing the database
+students = db.students.find()
+for student in students:
+ print(student)
+
+app = Flask(__name__)
+if __name__ == '__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+```sh
+{'_id': ObjectId('5df68a21f106fe2d315bbc8b'), 'name': 'Asabeneh', 'country': 'Finland', 'city': 'Helsinki', 'age': 250}
+{'_id': ObjectId('5df68a23f106fe2d315bbc8c'), 'name': 'David', 'country': 'UK', 'city': 'London', 'age': 34}
+{'_id': ObjectId('5df68a23f106fe2d315bbc8d'), 'name': 'John', 'country': 'Sweden', 'city': 'Stockholm', 'age': 28}
+{'_id': ObjectId('5df68a23f106fe2d315bbc8e'), 'name': 'Sami', 'country': 'Finland', 'city': 'Helsinki', 'age': 25}
+```
+
+We can specify which fields to return by passing second object in the _find({}, {})_. 0 means not include and 1 means include but we can not mix 0 and 1, except for \_id.
+
+```py
+# let's import the flask
+from flask import Flask, render_template
+import os # importing operating system module
+
+MONGODB_URI = 'mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+db = client['thirty_days_of_python'] # accessing the database
+students = db.students.find({}, {"_id":0, "name": 1, "country":1}) # 0 means not include and 1 means include
+for student in students:
+ print(student)
+
+app = Flask(__name__)
+if __name__ == '__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+```sh
+{'name': 'Asabeneh', 'country': 'Finland'}
+{'name': 'David', 'country': 'UK'}
+{'name': 'John', 'country': 'Sweden'}
+{'name': 'Sami', 'country': 'Finland'}
+```
+
+### Find with Query
+
+In mongoDB find take a query object. We can pass a query object and we can filter the documents we like to filter out.
+
+```py
+# let's import the flask
+from flask import Flask, render_template
+import os # importing operating system module
+
+MONGODB_URI = 'mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+db = client['thirty_days_of_python'] # accessing the database
+
+query = {
+ "country":"Finland"
+}
+students = db.students.find(query)
+
+for student in students:
+ print(student)
+
+
+app = Flask(__name__)
+if __name__ == '__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+```sh
+{'_id': ObjectId('5df68a21f106fe2d315bbc8b'), 'name': 'Asabeneh', 'country': 'Finland', 'city': 'Helsinki', 'age': 250}
+{'_id': ObjectId('5df68a23f106fe2d315bbc8e'), 'name': 'Sami', 'country': 'Finland', 'city': 'Helsinki', 'age': 25}
+```
+
+Query with modifiers
+
+```py
+# let's import the flask
+from flask import Flask, render_template
+import os # importing operating system module
+import pymongo
+
+MONGODB_URI = 'mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+db = client['thirty_days_of_python'] # accessing the database
+
+query = {
+ "city":"Helsinki"
+}
+students = db.students.find(query)
+for student in students:
+ print(student)
+
+
+app = Flask(__name__)
+if __name__ == '__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+```sh
+{'_id': ObjectId('5df68a21f106fe2d315bbc8b'), 'name': 'Asabeneh', 'country': 'Finland', 'city': 'Helsinki', 'age': 250}
+{'_id': ObjectId('5df68a23f106fe2d315bbc8e'), 'name': 'Sami', 'country': 'Finland', 'city': 'Helsinki', 'age': 25}
+```
+
+### Find query with modifier
+
+```py
+# let's import the flask
+from flask import Flask, render_template
+import os # importing operating system module
+import pymongo
+
+MONGODB_URI = 'mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+db = client['thirty_days_of_python'] # accessing the database
+query = {
+ "country":"Finland",
+ "city":"Helsinki"
+}
+students = db.students.find(query)
+for student in students:
+ print(student)
+
+
+app = Flask(__name__)
+if __name__ == '__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+```sh
+{'_id': ObjectId('5df68a21f106fe2d315bbc8b'), 'name': 'Asabeneh', 'country': 'Finland', 'city': 'Helsinki', 'age': 250}
+{'_id': ObjectId('5df68a23f106fe2d315bbc8e'), 'name': 'Sami', 'country': 'Finland', 'city': 'Helsinki', 'age': 25}
+```
+
+Query with modifiers
+
+```py
+# let's import the flask
+from flask import Flask, render_template
+import os # importing operating system module
+import pymongo
+
+MONGODB_URI = 'mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+db = client['thirty_days_of_python'] # accessing the database
+query = {"age":{"$gt":30}}
+students = db.students.find(query)
+for student in students:
+ print(student)
+
+app = Flask(__name__)
+if __name__ == '__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+```sh
+{'_id': ObjectId('5df68a21f106fe2d315bbc8b'), 'name': 'Asabeneh', 'country': 'Finland', 'city': 'Helsinki', 'age': 250}
+{'_id': ObjectId('5df68a23f106fe2d315bbc8c'), 'name': 'David', 'country': 'UK', 'city': 'London', 'age': 34}
+```
+
+```py
+# let's import the flask
+from flask import Flask, render_template
+import os # importing operating system module
+import pymongo
+
+MONGODB_URI = 'mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+db = client['thirty_days_of_python'] # accessing the database
+query = {"age":{"$gt":30}}
+students = db.students.find(query)
+for student in students:
+ print(student)
+```
+
+```sh
+{'_id': ObjectId('5df68a23f106fe2d315bbc8d'), 'name': 'John', 'country': 'Sweden', 'city': 'Stockholm', 'age': 28}
+{'_id': ObjectId('5df68a23f106fe2d315bbc8e'), 'name': 'Sami', 'country': 'Finland', 'city': 'Helsinki', 'age': 25}
+```
+
+### Limiting documents
+
+We can limit the number of documents we return using the _limit()_ method.
+
+```py
+# let's import the flask
+from flask import Flask, render_template
+import os # importing operating system module
+import pymongo
+
+MONGODB_URI = 'mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+db = client['thirty_days_of_python'] # accessing the database
+db.students.find().limit(3)
+```
+
+### Find with sort
+
+By default, sort is in ascending order. We can change the sorting to descending order by adding -1 parameter.
+
+```py
+# let's import the flask
+from flask import Flask, render_template
+import os # importing operating system module
+import pymongo
+
+MONGODB_URI = 'mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+db = client['thirty_days_of_python'] # accessing the database
+students = db.students.find().sort('name')
+for student in students:
+ print(student)
+
+
+students = db.students.find().sort('name',-1)
+for student in students:
+ print(student)
+
+students = db.students.find().sort('age')
+for student in students:
+ print(student)
+
+students = db.students.find().sort('age',-1)
+for student in students:
+ print(student)
+
+app = Flask(__name__)
+if __name__ == '__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+Ascending order
+
+```sh
+{'_id': ObjectId('5df68a21f106fe2d315bbc8b'), 'name': 'Asabeneh', 'country': 'Finland', 'city': 'Helsinki', 'age': 250}
+{'_id': ObjectId('5df68a23f106fe2d315bbc8c'), 'name': 'David', 'country': 'UK', 'city': 'London', 'age': 34}
+{'_id': ObjectId('5df68a23f106fe2d315bbc8d'), 'name': 'John', 'country': 'Sweden', 'city': 'Stockholm', 'age': 28}
+{'_id': ObjectId('5df68a23f106fe2d315bbc8e'), 'name': 'Sami', 'country': 'Finland', 'city': 'Helsinki', 'age': 25}
+```
+
+Descending order
+
+```sh
+{'_id': ObjectId('5df68a23f106fe2d315bbc8e'), 'name': 'Sami', 'country': 'Finland', 'city': 'Helsinki', 'age': 25}
+{'_id': ObjectId('5df68a23f106fe2d315bbc8d'), 'name': 'John', 'country': 'Sweden', 'city': 'Stockholm', 'age': 28}
+{'_id': ObjectId('5df68a23f106fe2d315bbc8c'), 'name': 'David', 'country': 'UK', 'city': 'London', 'age': 34}
+{'_id': ObjectId('5df68a21f106fe2d315bbc8b'), 'name': 'Asabeneh', 'country': 'Finland', 'city': 'Helsinki', 'age': 250}
+```
+
+### Update with query
+
+We will use *update_one()* method to update one item. It takes two object one is a query and the second is the new object.
+The first person, Asabeneh got a very implausible age. Let us update Asabeneh's age.
+
+```py
+# let's import the flask
+from flask import Flask, render_template
+import os # importing operating system module
+import pymongo
+
+MONGODB_URI = 'mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+db = client['thirty_days_of_python'] # accessing the database
+
+query = {'age':250}
+new_value = {'$set':{'age':38}}
+
+db.students.update_one(query, new_value)
+# lets check the result if the age is modified
+for student in db.students.find():
+ print(student)
+
+
+app = Flask(__name__)
+if __name__ == '__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+```sh
+{'_id': ObjectId('5df68a21f106fe2d315bbc8b'), 'name': 'Asabeneh', 'country': 'Finland', 'city': 'Helsinki', 'age': 38}
+{'_id': ObjectId('5df68a23f106fe2d315bbc8c'), 'name': 'David', 'country': 'UK', 'city': 'London', 'age': 34}
+{'_id': ObjectId('5df68a23f106fe2d315bbc8d'), 'name': 'John', 'country': 'Sweden', 'city': 'Stockholm', 'age': 28}
+{'_id': ObjectId('5df68a23f106fe2d315bbc8e'), 'name': 'Sami', 'country': 'Finland', 'city': 'Helsinki', 'age': 25}
+```
+
+When we want to update many documents at once we use *upate_many()* method.
+
+### Delete Document
+
+The method *delete_one()* deletes one document. The *delete_one()* takes a query object parameter. It only removes the first occurrence.
+Let us remove one John from the collection.
+
+```py
+# let's import the flask
+from flask import Flask, render_template
+import os # importing operating system module
+import pymongo
+
+MONGODB_URI = 'mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+db = client['thirty_days_of_python'] # accessing the database
+
+query = {'name':'John'}
+db.students.delete_one(query)
+
+for student in db.students.find():
+ print(student)
+# lets check the result if the age is modified
+for student in db.students.find():
+ print(student)
+
+
+app = Flask(__name__)
+if __name__ == '__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+```sh
+{'_id': ObjectId('5df68a21f106fe2d315bbc8b'), 'name': 'Asabeneh', 'country': 'Finland', 'city': 'Helsinki', 'age': 38}
+{'_id': ObjectId('5df68a23f106fe2d315bbc8c'), 'name': 'David', 'country': 'UK', 'city': 'London', 'age': 34}
+{'_id': ObjectId('5df68a23f106fe2d315bbc8e'), 'name': 'Sami', 'country': 'Finland', 'city': 'Helsinki', 'age': 25}
+```
+
+As you can see John has been removed from the collection.
+
+When we want to delete many documents we use *delete_many()* method, it takes a query object. If we pass an empty query object to *delete_many({})* it will delete all the documents in the collection.
+
+### Drop a collection
+
+Using the _drop()_ method we can delete a collection from a database.
+
+```py
+# let's import the flask
+from flask import Flask, render_template
+import os # importing operating system module
+import pymongo
+
+MONGODB_URI = 'mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+db = client['thirty_days_of_python'] # accessing the database
+db.students.drop()
+```
+
+Now, we have deleted the students collection from the database.
+
+## 💻 Exercises: Day 27
+
+🎉 CONGRATULATIONS ! 🎉
+
+[<< Day 26](../26_Day_Python_web/26_python_web.md) | [Day 28 >>](../28_Day_API/28_API.md)
diff --git a/27_python_with_mongodb/index.html b/27_python_with_mongodb/index.html
new file mode 100644
index 0000000..ba8f2c5
--- /dev/null
+++ b/27_python_with_mongodb/index.html
@@ -0,0 +1,1615 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 27 python with mongodb - 30天入门Python全栈开发
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Python is a backend technology and it can be connected with different data base applications. It can be connected to both SQL and noSQL databases. In this section, we connect Python with MongoDB database which is noSQL database.
MongoDB is a NoSQL database. MongoDB stores data in a JSON like document which make MongoDB very flexible and scalable. Let us see the different terminologies of SQL and NoSQL databases. The following table will make the difference between SQL versus NoSQL databases.
In this section, we will focus on a NoSQL database MongoDB. Lets sign up on mongoDB by click on the sign in button then click register on the next page.
+
+
Complete the fields and click continue
+
+
Select the free plan
+
+
Choose the proximate free region and give any name for you cluster.
Do not worry about the url, it is a means to connect your application with mongoDB.
+Let us replace the password placeholder with the password you used to add a user.
Now, I replaced everything and the password is 123123 and the name of the database is thirty_days_python. This is just an example, your password must be a bit stronger than this.
+
Python needs a mongoDB driver to access mongoDB database. We will use pymongo with dnspython to connect our application with mongoDB base . Inside your project directory install pymongo and dnspython.
+
pipinstallpymongodnspython
+
+
The "dnspython" module must be installed to use mongodb+srv:// URIs. The dnspython is a DNS toolkit for Python. It supports almost all record types.
# let's import the flask
+fromflaskimportFlask,render_template
+importos# importing operating system module
+MONGODB_URI='mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client=pymongo.MongoClient(MONGODB_URI)
+print(client.list_database_names())
+
+app=Flask(__name__)
+if__name__=='__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
+
When we run the above code we get the default mongoDB databases.
Let us create a database, database and collection in mongoDB will be created if it doesn't exist. Let's create a data base name thirty_days_of_python and students collection.
+To create a database
+
db=client.name_of_databse# we can create a database like this or the second way
+db=client['name_of_database']
+
+
# let's import the flask
+fromflaskimportFlask,render_template
+importos# importing operating system module
+MONGODB_URI='mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client=pymongo.MongoClient(MONGODB_URI)
+# Creating database
+db=client.thirty_days_of_python
+# Creating students collection and inserting a document
+db.students.insert_one({'name':'Asabeneh','country':'Finland','city':'Helsinki','age':250})
+print(client.list_database_names())
+
+app=Flask(__name__)
+if__name__=='__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
+
After we create a database, we also created a students collection and we used insert_one() method to insert a document.
+Now, the database thirty_days_of_python and students collection have been created and the document has been inserted.
+Check your mongoDB cluster and you will see both the database and the collection. Inside the collection, there will be a document.
+
['thirty_days_of_python','admin','local']
+
+
If you see this on the mongoDB cluster, it means you have successfully created a database and a collection.
+
+
If you have seen on the figure, the document has been created with a long id which acts as a primary key. Every time we create a document mongoDB create and unique id for it.
The insert_one() method inserts one item at a time if we want to insert many documents at once either we use insert_many() method or for loop.
+We can use for loop to inset many documents at once.
+
# let's import the flask
+fromflaskimportFlask,render_template
+importos# importing operating system module
+MONGODB_URI='mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client=pymongo.MongoClient(MONGODB_URI)
+
+students=[
+ {'name':'David','country':'UK','city':'London','age':34},
+ {'name':'John','country':'Sweden','city':'Stockholm','age':28},
+ {'name':'Sami','country':'Finland','city':'Helsinki','age':25},
+ ]
+forstudentinstudents:
+ db.students.insert_one(student)
+
+
+app=Flask(__name__)
+if__name__=='__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
The find() and findOne() methods are common method to find data in a collection in mongoDB database. It is similar to the SELECT statement in a MySQL database.
+Let us use the find_one() method to get a document in a database collection.
+
+
*find_one({"_id": ObjectId("id"}): Gets the first occurrence if an id is not provided
+
+
# let's import the flask
+fromflaskimportFlask,render_template
+importos# importing operating system module
+MONGODB_URI='mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client=pymongo.MongoClient(MONGODB_URI)
+db=client['thirty_days_of_python']# accessing the database
+student=db.students.find_one()
+print(student)
+
+
+app=Flask(__name__)
+if__name__=='__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
The above query returns the first entry but we can target specific document using specific _id. Let us do one example, use David's id to get David object.
+'_id':ObjectId('5df68a23f106fe2d315bbc8c')
+
# let's import the flask
+fromflaskimportFlask,render_template
+importos# importing operating system module
+frombson.objectidimportObjectId# id object
+MONGODB_URI='mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client=pymongo.MongoClient(MONGODB_URI)
+db=client['thirty_days_of_python']# accessing the database
+student=db.students.find_one({'_id':ObjectId('5df68a23f106fe2d315bbc8c')})
+print(student)
+
+app=Flask(__name__)
+if__name__=='__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
We have seen, how to use find_one() using the above examples. Let's move one to find()
+
+
find(): returns all the occurrence from a collection if we don't pass a query object. The object is pymongo.cursor object.
+
+
# let's import the flask
+fromflaskimportFlask,render_template
+importos# importing operating system module
+
+MONGODB_URI='mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client=pymongo.MongoClient(MONGODB_URI)
+db=client['thirty_days_of_python']# accessing the database
+students=db.students.find()
+forstudentinstudents:
+ print(student)
+
+app=Flask(__name__)
+if__name__=='__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
We can specify which fields to return by passing second object in the find({}, {}). 0 means not include and 1 means include but we can not mix 0 and 1, except for _id.
+
# let's import the flask
+fromflaskimportFlask,render_template
+importos# importing operating system module
+
+MONGODB_URI='mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client=pymongo.MongoClient(MONGODB_URI)
+db=client['thirty_days_of_python']# accessing the database
+students=db.students.find({},{"_id":0,"name":1,"country":1})# 0 means not include and 1 means include
+forstudentinstudents:
+ print(student)
+
+app=Flask(__name__)
+if__name__=='__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
In mongoDB find take a query object. We can pass a query object and we can filter the documents we like to filter out.
+
# let's import the flask
+fromflaskimportFlask,render_template
+importos# importing operating system module
+
+MONGODB_URI='mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client=pymongo.MongoClient(MONGODB_URI)
+db=client['thirty_days_of_python']# accessing the database
+
+query={
+ "country":"Finland"
+}
+students=db.students.find(query)
+
+forstudentinstudents:
+ print(student)
+
+
+app=Flask(__name__)
+if__name__=='__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
# let's import the flask
+fromflaskimportFlask,render_template
+importos# importing operating system module
+importpymongo
+
+MONGODB_URI='mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client=pymongo.MongoClient(MONGODB_URI)
+db=client['thirty_days_of_python']# accessing the database
+
+query={
+ "city":"Helsinki"
+}
+students=db.students.find(query)
+forstudentinstudents:
+ print(student)
+
+
+app=Flask(__name__)
+if__name__=='__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
# let's import the flask
+fromflaskimportFlask,render_template
+importos# importing operating system module
+importpymongo
+
+MONGODB_URI='mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client=pymongo.MongoClient(MONGODB_URI)
+db=client['thirty_days_of_python']# accessing the database
+query={
+ "country":"Finland",
+ "city":"Helsinki"
+}
+students=db.students.find(query)
+forstudentinstudents:
+ print(student)
+
+
+app=Flask(__name__)
+if__name__=='__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
# let's import the flask
+fromflaskimportFlask,render_template
+importos# importing operating system module
+importpymongo
+
+MONGODB_URI='mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client=pymongo.MongoClient(MONGODB_URI)
+db=client['thirty_days_of_python']# accessing the database
+query={"age":{"$gt":30}}
+students=db.students.find(query)
+forstudentinstudents:
+ print(student)
+
+app=Flask(__name__)
+if__name__=='__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
By default, sort is in ascending order. We can change the sorting to descending order by adding -1 parameter.
+
# let's import the flask
+fromflaskimportFlask,render_template
+importos# importing operating system module
+importpymongo
+
+MONGODB_URI='mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client=pymongo.MongoClient(MONGODB_URI)
+db=client['thirty_days_of_python']# accessing the database
+students=db.students.find().sort('name')
+forstudentinstudents:
+ print(student)
+
+
+students=db.students.find().sort('name',-1)
+forstudentinstudents:
+ print(student)
+
+students=db.students.find().sort('age')
+forstudentinstudents:
+ print(student)
+
+students=db.students.find().sort('age',-1)
+forstudentinstudents:
+ print(student)
+
+app=Flask(__name__)
+if__name__=='__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
We will use update_one() method to update one item. It takes two object one is a query and the second is the new object.
+The first person, Asabeneh got a very implausible age. Let us update Asabeneh's age.
+
# let's import the flask
+fromflaskimportFlask,render_template
+importos# importing operating system module
+importpymongo
+
+MONGODB_URI='mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client=pymongo.MongoClient(MONGODB_URI)
+db=client['thirty_days_of_python']# accessing the database
+
+query={'age':250}
+new_value={'$set':{'age':38}}
+
+db.students.update_one(query,new_value)
+# lets check the result if the age is modified
+forstudentindb.students.find():
+ print(student)
+
+
+app=Flask(__name__)
+if__name__=='__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
The method delete_one() deletes one document. The delete_one() takes a query object parameter. It only removes the first occurrence.
+Let us remove one John from the collection.
+
# let's import the flask
+fromflaskimportFlask,render_template
+importos# importing operating system module
+importpymongo
+
+MONGODB_URI='mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client=pymongo.MongoClient(MONGODB_URI)
+db=client['thirty_days_of_python']# accessing the database
+
+query={'name':'John'}
+db.students.delete_one(query)
+
+forstudentindb.students.find():
+ print(student)
+# lets check the result if the age is modified
+forstudentindb.students.find():
+ print(student)
+
+
+app=Flask(__name__)
+if__name__=='__main__':
+ # for deployment we use the environ
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
As you can see John has been removed from the collection.
+
When we want to delete many documents we use delete_many() method, it takes a query object. If we pass an empty query object to delete_many({}) it will delete all the documents in the collection.
+
+
+[<< Day 27](../27_Day_Python_with_mongodb/27_python_with_mongodb.md) | [Day 29 >>](../29_Day_Building_API/29_building_API.md)
+
+![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
+
+- [📘 Day 28](#-day-28)
+- [Application Programming Interface(API)](#application-programming-interfaceapi)
+ - [API](#api)
+ - [Building API](#building-api)
+ - [HTTP(Hypertext Transfer Protocol)](#httphypertext-transfer-protocol)
+ - [Structure of HTTP](#structure-of-http)
+ - [Initial Request Line(Status Line)](#initial-request-linestatus-line)
+ - [Initial Response Line(Status Line)](#initial-response-linestatus-line)
+ - [Header Fields](#header-fields)
+ - [The message body](#the-message-body)
+ - [Request Methods](#request-methods)
+ - [💻 Exercises: Day 28](#-exercises-day-28)
+
+# 📘 Day 28
+
+# Application Programming Interface(API)
+
+## API
+
+API stands for Application Programming Interface. The kind of API we will cover in this section is going to be Web APIs.
+Web APIs are the defined interfaces through which interactions happen between an enterprise and applications that use its assets, which also is a Service Level Agreement (SLA) to specify the functional provider and expose the service path or URL for its API users.
+
+In the context of web development, an API is defined as a set of specifications, such as Hypertext Transfer Protocol (HTTP) request messages, along with a definition of the structure of response messages, usually in an XML or a JavaScript Object Notation (JSON) format.
+
+Web API has been moving away from Simple Object Access Protocol (SOAP) based web services and service-oriented architecture (SOA) towards more direct representational state transfer (REST) style web resources.
+
+Social media services, web APIs have allowed web communities to share content and data between communities and different platforms.
+
+Using API, content that is created in one place dynamically can be posted and updated to multiple locations on the web.
+
+For example, Twitter's REST API allows developers to access core Twitter data and the Search API provides methods for developers to interact with Twitter Search and trends data.
+
+Many applications provide API end points. Some examples of API such as the countries [API](https://restcountries.eu/rest/v2/all), [cat's breed API](https://api.thecatapi.com/v1/breeds).
+
+In this section, we will cover a RESTful API that uses HTTP request methods to GET, PUT, POST and DELETE data.
+
+## Building API
+
+RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data. In the previous sections, we have learned about python, flask and mongoDB. We will use the knowledge we acquire to develop a RESTful API using Python flask and mongoDB database. Every application which has CRUD(Create, Read, Update, Delete) operation has an API to create data, to get data, to update data or to delete data from a database.
+
+To build an API, it is good to understand HTTP protocol and HTTP request and response cycle.
+
+## HTTP(Hypertext Transfer Protocol)
+
+HTTP is an established communication protocol between a client and a server. A client in this case is a browser and server is the place where you access data. HTTP is a network protocol used to deliver resources which could be files on the World Wide Web, whether they are HTML files, image files, query results, scripts, or other file types.
+
+A browser is an HTTP client because it sends requests to an HTTP server (Web server), which then sends responses back to the client.
+
+## Structure of HTTP
+
+HTTP uses client-server model. An HTTP client opens a connection and sends a request message to an HTTP server and the HTTP server returns response message which is the requested resources. When the request response cycle completes the server closes the connection.
+
+![HTTP request response cycle](../images/http_request_response_cycle.png)
+
+The format of the request and response messages are similar. Both kinds of messages have
+
+- an initial line,
+- zero or more header lines,
+- a blank line (i.e. a CRLF by itself), and
+- an optional message body (e.g. a file, or query data, or query output).
+
+Let us an example of request and response messages by navigating this site:https://thirtydaysofpython-v1-final.herokuapp.com/. This site has been deployed on Heroku free dyno and in some months may not work because of high request. Support this work to make the server run all the time.
+
+![Request and Response header](../images/request_response_header.png)
+
+## Initial Request Line(Status Line)
+
+The initial request line is different from the response.
+A request line has three parts, separated by spaces:
+
+- method name(GET, POST, HEAD)
+- path of the requested resource,
+- the version of HTTP being used. eg GET / HTTP/1.1
+
+GET is the most common HTTP that helps to get or read resource and POST is a common request method to create resource.
+
+### Initial Response Line(Status Line)
+
+The initial response line, called the status line, also has three parts separated by spaces:
+
+- HTTP version
+- Response status code that gives the result of the request, and a reason which describes the status code. Example of status lines are:
+ HTTP/1.0 200 OK
+ or
+ HTTP/1.0 404 Not Found
+ Notes:
+
+The most common status codes are:
+200 OK: The request succeeded, and the resulting resource (e.g. file or script output) is returned in the message body.
+500 Server Error
+A complete list of HTTP status code can be found [here](https://httpstatuses.com/). It can be also found [here](https://httpstatusdogs.com/).
+
+### Header Fields
+
+As you have seen in the above screenshot, header lines provide information about the request or response, or about the object sent in the message body.
+
+```sh
+GET / HTTP/1.1
+Host: thirtydaysofpython-v1-final.herokuapp.com
+Connection: keep-alive
+Pragma: no-cache
+Cache-Control: no-cache
+Upgrade-Insecure-Requests: 1
+User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36
+Sec-Fetch-User: ?1
+Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
+Sec-Fetch-Site: same-origin
+Sec-Fetch-Mode: navigate
+Referer: https://thirtydaysofpython-v1-final.herokuapp.com/post
+Accept-Encoding: gzip, deflate, br
+Accept-Language: en-GB,en;q=0.9,fi-FI;q=0.8,fi;q=0.7,en-CA;q=0.6,en-US;q=0.5,fr;q=0.4
+```
+
+### The message body
+
+An HTTP message may have a body of data sent after the header lines. In a response, this is where the requested resource is returned to the client (the most common use of the message body), or perhaps explanatory text if there's an error. In a request, this is where user-entered data or uploaded files are sent to the server.
+
+If an HTTP message includes a body, there are usually header lines in the message that describe the body. In particular,
+
+The Content-Type: header gives the MIME-type of the data in the body(text/html, application/json, text/plain, text/css, image/gif).
+The Content-Length: header gives the number of bytes in the body.
+
+### Request Methods
+
+The GET, POST, PUT and DELETE are the HTTP request methods which we are going to implement an API or a CRUD operation application.
+
+1. GET: GET method is used to retrieve and get information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.
+
+2. POST: POST request is used to create data and send data to the server, for example, creating a new post, file upload, etc. using HTML forms.
+
+3. PUT: Replaces all current representations of the target resource with the uploaded content and we use it modify or update data.
+
+4. DELETE: Removes data
+
+## 💻 Exercises: Day 28
+
+1. Read about API and HTTP
+
+🎉 CONGRATULATIONS ! 🎉
+
+[<< Day 27](../27_Day_Python_with_mongodb/27_python_with_mongodb.md) | [Day 29 >>](../29_Day_Building_API/29_building_API.md)
diff --git a/28_API/index.html b/28_API/index.html
new file mode 100644
index 0000000..8e4b365
--- /dev/null
+++ b/28_API/index.html
@@ -0,0 +1,1228 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 28 API - 30天入门Python全栈开发
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
API stands for Application Programming Interface. The kind of API we will cover in this section is going to be Web APIs.
+Web APIs are the defined interfaces through which interactions happen between an enterprise and applications that use its assets, which also is a Service Level Agreement (SLA) to specify the functional provider and expose the service path or URL for its API users.
+
In the context of web development, an API is defined as a set of specifications, such as Hypertext Transfer Protocol (HTTP) request messages, along with a definition of the structure of response messages, usually in an XML or a JavaScript Object Notation (JSON) format.
+
Web API has been moving away from Simple Object Access Protocol (SOAP) based web services and service-oriented architecture (SOA) towards more direct representational state transfer (REST) style web resources.
+
Social media services, web APIs have allowed web communities to share content and data between communities and different platforms.
+
Using API, content that is created in one place dynamically can be posted and updated to multiple locations on the web.
+
For example, Twitter's REST API allows developers to access core Twitter data and the Search API provides methods for developers to interact with Twitter Search and trends data.
+
Many applications provide API end points. Some examples of API such as the countries API, cat's breed API.
+
In this section, we will cover a RESTful API that uses HTTP request methods to GET, PUT, POST and DELETE data.
RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data. In the previous sections, we have learned about python, flask and mongoDB. We will use the knowledge we acquire to develop a RESTful API using Python flask and mongoDB database. Every application which has CRUD(Create, Read, Update, Delete) operation has an API to create data, to get data, to update data or to delete data from a database.
+
To build an API, it is good to understand HTTP protocol and HTTP request and response cycle.
HTTP is an established communication protocol between a client and a server. A client in this case is a browser and server is the place where you access data. HTTP is a network protocol used to deliver resources which could be files on the World Wide Web, whether they are HTML files, image files, query results, scripts, or other file types.
+
A browser is an HTTP client because it sends requests to an HTTP server (Web server), which then sends responses back to the client.
HTTP uses client-server model. An HTTP client opens a connection and sends a request message to an HTTP server and the HTTP server returns response message which is the requested resources. When the request response cycle completes the server closes the connection.
+
+
The format of the request and response messages are similar. Both kinds of messages have
+
+
an initial line,
+
zero or more header lines,
+
a blank line (i.e. a CRLF by itself), and
+
an optional message body (e.g. a file, or query data, or query output).
+
+
Let us an example of request and response messages by navigating this site:https://thirtydaysofpython-v1-final.herokuapp.com/. This site has been deployed on Heroku free dyno and in some months may not work because of high request. Support this work to make the server run all the time.
The initial response line, called the status line, also has three parts separated by spaces:
+
+
HTTP version
+
Response status code that gives the result of the request, and a reason which describes the status code. Example of status lines are:
+ HTTP/1.0 200 OK
+ or
+ HTTP/1.0 404 Not Found
+ Notes:
+
+
The most common status codes are:
+200 OK: The request succeeded, and the resulting resource (e.g. file or script output) is returned in the message body.
+500 Server Error
+A complete list of HTTP status code can be found here. It can be also found here.
As you have seen in the above screenshot, header lines provide information about the request or response, or about the object sent in the message body.
An HTTP message may have a body of data sent after the header lines. In a response, this is where the requested resource is returned to the client (the most common use of the message body), or perhaps explanatory text if there's an error. In a request, this is where user-entered data or uploaded files are sent to the server.
+
If an HTTP message includes a body, there are usually header lines in the message that describe the body. In particular,
+
The Content-Type: header gives the MIME-type of the data in the body(text/html, application/json, text/plain, text/css, image/gif).
+The Content-Length: header gives the number of bytes in the body.
The GET, POST, PUT and DELETE are the HTTP request methods which we are going to implement an API or a CRUD operation application.
+
+
+
GET: GET method is used to retrieve and get information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.
+
+
+
POST: POST request is used to create data and send data to the server, for example, creating a new post, file upload, etc. using HTML forms.
+
+
+
PUT: Replaces all current representations of the target resource with the uploaded content and we use it modify or update data.
+
+[<< Day 28](../28_Day_API/28_API.md) | [Day 29 >>](../30_Day_Conclusions/30_conclusions.md)
+
+![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
+
+- [Day 29](#day-29)
+- [Building API](#building-api)
+ - [Structure of an API](#structure-of-an-api)
+ - [Retrieving data using get](#retrieving-data-using-get)
+ - [Getting a document by id](#getting-a-document-by-id)
+ - [Creating data using POST](#creating-data-using-post)
+ - [Updating using PUT](#updating-using-put)
+ - [Deleting a document using Delete](#deleting-a-document-using-delete)
+- [💻 Exercises: Day 29](#-exercises-day-29)
+
+## Day 29
+
+## Building API
+
+
+In this section, we will cove a RESTful API that uses HTTP request methods to GET, PUT, POST and DELETE data.
+
+RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data. In the previous sections, we have learned about python, flask and mongoDB. We will use the knowledge we acquire to develop a RESTful API using python flask and mongoDB. Every application which has CRUD(Create, Read, Update, Delete) operation has an API to create data, to get data, to update data or to delete data from database.
+
+The browser can handle only get request. Therefore, we have to have a tool which can help us to handle all request methods(GET, POST, PUT, DELETE).
+
+Examples of API
+
+- Countries API: https://restcountries.eu/rest/v2/all
+- Cats breed API: https://api.thecatapi.com/v1/breeds
+
+[Postman](https://www.getpostman.com/) is a very popular tool when it comes to API development. So, if you like to do this section you need to [download postman](https://www.getpostman.com/). An alternative of Postman is [Insomnia](https://insomnia.rest/download).
+
+![Postman](../images/postman.png)
+
+### Structure of an API
+
+An API end point is a URL which can help to retrieve, create, update or delete a resource. The structure looks like this:
+Example:
+https://api.twitter.com/1.1/lists/members.json
+Returns the members of the specified list. Private list members will only be shown if the authenticated user owns the specified list.
+The name of the company name followed by version followed by the purpose of the API.
+The methods:
+HTTP methods & URLs
+
+The API uses the following HTTP methods for object manipulation:
+
+```sh
+GET Used for object retrieval
+POST Used for object creation and object actions
+PUT Used for object update
+DELETE Used for object deletion
+```
+
+Let us build an API which collects information about 30DaysOfPython students. We will collect the name, country, city, date of birth, skills and bio.
+
+To implement this API, we will use:
+
+- Postman
+- Python
+- Flask
+- MongoDB
+
+### Retrieving data using get
+
+In this step, let us use dummy data and return it as a json. To return it as json, will use json module and Response module.
+
+```py
+# let's import the flask
+
+from flask import Flask, Response
+import json
+
+app = Flask(__name__)
+
+@app.route('/api/v1.0/students', methods = ['GET'])
+def students ():
+ student_list = [
+ {
+ 'name':'Asabeneh',
+ 'country':'Finland',
+ 'city':'Helsinki',
+ 'skills':['HTML', 'CSS','JavaScript','Python']
+ },
+ {
+ 'name':'David',
+ 'country':'UK',
+ 'city':'London',
+ 'skills':['Python','MongoDB']
+ },
+ {
+ 'name':'John',
+ 'country':'Sweden',
+ 'city':'Stockholm',
+ 'skills':['Java','C#']
+ }
+ ]
+ return Response(json.dumps(student_list), mimetype='application/json')
+
+
+if __name__ == '__main__':
+ # for deployment
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+When you request the http://localhost:5000/api/v1.0/students url on the browser you will get this:
+
+![Get on browser](../images/get_on_browser.png)
+
+When you request the http://localhost:5000/api/v1.0/students url on the browser you will get this:
+
+![Get on postman](../images/get_on_postman.png)
+
+In stead of displaying dummy data let us connect the flask application with MongoDB and get data from mongoDB database.
+
+```py
+# let's import the flask
+
+from flask import Flask, Response
+import json
+import pymongo
+
+
+app = Flask(__name__)
+
+#
+MONGODB_URI='mongodb+srv://asabeneh:your_password@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+db = client['thirty_days_of_python'] # accessing the database
+
+@app.route('/api/v1.0/students', methods = ['GET'])
+def students ():
+
+ return Response(json.dumps(student), mimetype='application/json')
+
+
+if __name__ == '__main__':
+ # for deployment
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+By connecting the flask, we can fetch students collection data from the thirty_days_of_python database.
+
+```sh
+[
+ {
+ "_id": {
+ "$oid": "5df68a21f106fe2d315bbc8b"
+ },
+ "name": "Asabeneh",
+ "country": "Finland",
+ "city": "Helsinki",
+ "age": 38
+ },
+ {
+ "_id": {
+ "$oid": "5df68a23f106fe2d315bbc8c"
+ },
+ "name": "David",
+ "country": "UK",
+ "city": "London",
+ "age": 34
+ },
+ {
+ "_id": {
+ "$oid": "5df68a23f106fe2d315bbc8e"
+ },
+ "name": "Sami",
+ "country": "Finland",
+ "city": "Helsinki",
+ "age": 25
+ }
+]
+```
+
+### Getting a document by id
+
+We can access signle document using an id, let's access Asabeneh using his id.
+http://localhost:5000/api/v1.0/students/5df68a21f106fe2d315bbc8b
+
+```py
+# let's import the flask
+
+from flask import Flask, Response
+import json
+from bson.objectid import ObjectId
+import json
+from bson.json_util import dumps
+import pymongo
+
+
+app = Flask(__name__)
+
+#
+MONGODB_URI='mongodb+srv://asabeneh:your_password@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+db = client['thirty_days_of_python'] # accessing the database
+
+@app.route('/api/v1.0/students', methods = ['GET'])
+def students ():
+
+ return Response(json.dumps(student), mimetype='application/json')
+@app.route('/api/v1.0/students/', methods = ['GET'])
+def single_student (id):
+ student = db.students.find({'_id':ObjectId(id)})
+ return Response(dumps(student), mimetype='application/json')
+
+if __name__ == '__main__':
+ # for deployment
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+```sh
+[
+ {
+ "_id": {
+ "$oid": "5df68a21f106fe2d315bbc8b"
+ },
+ "name": "Asabeneh",
+ "country": "Finland",
+ "city": "Helsinki",
+ "age": 38
+ }
+]
+```
+
+### Creating data using POST
+
+We use the POST request method to create data
+
+```py
+# let's import the flask
+
+from flask import Flask, Response
+import json
+from bson.objectid import ObjectId
+import json
+from bson.json_util import dumps
+import pymongo
+from datetime import datetime
+
+
+app = Flask(__name__)
+
+#
+MONGODB_URI='mongodb+srv://asabeneh:your_password@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+db = client['thirty_days_of_python'] # accessing the database
+
+@app.route('/api/v1.0/students', methods = ['GET'])
+def students ():
+
+ return Response(json.dumps(student), mimetype='application/json')
+@app.route('/api/v1.0/students/', methods = ['GET'])
+def single_student (id):
+ student = db.students.find({'_id':ObjectId(id)})
+ return Response(dumps(student), mimetype='application/json')
+@app.route('/api/v1.0/students', methods = ['POST'])
+def create_student ():
+ name = request.form['name']
+ country = request.form['country']
+ city = request.form['city']
+ skills = request.form['skills'].split(', ')
+ bio = request.form['bio']
+ birthyear = request.form['birthyear']
+ created_at = datetime.now()
+ student = {
+ 'name': name,
+ 'country': country,
+ 'city': city,
+ 'birthyear': birthyear,
+ 'skills': skills,
+ 'bio': bio,
+ 'created_at': created_at
+
+ }
+ db.students.insert_one(student)
+ return ;
+def update_student (id):
+if __name__ == '__main__':
+ # for deployment
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+### Updating using PUT
+
+```py
+# let's import the flask
+
+from flask import Flask, Response
+import json
+from bson.objectid import ObjectId
+import json
+from bson.json_util import dumps
+import pymongo
+from datetime import datetime
+
+
+app = Flask(__name__)
+
+#
+MONGODB_URI='mongodb+srv://asabeneh:your_password@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+db = client['thirty_days_of_python'] # accessing the database
+
+@app.route('/api/v1.0/students', methods = ['GET'])
+def students ():
+
+ return Response(json.dumps(student), mimetype='application/json')
+@app.route('/api/v1.0/students/', methods = ['GET'])
+def single_student (id):
+ student = db.students.find({'_id':ObjectId(id)})
+ return Response(dumps(student), mimetype='application/json')
+@app.route('/api/v1.0/students', methods = ['POST'])
+def create_student ():
+ name = request.form['name']
+ country = request.form['country']
+ city = request.form['city']
+ skills = request.form['skills'].split(', ')
+ bio = request.form['bio']
+ birthyear = request.form['birthyear']
+ created_at = datetime.now()
+ student = {
+ 'name': name,
+ 'country': country,
+ 'city': city,
+ 'birthyear': birthyear,
+ 'skills': skills,
+ 'bio': bio,
+ 'created_at': created_at
+
+ }
+ db.students.insert_one(student)
+ return
+@app.route('/api/v1.0/students/', methods = ['PUT']) # this decorator create the home route
+def update_student (id):
+ query = {"_id":ObjectId(id)}
+ name = request.form['name']
+ country = request.form['country']
+ city = request.form['city']
+ skills = request.form['skills'].split(', ')
+ bio = request.form['bio']
+ birthyear = request.form['birthyear']
+ created_at = datetime.now()
+ student = {
+ 'name': name,
+ 'country': country,
+ 'city': city,
+ 'birthyear': birthyear,
+ 'skills': skills,
+ 'bio': bio,
+ 'created_at': created_at
+
+ }
+ db.students.update_one(query, student)
+ # return Response(dumps({"result":"a new student has been created"}), mimetype='application/json')
+ return
+def update_student (id):
+if __name__ == '__main__':
+ # for deployment
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+### Deleting a document using Delete
+
+```py
+# let's import the flask
+
+from flask import Flask, Response
+import json
+from bson.objectid import ObjectId
+import json
+from bson.json_util import dumps
+import pymongo
+from datetime import datetime
+
+
+app = Flask(__name__)
+
+#
+MONGODB_URI='mongodb+srv://asabeneh:your_password@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client = pymongo.MongoClient(MONGODB_URI)
+db = client['thirty_days_of_python'] # accessing the database
+
+@app.route('/api/v1.0/students', methods = ['GET'])
+def students ():
+
+ return Response(json.dumps(student), mimetype='application/json')
+@app.route('/api/v1.0/students/', methods = ['GET'])
+def single_student (id):
+ student = db.students.find({'_id':ObjectId(id)})
+ return Response(dumps(student), mimetype='application/json')
+@app.route('/api/v1.0/students', methods = ['POST'])
+def create_student ():
+ name = request.form['name']
+ country = request.form['country']
+ city = request.form['city']
+ skills = request.form['skills'].split(', ')
+ bio = request.form['bio']
+ birthyear = request.form['birthyear']
+ created_at = datetime.now()
+ student = {
+ 'name': name,
+ 'country': country,
+ 'city': city,
+ 'birthyear': birthyear,
+ 'skills': skills,
+ 'bio': bio,
+ 'created_at': created_at
+
+ }
+ db.students.insert_one(student)
+ return
+@app.route('/api/v1.0/students/', methods = ['PUT']) # this decorator create the home route
+def update_student (id):
+ query = {"_id":ObjectId(id)}
+ name = request.form['name']
+ country = request.form['country']
+ city = request.form['city']
+ skills = request.form['skills'].split(', ')
+ bio = request.form['bio']
+ birthyear = request.form['birthyear']
+ created_at = datetime.now()
+ student = {
+ 'name': name,
+ 'country': country,
+ 'city': city,
+ 'birthyear': birthyear,
+ 'skills': skills,
+ 'bio': bio,
+ 'created_at': created_at
+
+ }
+ db.students.update_one(query, student)
+ # return Response(dumps({"result":"a new student has been created"}), mimetype='application/json')
+ return
+@app.route('/api/v1.0/students/', methods = ['PUT']) # this decorator create the home route
+def update_student (id):
+ query = {"_id":ObjectId(id)}
+ name = request.form['name']
+ country = request.form['country']
+ city = request.form['city']
+ skills = request.form['skills'].split(', ')
+ bio = request.form['bio']
+ birthyear = request.form['birthyear']
+ created_at = datetime.now()
+ student = {
+ 'name': name,
+ 'country': country,
+ 'city': city,
+ 'birthyear': birthyear,
+ 'skills': skills,
+ 'bio': bio,
+ 'created_at': created_at
+
+ }
+ db.students.update_one(query, student)
+ # return Response(dumps({"result":"a new student has been created"}), mimetype='application/json')
+ return ;
+@app.route('/api/v1.0/students/', methods = ['DELETE'])
+def delete_student (id):
+ db.students.delete_one({"_id":ObjectId(id)})
+ return
+if __name__ == '__main__':
+ # for deployment
+ # to make it work for both production and development
+ port = int(os.environ.get("PORT", 5000))
+ app.run(debug=True, host='0.0.0.0', port=port)
+```
+
+## 💻 Exercises: Day 29
+
+1. Implement the above example and develop [this](https://thirtydayofpython-api.herokuapp.com/)
+
+🎉 CONGRATULATIONS ! 🎉
+
+[<< Day 28](../28_Day_API/28_API.md) | [Day 30 >>](../30_Day_Conclusions/30_conclusions.md)
diff --git a/29_building_API/index.html b/29_building_API/index.html
new file mode 100644
index 0000000..45e46c1
--- /dev/null
+++ b/29_building_API/index.html
@@ -0,0 +1,1759 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 29 building API - 30天入门Python全栈开发
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
In this section, we will cove a RESTful API that uses HTTP request methods to GET, PUT, POST and DELETE data.
+
RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data. In the previous sections, we have learned about python, flask and mongoDB. We will use the knowledge we acquire to develop a RESTful API using python flask and mongoDB. Every application which has CRUD(Create, Read, Update, Delete) operation has an API to create data, to get data, to update data or to delete data from database.
+
The browser can handle only get request. Therefore, we have to have a tool which can help us to handle all request methods(GET, POST, PUT, DELETE).
+
Examples of API
+
+
Countries API: https://restcountries.eu/rest/v2/all
Postman is a very popular tool when it comes to API development. So, if you like to do this section you need to download postman. An alternative of Postman is Insomnia.
An API end point is a URL which can help to retrieve, create, update or delete a resource. The structure looks like this:
+Example:
+https://api.twitter.com/1.1/lists/members.json
+Returns the members of the specified list. Private list members will only be shown if the authenticated user owns the specified list.
+The name of the company name followed by version followed by the purpose of the API.
+The methods:
+HTTP methods & URLs
+
The API uses the following HTTP methods for object manipulation:
In this step, let us use dummy data and return it as a json. To return it as json, will use json module and Response module.
+
# let's import the flask
+
+fromflaskimportFlask,Response
+importjson
+
+app=Flask(__name__)
+
+@app.route('/api/v1.0/students',methods=['GET'])
+defstudents():
+ student_list=[
+ {
+ 'name':'Asabeneh',
+ 'country':'Finland',
+ 'city':'Helsinki',
+ 'skills':['HTML','CSS','JavaScript','Python']
+ },
+ {
+ 'name':'David',
+ 'country':'UK',
+ 'city':'London',
+ 'skills':['Python','MongoDB']
+ },
+ {
+ 'name':'John',
+ 'country':'Sweden',
+ 'city':'Stockholm',
+ 'skills':['Java','C#']
+ }
+ ]
+ returnResponse(json.dumps(student_list),mimetype='application/json')
+
+
+if__name__=='__main__':
+ # for deployment
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
+
When you request the http://localhost:5000/api/v1.0/students url on the browser you will get this:
+
+
When you request the http://localhost:5000/api/v1.0/students url on the browser you will get this:
+
+
In stead of displaying dummy data let us connect the flask application with MongoDB and get data from mongoDB database.
+
# let's import the flask
+
+fromflaskimportFlask,Response
+importjson
+importpymongo
+
+
+app=Flask(__name__)
+
+#
+MONGODB_URI='mongodb+srv://asabeneh:your_password@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client=pymongo.MongoClient(MONGODB_URI)
+db=client['thirty_days_of_python']# accessing the database
+
+@app.route('/api/v1.0/students',methods=['GET'])
+defstudents():
+
+ returnResponse(json.dumps(student),mimetype='application/json')
+
+
+if__name__=='__main__':
+ # for deployment
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
+
By connecting the flask, we can fetch students collection data from the thirty_days_of_python database.
We can access signle document using an id, let's access Asabeneh using his id.
+http://localhost:5000/api/v1.0/students/5df68a21f106fe2d315bbc8b
+
# let's import the flask
+
+fromflaskimportFlask,Response
+importjson
+frombson.objectidimportObjectId
+importjson
+frombson.json_utilimportdumps
+importpymongo
+
+
+app=Flask(__name__)
+
+#
+MONGODB_URI='mongodb+srv://asabeneh:your_password@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
+client=pymongo.MongoClient(MONGODB_URI)
+db=client['thirty_days_of_python']# accessing the database
+
+@app.route('/api/v1.0/students',methods=['GET'])
+defstudents():
+
+ returnResponse(json.dumps(student),mimetype='application/json')
+@app.route('/api/v1.0/students/<id>',methods=['GET'])
+defsingle_student(id):
+ student=db.students.find({'_id':ObjectId(id)})
+ returnResponse(dumps(student),mimetype='application/json')
+
+if__name__=='__main__':
+ # for deployment
+ # to make it work for both production and development
+ port=int(os.environ.get("PORT",5000))
+ app.run(debug=True,host='0.0.0.0',port=port)
+
+
+[<< Day 29](../29_Day_Building_API/29_building_API.md)
+![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
+
+- [Day 30](#day-30)
+ - [Conclusions](#conclusions)
+
+# Day 30
+
+
+## Conclusions
+
+In the process of preparing this material I have learned quite a lot and you have inspired me to do more. Congratulations for making it to this level. If you have done all the exercise and the projects, now you are capable to go to a data analysis, data science, machine learning or web development paths. [Support the author for more educational materials](https://www.paypal.com/paypalme/asabeneh).
+
+## Testimony
+Now it is time to express your thoughts about the Author and 30DaysOfPyhton. You can leave your testimonial on this [link](https://testimonial-vdzd.onrender.com/)
+
+GIVE FEEDBACK:
+http://thirtydayofpython-api.herokuapp.com/feedback
+
+🎉 CONGRATULATIONS ! 🎉
+
+[<< Day 29](../29_Day_Building_API/29_building_API.md)
diff --git a/30_conclusions/index.html b/30_conclusions/index.html
new file mode 100644
index 0000000..f6cef29
--- /dev/null
+++ b/30_conclusions/index.html
@@ -0,0 +1,1186 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 30 conclusions - 30天入门Python全栈开发
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
In the process of preparing this material I have learned quite a lot and you have inspired me to do more. Congratulations for making it to this level. If you have done all the exercise and the projects, now you are capable to go to a data analysis, data science, machine learning or web development paths. Support the author for more educational materials.