A step by step guide to learn Python Programming
https://github.com/Asabeneh/Python-for-Everyone.git
Python for Everyon is a step by step guide to learn Python and programming in general. Python For Everyone is a guide for both beginners and advanced Python developers. Welcome to Python For Everyone.
Congratulations for deciding to learn Python. Python is eating the world. It has been the choice for developers to use python for different puprpose specifically to deal with data.
In this step by step guide, you will learn Pyton. Python is one of the most popular programming language. You use Python to develop web applicaton, to develop mobile apps, desktop applications, games and mostly Python is use for data Sciecne, machine learning and AI. Python has increased in popularity in recent years and has been the leading programming language for data science and machine language.
First install Python. To write python code, we need to have a code editor. Lets install text or code editor. There are many open source code editors which can help you to write python code. These are the most commonly used ones:
To run a python script you need to install python. Let's download python. If your are a windows user. Click the button encircled in red.
If you are a macOS user. Click the button encircled in red.
To check if python is installed write the following command on your device terminal.
python --version
As you can see from the terminal, I am using python 3.7.5 version at the moment. If you mange to see the python version, well done. Python has been installed on your machine. Continue to the next section.
Python is an interpreted scripting language, so it doesn't need to be compiled. It means it executes the code line by line. Python comes with a Python Shell (Python Interactive Shell). It is used to execute a single python command and get the result.
Python Shell waits for the python code from the user. When you enter the code, it interprets the code and shows the result in the next line. Open your terminal or command prompt(cmd) and write:
python
The python interactive shell is opened and it is waiting for you to write python code. You will write your python script next to this symbol >>> and then click Enter. Lets write our very first script on the python scripting shell.
Well done, you wrote your first python script on python interactive shell. How do we close this shell ? To close the shell, next to this symbol >> write exit() command and press Enter.
Now, you know how to open the python interactive shell and how to exit from it.
Python will give you results if you write scripts that python understands, if not it returns errors. Let's make a deliberate mistake and see what python will return.
As you can see from the returned error, python is so clever that it knows the mistake we made and which was Syntax Error: invalid syntax. Using x as multiplication in python is a syntax error because (x) is not a valid syntax in python. Instead of (x) we use asterisk (*) for multiplication. The returned error clearly shows what to fix. The process of identifying and removing errors from a program is called debugging. Let's debug it by putting * in place of x.
Our bug was fixed, the code ran and we got a result we were expecting. As a programmer you will see such kind of errors on daily basis. It is good to know how to debug. To be good at debugging you should understand what kind of errors you are facing:SyntaxError, IndexError, ModuleNotFoundError, KeyError, ImportError etc. We will see more about different python error types in later sections.
Let's practice more how to use python interactive shell. Go to your terminal or command prompt and write the word python.
The python interactive shell is opened. Let's do some basic mathematic operations (addition, subtraction, multiplication, division, modulus, exponential). Lets do some maths first before we write any python code:
# comment starts with hash
# this is a python comment, because it starts with a (#) symbol
Before we move on to the next section, lets practice more on the python interactive shell. Close the opened shell by writing exit() on the shell and open it again and let's practice how to write text on the python shell.
The python interactive shell is good to try and test small script codes but it won't be for a big project. In real work environment, developers use different code editors to write codes. In this 30 days of python programming challenge we will use visual studio code. Visual studio code is a very popular open source text editor. I am a fan of vscode and I would recommend to download visual studio code, but if you are in favor of other editors, feel free to follow with what you have.
If you installed visual studio code, let's see how to use it.
Open the visual studio code by double clicking the visual studio icon. When you open it you will get this kind of interface. Try to interact with the labeled icons.
Create a folder named 30DaysOfPython on your desktop. Then open it using visual studio code.
After opening it you will see shortcuts for creating files and folders inside of 30DaysOfPython project's directory. As you can see below, I have created the very first file, helloworld.py. You can do the same.
After a long day of coding, you want to close your code editor, right? This is how you will close the opened project.
Congratulations, you have finished setting up the development environment. Let's start coding.
A python script can be written in python interactive shell or in the code editor. A python file has an extension .py.
An indentation is a white space in a text. Indentation in many languages is used to increase code readability, however python uses indentation to create block of codes. In other programming languages curly brackets are used to create blocks of codes instead of indentation. One of the common bugs when writing python code is wrong indentation.
Comments are very important to make the code more readable and to leave remarks in our code. Python doesn't run comment parts of our code. Any text starting with hash(#) in python is a comment.
Example: Single Line Comment
# This is the first comment
# This is the second comment
# Python is eating the world
Example: Multiline Comment
Triple quote can be used for multiline comment if it is not assigned to a variable
"""This is multiline comment
multiline comment takes multiple lines.
python is eating the world
"""
In python there are several types of data types. Let's get started with the most common ones. Different data types will be covered in detail in other sections. For the time being let us just go through the different data types and get familiar with them. You do not have to have a clear understanding now.
A collection of one or more characters under a single or double quote. If a string is more than one sentence then we use a triple quote.
Example:
'Asabeneh'
'Finland'
'Python'
'I love teaching'
'I hope you are enjoying Python for Everyone'
A boolean data type is either a True or False value. T and F should be always uppercase.
Example:
True # Is the light on? If it is on, then the value is True
False # Is the light on? If it is off, then the value is False
Python list is an ordered collection which allows to store different data type items. A list is similar to an array in JavaScript.
Example:
[0, 1, 2, 3, 4, 5] # all are the same data types - a list of numbers
['Banana', 'Orange', 'Mango', 'Avocado'] # all the same data types - a list of strings (fruits!)
['Finland','Estonia', 'Sweden','Norway'] # all the same data types - a list of strings (countries!)
['Banana', 10, False, 9.81] # different data types in the list - string, integer, boolean and float
A python dictionary object is an unordered collection of data in a key:value pair format.
Example:
{'name':'Asabeneh', 'country':'Finland', age:250, 'is_married':True}
A tuple is an ordered collection of different data types like list but tuples can not be modified once they are created. They are immutable.
Example:
('Asabeneh', 'Brook', 'Abraham', 'Lidiya')
A set is a collection of data types similar to list and tuple. Unlike list and tuple, set is not an ordered collection of items. Like in mathematics, set in python stores only unique items.
In later sections, we will go in detail about each and every python data type.
Example:
{3.14, 9.81, 2.7} # order is not important in set
To check the data type of certain data/variable we use the type function. In the following terminal you will see different python data types:
First open your project folder, 30DaysOfPython. If you don't have this folder, create a folder name called 30DaysOfPython. Inside this folder, create a file called helloworld.py. Now, let's do what we did on python interactive shell using visual studio code. The python interactive shell was printing without using print but on visual studio code to see our result we should use a built in function print(some data to print). See the examples below.
Example:
The file name is helloworld.py
# Day 1 - 30DaysOfPython Challenge
print(2 + 3) # addition(+)
print(3 - 1) # subtraction(-)
print(2 * 3) # multiplication(*)
print(3 / 2) # division(/)
print(3 ** 2) # exponential(**)
print(3 % 2) # modulus(%)
print(3 // 2) # Floor division operator(//)
# Checking data types
print(type(10)) # Int
print(type(3.14)) # Float
print(type(1 + 3j)) # Complex number
print(type('Asabeneh')) # String
print(type([1, 2, 3])) # List
print(type({'name':'Asabeneh'})) # Dictionary
print(type({9.8, 3.14, 2.7})) # Set
print(type((9.8, 3.14, 2.7))) # Tuple
To run the python file check the image below. You can run the python file either by running the green button or by typing python helloworld.py in the terminal .
Comment is essential to make code readable to ourselves or for others. Commented part of our code is not interperated by Python interperator.
# This is a single line comment
# Printing in Python
print('Hello, World!')
print('Hello,', "World", "!")
print("Welcome to Python!")
print('Learn python in', 2019)
print('Welcome to Python for Everyone')
print('Welcome','to', 'Python','for','Everyone')
Hello, World! Hello, World ! Welcome to Python! Learn python in 2019 Welcome to Python for Everyone Welcome to Python for Everyone
Variables are containers or a means to store data in computer memory. pneumonic varialbes recommend to use in many programming langauges.
Valid variable names
firstname
lastname
age
country
city
first_name
last_name
capital_city
_if # if we want to use reservered word as a variable
first_name
year_2019
year2019
current_year_2019
num1
num2
Invalid varaible names
# Variables in Python
first_name = "Asabeneh"
last_name = "Yetayeh"
country = "Helsinki"
age = 250
is_married = True
# Printing the values stored in the variables
print('First name:', first_name)
print('Last name: ', last_name)
print('Country: ', country)
print('Age: ', age)
print('Married: ', is_married)
First name: Asabeneh Last name: Yetayeh Country: Helsinki Age: 250 Married: True
Variable can also be declared in one line:
first_name, last_name, country, age, is_married = 'Asabeneh', 'Yetayeh', 'Helsink', 250, True
print(first_name, last_name, country, age, is_married)
print('First name:', first_name)
print('Last name: ', last_name)
print('Country: ', country)
print('Age: ', age)
print('Married: ', is_married)
Asabeneh Yetayeh Helsink 250 True First name: Asabeneh Last name: Yetayeh Country: Helsink Age: 250 Married: True
name = input('What is your name ?')
age = int(input('How old are you ?'))
if age < 18:
print(name,' you are under age')
else:
print(name, ', you are old enough')
What is your name ?Asabeneh How old are you ?100 Asabeneh , you are old enough
Different data types in python. There are different data type in python programming. To identify the data tpe we use the type method.
# Different python data types
# Let's declare different data tyeps
first_name = 'Asabeneh' # String
last_name = 'Yetayeh' # String
country = 'Finland' # String
city= 'Helsinki'
age = 250 # Number, it is not my real age, don't worry about it
print(type('Asabeneh'))
print(type(first_name))
print(type(10))
print(type(3.14))
print(type(1 + 1j))
print(type(True))
print(type([1, 2,3,4]))
print(type({"name":"Asabeneh","age":250, "is_married":250}))
print(type((1,2)))
print(type(zip([1,2],[3,4])))
# Arthimetic Operations in Python
# Integers
print('Additon: ', 1 + 2)
print('Substraction: ', 2 - 1)
print('Multiplication: ', 2 * 3)
print ('Division: ', 4 / 2) # Division in python gives floating number
print('Division: ', 6 / 2)
print('Division: ', 7 / 2)
print('Division without the remainder: ', 7 // 2) # gives without the floating number or without the remaining
print('Modulous: ', 3 % 2)
print ('Division without the remainder: ',7 // 3)
print('Exponentation: ', 3 ** 2)
# Floating numbers
print('Floating Number', 3.14)
# Complex numbers
print('Complex number: ', 1+1j)
print('Multiplying complext number: ',(1+1j) * (1-1j))
print('== Additon, Subtraction, Multipliation, Division, Modules ==')
num_one = 3
num_two = 4
total = num_one + num_two
diff = num_two - num_one
product = num_one * num_two
div = num_two / num_two
remainder = num_two % num_one
print('sum: ', total)
print('difference: ', diff)
print('product: ', product)
print('division: ', div)
print('remainder: ', remainder)
mass = 75
gravity = 9.81
# Calcualte the wieght of the object on planet earth
weight = mass * gravity
print(weight, 'N')
Additon: 3 Substraction: 1 Multiplication: 6 Division: 2.0 Division: 3.0 Division: 3.5 Division without the remainder: 3 Modulous: 1 Division without the remainder: 2 Exponentation: 9 Floating Number 3.14 Complex number: (1+1j) Multiplying complext number: (2+0j) == Additon, Subtraction, Multipliation, Division, Modules == sum: 7 difference: 1 product: 12 division: 1.0 remainder: 1 735.75 N
String is data type. Any data under single or double quot are a string. There are diferent string methods to deal with string data types. To check the length of a string use the len() method.
# Assigning variables to string value
first_name = "Asabeneh"
space = ' ' # an empty space string
last_name = "Yetayeh"
country = "Helsinki"
full_name = ' Asabeneh Yetayeh'
print(first_name)
print(len(first_name))
print(last_name)
print(len(last_name))
print(country)
print(space) # You don't see the printed empty space
print(full_name) # there is indent because of the trailing space in the full name is string
Asabeneh
8
Yetayeh
7
Helsinki
Asabeneh Yetayeh
Merging two or more strings together is called concatination
# String concatination
first_name = "Asabeneh"
space = ' ' # an empty space string
last_name = "Yetayeh"
country = "Finland"
city = 'Helsinki'
full_name = first_name + space + last_name
git_repo= 'Python ' + ' for ' + ' Everyone'
person_info = 'I am ' + full_name + '. I live in ' + country + ', '+ city + '.'
print('Full name: ', full_name)
print('Person Information:', person_info)
print('Git repository: ', git_repo)
Full name: Asabeneh Yetayeh Person Information: I am Asabeneh Yetayeh. I live in Finland, Helsinki. Git repository: Python for Everyone
firstName = 'Asabeneh'
lastName = 'Yetayeh'
space = ' '
full_Name = first_name + space + last_name;
# Changing string to lower case
print('=== change to lower case ===')
print(first_name.lower())
print(last_name.lower())
print(full_Name.lower())
# Changing string to upper case
print('=== change to upper case ===')
print(first_name.upper())
print(lastName.upper())
print(full_Name.upper())
# Changing string to capitalize
print('=== change to capitalize ===')
print(first_name.capitalize())
print(lastName.capitalize())
print(full_Name.capitalize())
# Changing string to capitalize
print('=== change to title ===')
title = 'python for everyone'
sub_title = 'learning programming using python'
lang = 'python'
level = 'both begineer and advanced learners'
print(title.title())
print(sub_title.title())
print(lang.title())
print(level.title())
# Changing string to swapcase()
print('=== swapping cases ===')
first_name = 'ASABEEH'
last_name = 'yetayeh'
space = ' '
full_name = first_name + space + last_name;
print(first_name.swapcase())
print(last_name.swapcase())
print(full_name.swapcase())
=== change to lower case === asabeneh yetayeh asabeneh yetayeh === change to upper case === ASABENEH YETAYEH ASABENEH YETAYEH === change to capitalize === Asabeneh Yetayeh Asabeneh yetayeh === change to title === Python For Everyone Learning Programming Using Python Python Both Begineer And Advanced Learners === swapping cases === asabeeh YETAYEH asabeeh YETAYEH
Empty space is the default paramter.
# Spliting a stirng to list
first_name = 'Asabeneh'
last_name = 'Yetayeh'
full_name = 'Asabeneh Yetayeh'
programming_lang = 'python, R, matlab, and Java'
# Changing a string to list
print(first_name.split())
print(list(first_name))
print(full_name.split(' '))
print(programming_lang.split(','))
# To check the length of string
print(len(first_name))
# To check the data type of the string
print(type(first_name))
['Asabeneh']
['A', 's', 'a', 'b', 'e', 'n', 'e', 'h']
['Asabeneh', 'Yetayeh']
['python', ' R', ' matlab', ' and Java']
8
A boolean value is either True or False.
num_str = '10'
print(num_str)
print(type(num_str))
num_int = int(num)
print(num_int)
print(type(num_int))
num_float = float(num_str)
print(num_float)
e = 2.71
10
# Comparing something give either a True or False
print('True == True: ', True == True)
print('True == False: ', True == False)
print('False == False:', False == False)
print('True and True: ', True and True)
print('True or False:', True or False)
print('a in an:', 'a' in 'an')
print('4 is 2 ** 2:', 4 is 2 **2)
print(3 > 2)
print(3 >= 2)
print(3 != 2)
print(3 < 2)
print(3 <= 2)
print(3==2)
print(3!='3')
print(3==3)
True == True: True True == False: False False == False: True True and True: True True or False: True a in an: True 4 is 2 ** 2: True True True True False False False True True
num = 10
if num > 5:
print('Number is greater than 5')
# The is conditon does't get exected so we need else
if num < 5:
print('Number is less than 5')
if num < 5:
print('Number is less than 5')
else:
print('Number is greater than 5')
num = 5
if num < 5:
print('Number is less than 5')
elif num == 5:
print('Number is 5')
else:
print('Number is greater than 5')
Number is greater than 5 Number is greater than 5 Number is 5
Enter your age: 30
You are old enough to drive.
Enter your age:15
You are left with 3 years to drive.
Enter your age: 30
You are 5 years older than me.
sh let a = 4; let b = 3; 4 is greater than 3
print('=== Whole Numbers===')
for x in range(11):
print(x)
print('=== Even Numbers===')
# the range(initial, stop, step)
for n in range(0, 11, 2):
print(n)
print('=== Odd Numbers===')
# the range(initial, stop, step)
for n in range(1, 11, 2):
print(n)
=== Whole Numbers=== 0 1 2 3 4 5 6 7 8 9 10 === Even Numbers=== 0 2 4 6 8 10 === Odd Numbers=== 1 3 5 7 9
for x in range(10):
if (x is 1):
continue
if (x > 5):
break
print(x)
0 2 3 4 5
x = 0
while (x < 10):
print(x)
x += 1
0 1 2 3 4 5 6 7 8 9
#
##
###
####
#####
######
#######
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
# The sum of all numbers is 5050.
# The sum of all evens is 2550. And the sum of all odds is 2500.
Lists are like arrays in JavaScript. They store different elements unlike other varialbles. List has different methods to modify and manipulate the list. Some of the methods which are used to modiyf lists append, insert, extend.
# Creating lists
lst = [] # empty list
print(list)
lst = list() # empty list
print(lst)
numbers = [1, 2, 3, 4, 5, 6] # creating list 1 to 6
print(numbers)
one_to_hunderd = list(range(11)) # creating list 1 to 10
even_numbers = list(range(0,51, 2)) # Create even number lists 0 to 50
print(one_to_hunderd)
print(even_numbers)
names = ['Asabeneh','Brook','Sami','David']
print(names)
it_companies = ['Google','Facebook','Nokia','Apple','Oracle','Amazon','IBM']
print(it_companies)
constants = [3.14, 9.81, 98.6, 100]
print(constants)
for number in numbers:
print(number)
if (number % 2 == 0):
print("is even")
else:
print("is odd")
print ("All done.")
1 is odd 2 is even 3 is odd 4 is even 5 is odd 6 is even All done.
Using different methods to manipulate lists. Slicing using :, appending, extending and inserting:
# First lets create a list
x = [1, 2, 3, 4, 5, 6]
print(len(x)) # to check the length of the list
6
x.append(8)
x.append(9)
x
[1, 2, 3, 4, 5, 6, 8, 9]
Inserting a value at a certain index
x.insert(6,7) # the missing value in the list which is 7 is inserted
x
[1, 2, 3, 4, 5, 6, 7, 8, 9]
x.extend([9,10])
x
[1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10]
x[:3]
[1, 2, 3]
x[3:]
[4, 5, 6, 7, 8, 9, 9, 10]
x[-2:]
[9, 10]
a = [1, 2,3]
b = [4, 5,6]
c = a + b
print(c)
[1, 2, 3, 4, 5, 6]
List of lists
x = [1, 2,3]
y = [4, 5,6];
z = [x, y]
print(z)
[[1, 2, 3], [4, 5, 6]]
print(x[0])
print(z[0])
print(z[0][0])
print(z[1])
print(z[1][0])
1 [1, 2, 3] 1 [4, 5, 6] 4
z = [3, 2, 1]
z.sort()
z
[1, 2, 3]
z.sort(reverse=True)
z
[3, 2, 1]
nums_one = [3,1,4,2,5]
nums_sort =nums_one.sort() # mutate the original list and return None
print(nums_one)
print(nums_sort) # return None
nums_two = [3,1,4,2,5]
nums_sorted = sorted(nums_two) # Do not mutate the original list
print(nums_two)
print(nums_sorted)
[1, 2, 3, 4, 5] None [3, 1, 4, 2, 5] [1, 2, 3, 4, 5]
even_nums = [2,4,6,8,10]
for i, n in enumerate(even_nums):
print('index:',i, n)
index: 0 2 index: 1 4 index: 2 6 index: 3 8 index: 4 10
for i, n in enumerate(range (10)):
print('index:',i, n)
index: 0 0 index: 1 1 index: 2 2 index: 3 3 index: 4 4 index: 5 5 index: 6 6 index: 7 7 index: 8 8 index: 9 9
(name, age) = ['Asabeneh', 250]
print(name, age)
Asabeneh 250
[i for i in range (11)] # list of whole numbers
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[i * i for i in range (11)] # List of squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
even_nums = [i for i in range(51) if i % 2 == 0] # List of even numbers
print(even_nums)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50]
odds = [i for i in range(51) if i % 2 != 0] # List of odd numbers
print(odds)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]
numbers = [1, 2, 3,4,5]
[n * n for n in numbers] # maping using list comprehension
[1, 4, 9, 16, 25]
names = ['Asabeneh','Brook','David','Martha']
[name.upper () for name in names] # Mapping using list comprehension
['ASABENEH', 'BROOK', 'DAVID', 'MARTHA']
numbers = [-4, -3, -2, -1, 0, 2, 4, 6]
[n for n in numbers if n > 0] # Filtering using list comprehension
[2, 4, 6]
#Tuples are just immutable lists. Use () instead of []
tp = tuple()
print(type (tp))
tp = ()
print(type(tp))
nums = (1, 2, 3)
print(nums)
len(nums)
3
for n in nums:
print(n)
1 2 3
x = (1, 2, 3)
y = (4, 5, 6)
print(x[0])
y[0]
1
4
list_of_tuples = [x, y]
list_of_tuples
[(1, 2, 3), (4, 5, 6)]
(age, income) = "32,120000".split(',')
print(age)
print(income)
32 120000
numbers = [1, 2, 3,4,5]
[(n,n * n) for n in numbers] # maping using list comprehension
[(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
names = ['Asabeneh','Brook','David','Martha']
[(name.upper (), len(name)) for name in names] # Mapping using list comprehension
[('ASABENEH', 8), ('BROOK', 5), ('DAVID', 5), ('MARTHA', 6)]
# Creating empty dictionary
dic = dict()
dic = {}
# Another more dictionary
person = {
'name':'Asabenh',
'age':250,
'country':'Finland',
'city':'Helsinki',
'job':'instructor and developer',
'skills':['HTML','CSS','JavaScript','Python','Node'],
'is_married':True
}
print(person)
print(len(person))
{'name': 'Asabenh', 'age': 250, 'country': 'Finland', 'city': 'Helsinki', 'job': 'instructor and developer', 'skills': ['HTML', 'CSS', 'JavaScript', 'Python', 'Node'], 'is_married': True} 7
print(person)
{'name': 'Asabenh', 'age': 250, 'country': 'Finland', 'city': 'Helsinki', 'job': 'instructor and developer', 'skills': ['HTML', 'CSS', 'JavaScript', 'Python', 'Node'], 'is_married': True}
print(person["name"])
Asabenh
print(person["age"])
250
print(person.get("is_married"))
True
for key in person:
print(key + ": " + str(person[key]))
name: Asabenh age: 250 country: Finland city: Helsinki job: instructor and developer skills: ['HTML', 'CSS', 'JavaScript', 'Python', 'Node'] is_married: True
keys, values, items
keys = person.keys()
values = person.values()
items = person.items()
print(keys)
print(values)
print(items)
dictkeys(['name', 'age', 'country', 'city', 'job', 'skills', 'ismarried']) dict_values(['Asabenh', 250, 'Finland', 'Helsinki', 'instructor and developer', ['HTML', 'CSS', 'JavaScript', 'Python', 'Node'], True]) dictitems([('name', 'Asabenh'), ('age', 250), ('country', 'Finland'), ('city', 'Helsinki'), ('job', 'instructor and developer'), ('skills', ['HTML', 'CSS', 'JavaScript', 'Python', 'Node']), ('ismarried', True)])
for k, v in items:
print(k,':', v)
name : Asabenh age : 250 country : Finland city : Helsinki job : instructor and developer skills : ['HTML', 'CSS', 'JavaScript', 'Python', 'Node'] is_married : True
numbers = [1, 2, 3,4,5]
[{n:n * n} for n in numbers] # maping using list comprehension
[{1: 1}, {2: 4}, {3: 9}, {4: 16}, {5: 25}]
names = ['Asabeneh','Brook','David','Martha']
[{name:len(name)} for name in names] # Mapping using list comprehension
[{'Asabeneh': 8}, {'Brook': 5}, {'David': 5}, {'Martha': 6}]
one, two, three = (1, 2,3)
print(one)
print(two)
print(three)
first_name, last_name, country = ('Asabeneh','Yetayeh','Finland')
print(first_name)
print(last_name)
print(country)
1 2 3 Asabeneh Yetayeh Finland
# First Example about unpacking list
names = ['Asabeneh','Eyob','David','Lidiya', 'Woyneshet','Yetayeh']
first_person, second_person, *rest = names
print(first_person)
print(second_person)
print(rest)
# Second Example about unpacking list
first, second, third,*rest, tenth = [1,2,3,4,5,6,7,8,9,10]
print(first)
print(second)
print(third)
print(rest)
print(tenth)
# Third Example about unpacking list
countries = ['Germany', 'France','Belgium','Sweden','Denmark','Finland','Norway','Iceland','Estonia']
gr, fr, bg, sw, *scandic, es = countries
print(gr)
print(fr)
print(bg)
print(sw)
print(scandic)
print(es)
Asabeneh Eyob ['David', 'Lidiya', 'Woyneshet', 'Yetayeh'] 1 2 3 [4, 5, 6, 7, 8, 9] 10 Germany France Belgium Sweden ['Denmark', 'Finland', 'Norway', 'Iceland'] Estonia
Creating a set
s = set()
s.add(1) # adding value to a set
s.add(2)
s.add(3)
s.add(4)
s.add(5)
print(s)
print(len(s))
nums = [1, 2,2,3,4,5,4,6,7,6]
numbers = set(nums)
print(numbers)
print(len(numbers))
for n in numbers:
print(n)
{1, 2, 3, 4, 5} 5 {1, 2, 3, 4, 5, 6, 7} 7 1 2 3 4 5 6 7
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() # call the functionK
# Function can also return values, if a function doen't 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 priting 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())
Asabeneh Yetayeh 5 Asabeneh Yetayeh 5
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))
Asabeneh, welcome to Python for Everyone! 100 4 314.0
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))
Full Name: Asabeneh Yetayeh Sum of two numbers: 10 Age: 200 Weight of an object in Newton: 981.0 N
def sum_of_numbers(*args):
s = 0;
for i in args:
s = s + i
return s
print(sum_of_numbers(1,2,3))
print(sum_of_numbers(1,2,3, 4,5,6,7,8,9,10))
6 55
def greetings (name = 'Anonymous'):
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)) # gravit at surface of Moon
Anonymous, welcome to Python for Everyone! Asabeneh, welcome to Python for Everyone! Asabeneh Yetayeh David Smith Age: 200 Weight of an object in Newton: 981.0 N Weight of an object in Newton: 162.0 N
def generate_groups (team,*args):
print(team)
for i in args:
print(i)
generate_groups('Team-1','Asabeneh','Brook','David','Eyob')
Team-1 Asabeneh Brook David Eyob
#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))
9
Lamda function is similary to annonymous function in JavaScript. When we do not like to reuse a function we can make a lambda function. See the example below.
#Lambda functions let you inline simple functions
print(do_something(lambda x: x * x * x, 3))
27
swap_values(3, 4) # x => 4, y=>3
swap_values(4, 5) # x = 5, y = 4
pirnt(reverseArray([1, 2, 3, 4, 5]))
# [5, 4, 3, 2, 1]
print(reverseArray(["A", "B", "C"]))
# ["C", "B", "A"]
print(evens_and_odds(100))
# The number of odds are 50.
# The number of evens are 51.
sum_all_numbers(1, 2, 3) // -> 6
sum_all_numbers(1, 2, 3, 4) // -> 10
print(random_hexa_gen());
# '#ee33df'
print(user_id_gen());
# 41XTDbE
user_id_gen_by_user()
"kcsy2
SMFYb
bWmeq
ZXOYh
2Rgxf
"
user_id_gen_by_user()
"1GCSgPLMaBAVQZ26
YD7eFwNQKNs7qXaT
ycArC5yrRupyG00S
UbGxOFI7UXSWAyKN
dIV0SSUTgAdKwStr
"
print(rgb_color_gen())
# rgb(125,244,255)
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)'
print(modifyArray(["Avocado", "Tomato", "Potato","Mango", "Lemon","Carrot"]);
# β["Avocado", "Tomato", "Potato","Mango", "LEMON", "Carrot"]
print(modifyArray(["Google", "Facebook","Apple", "Amazon","Microsoft", "IBM"]);
# β["Google", "Facebook","Apple", "Amazon","MICROSOFT", "IBM"]
print(modifyArray(["Google", "Facebook","Apple", "Amazon"]);
# β"Not Found"
Python is an object oriented programming language. Everything in Python is an object, with its properties and methods. A number, string, list, dictionary,tuple, set etc. used in a program is an object of a corresponding built-in class. We create class to create an object. A Class is like an object constructor, or a "blueprint" for creating objects. We instantiate a class to create an object. The class defines attributes and the behavior of the object, while the object, on the other hand, represents the class.
We have been working with classes and objects right from the beginning of these challenge unknowingly. Every element in a Python program is an object of a class. Let's check if everything in python is class:
Last login: Tue Dec 10 09:35:28 on console
asabeneh@Asabeneh:~$ pyhton
-bash: pyhton: command not found
asabeneh@Asabeneh:~$ python
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> num = 10
>>> type(num)
<class 'int'>
>>> string = 'string'
>>> type(string)
<class 'str'>
>>> boolean = True
>>> type(boolean)
<class 'bool'>
>>> lst = []
>>> type(lst)
<class 'list'>
>>> tpl = ()
>>> type(tpl)
<class 'tuple'>
>>> set1 = set()
>>> type(set1)
<class 'set'>
>>> dct = {}
>>> type(dct)
<class 'dict'>
To create a class we need the key word class followed by colon. Class name should be CamelCase.
# syntax
class ClassName:
code goes here
Example:
class Person:
pass
<__main__.Person object at 0x10804e510>
We can create an object by calling the class.
p = Person()
print(p)
In the above examples, we have created an object from the Person class. However, Class without a constructor is not really useful in real applications. Let's use constructor function to make our class more useful. Like the constructor function in Java or JavaScript, python has also a builtin init() constructor function. The init constructor function has self parameter which is a reference to the current instance of the class Examples:
class Person:
def __init__ (self, name):
self.name =name
p = Person('Asabeneh')
print(p.name)
print(p)
# output
Asabeneh
Let's add more parameter to the constructor function.
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)
# output
Asabeneh
Yetayeh
250
Finland
Helsinki
Objects can have methods. The methods are functions which are belongs to the object. Example:
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} year old. He lives in {self.city}, {self.country}'
p = Person('Asabeneh', 'Yetayeh', 250, 'Finland', 'Helsinki')
print(p.person_info())
# output
Asabeneh Yetayeh is 250 year old. He lives in Helsinki, Finland
Sometimes, you may want to have a default values for you object methods. If we give a default values for the parameters in the constructor, we can avoid error when we call or instantiate our class without parameters. Let's see how it looks using example.
Example:
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} year 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())
# output
Asabeneh Yetayeh is 250 year old. He lives in Helsinki, Finland.
John Doe is 30 year old. He lives in Noman city, Nomanland.
In the example below, the person class, all the constructor parameters have default values and in addition to that we have a skills default value which we can access it using method. Let's create add_skill method to add skill to the skills list.
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} year 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)
# output
Asabeneh Yetayeh is 250 year old. He lives in Helsinki, Finland.
John Doe is 30 year old. He lives in Noman city, Nomanland.
['HTML', 'CSS', 'JavaScript']
[]
Using inheritance we can reuse parent class code. Inheritance allows us to define a class that inherits all the methods and properties from another class. The parent class or super or base class is the class which gives all the methods and properties. Child class is the class the inherits from another class. Let's see create a student class by inheriting from person class.
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)
output
Eyob Yetayeh is 30 year old. He lives in Helsinki, Finland.
['JavaScript', 'React', 'Python']
Lidiya Teklemariam is 28 year old. He lives in Espoo, Finland.
['Organizing', 'Marketing', 'Digital Marketing']
We didn't call the init() constructor in the child class. If we didn't call it we can access all the properties but if we call it once we access the parent properties by calling super. We can write add a new method to the child or we can overwrite the parent class by creating the same method name in the child class. When we add the init() function, the child class will no longer inherit the parent's init() function.
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} year 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)
Eyob Yetayeh is 30 year old. He lives in Helsinki, Finland.
['JavaScript', 'React', 'Python']
Lidiya Teklemariam is 28 year old. She lives in Espoo, Finland.
['Organizing', 'Marketing', 'Digital Marketing']
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('Variance: ',data.var()) # 17.5
print('Standard Deviation: ', data.std()) # 4.2
print('Variance: ',data.var()) # 17.5
print('Frequency Distribution: ',data.freq_dist()) # [(20.0, 26), (16.0, 27), (12.0, 32), (8.0, 37), (8.0, 34), (8.0, 33), (8.0, 31), (8.0, 24), (4.0, 38), (4.0, 29), (4.0, 25)]
# 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)]
A regular expression or RegEx is a small programming language that helps to find pattern 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 re.
After importing the module we can use it to detect or find patterns.
import re
To find a pattern we use different set of re functions that allows to search a string for match.
# 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
txt = 'I love to teach python or javaScript'
# It return 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 above example, the pattern we are looking for or the substring I love to teach is the beginning of the text. The match function only returns an object if the text starts with the pattern.
# 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
txt = '''Python is the most beautiful language that a human begin has ever created.
I recommend python for a first programming language'''
# It return 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 through out the text. Search return returns a match object right way a first match found. A much better re function is findall. This function check the pattern through the string and returns all the matches as a list.
findall() returns all the matches as a list
txt = '''Python is the most beautiful language that a human begin 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 found two times in the string. Let's practice more
Let's look for the word both Python and python in the string
txt = '''Python is the most beautiful language that a human begin 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 are included but if we don't have the flag, we write our pattern differently. Let's see that
txt = '''Python is the most beautiful language that a human begin 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 begin 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 begin 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 begin has ever created.
Let's add one more example, the following string is really hard to read unless we remove the % symbol. Replacing the % with a 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 m%ore 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) # ['Python', 'python']
I am 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.
txt = '''I am 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(re.split('\n', txt))
['I am 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.']
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.
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 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']
Let's use example to clarify the above meta characters
Let's 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 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', '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 in December 6, 2019.'
matches = re.findall(regex_pattern, txt)
print(matches) # ['6', '2', '0', '1', '9'], this is not what we want
regex_pattern = r'\d+' # d is a special character which means digits, + mean one or more
txt = 'This regular expression example was made in December 6, 2019.'
matches = re.findall(regex_pattern, txt)
print(matches) # ['6', '2019']
regex_pattern = r'\d+' # d is a special character which means digits, + mean one or more times
txt = 'This regular expression example was made in December 6, 2019.'
matches = re.findall(regex_pattern, txt)
print(matches) # ['6', '2019']
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 one or more times
txt = '''Apple and banana are fruits'''
matches = re.findall(regex_pattern, txt)
print(matches) # ['and banana are fruits']
Zero or one times. The pattern could 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 email others may write it as Email or E-mail.'''
regex_pattern = r'[Ee]-?mail' # ? means optional
matches = re.findall(regex_pattern, txt)
print(matches) # ['e-mail', 'email', 'Email', 'E-mail']
We can specify the length of the substring we look for in a text, using a curly bracket. Lets imagine, we are interested in substring that their length are 4 characters
txt = 'This regular expression example was made in December 6, 2019.'
regex_pattern = r'\d{4}' # exactly four times
matches = re.findall(regex_pattern, txt)
print(matches) # ['2019']
txt = 'This regular expression example was made in December 6, 2019.'
regex_pattern = r'\d{1, 4}' # 1 to 4
matches = re.findall(regex_pattern, txt)
print(matches) # ['6', '2019']
txt = 'This regular expression example was made in December 6, 2019.'
regex_pattern = r'^This' # ^ means starts with
print(matches) # ['This']
txt = 'This regular expression example was made in December 6, 2019.'
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) # ['e-mail', 'email', 'Email', 'E-mail']
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.
[(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')]
points = ['-1', '2', '-4', '-3', '-1', '0', '4', '8']
sorted_points = [-4, -3, -1, -1, 0, 2, 4, 8]
distance = 12
is_valid_variable('first_name') # True
is_valid_variable('first-name') # False
is_valid_variable('1first_name') # False
is_valid_variable('firstname') # True
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')]
import math
import re
import pandas as pd
import numpy as np
import urllib3
import lxml
print(math.sqrt(2))
print(math.pow(3,2))
print(math.pow(3,2) == 3 ** 2)
print(math.pi)
print(math.e)
# constantcs
pi = math.pi
e = math.e
t = math.tau # 2pi
print(pi)
print(e)
print(t)
A = np.random.normal(25, 5.0, 10)
print (A)
1.4142135623730951 9.0 True 3.141592653589793 2.718281828459045 3.141592653589793 2.718281828459045 6.283185307179586 [22.76977213 31.19118012 31.39357782 28.82041376 18.95246175 33.6695274 22.31571545 21.39637955 28.40572587 24.38512071]
Statistics is the discipline that studies the collection, organization, displaying, analysis, 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 to 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 decision 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 into context, it doesn't give 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 and unstructure data.
Data can be found as small or big data 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 numeric 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 (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 jupter 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.
asabeneh@Asabeneh:~/Desktop/30DaysOfPython$ pip install numpy
Jupyter notebook is available if your are in favor of jupyter notebook
# How to import numpy
import numpy as np
# How to check the version of the numpy package
print('numpy:', np.__version__)
# Checking the available methods
print(dir(np))
# Creating python List
python_list = [1,2,3,4,5]
# Checking data types
print('Type:', type (python_list)) # <class 'list'>
#
print(python_list) # [1, 2, 3, 4, 5]
two_dimensional_list = [[0,1,2], [3,4,5], [6,7,8]]
print(two_dimensional_list) # [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
# Creating Numpy(Numerical Python) array from python list
numpy_array_from_list = np.array(python_list)
print(type (numpy_array_from_list)) # <class 'numpy.ndarray'>
print(numpy_array_from_list) # array([1, 2, 3, 4, 5])
# Python list
python_list = [1,2,3,4,5]
numy_array_from_list2 = np.array(python_list, dtype=float)
print(numy_array_from_list2) # array([1., 2., 3., 4., 5.])
Creating a boolean a numpy array from list
numpy_bool_array = np.array([0, 1, -1, 0, 0], dtype=bool)
print(numpy_bool_array) # array([False, True, True, False, False])
A numpy array may have one or multiple rors and columns
two_dimensional_list = [[0,1,2], [3,4,5], [6,7,8]]
numpy_two_dimensional_list = np.array(two_dimensional_list)
print(type (numpy_two_dimensional_list))
print(numpy_two_dimensional_list)
<class 'numpy.ndarray'>
[[0 1 2]
[3 4 5]
[6 7 8]]
# 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())
<class 'list'>
one dimensional array: [1, 2, 3, 4, 5]
two dimensional array: [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
# Numpy array from tuple
# Creating tuple in Python
python_tuple = (1,2,3,4,5)
print(type (python_tuple)) # <class 'tuple'>
print('python_tuple: ', python_tuple) # python_tuple: (1, 2, 3, 4, 5)
numpy_array_from_tuple = np.array(python_tuple)
print(type (numpy_array_from_tuple)) # <class 'numpy.ndarray'>
print('numpy_array_from_tuple: ', numpy_array_from_tuple) # numpy_array_from_tuple: [1 2 3 4 5]
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.
nums = np.array([1, 2, 3, 4, 5])
print(nums)
print('shape of nums: ', nums.shape)
print(numpy_two_dimensional_list)
print('shape of numpy_two_dimensional_list: ', numpy_two_dimensional_list.shape)
three_by_four_array = np.array([[0, 1, 2, 3],
[4,5,6,7],
[8,9,10, 11]])
print(three_by_four_array.shape)
[1 2 3 4 5]
shape of nums: (5,)
[[0 1 2]
[3 4 5]
[6 7 8]]
shape of numpy_two_dimensional_list: (3, 3)
(3, 4)
Type of data types: str, int, float, complex, bool, list, None
int_lists = [-3, -2, -1, 0, 1, 2,3]
int_array = np.array(int_lists)
float_array = np.array(int_lists, dtype=float)
print(int_array)
print(int_array.dtype)
print(float_array)
print(float_array.dtype)
[-3 -2 -1 0 1 2 3]
int64
[-3. -2. -1. 0. 1. 2. 3.]
float64
In numpy to know the number of items in a numpy array list we use size
numpy_array_from_list = np.array([1, 2, 3, 4, 5])
two_dimensional_list = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
print('The size:', numpy_array_from_list.size) # 5
print('The size:', two_dimensional_list.size) # 3
The size: 5
The size: 9
Numpy array is not like exactly like python list. To do mathematical operation in pyhton list we have to loop through the items but numpy can allow to do any mathematical operation without looping. Mathematical Operation:
# Mathematical Operation
# Addition
numpy_array_from_list = np.array([1, 2, 3, 4, 5])
print('original array: ', numpy_array_from_list)
ten_plus_original = numpy_array_from_list + 10
print(ten_plus_original)
original array: [1 2 3 4 5]
[11 12 13 14 15]
# Subtraction
numpy_array_from_list = np.array([1, 2, 3, 4, 5])
print('original array: ', numpy_array_from_list)
ten_minus_original = numpy_array_from_list - 10
print(ten_minus_original)
original array: [1 2 3 4 5]
[-9 -8 -7 -6 -5]
# Multiplication
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)
original array: [1 2 3 4 5]
[10 20 30 40 50]
# Division
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)
original array: [1 2 3 4 5]
[0.1 0.2 0.3 0.4 0.5]
# Modulus; Finding 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 % 3
print(ten_times_original)
original array: [1 2 3 4 5]
[1 2 0 1 2]
# 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)
original array: [1 2 3 4 5]
[ 1 4 9 16 25]
#Int, Float numbers
numpy_int_arr = np.array([1,2,3,4])
numpy_float_arr = np.array([1.1, 2.0,3.2])
numpy_bool_arr = np.array([-3, -2, 0, 1,2,3], dtype='bool')
print(numpy_int_arr.dtype)
print(numpy_float_arr.dtype)
print(numpy_bool_arr.dtype)
int64
float64
bool
We can convert the data types of numpy array
numpy_int_arr = np.array([1,2,3,4], dtype = 'float')
numpy_int_arr
array([1., 2., 3., 4.])
numpy_int_arr = np.array([1., 2., 3., 4.], dtype = 'int')
numpy_int_arr
array([1, 2, 3, 4])
np.array([-3, -2, 0, 1,2,3], dtype='bool')
array([ True, True, False, True, True, True])
numpy_float_list.astype('int').astype('str')
array(['1', '2', '3'], dtype='<U21')
# 2 Dimension Array
two_dimension_array = np.array([(1,2,3),(4,5,6), (7,8,9)])
print(type (two_dimension_array))
print(two_dimension_array)
print('Shape: ', two_dimension_array.shape)
print('Size:', two_dimension_array.size)
print('Data type:', two_dimension_array.dtype)
<class 'numpy.ndarray'>
[[1 2 3]
[4 5 6]
[7 8 9]]
Shape: (3, 3)
Size: 9
Data type: int64
# 2 Dimension Array
two_dimension_array = np.array([[1,2,3],[4,5,6], [7,8,9]])
first_row = two_dimension_array[0]
second_row = two_dimension_array[1]
third_row = two_dimension_array[2]
print('First row:', first_row)
print('Second row:', second_row)
print('Third row: ', third_row)
First row: [1 2 3]
Second row: [4 5 6]
Third row: [7 8 9]
first_column= two_dimension_array[:,0]
second_column = two_dimension_array[:,1]
third_column = two_dimension_array[:,2]
print('First column:', first_column)
print('Second column:', second_column)
print('Third column: ', third_column)
print(two_dimension_array)
First column: [1 4 7]
Second column: [2 5 8]
Third column: [3 6 9]
[[1 2 3]
[4 5 6]
[7 8 9]]
two_dimension_array = np.array([[1,2,3],[4,5,6], [7,8,9]])
first_two_rows_and_columns = two_dimension_array[0:2, 0:2]
print(first_two_rows_and_columns)
[[1 2]
[4 5]]
two_dimension_array[::]
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
two_dimension_array = np.array([[1,2,3],[4,5,6], [7,8,9]])
two_dimension_array[::-1,::-1]
array([[9, 8, 7],
[6, 5, 4],
[3, 2, 1]])
print(two_dimension_array)
two_dimension_array[1,1] = 55
two_dimension_array[1,2] =44
print(two_dimension_array)
[[1 2 3]
[4 5 6]
[7 8 9]]
[[ 1 2 3]
[ 4 55 44]
[ 7 8 9]]
# Numpy Zeroes
# numpy.zeros(shape, dtype=float, order='C')
numpy_zeroes = np.zeros((3,3),dtype=int,order='C')
numpy_zeroes
array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
# Numpy Zeroes
numpy_ones = np.ones((3,3),dtype=int,order='C')
print(numpy_ones)
[[1 1 1]
[1 1 1]
[1 1 1]]
twoes = numpy_ones * 2
# Reshape
# numpy.reshape(), numpy.flatten()
first_shape = np.array([(1,2,3), (4,5,6)])
print(first_shape)
reshaped = first_shape.reshape(3,2)
print(reshaped)
[[1 2 3]
[4 5 6]]
[[1 2]
[3 4]
[5 6]]
flattened = reshaped.flatten()
flattened
array([1, 2, 3, 4, 5, 6])
## Horitzontal Stack
np_list_one = np.array([1,2,3])
np_list_two = np.array([4,5,6])
print(np_list_one + np_list_two)
print('Horizontal Append:', np.hstack((np_list_one, np_list_two)))
[5 7 9]
Horizontal Append: [1 2 3 4 5 6]
## Vertical Stack
print('Vertical Append:', np.vstack((np_list_one, np_list_two)))
Vertical Append: [[1 2 3]
[4 5 6]]
# Generate a random float number
random_float = np.random.random()
random_float
0.018929887384753874
# Generate a random float number
random_floats = np.random.random(5)
random_floats
array([0.26392192, 0.35842215, 0.87908478, 0.41902195, 0.78926418])
# Generating a random integers between 0 and 10
random_int = np.random.randint(0, 11)
random_int
4
# Generating a random integers between 2 and 11, and creating a one row array
random_int = np.random.randint(2,10, size=4)
random_int
array([8, 8, 8, 2])
# Generating a random integers between 0 and 10
random_int = np.random.randint(2,10, size=(3,3))
random_int
array([[3, 5, 3],
[7, 3, 6],
[2, 3, 3]])
# np.random.normal(mu, sigma, size)
normal_array = np.random.normal(79, 15, 80)
normal_array
array([ 89.49990595, 82.06056961, 107.21445842, 38.69307086,
47.85259157, 93.07381061, 76.40724259, 78.55675184,
72.17358173, 47.9888899 , 65.10370622, 76.29696568,
95.58234254, 68.14897213, 38.75862686, 122.5587927 ,
67.0762565 , 95.73990864, 81.97454563, 92.54264805,
59.37035153, 77.76828101, 52.30752166, 64.43109931,
62.63695351, 90.04616138, 75.70009094, 49.87586877,
80.22002414, 68.56708848, 76.27791052, 67.24343975,
81.86363935, 78.22703433, 102.85737041, 65.15700341,
84.87033426, 76.7569997 , 64.61321853, 67.37244562,
74.4068773 , 58.65119655, 71.66488727, 53.42458179,
70.26872028, 60.96588544, 83.56129414, 72.14255326,
81.00787609, 71.81264853, 72.64168853, 86.56608717,
94.94667321, 82.32676973, 70.5165446 , 85.43061003,
72.45526212, 87.34681775, 87.69911217, 103.02831489,
75.28598596, 67.17806893, 92.41274447, 101.06662611,
87.70013935, 70.73980645, 46.40368207, 50.17947092,
61.75618542, 90.26191397, 78.63968639, 70.84550744,
88.91826581, 103.91474733, 66.3064638 , 79.49726264,
70.81087439, 83.90130623, 87.58555972, 59.95462521])
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
plt.hist(normal_array, color="grey", bins=50)
(array([2., 0., 0., 0., 1., 2., 2., 0., 2., 0., 0., 1., 2., 2., 1., 4., 3.,
4., 2., 7., 2., 2., 5., 4., 2., 4., 3., 2., 1., 5., 3., 0., 3., 2.,
1., 0., 0., 1., 3., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 1.]),
array([ 38.69307086, 40.37038529, 42.04769973, 43.72501417,
45.4023286 , 47.07964304, 48.75695748, 50.43427191,
52.11158635, 53.78890079, 55.46621523, 57.14352966,
58.8208441 , 60.49815854, 62.17547297, 63.85278741,
65.53010185, 67.20741628, 68.88473072, 70.56204516,
72.23935959, 73.91667403, 75.59398847, 77.27130291,
78.94861734, 80.62593178, 82.30324622, 83.98056065,
85.65787509, 87.33518953, 89.01250396, 90.6898184 ,
92.36713284, 94.04444727, 95.72176171, 97.39907615,
99.07639058, 100.75370502, 102.43101946, 104.1083339 ,
105.78564833, 107.46296277, 109.14027721, 110.81759164,
112.49490608, 114.17222052, 115.84953495, 117.52684939,
119.20416383, 120.88147826, 122.5587927 ]),
<a list of 50 Patch objects>)
four_by_four_matrix = np.matrix(np.ones((4,4), dtype=float))
four_by_four_matrix
matrix([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
np.asarray(four_by_four_matrix)[2] = 2
four_by_four_matrix
matrix([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[2., 2., 2., 2.],
[1., 1., 1., 1.]])
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)
for l in lst:
print(l)
``sh 0
2
4
6
8
10
%%CODEBLOCK239%%py
# Similar to range arange numpy.arange(start, stop, step)
whole_numbers = np.arange(0, 20, 1)
whole_numbers
%%CODEBLOCK240%%sh
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19])
%%CODEBLOCK241%%py
natural_numbers = np.arange(1, 20, 1)
natural_numbers
%%CODEBLOCK242%%py
odd_numbers = np.arange(1, 20, 2)
odd_numbers
%%CODEBLOCK243%%sh
array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19])
%%CODEBLOCK244%%py
even_numbers = np.arange(2, 20, 2)
even_numbers
%%CODEBLOCK245%%sh
array([ 2, 4, 6, 8, 10, 12, 14, 16, 18])
%%CODEBLOCK246%%py
# 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)
%%CODEBLOCK247%%sh
array([1. , 1.44444444, 1.88888889, 2.33333333, 2.77777778,
3.22222222, 3.66666667, 4.11111111, 4.55555556, 5. ])
%%CODEBLOCK248%%py
# not to include the last value in the interval
np.linspace(1.0, 5.0, num=5, endpoint=False)
%%CODEBLOCK249%%
array([1. , 1.8, 2.6, 3.4, 4.2])
%%CODEBLOCK250%%py
# 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)
%%CODEBLOCK251%%sh
array([ 100. , 464.15888336, 2154.43469003, 10000. ])
%%CODEBLOCK252%%py
# to check the size of an array
x = np.array([1,2,3], dtype=np.complex128)
%%CODEBLOCK253%%py
x
%%CODEBLOCK254%%sh
array([1.+0.j, 2.+0.j, 3.+0.j])
%%CODEBLOCK255%%py
x.itemsize
%%CODEBLOCK256%%sh
16
%%CODEBLOCK257%%py
# indexing and Slicing NumPy Arrays in Python
np_list = np.array([(1,2,3), (4,5,6)])
np_list
%%CODEBLOCK258%%sh
array([[1, 2, 3],
[4, 5, 6]])
%%CODEBLOCK259%%py
print('First row: ', np_list[0])
print('Second row: ', np_list[1])
%%CODEBLOCK260%%sh
First row: [1 2 3]
Second row: [4 5 6]
%%CODEBLOCK261%%p
print('First column: ', np_list[:,0])
print('Second column: ', np_list[:,1])
print('Third column: ', np_list[:,2])
%%CODEBLOCK262%%sh
First column: [1 4]
Second column: [2 5]
Third column: [3 6]
%%CODEBLOCK263%%python
np_normal_dis = np.random.normal(5, 0.5, 100)
np_normal_dis
## min, max, mean, median, sd
print('min: ', two_dimension_array.min())
print('max: ', two_dimension_array.max())
print('mean: ',two_dimension_array.mean())
# print('median: ', two_dimension_array.median())
print('sd: ', two_dimension_array.std())
%%CODEBLOCK264%%python
min: 1
max: 55
mean: 14.777777777777779
sd: 18.913709183069525
%%CODEBLOCK265%%python
print(two_dimension_array)
print('Column with minimum: ', np.amin(two_dimension_array,axis=0))
print('Column with maximum: ', np.amax(two_dimension_array,axis=0))
print('=== Row ==')
print('Row with minimum: ', np.amin(two_dimension_array,axis=1))
print('Row with maximum: ', np.amax(two_dimension_array,axis=1))
%%CODEBLOCK266%%python
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))
%%CODEBLOCK267%%python
# One random number between [0,1)
one_random_num = np.random.random()
one_random_in = np.random
print(one_random_num)
%%CODEBLOCK268%%python
0.4763968133790438
%%CODEBLOCK269%%python
# Random numbers between [0,1) of shape 2,3
r = np.random.random(size=[2,3])
print(r)
%%CODEBLOCK270%%python
print(np.random.choice(['a', 'e', 'i', 'o', 'u'], size=10))
%%CODEBLOCK271%%python
['i' 'u' 'e' 'o' 'a' 'i' 'e' 'u' 'o' 'i']
%%CODEBLOCK272%%python
## Random numbers between [0, 1] of shape 2, 2
rand = np.random.rand(2,2)
rand
%%CODEBLOCK273%%python
rand2 = np.random.randn(2,2)
rand2
%%CODEBLOCK274%%python
# Random integers between [0, 10) of shape 2,5
rand_int = np.random.randint(0, 10, size=[5,3])
rand_int
%%CODEBLOCK275%%py
from scipy import stats
np_normal_dis = np.random.normal(5, 0.5, 1000) # mean, standard deviation, number of samples
np_normal_dis
## min, max, mean, median, sd
print('min: ', np.min(np_normal_dis))
print('max: ', np.max(np_normal_dis))
print('mean: ', np.mean(np_normal_dis))
print('median: ', np.median(np_normal_dis))
print('mode: ', stats.mode(np_normal_dis))
print('sd: ', np.std(np_normal_dis))
%%CODEBLOCK276%%sh
min: 3.557811005458804
max: 6.876317743643499
mean: 5.035832048106663
median: 5.020161980441937
mode: ModeResult(mode=array([3.55781101]), count=array([1]))
sd: 0.489682424165213
%%CODEBLOCK277%%python
plt.hist(np_normal_dis, color="grey", bins=21)
plt.show()
%%CODEBLOCK278%%python
# 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)
%%CODEBLOCK279%%python
## Linear algebra
### Dot product: product of two arrays
f = np.array([1,2,3])
g = np.array([4,5,3])
### 1*4+2*5 + 3*6
np.dot(f, g) # 23
%%CODEBLOCK280%%python
### Matmul: matruc product of two arrays
h = [[1,2],[3,4]]
i = [[5,6],[7,8]]
### 1*5+2*7 = 19
np.matmul(h, i)
%%CODEBLOCK281%%sh
array([[19, 22],
[43, 50]])
%%CODEBLOCK282%%py
## Determinant 2*2 matrix
### 5*8-7*6np.linalg.det(i)
%%CODEBLOCK283%%python
np.linalg.det(i)
%%CODEBLOCK284%%python
Z = np.zeros((8,8))
Z[1::2,::2] = 1
Z[::2,1::2] = 1
%%CODEBLOCK285%%python
Z
%%CODEBLOCK286%%python
new_list = [ x + 2 for x in range(0, 11)]
%%CODEBLOCK287%%python
new_list
%%CODEBLOCK288%%python
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
%%CODEBLOCK289%%python
np_arr = np.array(range(0, 11))
np_arr + 2
%%CODEBLOCK290%%python
temp = np.array([1,2,3,4,5])
pressure = temp * 2 + 5
pressure
%%CODEBLOCK291%%python
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()
%%CODEBLOCK292%%python
mu = 28
sigma = 15
samples = 100000
x = np.random.normal(mu, sigma, samples)
ax = sns.distplot(x);
ax.set(xlabel="x", ylabel='y')
plt.show()
%%CODEBLOCK293%%py
pip install conda
conda install pandas
%%CODEBLOCK294%%python
import pandas as pd # importing pandas as pd
import numpy as np # importing numpy as np
%%CODEBLOCK295%%python
nums = [1, 2, 3, 4,5]
s = pd.Series(nums)
s
%%CODEBLOCK296%%python
nums = [1, 2, 3, 4, 5]
s = pd.Series(nums, index=[1, 2, 3, 4, 5])
s
%%CODEBLOCK297%%python
fruits = ['Orange','Banana','Mangao']
fruits = pd.Series(fruits, index=[1, 2, 3])
fruits
%%CODEBLOCK298%%python
dct = {'name':'Asabeneh','country':'Finland','city':'Helsinki'}
%%CODEBLOCK299%%python
s = pd.Series(dct)
s
%%CODEBLOCK300%%python
s = pd.Series(10, index = [1, 2,3])
s
%%CODEBLOCK301%%python
s = pd.Series(np.linspace(5, 20, 10)) # linspace(starting, end, items)
s
%%CODEBLOCK302%%python
data = [
['Asabeneh', 'Finland', 'Helsink'],
['David', 'UK', 'London'],
['John', 'Sweden', 'Stockholm']
]
df = pd.DataFrame(data, columns=['Names','Country','City'])
df
%%CODEBLOCK303%%python
data = {'Name': ['Asabeneh', 'David', 'John'], 'Country':[
'Finland', 'UK', 'Sweden'], 'City': ['Helsiki', 'London', 'Stockholm']}
df = pd.DataFrame(data)
df
%%CODEBLOCK304%%python
%%CODEBLOCK305%%python
data = [
{'Name': 'Asabeneh', 'Country': 'Finland', 'City': 'Helsinki'},
{'Name': 'David', 'Country': 'UK', 'City': 'London'},
{'Name': 'John', 'Country': 'Sweden', 'City': 'Stockholm'}]
df = pd.DataFrame(data)
df
%%CODEBLOCK306%%python
import pandas as pd
df = pd.read_csv('./data/weight-height.csv')
%%CODEBLOCK307%%python
df.head() # give five rows we can increase the number of rows by passing argument to the head() method
%%CODEBLOCK308%%python
df.shape # as you can see 10000 rows and three columns
%%CODEBLOCK309%%python
df.columns
%%CODEBLOCK310%%python
df.tail() # tails give the last five rows, we can increase the rows by passing argument to tail method
%%CODEBLOCK311%%python
heights = df['Height'] # this is now a a series
%%CODEBLOCK312%%python
heights
%%CODEBLOCK313%%python
weights = df['Weight'] # this is now a series
%%CODEBLOCK314%%python
weights
%%CODEBLOCK315%%python
len(heights) == len(weights)
%%CODEBLOCK316%%python
heights.describe() # give statisical information about height data
%%CODEBLOCK317%%python
weights.describe()
%%CODEBLOCK318%%python
df.describe() # describe can also give statistical information from a datafrom
%%CODEBLOCK319%%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)
df
%%CODEBLOCK320%%python
weights = [74, 78, 69]
df['Weight'] = weights
df
%%CODEBLOCK321%%python
heights = [173, 175, 169]
df['Height'] =heights
df
%%CODEBLOCK322%%python
df['Height'] = df['Height'] * 0.01
df
%%CODEBLOCK323%%python
# Using function makes our code clean but you can just calculate the bmi without function
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()
%%CODEBLOCK324%%python
df['BMI'] = bmi
df
%%CODEBLOCK325%%python
df['BMI'] = round(df['BMI'], 1)
df
%%CODEBLOCK326%%python
birth_year = ['1769', '1985', '1990']
current_year = pd.Series(2019, index=[0, 1,2])
df['Birth Year'] = birth_year
df['Current Year'] = current_year
df
%%CODEBLOCK327%%python
df.Weight.dtype
%%CODEBLOCK328%%python
df['Birth Year'].dtype # it give string object , we should change this to number
%%CODEBLOCK329%%python
df['Birth Year'] = df['Birth Year'].astype('int')
df['Birth Year'].dtype # let's check the data type now
%%CODEBLOCK330%%python
df['Current Year'] = df['Current Year'].astype('int')
df['Current Year'].dtype
%%CODEBLOCK331%%python
ages = df['Current Year'] - df['Birth Year']
ages
%%CODEBLOCK332%%python
df['Ages'] = ages
df
%%CODEBLOCK333%%python
mean = (34 + 29)/ 2
mean
%%CODEBLOCK334%%python
df[df['Ages'] > 120]
%%CODEBLOCK335%%python
df[df['Ages'] < 120]
%%CODEBLOCK336%%python
df['Ages'] = df[df['Ages'] > 120]
%%CODEBLOCK337%%python
list_nums = range(100);
print(list(list_nums))
%%CODEBLOCK338%%python
def sum_of_evens_and_sum_of_odds (n):
numbers = range(n)
evens_sum = 0;
odds_sum = 0;
for n in numbers:
if n % 2 == 0 :
evens_sum = evens_sum + n
else:
odds_sum = odds_sum + n;
return [evens_sum, odds_sum]
%%CODEBLOCK339%%python
sum_of_evens_and_sum_of_odds(101)
%%CODEBLOCK340%%python
``