𧳠Part 1: Day 1 - 3
𧳠Part 2: Day 4 - 6
𧳠Part 3: Day 7 - 9
𧳠Part 4: Day 10 - 12
𧳠Part 5: Day 13 - 15
𧳠Part 6: Day 16 - 18
𧳠Part 7: Day 19 - 21
𧳠Part 8: Day 22 - 24
𧳠Part 9: Day 25 - 27
𧳠Part 10: Day 28 - 30
- π Day 10
- Loops
- π» Exercises: Day 10
- π Day 11
- Functions
- Defining a Function
- Declaring and calling a function
- Function without parameters
- Function returning value
- Function with parameters
- Passing arguments with key and value
- Returning a value from a function
- Function with default parameters
- Arbitrary number of arguments
- Default and arbitrary number of parameters in function
- Function as parameter of other function
- π» Exercises: Day 11
- π Day 12
- Module
- Import Builtin Modules
- π» Exercises: Day 12
GIVE FEEDBACK: http://thirtydayofpython-api.herokuapp.com/feedback
π Day 10
Loops
Life is full of routines. In programming also we do lots of repetitive tasks. In order to handle repetitive task programming languages provide loops. Python programming language also provides the following types of two loops to handle looping. 1. while loop 2. for loop
While Loop
We use the reserved word while to make a while loop. While loop is used to execute a block of statements repeatedly until a given condition is satisfied. When the condition becomes false, the line immediately after the loop will be executed.
Example: In the above while loop, the condition become false when count is 5, then the loop stops. If we are interested to run block of code once the condition is no longer true, we can use else. Example: The above loop condition will be false when count is 5 and the loop stops, and execution starts the else statement and 5 prints in the else statement.Break and continue
- Break: We use break when we like to get out or stop the loop. Example: The above while loop only prints 0, 1, 2, but when it reaches 3 it stops.
- Continue: With the continue statement we can stop the current iteration, and continue with the next: Example: The above while loop only prints 0, 1, 2,4 but skips 3.
For Loop
A for key word used to make a for loop like in other programming language but with some syntax difference. Loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). * For loop with list
Example: * For loop with string Example: * For loop with tuple Example: * For loop with dictionary Looping through a dictionary gives you the key of the dictionary. Example: person = {
'first_name':'Asabeneh',
'last_name':'Yetayeh',
'age':250,
'country':'Finland',
'is_marred':True,
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
'address':{
'street':'Space street',
'zipcode':'02210'
}
}
for key in person:
print(key)
for key, value in person.items():
print(key, value) #
it_companies = {'Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon'}
for company in it_companies:
print(company)
Break and Continue
Break: We use break when we like to stop our loop before the loop is completed.
Example: In the above example, the loop stops when it reaches 3. Continue: We use continue when we like to skip some of the step in the iteration of the loop. Example: In the above example, if number is 3 the skip step and continues to the next.The range function
The range() function uses to loop through a set of code a certain number of times. The range(start,end, step) takes three parameters:starting, ending and increment.By default it starts from 0 and the increment is 1. The range sequence doesn't include the end. Creating sequence using range
lst = list(rang(11))
print(lst) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
st = set(range(11))
print(st) # {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
lst = list(rang(0,11,2))
print(lst) # [0, 2, 4, 6, 8, 10]
st = set(range(0,11,2))
print(st) # {0, 2, 4, 6, 8, 10}
for number in range(11):
print(number) # prints 0 to 10, not including 11
fruits = ['banana', 'orange', 'mango', 'lemon']
for fruit in fruits:
print(fruit)
Nested for loop
We can write loop inside another loop.
Example: person = {
'first_name': 'Asabeneh',
'last_name': 'Yetayeh',
'age': 250,
'country': 'Finland',
'is_marred': True,
'skills': ['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
'address': {
'street': 'Space street',
'zipcode': '02210'
}
}
for key in person:
if key == 'skills':
for skill in person['skills']:
print(skill)
For Else
If we want to execute some message when the loop ends, we use else.
# syntax
for iterator in range(start, end, increment):
do something
else:
print('The loop is ended')
for number in range(11):
print(number) # prints 0 to 10, not including 11
else:
print('The loop stops at', number)
Pass
In python after semicolon, it requires some code to run but we don't like to execute any code after if or for loop we can write the word pass to avoid error.
π» Exercises: Day 10
- Iterate 0 to 10 using for loop, do the same using while and do while loop.
- Iterate 10 to 0 using for loop, do the same using while and do while loop.
- Write a loop that makes seven calls to print() output the following triangle:
- Use nested loops to create the following:
- Print the following pattern:
- Iterate through the list, ['Python', 'Numpy','Pandas','Django', 'Flask'] using a for loop and print out the items.
- Use for loop to iterate from 0 to 100 and print only even numbers
- Use for loop to iterate from 0 to 100 and print only odd numbers
- Use for loop to iterate from 0 to 100 and print the sum of all numbers.
- Use for loop to iterate from 0 to 100 and print the sum of all evens and the sum of all odds.
- Go to the data folder and use the countries.py file. Loop through the countries and extract all the countries containing the word land.
- This is a fruit list, ['banana', 'orange', 'mango', 'lemon'] reverse the order using loop.
π Day 11
Functions
So far we have seen many builtin python functions. In this section, we will focus on custom functions. What is a function? Before we start making functions, lets understand what function is and why we need function?
Defining a Function
A function is a reusable block of code or programming statements designed to perform a certain task. To define a function, Python provides the def keyword. The following is the syntax of defining a function. The function block of code only executed only if we call the function.
Declaring and calling a function
When we make a function we call it declaring a function. When we start using the function we call it calling or invoking a function. Function can be declared with or without a parameter.
Function without parameters
Function can be declared without a parameter. Example:
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 value
Function can also return values, if a function does not return values the value of the function is None. Lets rewrite the above functions using return. From now on, we return value to a function instead of printing it.
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
# syntax
# Declaring a function
def function_name(parameter):
codes
codes
# Calling function
function_name(parameter)
````
**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)
sum_of_numbers(10) # 55
sum_of_numbers(100) # 5050
# syntax
# Declaring a function
def function_name(para1, para2):
codes
codes
# Calling function
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(2019, 1819))
def weight_of_object (mass, gravity):
weight = str(mass * gravity)+ ' N' # the value has to be changed to string first
return weight
print('Weight of an object in Newton: ', 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.
# syntax
# Declaring a function
def function_name(para1, para2):
codes
codes
# Calling function
function_name(para1='John', para2='Doe') # the order of argument now does not matter
````
**Example:**
```py
def print_fullname(firstname, lastname):
space = ' '
full_name = firstname + space + lastname
print(full_name)
print_fullname(firstname='Asabeneh', lastname='Yetayeh')
def add_two_numbers (num1, num2):
total = num1 + num2
print(total)
add_two_numbers(num2=3, num1=2) # Order does not matter
Returning a value from a function
If we do not return a value from a function, then our function is returning None by default. To return a value from a function we use the key word return followed by the data type we are returning. We can return any kind of data types from a function. * Returning string: Example:
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')
Example:
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))
def is_even (n):
if n % 2 == 0:
print('even')
return True
return False
print(is_even(10)) # True
print(is_even(7)) # False
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 an argument the default value will be used.
# 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 = 2019):
age = current_year - birth_year
return age;
print('Age: ', calculate_age(1819))
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 Newton: ', weight_of_object(100)) # 9.81 gravity at the surface of Earth
print('Weight of an object in Newton: ', weight_of_object(100, 1.62)) # gravity at surface of 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 add * before the parameter name.
# syntax
# Declaring a function
def function_name(*args):
codes
codes
# Calling function
function_name(param1, param2, param3,..)
def sum_all_nums(*nums):
total = 0
for num in nums:
total += num
return total
print(sum_all_nums(2, 3, 5))
Default and arbitrary number of parameters in function
def generate_groups (team,*args):
print(team)
for i in args:
print(i)
generate_groups('Team-1','Asabeneh','Brook','David','Eyob')
Function as parameter of other function
#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))
π» Exercises: Day 11
- Declare a function add_two_numbers and it takes two two parameters and it returns sum.
- Area of a circle is calculated as follows: area = Ο x r x r. Write a function which calculates area_of_circle.
- Write a function called add_all_nums which take arbitrary number of arguments and sum all the arguments. Check if all the list items are number types. If not give return reasonable feedback.
- Temperature in oC can be converted to oF using this formula: oF = (oC x 9/5) + 32. Write a function which converts oC to oF, convert_celcius_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 slop of a linear equation
- Quadratic equation is calculated as follows: ax2 + bx + c = 0. Write a function which calculates solution set of a quadratic equation, solve_quadratic_eqn.
- Declare a function name print_list. It takes list as a parameter and it prints out each element of the list.
- Declare a function name reverse_list. It takes array as a parameter and it returns the reverse of the array (dontβ use method).
- Declare a function name capitalize_list_items. It takes list as a parameter and it returns the capitalized list of the items
- Declare a function name add_item. It takes a list and an item parameter and it returns a list after adding the item
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]
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]
π Day 12
Module
What is a module
A module is a file containing set of codes or a set of function which can be included to an application. A module could be a file containing a single variable, or function, a big code base.
Creating a module
To create a module we write our codes in a python script and we save it as .py file. Create a file named mymodule.py inside your project folder. Let write code on this file.
# mymodule.py file
def generate_full_name(firstname, lastname):
space = ' '
fullname = firstname + space + lastname
return fullname
Importing a module
To import the file we use the import keyword and the name of the file only.
Import functions from a module
We can have many functions in a file and we can import all the functions differently.
# main.py file
from mymodule import generate_full_name, sum_two_nums, person, gravity
print(generate_full_name('Asabneh','Yetay'))
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.
# 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 Builtin Modules
Like other programming languages we can also import modules by importing the file/function using the key word import. Lets import the common module we will use most of the time. Some of the common builtin 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.
# 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. 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 argument passed from the command line.
import sys
print(sys.argv[0], argv[1],sys.argv[2])
print('Welcome {}. Enjoy {} challenge!'.format(sys.argv[1], sys.argv[2]))
output
Welcome Asabeneh. Enjoy 30DayOfPython challenge!
# to exist syst
sys.exit()
# To know the largest integer variable it takes
sys.maxsize
# To know environment path
syst.path
# To know the version of python you are using
sys.version
Statistics Module
The statistics module provides functions to mathematical statistics of numeric data. The popular statistical functions which are defined in this module mean, median, mode, stdev etc.
from statistics import * # importing all the statistics module
print(mean(ages)) # 22.4
print(median(ages)) # 23
print(mode(ages)) # 20
print(stdev(ages)) # 2.3
Math Module
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
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
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
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
Random Module
By now you are familiar with importing modules. Lets do another more import to be very familiar with importing. Let's import random module which can gives random number between 0 and 0.9999.... The random module has lots of functions but in this section we will only see random and randint.
from random import random, randint
print(random()) # it doesn't take argument and return 0 to 0.9999
print(randint(5, 20)) # it returns a random number between 5 and 20
π» Exercises: Day 12
- Writ a function which generates a six digit random_user_id.
- Modify question number above . Declare a function name user_id_gen_by_user. It doesnβt take any parameter but it takes two inputs using input(). One of the input is the number of characters and the second input is the number of ids which are supposed to be generated.
- Write a function name rgb_color_gen and it generates rgb colors.
- Write a function list_of_hexa_colors which return any number of hexadecimal colors in an array.
- Write a function list_of_rgb_colors which return any number of RGB colors in an array. Write a function generate_colors which can generate any number of hexa or rgb colors.
- Call your function shuffle_list, it takes a list as a parameter and it returns a shuffled list
- Write a function which returns array of seven random numbers in a range of 0-9. All the numbers must be unique.