Python Lab
Python Lab
Course Outcomes:
1 To master an understanding of scripting & the contributions of scripting
languages
2 Design real life problems and think creatively about solutions
3 Apply a solution in a program using R/Matlab/Python.
4 To be exposed to advanced applications of mathematics, engineering and
natural sciences to program real life problems.
Pre-Requisite:
1. Knowledge of Programming Logic
2. Experience with a high level language (C/C++,) is suggested.
3. Prior knowledge of a scripting language and Object-Oriented concepts is
helpful but not mandatory.
Syllabus:
Conditional Statements
If, If- else, Nested if-else, Looping, For, While, Nested loops
Control Statements
Break, Continue, Pass String Manipulation Accessing Strings, Basic Operations, String slices, Function
and Methods
Lists
Introduction, Accessing list, Operations, Working with lists, Function and Methods
Tuple
Introduction, Accessing tuples, Operations, Working, Functions and Methods
Dictionaries
Introduction, Accessing values in dictionaries, Working with dictionaries, Properties
Functions
Defining a function, Calling a function, Types of functions, Function Arguments, Anonymous
functions, Global and local variables
Modules
Importing module, Math module, Random module, Packages, Composition, Input-Output Printing on
screen, Reading data from keyboard, Opening and closing file, Reading and writing files, Functions
Exception Handling
Exception, Exception Handling, Except clause, Try? finally clause, User Defined Exceptions.
What is Python?
Python is a popular programming language. It was created by Guido van Rossum, and released in
1991.
It is used for:
Lab 1
● How to print in python
print("Hello, World!")
● Python Comments
#This is a comment
print("Hello, World!")
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
● Creating Variables
Variables are containers for storing data values.
Unlike other programming languages, Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
x=5
y = "John"
print(x)
print(y)
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
● Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume). Rules for Python variables:
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case-sensitive (age, Age and AGE are three different variables)
Remember that variable names are case-sensitive
● Output Variables
The Python print statement is often used to output variables.
To combine both text and a variable, Python uses the + character:
x = "awesome"
print("Python is " + x)
x=5
print(type(x))
x = str("Hello World")str
x = int(20)int
x = float(20.5)float
x = complex(1j)complex
x = list(("apple", "banana", "cherry"))list
x = tuple(("apple", "banana", "cherry"))tuple
x = range(6)range
x = dict(name="John", age=36)dict
x = set(("apple", "banana", "cherry"))set
x = frozenset(("apple", "banana", "cherry"))frozenset
x = bool(5)bool
x = bytes(5)bytes
x = bytearray(5)bytearray
x = memoryview(bytes(5))memoryview
Python Casting
There may be times when you want to specify a type on to a variable. This can be done with casting.
Python is an object-orientated language, and as such it uses classes to define data types, including
its primitive types.
Casting in python is therefore done using constructor functions:
int() - constructs an integer number from an integer literal, a float literal (by rounding down to the
previous whole number), or a string literal (providing the string represents a whole number)
float() - constructs a float number from an integer literal, a float literal or a string literal (providing the
string represents a float or an integer)
str() - constructs a string from a wide variety of data types, including strings, integer literals and float
literals
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
● Inputs in Python 3
● Python Operators
Equals: a == b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
These conditions can be used in several ways, most commonly in "if statements" and loops.
An "if statement" is written by using the if keyword.
a = 33
b = 200
if b > a:
print("b is greater than a")
Elif
The elif keyword is pythons way of saying "if the previous conditions were not true, then try this
condition".
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
Else
The else keyword catches anything which isn't caught by the preceding conditions.
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
And
The and keyword is a logical operator, and is used to combine conditional statements:
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
Or
The or keyword is a logical operator, and is used to combine conditional statements:
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")
Nested If
You can have if statements inside if statements, this is called nested if statements.
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
Python Loops
Python has two primitive loop commands:
while loops
for loops
i=1
while i < 6:
print(i)
i += 1
i=1
while i < 6:
print(i)
if i == 3:
break
i += 1
i=0
while i < 6:
i += 1
if i == 3:
continue
print(i)
for x in "banana":
print(x)
for x in range(6):
print(x)
for x in range(6):
print(x)
else:
print("Finally finished!")
Nested Loops
A nested loop is a loop inside a loop.
The "inner loop" will be executed one time for each iteration of the "outer loop":
Lab 3
a = "Hello, World!"
print(a[1])
Slicing
You can return a range of characters by using the slice syntax.
Specify the start index and the end index, separated by a colon, to return a part of the string.
Negative Indexing
Use negative indexes to start the slice from the end of the string:
Get the characters from position 5 to position 1, starting the count from the end of the string:
b = "Hello, World!"
print(b[-5:-2])
String Length
To get the length of a string, use the len() function.
a = "Hello, World!"
print(len(a))
String Methods
Python has a set of built-in methods that you can use on strings.
The strip() method removes any whitespace from the beginning or the end:
a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"
The split() method splits the string into substrings if it finds instances of the separator:
a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']
MethodDescription
capitalize()Converts the first character to upper case
casefold()Converts string into lower case
center()Returns a centered string
count()Returns the number of times a specified value occurs in a string
encode()Returns an encoded version of the string
endswith()Returns true if the string ends with the specified value
expandtabs()Sets the tab size of the string
find()Searches the string for a specified value and returns the position of where it was found
format()Formats specified values in a string
format_map()Formats specified values in a string
index()Searches the string for a specified value and returns the position of where it was found
isalnum()Returns True if all characters in the string are alphanumeric
isalpha()Returns True if all characters in the string are in the alphabet
isdecimal()Returns True if all characters in the string are decimals
isdigit()Returns True if all characters in the string are digits
isidentifier()Returns True if the string is an identifier
islower()Returns True if all characters in the string are lower case
isnumeric()Returns True if all characters in the string are numeric
isprintable()Returns True if all characters in the string are printable
isspace()Returns True if all characters in the string are whitespaces
istitle()Returns True if the string follows the rules of a title
isupper()Returns True if all characters in the string are upper case
join()Joins the elements of an iterable to the end of the string
ljust()Returns a left justified version of the string
lower()Converts a string into lower case
lstrip()Returns a left trim version of the string
maketrans()Returns a translation table to be used in translations
partition()Returns a tuple where the string is parted into three parts
replace()Returns a string where a specified value is replaced with a specified value
rfind()Searches the string for a specified value and returns the last position of where it was found
rindex()Searches the string for a specified value and returns the last position of where it was found
rjust()Returns a right justified version of the string
rpartition()Returns a tuple where the string is parted into three parts
rsplit()Splits the string at the specified separator, and returns a list
rstrip()Returns a right trim version of the string
split()Splits the string at the specified separator, and returns a list
splitlines()Splits the string at line breaks and returns a list
startswith()Returns true if the string starts with the specified value
strip()Returns a trimmed version of the string
swapcase()Swaps cases, lower case becomes upper case and vice versa
title()Converts the first character of each word to upper case
translate()Returns a translated string
upper()Converts a string into upper case
zfill()Fills the string with a specified number of 0 values at the beginning
Check String
To check if a certain phrase or character is present in a string, we can use the keywords in or not in.
Example
String Concatenation
To concatenate, or combine, two strings you can use the + operator.
Example
Merge variable a with variable b into variable c:
a = "Hello"
b = "World"
c=a+b
print(c)
String Format
age = 36
txt = "My name is John, I am " + age
print(txt)
The format() method takes the passed arguments, formats them, and places them in the string where
the placeholders {} are:
Example
Use the format() method to insert numbers into strings:
age = 36
txt = "My name is John, and I am {}"
print(txt.format(age))
The format() method takes unlimited number of arguments, and are placed into the respective
placeholders:
quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print(myorder.format(quantity, itemno, price))
You can use index numbers {0} to be sure the arguments are placed in the correct placeholders:
Example
quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2} dollars for {0} pieces of item {1}."
print(myorder.format(quantity, itemno, price))
CodeResult
\'Single Quote
\\Backslash
\nNew Line
\rCarriage Return
\tTab
\bBackspace
\fForm Feed
\oooOctal value
\xhhHex value
Lab 4
Lists are just like the arrays, declared in other languages. Lists need not be homogeneous always
which makes it a most powerful tool in Python. A single list may contain DataTypes like Integers,
Strings, as well as Objects. Lists are mutable, and hence, they can be altered even after their creation.
List in Python are ordered and have a definite count. The elements in a list are indexed according to a
definite sequence and the indexing of a list is done with 0 being the first index. Each element in the
list has its definite place in the list, which allows duplicating of elements in the list, with each element
having its own distinct place and credibility.
Note- Lists are a useful tool for preserving a sequence of data and further iterating over it.
Create a List:
thislist = ["apple", "banana", "cherry"]
print(thislist)
List Length
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
Add Items
To add an item to the end of the list, use the append() method:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
The pop() method removes the specified index, (or the last item if index is not specified):
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
Copy a List
You cannot copy a list simply by typing list2 = list1, because: list2 will only be a reference to list1, and
changes made in list1 will automatically also be made in list2.
There are ways to make a copy, one way is to use the built-in List method copy().
Make a copy of a list with the copy() method:
thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)
Extend
fruits = ['apple', 'banana', 'cherry']
cars = ['Ford', 'BMW', 'Volvo']
fruits.extend(cars)
print(fruits)
index() Method
fruits = ['apple', 'banana', 'cherry']
x = fruits.index("cherry")
***********************************************************************************************
Tuple
A tuple is a collection which is ordered and unchangeable. In Python tuples are written with round
brackets.
The sequence of values stored in a tuple can be of any type, and they are indexed by integers. The
important difference between a list and a tuple is that tuples are immutable. Also, Tuples are
hashable whereas lists are not.
Values of a tuple are syntactically separated by ‘commas’. Although it is not necessary, it is more
common to define a tuple by closing the sequence of values in parentheses. This helps in
understanding the Python tuples more easily.
Tuples are immutable, and usually, they contain a sequence of heterogeneous elements that are
accessed via unpacking or indexing (or even by attribute in the case of named tuples). Lists are
mutable, and their elements are usually homogeneous and are accessed by iterating over the list.
Add Items
Once a tuple is created, you cannot add items to it. Tuples are unchangeable.
Example
You cannot add items to a tuple:
thistuple = ("apple", "banana", "cherry")
thistuple[3] = "orange" # This will raise an error
print(thistuple)
Remove Items
Note: You cannot remove items in a tuple.
Tuples are unchangeable, so you cannot remove items from it, but you can delete the tuple
completely:
thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x = thistuple.count(5) #2
print(x)
Search for the first occurrence of the value 8, and return its position:
thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x = thistuple.index(8)
print(x)
****************************************************************************************
Set
A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets.
Example
Create a Set:
thisset = {"apple", "banana", "cherry"}
print(thisset)
Note: Sets are unordered, so you cannot be sure in which order the items will appear.
Access Items
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
Change Items
Once a set is created, you cannot change its items, but you can add new items.
Add Items
To add one item to a set use the add() method.
To add more than one item to a set use the update() method.
Example
Add an item to a set, using the add() method:
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
Example
Get the number of items in a set:
thisset = {"apple", "banana", "cherry"}
print(len(thisset))
Remove Item
To remove an item in a set, use the remove(), or the discard() method.
Example
Remove "banana" by using the remove() method:
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)
If the item to remove does not exist, discard() will NOT raise an error.
You can also use the pop(), method to remove an item, but this method will remove the last item.
Remember that sets are unordered, so you will not know what item that gets removed.
MethodDescription
add()Adds an element to the set
clear()Removes all the elements from the set
copy()Returns a copy of the set
difference()Returns a set containing the difference between two or more sets
difference_update()Removes the items in this set that are also included in another, specified set
discard()Remove the specified item
intersection()Returns a set, that is the intersection of two other sets
intersection_update()Removes the items in this set that are not present in other, specified set(s)
isdisjoint()Returns whether two sets have a intersection or not
issubset()Returns whether another set contains this set or not
issuperset()Returns whether this set contains another set or not
pop()Removes an element from the set
remove()Removes the specified element
symmetric_difference()Returns a set with the symmetric differences of two sets
symmetric_difference_update()inserts the symmetric differences from this set and another
union()Return a set containing the union of sets
update()Update the set with the union of this set and others
******************************************************************************************************
***********************************************
A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are
written with curly brackets, and they have keys and values.
Accessing Items
x = thisdict["model"]
There is also a method called get() that will give you the same result:
x = thisdict.get("model")
Change Values
thisdict ={
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["year"] = 2018
You can also use the values() function to return values of a dictionary:
thisdict ={
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
for x in thisdict.values():
print(x)
Loop through both keys and values, by using the items() function:
thisdict ={
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
for x, y in thisdict.items():
print(x, y)
brand Ford
model Mustang
year 1964
Dictionary Length
To determine how many items (key-value pairs) a dictionary has, use the len() method.
thisdict ={
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(len(thisdict))
Adding Items
Adding an item to the dictionary is done by using a new index key and assigning a value to it:
thisdict ={
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["color"] = "red"
print(thisdict)
Removing Items
The pop() method removes the item with the specified key name:
thisdict ={
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.pop("model")
print(thisdict)
The popitem() method removes the last inserted item (in versions before 3.7, a random item is
removed instead):
thisdict ={
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.popitem()
print(thisdict)
The del keyword removes the item with the specified key name:
thisdict ={
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict["model"]
print(thisdict)
Copy a Dictionary
thisdict ={
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = thisdict.copy()
print(mydict)
Dictionary Methods
Python has a set of built-in methods that you can use on dictionaries.
MethodDescription
clear()Removes all the elements from the dictionary
copy()Returns a copy of the dictionary
fromkeys()Returns a dictionary with the specified keys and values
get()Returns the value of the specified key
items()Returns a list containing the a tuple for each key value pair
keys()Returns a list containing the dictionary's keys
pop()Removes the element with the specified key
popitem()Removes the last inserted key-value pair
setdefault()Returns the value of the specified key. If the key does not exist: insert the key, with the
specified value
update()Updates the dictionary with the specified key-value pairs
values()Returns a list of all the values in the dictionary
Function
def my_function():
print("Hello from a function")
my_function()
You can send any data types of parameter to a function (string, number, list, dictionary etc.), and it
will be treated as the same data type inside the function.
E.g. if you send a List as a parameter, it will still be a List when it reaches the function:
Example
def my_function(food):
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
Return Values
To let a function return a value, use the return statement:
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))
Recursion:
def tri_recursion(k):
if(k>0):
result = k+tri_recursion(k-1)
print(result)
else:
result = 0
return result
x = "global"
def foo():
print("x inside :", x)
foo()
print("x outside:", x)
Python Dates
import datetime
x = datetime.datetime.now()
print(x)
import datetime
x = datetime.datetime.now()
print(x.year)
print(x.strftime("%A"))
import datetime
x = datetime.datetime(2020, 5, 17)
print(x)
DirectiveDescriptionExampleTry it
%aWeekday, short versionWed
%AWeekday, full versionWednesday
%wWeekday as a number 0-6, 0 is Sunday3
%dDay of month 01-3131
%bMonth name, short versionDec
%BMonth name, full versionDecember
%mMonth as a number 01-1212
%yYear, short version, without century18
%YYear, full version2018
%HHour 00-2317
%IHour 00-1205
%pAM/PMPM
%MMinute 00-5941
%SSecond 00-5908
%fMicrosecond 000000-999999548513
%zUTC offset+0100
%ZTimezoneCST
%jDay number of year 001-366365
%UWeek number of year, Sunday as the first day of week, 00-5352
%WWeek number of year, Monday as the first day of week, 00-5352
%cLocal version of date and timeMon Dec 31 17:41:00 2018
%xLocal version of date12/31/18
%XLocal version of time17:41:00
%%A % character%
Lab 5 (Numerical)
@author: USER
"""
import numpy as np
#Our first simple Numpy example deals with temperatures. Given is a list with values, e.g.
temperatures in Celsius:
cvalues = [20.1, 20.8, 21.9, 22.5, 22.7, 22.3, 21.8, 21.2, 20.9, 20.1]
#We will turn our list "cvalues" into a one-dimensional numpy array:
C = np.array(cvalues)
print(C)
#Let's assume, we want to turn the values into degrees Fahrenheit. This is very easy to accomplish
with a #numpy array. The solution to our problem can be achieved by simple scalar multiplication:
print(C * 9 / 5 + 32)
fvalues = [ x*9/5 + 32 for x in cvalues]
print(fvalues)
#So far, we referred to C as an array. The internal type is "ndarray" or to be even more precise "C is an
instance #of the class numpy.ndarray":
print(type(C))
'''
The size of a Python list consists of the general list information, the size needed for the references to
the elements and the size of all the elements of the list. If we apply sys.getsizeof to a list, we get only
the size without the size of the elements. In the previous example, we made the assumption that all
the integer elements of our list have the same size. Of course, this is not valid in general, because
memory consumption will be higher for larger integers.
We will check now, how the memory usage changes, if we add another integer element to the list. We
also look at an empty list:
'''
lst = []
print("Emtpy list size: ", size(lst))
x = range(1, 10)
print(x) # x is an iterator
print(list(x))
x = range(1, 10)
print(x) # x is an iterator
print(list(x))
import numpy as np
x = np.arange(10.4)
print(x)
x = np.arange(0.5, 10.4, 0.8)
print(x)
x = np.arange(0.5, 10.4, 0.8, int)
print(x)
print(np.shape(x))
print(x.shape)
#"shape" can also be used to change the shape of an array.
x.shape = (3, 6)
print(x)
x.shape = (2, 9)
print(x)
#We illustrate the operating principle of "slicing" with some examples. We start with the easiest case,
i.e. the slicing of a one-dimensional array:
S = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(S[2:5])
print(S[:4])
print(S[6:])
print(S[:])
A = np.array([
[11, 12, 13, 14, 15],
[21, 22, 23, 24, 25],
[31, 32, 33, 34, 35],
[41, 42, 43, 44, 45],
[51, 52, 53, 54, 55]])
print(A[:3, 2:])
print(A[3:, :])
print(A[:, 4:])
#The following two examples use the third
#parameter "step". The reshape function is used
#to construct the two-dimensional array.
#We will explain reshape in the following
#subchapter:
import numpy as np
X = np.arange(28).reshape(4, 7)
print(X)
print(X[::2, ::3])
print(X[::, ::3])
#Creating Arrays with Ones, Zeros and Empty
#import numpy as np
E = np.ones((2,3))
print(E)
#
F = np.ones((3,4),dtype=int)
print(F)
#
Z = np.zeros((2,4))
print(Z)
#
x = np.array([2,5,18,14,4])
E = np.ones_like(x)
print(E)
#
Z = np.zeros_like(x)
print(Z)
#Copying Arrays
#numpy.copy()
import numpy as np
x = np.array([[42,22,12],[44,53,66]], order='F')
y = x.copy()
x[0,0] = 1001
print(x)
print(y)
print(x.flags['C_CONTIGUOUS'])
print(y.flags['C_CONTIGUOUS'])
#ndarray.copy()
import numpy as np
x = np.array([[42,22,12],[44,53,66]], order='F')
y = x.copy()
x[0,0] = 1001
print(x)
print(y)
print(x.flags['C_CONTIGUOUS'])
print(y.flags['C_CONTIGUOUS'])
'''
Identity Array
In linear algebra, the identity matrix, or unit matrix, of size n is the n × n square matrix with ones on
the main diagonal and zeros elsewhere.
identy
eye
The identity Function
We can create identity arrays with the function identity:
identity(n, dtype=None)
The parameters:
ParameterMeaning
nAn integer number defining the number of rows and columns of the output, i.e. 'n' x 'n'
dtypeAn optional argument, defining the data-type of the output. The default is 'float'
The output of identity is an 'n' x 'n' array with its main diagonal set to one, and all other elements are 0.
'''
np.identity(4)
'''
The eye Function
Another way to create identity arrays provides the function eye. This function creates also diagonal
arrays consisting solely of ones.
It returns a 2-D array with ones on the diagonal and zeros elsewhere.
ParameterMeaning
NAn integer number defining the rows of the output array.
MAn optional integer for setting the number of columns in the output. If it is None, it defaults to 'N'.
kDefining the position of the diagonal. The default is 0. 0 refers to the main diagonal. A positive value
refers to an upper diagonal, and a negative value to a lower diagonal.
dtypeOptional data-type of the returned array.
eye returns an ndarray of shape (N,M). All elements of this array are equal to zero, except for the 'k'-th
diagonal, whose values are equal to one.
'''
#import numpy as np
i16 = np.dtype(np.int16)
print(i16)
A = np.array(lst, dtype=i16)
print(A)
#
dt = np.dtype([('country', 'S20'), ('density', 'i4'), ('area', 'i4'), ('population', 'i4')])
population_table = np.array([
('Netherlands', 393, 41526, 16928800),
('Belgium', 337, 30510, 11007020),
('United Kingdom', 256, 243610, 62262000),
('Germany', 233, 357021, 81799600),
('Liechtenstein', 205, 160, 32842),
('Italy', 192, 301230, 59715625),
('Switzerland', 177, 41290, 7301994),
('Luxembourg', 173, 2586, 512000),
('France', 111, 547030, 63601002),
('Austria', 97, 83858, 8169929),
('Greece', 81, 131940, 11606813),
('Ireland', 65, 70280, 4581269),
('Sweden', 20, 449964, 9515744),
('Finland', 16, 338424, 5410233),
('Norway', 13, 385252, 5033675)],
dtype=dt)
print(population_table[:12])
print(population_table['density'])
print(population_table['country'])
print(population_table['area'][2:5])
#
lst = [2,3, 7.9, 3.3, 6.9, 0.11, 10.3, 12.9]
v = np.array(lst)
v=v+2
print(v)
print(v * 2.2)
print(v - 1.38)
#
print(v ** 2)
print(v ** 1.5)
#
lst = [2,3, 7.9, 3.3, 6.9, 0.11, 10.3, 12.9]
res = []
for val in lst:
res.append(val + 2)
print(res)
#
res = [ val + 2 for val in lst]
print(res)
#
v = np.random.randint(0, 100, 1000)
%timeit v + 1
#
lst = list(v)
A = np.array([ [11, 12, 13], [21, 22, 23], [31, 32, 33] ])
B = np.ones((3,3))
np.dot(A, B)
#Examples of Using the dot Product
#We will begin with the cases in which both arguments are scalars or one-dimensional array:
print(np.dot(3, 4))
x = np.array([3])
y = np.array([4])
print(x.ndim)
print(np.dot(x, y))
x = np.array([3, -2])
y = np.array([-4, 1])
print(np.dot(x, y))
#Let's go to the two-dimensional use case:
A = np.array([ [1, 2, 3],
[3, 2, 1] ])
B = np.array([ [2, 3, 4, -2],
[1, -1, 2, 3],
[1, 2, 3, 0] ])
# es muss gelten:
print(A.shape[-1] == B.shape[-2], A.shape[1])
print(np.dot(A, B))
#The dot Product in the 3-dimensional Case
import numpy as np
X = np.array( [[[3, 1, 2],
[4, 2, 2],
[2, 4, 1]],
[[3, 2, 2],
[4, 4, 3],
[4, 1, 1]],
[[2, 2, 1],
[3, 1, 3],
[3, 2, 3]]])
[[1, 4, 1],
[4, 1, 2],
[4, 1, 2]],
[[1, 2, 3],
[4, 1, 1],
[3, 1, 4]]])
R = np.dot(X, Y)
print("The shapes:")
print(X.shape)
print(Y.shape)
print(R.shape)
[[-1, 0, 1],
[1, -1, -2]],
[[3, 2, 2],
[4, 4, 3]],
[[2, 2, 1],
[3, 1, 3]]])
Y = np.array(
[[[2, 3, 1, 2, 1],
[2, 2, 2, 0, 0],
[3, 4, 0, 1, -1]],
[[1, 4, 3, 2, 2],
[4, 1, 1, 4, -3],
[4, 1, 0, 3, 0]]])
R = np.dot(X, Y)
print(R[0])
#This means that we could have created the array R by applying the sum products in the way above.
To "prove" this, we will create an array R2 by using the sum product, which is equal to R in the
following example:
R2 = np.zeros(R.shape, dtype=np.int)
for i in range(X.shape[0]):
for j in range(X.shape[1]):
for k in range(Y.shape[0]):
for m in range(Y.shape[2]):
R2[i, j, k, m] = sum(X[i, j, :] * Y[k, :, m])
R=A*B
print(R)
#
MA = np.mat(A)
MB = np.mat(B)
R = MA * MB
print(R)
#Comparison Operators
import numpy as np
A = np.array([ [11, 12, 13], [21, 22, 23], [31, 32, 33] ])
B = np.array([ [11, 102, 13], [201, 22, 203], [31, 32, 303] ])
A == B
#It is possible to compare complete arrays for equality as well. We use array_equal for this purpose.
array_equal returns True if two arrays have the same shape and elements, otherwise False will be
returned.
print(np.array_equal(A, B))
print(np.array_equal(A, A))
#Logical Operators
a = np.array([ [True, True], [False, False]])
b = np.array([ [True, False], [True, False]])
print(np.logical_or(a, b))
print(np.logical_and(a, b))
'''
Broadcasting
Numpy provides a powerful mechanism, called Broadcasting, which allows to perform arithmetic
operations on arrays of different shapes. This means that we have a smaller array and a larger array,
and we transform or apply the smaller array multiple times to perform some operation on the larger
array. In other words: Under certain conditions, the smaller array is "broadcasted" in a way that it has
the same shape as the larger array.
With the aid of broadcasting we can avoid loops in our Python program. The looping occurs implicitly
in the Numpy implementations, i.e. in C. We also avoid creating unnecessary copies of our data.
'''
import numpy as np
A = np.array([ [11, 12, 13], [21, 22, 23], [31, 32, 33] ])
B = np.array([1, 2, 3])
A * B[:, np.newaxis]
#
A = np.array([10, 20, 30])
B = np.array([1, 2, 3])
A[:, np.newaxis]
#
A[:, np.newaxis] * B
#Distance Matrix
'''
In mathematics, computer science and especially graph theory, a distance matrix is a matrix or a
two-dimensional array, which contains the distances between the elements of a set, pairwise taken.
The size of this two-dimensional array in n x n, if the set consists of n elements.
'''
cities = ["Barcelona", "Berlin", "Brussels", "Bucharest",
"Budapest", "Copenhagen", "Dublin", "Hamburg", "Istanbul",
"Kiev", "London", "Madrid", "Milan", "Moscow", "Munich",
"Paris", "Prague", "Rome", "Saint Petersburg",
"Stockholm", "Vienna", "Warsaw"]
dists = np.array(dist2barcelona[:12])
print(dists)
print(np.abs(dists - dists[:, np.newaxis]))
#3-Dimensional Broadcasting
A = np.array([ [[3, 4, 7], [5, 0, -1] , [2, 1, 5]],
[[1, 0, -1], [8, 2, 4], [5, 2, 1]],
[[2, 1, 3], [1, 9, 4], [5, -2, 4]]])
B*A
'''
Flatten and Reshape Arrays
There are two methods to flatten a multidimensional array:
flatten()
ravel()
flatten
'''
import numpy as np
A = np.array([[[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7]],
[[ 8, 9],
[10, 11],
[12, 13],
[14, 15]],
[[16, 17],
[18, 19],
[20, 21],
[22, 23]]])
Flattened_X = A.flatten()
print(Flattened_X)
print(A.flatten(order="C"))
print(A.flatten(order="F"))
print(A.flatten(order="A"))
'''
ravel
The order of the elements in the array returned by ravel() is normally "C-style".
ravel(a, order='C')
'C': C-like order, with the last axis index changing fastest, back to the first axis index changing slowest.
"C" is the default!
'F': Fortran-like index order with the first index changing fastest, and the last index changing slowest.
'A': Fortran-like index order if the array "a" is Fortran contiguous in memory, C-like order otherwise.
'K': read the elements in the order they occur in memory, except for reversing the data when strides are
negative.
'''
print(A.ravel())
print(A.ravel(order="A"))
print(A.ravel(order="F"))
print(A.ravel(order="A"))
print(A.ravel(order="K"))
'''
reshape
The method reshape() gives a new shape to an array without changing its data, i.e. it returns a new
array with a new shape.
ParameterMeaning
aarray_like, Array to be reshaped.
newshapeint or tuple of ints
order'C', 'F', 'A', like in flatten or ravel
'''
X = np.array(range(24))
Y = X.reshape((3,4,2))
Y
'''
Concatenating Arrays
In the following example we concatenate three one-dimensional arrays to one array. The elements of
the second array are appended to the first array. After this the elements of the third array are
appended:
'''
x = np.array([11,22])
y = np.array([18,7,6])
z = np.array([1,3,5])
c = np.concatenate((x,y,z))
print(c)
'''
If we are concatenating multidimensional arrays, we can concatenate the arrays according to axis.
Arrays must have the same shape to be concatenated with concatenate().
In the case of multidimensional arrays, we can arrange them according to the axis.
The default value is axis = 0:
'''
x = np.array(range(24))
x = x.reshape((3,4,2))
y = np.array(range(100,124))
y = y.reshape((3,4,2))
z = np.concatenate((x,y))
print(z)
#We do the same concatenation now with axis=1:
z = np.concatenate((x,y),axis = 1)
print(z)
In another usecase you may have a two-dimensional array like np.array([ [1, 2], [3, 4]]),
which you intend to use as a building block to construe the array with the shape (6, 8):
'''
array([[1, 2, 1, 2, 1, 2, 1, 2],
[3, 4, 3, 4, 3, 4, 3, 4],
[1, 2, 1, 2, 1, 2, 1, 2],
[3, 4, 3, 4, 3, 4, 3, 4],
[1, 2, 1, 2, 1, 2, 1, 2],
[3, 4, 3, 4, 3, 4, 3, 4]])
#
x = np.array([ [1, 2], [3, 4]])
np.tile(x, (3,4))
#
x = np.array([ 3.4])
y = np.tile(x, (5,))
print(y)
#
import numpy as np
x = np.array([[1, 2], [3, 4]])
print(np.tile(x, 2))
#
import numpy as np
x = np.array([[1, 2], [3, 4]])
print(np.tile(x, (2, 1)))
#
import numpy as np
x = np.array([[1, 2], [3, 4]])
print(np.tile(x, (2, 2)))
#
import random
random_number = random.random()
print(random_number)
#
from random import SystemRandom
crypto = SystemRandom()
print(crypto.random())
#Generate a list of Random Numbers
import random
print(random_list(10, secure=False))
#
import random
outcome = random.randint(1,6)
print(outcome)
#
import random
print(choice(possible_destinations))
#Random Samples with Python
import numpy as np
x = np.random.random_sample((3, 4))
print(x)
#Random seed
import random
random.seed(42)
for _ in range(10):
print(random.randint(1, 10), end=", ")
n = 1000
values = []
frequencies = {}
print(values[:10])
#The following program plots the random values, which we have created before.
#We haven't covered matplotlib so far, so it's not necessary to understand the code:
%matplotlib inline
freq = list(frequencies.items())
freq.sort()
plt.plot(*list(zip(*freq)))
import bk_random
number_of_specialists = 15
employees = set()
while len(employees) < number_of_specialists:
employee = bk_random.cartesian_choice(firstnames, surnames)
employees.add(" ".join(employee))
print(employees)
A = np.array([4, 7, 3, 4, 2, 8])
print(A == 4)
print(A < 5)
#
B = np.array([[42,56,89,65],
[99,88,42,12],
[55,42,17,18]])
print(B>=42)
'''
Extract from the array np.array([3,4,6,10,24,89,45,43,46,99,100]) with Boolean masking all the number
'''
import numpy as np
A = np.array([3,4,6,10,24,89,45,43,46,99,100])
div3 = A[A%3!=0]
print("Elements of A not divisible by 3:")
print(div3)
div5 = A[A%5==0]
print("Elements of A divisible by 5:")
print(div5)
A[A%3==0] = 42
print("""New values of A after setting the elements of A,
which are divisible by 3, to 42:""")
print(A)
#
import matplotlib.pyplot as plt
# our X values:
days = list(range(0, 22, 3))
print(days)
# our Y values:
celsius_values = [25.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1]
plt.plot(days, celsius_values)
plt.show()
plt.plot(days, celsius_values, 'bo')
plt.show()
#
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
print(type(fig))
print(type(ax))
#
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(X, Y)
#Labels on Axes
import matplotlib.pyplot as plt
days = list(range(1,9))
celsius_values = [25.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1]
fig, ax = plt.subplots()
ax.plot(days, celsius_values)
ax.set(xlabel='Day',
ylabel='Temperature in Celsius',
title='Temperature Graph')
plt.show()
#The plot Function
import matplotlib.pyplot as plt
days = list(range(1,9))
celsius_min = [19.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1]
celsius_max = [24.8, 28.9, 31.3, 33.0, 34.9, 35.6, 38.4, 39.2]
fig, ax = plt.subplots()
ax.set(xlabel='Day',
ylabel='Temperature in Celsius',
title='Temperature Graph')
ax.plot(days, celsius_min,
days, celsius_min, "oy",
days, celsius_max,
days, celsius_max, "or")
plt.show()
#
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set(xlabel='Day',
ylabel='Temperature in Celsius',
title='Temperature Graph')
ax.plot(days, celsius_min)
ax.plot(days, celsius_min, "oy")
ax.plot(days, celsius_max)
ax.plot(days, celsius_max, "or")
plt.show()
#Checking and Defining the Range of Axes
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(days, celsius_values)
ax.set(xlabel='Day',
ylabel='Temperature in Celsius',
title='Temperature Graph')
fig, ax = plt.subplots()
fig, ax = plt.subplots()
startx, endx = -2 * np.pi - 0.1, 2*np.pi + 0.1
starty, endy = -3.1, 3.1
We can use linewidth to set the width of a line as the name implies.
'''
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(X, F1, color="blue", linewidth=2.5, linestyle="-")
ax.plot(X, F2, color="red", linewidth=1.5, linestyle="--")
ax.plot(X, F3, color="green", linewidth=2, linestyle=":")
ax.plot(X, F4, color="grey", linewidth=2, linestyle="-.")
plt.show()
'''
Draw LPoints in Matplotlib
We will learn now how to draw single points in Matplotlib.
'''
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.scatter(3, 7, s=42)
#
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
ax.plot (X, Y, color='blue', alpha=1.00)
ax.fill_between(X, 0, Y, color='blue', alpha=.1)
plt.show()
#
import numpy as np
import matplotlib.pyplot as plt
n = 256
X = np.linspace(-np.pi,np.pi,n,endpoint=True)
Y = np.sin(2*X)
fig, ax = plt.subplots()
fig, ax = plt.subplots()
plt.show()
'''
Customizing Ticks
Matplotlib has so far - in all our previous examples - automatically taken over the task of
spacing points on the axis. We can see for example that the X axis in our previous example was
numbered -6. -4, -2, 0, 2, 4, 6, whereas the Y axis was numbered -1.0, 0, 1.0, 2.0, 3.0
xticks is a method, which can be used to get or to set the current tick locations and the labels.
The same is true for yticks:
'''
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
xticks = ax.get_xticks()
xticklabels = ax.get_xticklabels()
print(xticks, xticklabels)
for i in range(6):
print(xticklabels[i])
yticks = ax.get_yticks()
print(yticks)
#As we said before, we can also use xticks to set the location of xticks:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
plt.show()
#There is an easier way to set the values of the xticks so that we do not have to caculate them
manually. We use plt.MultipleLocator with np.pi/2 as argument:
import numpy as np
import matplotlib.pyplot as plt
ax.xaxis.set_major_locator(plt.MultipleLocator(np.pi / 2))
plt.show()
#Adding a Legend
from polynomials import Polynomial
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
X = np.linspace(-2, 3, 50, endpoint=True)
F = p(X)
F_derivative = p_der(X)
ax.plot(X, F)
ax.plot(X, F_derivative)
p = Polynomial(2, 3, -4, 6)
p_der = p.derivative()
fig, ax = plt.subplots()
X = np.linspace(-2, 3, 50, endpoint=True)
F = p(X)
F_derivative = p_der(X)
ax.plot(X, F, label="$2x^{3} + 3x^{2} - 4x + 6$")
ax.plot(X, F_derivative, label="$6x^{2} - 6x -4$")
ax.legend(loc='upper left')
'''
Annotations
The visualizations of function plots often makes annotations necessary.
This means we draw the readers attentions to important points and areas of the plot.
To this purpose we use texts, labels and arrows. We have already used axis labels and titles for
this purpose, but these are 'annotations' for the whole plot. We can easily annotate points inside
the axis or on the graph with the annotate method of an axes object. In an annotation,
there are two points to consider: the location being annotated represented by the argument
xy and the location of the text xytext. Both of these arguments are (x,y) tuples.
p = Polynomial(1, 0, -12, 0)
p_der = p.derivative()
fig, ax = plt.subplots()
X = np.arange(-5, 5, 0.1)
F = p(X)
F_derivative = p_der(X)
ax.grid()
ax.legend(loc='best')
plt.show()
#If you are not satisfied with the automatic positioning of the text, you can assign a tuple with a
position for the text to the keyword parameter xytext:
p = Polynomial(1, 0, -12, 0)
p_der = p.derivative()
fig, ax = plt.subplots()
X = np.arange(-5, 5, 0.1)
F = p(X)
F_derivative = p_der(X)
ax.grid()
ax.annotate("local maximum",
xy=(-2, p(-2)),
xytext=(-1, p(-2)+35),
arrowprops=dict(facecolor='orange'))
ax.annotate("local minimum",
xy=(2, p(2)),
xytext=(-2, p(2)-40),
arrowprops=dict(facecolor='orange', shrink=0.05))
ax.annotate("inflection point",
xy=(0, p(0)),
xytext=(-3, -30),
arrowprops=dict(facecolor='orange', shrink=0.05))
ax.plot(X, F, label="p")
ax.plot(X, F_derivative, label="derivation of p")
ax.legend(loc='best')
plt.show()
#
import matplotlib.pyplot as plt
for ax in axs.flat:
ax.set(xlim=(0, 1), ylim=(0, 1), xticks=[], yticks=[], aspect=1)
fig.tight_layout(pad=0.2)
plt.show()
#Multiple Plots and Double Axes
#Working with Multiple Figures and Axes
import matplotlib.pyplot as plt
python_course_green = "#476042"
plt.figure(figsize=(6, 4))
plt.subplot(221) # equivalent to: plt.subplot(2, 2, 1)
plt.figure()
cp = plt.contour(X, Y,Z)
plt.clabel(cp, inline=True,
fontsize=10)
plt.title('Contour Plot')
plt.xlabel('x (cm)')
plt.ylabel('y (cm)')
plt.show()
#
import matplotlib.pyplot as plt
plt.figure()
cp = plt.contour(X, Y, Z, colors='black', linestyles='dashed')
plt.clabel(cp, inline=True,
fontsize=10)
plt.title('Contour Plot')
plt.xlabel('x (cm)')
plt.ylabel('y (cm)')
plt.show()
#Filled Contours
import numpy as np
import matplotlib.pyplot as plt
plt.figure()
cp = plt.contourf(X, Y, Z)
plt.colorbar(cp)
plt.figure()
contour = plt.contourf(X, Y, Z)
plt.clabel(contour, colors = 'k', fmt = '%2.1f', fontsize=12)
c = ('#ff0000', '#ffff00', '#0000FF', '0.6', 'c', 'm')
contour_filled = plt.contourf(X, Y, Z, colors=c)
plt.colorbar(contour)
Z = np.sqrt(X ** 2 + Y ** 2 )
plt.figure()
y, x = np.ogrid[-1:2:100j, -1:1:100j]
plt.contour(x.ravel(),
y.ravel(),
x**2 + (y-((x**2)**(1.0/3)))**2,
[1],
colors='red',)
plt.axis('equal')
plt.show()
#Introduction into Pandas
#Data Structures
import pandas as pd
S = pd.Series([11, 28, 72, 3, 5, 8])
S
print(S.index)
print(S.values)
#
fruits = ['apples', 'oranges', 'cherries', 'pears']
quantities = [20, 33, 52, 10]
S = pd.Series(quantities, index=fruits)
S
#If we add two series with the same indices, we get a new series with the same index and the
correponding values will be added:
exchange_rates = pd.read_csv("dollar_euro.txt",
sep="\t")
print(exchange_rates)
#
import pandas as pd
exchange_rates = pd.read_csv("dollar_euro.txt",
sep="\t",
header=0,
names=["year", "min", "max", "days"])
print(exchange_rates)
@author: USER
"""
import numpy as np
#Our first simple Numpy example deals with temperatures. Given is a list with values, e.g.
temperatures in Celsius:
cvalues = [20.1, 20.8, 21.9, 22.5, 22.7, 22.3, 21.8, 21.2, 20.9, 20.1]
#We will turn our list "cvalues" into a one-dimensional numpy array:
C = np.array(cvalues)
print(C)
#Let's assume, we want to turn the values into degrees Fahrenheit. This is very easy to accomplish
with a numpy array. The solution to our problem can be achieved by simple scalar multiplication:
print(C * 9 / 5 + 32)
fvalues = [ x*9/5 + 32 for x in cvalues]
print(fvalues)
#So far, we referred to C as an array. The internal type is "ndarray" or to be even more precise "C is an
instance of the class numpy.ndarray":
print(type(C))
plt.plot(C)
plt.show()
'''
Memory Consumption: ndarray and list
The main benefits of using numpy arrays should be smaller memory consumption
and better runtime behaviour.
We want to look at the memory usage of numpy arrays in this subchapter of our
turorial and compare it to
the memory consumption of Python lists.
'''
from sys import getsizeof as size
'''
The size of a Python list consists of the general list information, the size needed for the references to
the elements and the size of all the elements of the list. If we apply sys.getsizeof to a list, we get only
the size without the size of the elements. In the previous example, we made the assumption that all
the integer elements of our list have the same size. Of course, this is not valid in general, because
memory consumption will be higher for larger integers.
We will check now, how the memory usage changes, if we add another integer element to the list. We
also look at an empty list:
'''
lst = []
print("Emtpy list size: ", size(lst))
x = range(1, 10)
print(x) # x is an iterator
print(list(x))
x = range(1, 10)
print(x) # x is an iterator
print(list(x))
import numpy as np
x = np.arange(10.4)
print(x)
x = np.arange(0.5, 10.4, 0.8)
print(x)
x = np.arange(0.5, 10.4, 0.8, int)
print(x)
print(np.shape(x))
print(x.shape)
#"shape" can also be used to change the shape of an array.
x.shape = (3, 6)
print(x)
x.shape = (2, 9)
print(x)
#We illustrate the operating principle of "slicing" with some examples. We start with the easiest case,
i.e. the slicing of a one-dimensional array:
S = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(S[2:5])
print(S[:4])
print(S[6:])
print(S[:])
A = np.array([
[11, 12, 13, 14, 15],
[21, 22, 23, 24, 25],
[31, 32, 33, 34, 35],
[41, 42, 43, 44, 45],
[51, 52, 53, 54, 55]])
print(A[:3, 2:])
print(A[3:, :])
print(A[:, 4:])
#The following two examples use the third
#parameter "step". The reshape function is used
#to construct the two-dimensional array.
#We will explain reshape in the following
#subchapter:
import numpy as np
X = np.arange(28).reshape(4, 7)
print(X)
print(X[::2, ::3])
print(X[::, ::3])
#Creating Arrays with Ones, Zeros and Empty
#import numpy as np
E = np.ones((2,3))
print(E)
#
F = np.ones((3,4),dtype=int)
print(F)
#
Z = np.zeros((2,4))
print(Z)
#
x = np.array([2,5,18,14,4])
E = np.ones_like(x)
print(E)
#
Z = np.zeros_like(x)
print(Z)
#Copying Arrays
#numpy.copy()
import numpy as np
x = np.array([[42,22,12],[44,53,66]], order='F')
y = x.copy()
x[0,0] = 1001
print(x)
print(y)
print(x.flags['C_CONTIGUOUS'])
print(y.flags['C_CONTIGUOUS'])
#ndarray.copy()
import numpy as np
x = np.array([[42,22,12],[44,53,66]], order='F')
y = x.copy()
x[0,0] = 1001
print(x)
print(y)
print(x.flags['C_CONTIGUOUS'])
print(y.flags['C_CONTIGUOUS'])
'''
Identity Array
In linear algebra, the identity matrix, or unit matrix, of size n is the n × n square matrix with ones on
the main diagonal and zeros elsewhere.
identy
eye
The identity Function
We can create identity arrays with the function identity:
identity(n, dtype=None)
The parameters:
ParameterMeaning
nAn integer number defining the number of rows and columns of the output, i.e. 'n' x 'n'
dtypeAn optional argument, defining the data-type of the output. The default is 'float'
The output of identity is an 'n' x 'n' array with its main diagonal set to one, and all other elements are 0.
'''
np.identity(4)
'''
The eye Function
Another way to create identity arrays provides the function eye. This function creates also diagonal
arrays consisting solely of ones.
It returns a 2-D array with ones on the diagonal and zeros elsewhere.
ParameterMeaning
NAn integer number defining the rows of the output array.
MAn optional integer for setting the number of columns in the output. If it is None, it defaults to 'N'.
kDefining the position of the diagonal. The default is 0. 0 refers to the main diagonal. A positive value
refers to an upper diagonal, and a negative value to a lower diagonal.
dtypeOptional data-type of the returned array.
eye returns an ndarray of shape (N,M). All elements of this array are equal to zero, except for the 'k'-th
diagonal, whose values are equal to one.
'''
#import numpy as np
np.eye(5, 8, k=1, dtype=int)
#dtype
import numpy as np
i16 = np.dtype(np.int16)
print(i16)
A = np.array(lst, dtype=i16)
print(A)
#
dt = np.dtype([('country', 'S20'), ('density', 'i4'), ('area', 'i4'), ('population', 'i4')])
population_table = np.array([
('Netherlands', 393, 41526, 16928800),
('Belgium', 337, 30510, 11007020),
('United Kingdom', 256, 243610, 62262000),
('Germany', 233, 357021, 81799600),
('Liechtenstein', 205, 160, 32842),
('Italy', 192, 301230, 59715625),
('Switzerland', 177, 41290, 7301994),
('Luxembourg', 173, 2586, 512000),
('France', 111, 547030, 63601002),
('Austria', 97, 83858, 8169929),
('Greece', 81, 131940, 11606813),
('Ireland', 65, 70280, 4581269),
('Sweden', 20, 449964, 9515744),
('Finland', 16, 338424, 5410233),
('Norway', 13, 385252, 5033675)],
dtype=dt)
print(population_table[:12])
print(population_table['density'])
print(population_table['country'])
print(population_table['area'][2:5])
#
lst = [2,3, 7.9, 3.3, 6.9, 0.11, 10.3, 12.9]
v = np.array(lst)
v=v+2
print(v)
print(v * 2.2)
print(v - 1.38)
#
print(v ** 2)
print(v ** 1.5)
#
lst = [2,3, 7.9, 3.3, 6.9, 0.11, 10.3, 12.9]
res = []
for val in lst:
res.append(val + 2)
print(res)
#
res = [ val + 2 for val in lst]
print(res)
#
v = np.random.randint(0, 100, 1000)
%timeit v + 1
#
lst = list(v)
A = np.array([ [11, 12, 13], [21, 22, 23], [31, 32, 33] ])
B = np.ones((3,3))
np.dot(A, B)
#Examples of Using the dot Product
#We will begin with the cases in which both arguments are scalars or one-dimensional array:
print(np.dot(3, 4))
x = np.array([3])
y = np.array([4])
print(x.ndim)
print(np.dot(x, y))
x = np.array([3, -2])
y = np.array([-4, 1])
print(np.dot(x, y))
#Let's go to the two-dimensional use case:
A = np.array([ [1, 2, 3],
[3, 2, 1] ])
B = np.array([ [2, 3, 4, -2],
[1, -1, 2, 3],
[1, 2, 3, 0] ])
# es muss gelten:
print(A.shape[-1] == B.shape[-2], A.shape[1])
print(np.dot(A, B))
#The dot Product in the 3-dimensional Case
import numpy as np
X = np.array( [[[3, 1, 2],
[4, 2, 2],
[2, 4, 1]],
[[3, 2, 2],
[4, 4, 3],
[4, 1, 1]],
[[2, 2, 1],
[3, 1, 3],
[3, 2, 3]]])
[[1, 4, 1],
[4, 1, 2],
[4, 1, 2]],
[[1, 2, 3],
[4, 1, 1],
[3, 1, 4]]])
R = np.dot(X, Y)
print("The shapes:")
print(X.shape)
print(Y.shape)
print(R.shape)
[[-1, 0, 1],
[1, -1, -2]],
[[3, 2, 2],
[4, 4, 3]],
[[2, 2, 1],
[3, 1, 3]]])
Y = np.array(
[[[2, 3, 1, 2, 1],
[2, 2, 2, 0, 0],
[3, 4, 0, 1, -1]],
[[1, 4, 3, 2, 2],
[4, 1, 1, 4, -3],
[4, 1, 0, 3, 0]]])
R = np.dot(X, Y)
print(R[0])
#This means that we could have created the array R by applying the sum products in the way above.
To "prove" this, we will create an array R2 by using the sum product, which is equal to R in the
following example:
R2 = np.zeros(R.shape, dtype=np.int)
for i in range(X.shape[0]):
for j in range(X.shape[1]):
for k in range(Y.shape[0]):
for m in range(Y.shape[2]):
R2[i, j, k, m] = sum(X[i, j, :] * Y[k, :, m])
R=A*B
print(R)
#
MA = np.mat(A)
MB = np.mat(B)
R = MA * MB
print(R)
#Comparison Operators
import numpy as np
A = np.array([ [11, 12, 13], [21, 22, 23], [31, 32, 33] ])
B = np.array([ [11, 102, 13], [201, 22, 203], [31, 32, 303] ])
A == B
#It is possible to compare complete arrays for equality as well. We use array_equal for this purpose.
array_equal returns True if two arrays have the same shape and elements, otherwise False will be
returned.
print(np.array_equal(A, B))
print(np.array_equal(A, A))
#Logical Operators
a = np.array([ [True, True], [False, False]])
b = np.array([ [True, False], [True, False]])
print(np.logical_or(a, b))
print(np.logical_and(a, b))
'''
Broadcasting
Numpy provides a powerful mechanism, called Broadcasting, which allows to perform arithmetic
operations on arrays of different shapes. This means that we have a smaller array and a larger array,
and we transform or apply the smaller array multiple times to perform some operation on the larger
array. In other words: Under certain conditions, the smaller array is "broadcasted" in a way that it has
the same shape as the larger array.
With the aid of broadcasting we can avoid loops in our Python program. The looping occurs implicitly
in the Numpy implementations, i.e. in C. We also avoid creating unnecessary copies of our data.
'''
import numpy as np
A = np.array([ [11, 12, 13], [21, 22, 23], [31, 32, 33] ])
B = np.array([1, 2, 3])
A * B[:, np.newaxis]
#
A = np.array([10, 20, 30])
B = np.array([1, 2, 3])
A[:, np.newaxis]
#
A[:, np.newaxis] * B
#Distance Matrix
'''
In mathematics, computer science and especially graph theory, a distance matrix is a matrix or a
two-dimensional array, which contains the distances between the elements of a set, pairwise taken.
The size of this two-dimensional array in n x n, if the set consists of n elements.
'''
cities = ["Barcelona", "Berlin", "Brussels", "Bucharest",
"Budapest", "Copenhagen", "Dublin", "Hamburg", "Istanbul",
"Kiev", "London", "Madrid", "Milan", "Moscow", "Munich",
"Paris", "Prague", "Rome", "Saint Petersburg",
"Stockholm", "Vienna", "Warsaw"]
dists = np.array(dist2barcelona[:12])
print(dists)
print(np.abs(dists - dists[:, np.newaxis]))
#3-Dimensional Broadcasting
A = np.array([ [[3, 4, 7], [5, 0, -1] , [2, 1, 5]],
[[1, 0, -1], [8, 2, 4], [5, 2, 1]],
[[2, 1, 3], [1, 9, 4], [5, -2, 4]]])
B*A
'''
Flatten and Reshape Arrays
There are two methods to flatten a multidimensional array:
flatten()
ravel()
flatten
'''
import numpy as np
A = np.array([[[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7]],
[[ 8, 9],
[10, 11],
[12, 13],
[14, 15]],
[[16, 17],
[18, 19],
[20, 21],
[22, 23]]])
Flattened_X = A.flatten()
print(Flattened_X)
print(A.flatten(order="C"))
print(A.flatten(order="F"))
print(A.flatten(order="A"))
'''
ravel
The order of the elements in the array returned by ravel() is normally "C-style".
ravel(a, order='C')
'C': C-like order, with the last axis index changing fastest, back to the first axis index changing slowest.
"C" is the default!
'F': Fortran-like index order with the first index changing fastest, and the last index changing slowest.
'A': Fortran-like index order if the array "a" is Fortran contiguous in memory, C-like order otherwise.
'K': read the elements in the order they occur in memory, except for reversing the data when strides are
negative.
'''
print(A.ravel())
print(A.ravel(order="A"))
print(A.ravel(order="F"))
print(A.ravel(order="A"))
print(A.ravel(order="K"))
'''
reshape
The method reshape() gives a new shape to an array without changing its data, i.e. it returns a new
array with a new shape.
ParameterMeaning
aarray_like, Array to be reshaped.
newshapeint or tuple of ints
order'C', 'F', 'A', like in flatten or ravel
'''
X = np.array(range(24))
Y = X.reshape((3,4,2))
Y
'''
Concatenating Arrays
In the following example we concatenate three one-dimensional arrays to one array. The elements of
the second array are appended to the first array. After this the elements of the third array are
appended:
'''
x = np.array([11,22])
y = np.array([18,7,6])
z = np.array([1,3,5])
c = np.concatenate((x,y,z))
print(c)
'''
If we are concatenating multidimensional arrays, we can concatenate the arrays according to axis.
Arrays must have the same shape to be concatenated with concatenate().
In the case of multidimensional arrays, we can arrange them according to the axis.
The default value is axis = 0:
'''
x = np.array(range(24))
x = x.reshape((3,4,2))
y = np.array(range(100,124))
y = y.reshape((3,4,2))
z = np.concatenate((x,y))
print(z)
#We do the same concatenation now with axis=1:
z = np.concatenate((x,y),axis = 1)
print(z)
In another usecase you may have a two-dimensional array like np.array([ [1, 2], [3, 4]]),
which you intend to use as a building block to construe the array with the shape (6, 8):
'''
array([[1, 2, 1, 2, 1, 2, 1, 2],
[3, 4, 3, 4, 3, 4, 3, 4],
[1, 2, 1, 2, 1, 2, 1, 2],
[3, 4, 3, 4, 3, 4, 3, 4],
[1, 2, 1, 2, 1, 2, 1, 2],
[3, 4, 3, 4, 3, 4, 3, 4]])
#
x = np.array([ [1, 2], [3, 4]])
np.tile(x, (3,4))
#
x = np.array([ 3.4])
y = np.tile(x, (5,))
print(y)
#
import numpy as np
x = np.array([[1, 2], [3, 4]])
print(np.tile(x, 2))
#
import numpy as np
x = np.array([[1, 2], [3, 4]])
print(np.tile(x, (2, 1)))
#
import numpy as np
x = np.array([[1, 2], [3, 4]])
print(np.tile(x, (2, 2)))
#
import random
random_number = random.random()
print(random_number)
#
from random import SystemRandom
crypto = SystemRandom()
print(crypto.random())
#Generate a list of Random Numbers
import random
print(random_list(10, secure=False))
#
import random
outcome = random.randint(1,6)
print(outcome)
#
import random
print(choice(possible_destinations))
#Random Samples with Python
import numpy as np
x = np.random.random_sample((3, 4))
print(x)
#Random seed
import random
random.seed(42)
for _ in range(10):
print(random.randint(1, 10), end=", ")
n = 1000
values = []
frequencies = {}
print(values[:10])
#The following program plots the random values, which we have created before.
#We haven't covered matplotlib so far, so it's not necessary to understand the code:
%matplotlib inline
freq = list(frequencies.items())
freq.sort()
plt.plot(*list(zip(*freq)))
import bk_random
number_of_specialists = 15
employees = set()
while len(employees) < number_of_specialists:
employee = bk_random.cartesian_choice(firstnames, surnames)
employees.add(" ".join(employee))
print(employees)
A = np.array([4, 7, 3, 4, 2, 8])
print(A == 4)
print(A < 5)
#
B = np.array([[42,56,89,65],
[99,88,42,12],
[55,42,17,18]])
print(B>=42)
'''
Extract from the array np.array([3,4,6,10,24,89,45,43,46,99,100]) with Boolean masking all the number
'''
import numpy as np
A = np.array([3,4,6,10,24,89,45,43,46,99,100])
div3 = A[A%3!=0]
print("Elements of A not divisible by 3:")
print(div3)
div5 = A[A%5==0]
print("Elements of A divisible by 5:")
print(div5)
A[A%3==0] = 42
print("""New values of A after setting the elements of A,
which are divisible by 3, to 42:""")
print(A)
#
import matplotlib.pyplot as plt
# our X values:
days = list(range(0, 22, 3))
print(days)
# our Y values:
celsius_values = [25.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1]
plt.plot(days, celsius_values)
plt.show()
plt.plot(days, celsius_values, 'bo')
plt.show()
#
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
print(type(fig))
print(type(ax))
#
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(X, Y)
#Labels on Axes
import matplotlib.pyplot as plt
days = list(range(1,9))
celsius_values = [25.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1]
fig, ax = plt.subplots()
ax.plot(days, celsius_values)
ax.set(xlabel='Day',
ylabel='Temperature in Celsius',
title='Temperature Graph')
plt.show()
#The plot Function
import matplotlib.pyplot as plt
days = list(range(1,9))
celsius_min = [19.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1]
celsius_max = [24.8, 28.9, 31.3, 33.0, 34.9, 35.6, 38.4, 39.2]
fig, ax = plt.subplots()
ax.set(xlabel='Day',
ylabel='Temperature in Celsius',
title='Temperature Graph')
ax.plot(days, celsius_min,
days, celsius_min, "oy",
days, celsius_max,
days, celsius_max, "or")
plt.show()
#
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set(xlabel='Day',
ylabel='Temperature in Celsius',
title='Temperature Graph')
ax.plot(days, celsius_min)
ax.plot(days, celsius_min, "oy")
ax.plot(days, celsius_max)
ax.plot(days, celsius_max, "or")
plt.show()
#Checking and Defining the Range of Axes
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fig, ax = plt.subplots()
startx, endx = -2 * np.pi - 0.1, 2*np.pi + 0.1
starty, endy = -3.1, 3.1
We can use linewidth to set the width of a line as the name implies.
'''
import matplotlib.pyplot as plt
X = np.linspace(0, 2 * np.pi, 50, endpoint=True)
F1 = 3 * np.sin(X)
F2 = np.sin(2*X)
F3 = 0.3 * np.sin(X)
F4 = np.cos(X)
fig, ax = plt.subplots()
ax.plot(X, F1, color="blue", linewidth=2.5, linestyle="-")
ax.plot(X, F2, color="red", linewidth=1.5, linestyle="--")
ax.plot(X, F3, color="green", linewidth=2, linestyle=":")
ax.plot(X, F4, color="grey", linewidth=2, linestyle="-.")
plt.show()
'''
Draw LPoints in Matplotlib
We will learn now how to draw single points in Matplotlib.
'''
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.scatter(3, 7, s=42)
#
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
ax.plot (X, Y, color='blue', alpha=1.00)
ax.fill_between(X, 0, Y, color='blue', alpha=.1)
plt.show()
#
import numpy as np
import matplotlib.pyplot as plt
n = 256
X = np.linspace(-np.pi,np.pi,n,endpoint=True)
Y = np.sin(2*X)
fig, ax = plt.subplots()
fig, ax = plt.subplots()
plt.show()
'''
Customizing Ticks
Matplotlib has so far - in all our previous examples - automatically taken over the task of
spacing points on the axis. We can see for example that the X axis in our previous example was
numbered -6. -4, -2, 0, 2, 4, 6, whereas the Y axis was numbered -1.0, 0, 1.0, 2.0, 3.0
xticks is a method, which can be used to get or to set the current tick locations and the labels.
The same is true for yticks:
'''
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
xticks = ax.get_xticks()
xticklabels = ax.get_xticklabels()
print(xticks, xticklabels)
for i in range(6):
print(xticklabels[i])
yticks = ax.get_yticks()
print(yticks)
#As we said before, we can also use xticks to set the location of xticks:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
plt.show()
#There is an easier way to set the values of the xticks so that we do not have to caculate them
manually. We use plt.MultipleLocator with np.pi/2 as argument:
import numpy as np
import matplotlib.pyplot as plt
ax.xaxis.set_major_locator(plt.MultipleLocator(np.pi / 2))
plt.show()
#Adding a Legend
from polynomials import Polynomial
import numpy as np
import matplotlib.pyplot as plt
p = Polynomial(2, 3, -4, 6)
p_der = p.derivative()
fig, ax = plt.subplots()
X = np.linspace(-2, 3, 50, endpoint=True)
F = p(X)
F_derivative = p_der(X)
ax.plot(X, F, label="$2x^{3} + 3x^{2} - 4x + 6$")
ax.plot(X, F_derivative, label="$6x^{2} - 6x -4$")
ax.legend(loc='upper left')
'''
Annotations
The visualizations of function plots often makes annotations necessary.
This means we draw the readers attentions to important points and areas of the plot.
To this purpose we use texts, labels and arrows. We have already used axis labels and titles for
this purpose, but these are 'annotations' for the whole plot. We can easily annotate points inside
the axis or on the graph with the annotate method of an axes object. In an annotation,
there are two points to consider: the location being annotated represented by the argument
xy and the location of the text xytext. Both of these arguments are (x,y) tuples.
p = Polynomial(1, 0, -12, 0)
p_der = p.derivative()
fig, ax = plt.subplots()
X = np.arange(-5, 5, 0.1)
F = p(X)
F_derivative = p_der(X)
ax.grid()
ax.plot(X, F, label="p")
ax.plot(X, F_derivative, label="derivation of p")
ax.legend(loc='best')
plt.show()
#If you are not satisfied with the automatic positioning of the text, you can assign a tuple with a
position for the text to the keyword parameter xytext:
p = Polynomial(1, 0, -12, 0)
p_der = p.derivative()
fig, ax = plt.subplots()
X = np.arange(-5, 5, 0.1)
F = p(X)
F_derivative = p_der(X)
ax.grid()
ax.annotate("local maximum",
xy=(-2, p(-2)),
xytext=(-1, p(-2)+35),
arrowprops=dict(facecolor='orange'))
ax.annotate("local minimum",
xy=(2, p(2)),
xytext=(-2, p(2)-40),
arrowprops=dict(facecolor='orange', shrink=0.05))
ax.annotate("inflection point",
xy=(0, p(0)),
xytext=(-3, -30),
arrowprops=dict(facecolor='orange', shrink=0.05))
ax.plot(X, F, label="p")
ax.plot(X, F_derivative, label="derivation of p")
ax.legend(loc='best')
plt.show()
#
import matplotlib.pyplot as plt
for ax in axs.flat:
ax.set(xlim=(0, 1), ylim=(0, 1), xticks=[], yticks=[], aspect=1)
fig.tight_layout(pad=0.2)
plt.show()
#Multiple Plots and Double Axes
#Working with Multiple Figures and Axes
import matplotlib.pyplot as plt
python_course_green = "#476042"
plt.figure(figsize=(6, 4))
plt.subplot(221) # equivalent to: plt.subplot(2, 2, 1)
plt.figure()
cp = plt.contour(X, Y,Z)
plt.clabel(cp, inline=True,
fontsize=10)
plt.title('Contour Plot')
plt.xlabel('x (cm)')
plt.ylabel('y (cm)')
plt.show()
#
import matplotlib.pyplot as plt
plt.figure()
cp = plt.contour(X, Y, Z, colors='black', linestyles='dashed')
plt.clabel(cp, inline=True,
fontsize=10)
plt.title('Contour Plot')
plt.xlabel('x (cm)')
plt.ylabel('y (cm)')
plt.show()
#Filled Contours
import numpy as np
import matplotlib.pyplot as plt
cp = plt.contourf(X, Y, Z)
plt.colorbar(cp)
plt.figure()
contour = plt.contourf(X, Y, Z)
plt.clabel(contour, colors = 'k', fmt = '%2.1f', fontsize=12)
c = ('#ff0000', '#ffff00', '#0000FF', '0.6', 'c', 'm')
contour_filled = plt.contourf(X, Y, Z, colors=c)
plt.colorbar(contour)
Z = np.sqrt(X ** 2 + Y ** 2 )
plt.figure()
y, x = np.ogrid[-1:2:100j, -1:1:100j]
plt.contour(x.ravel(),
y.ravel(),
x**2 + (y-((x**2)**(1.0/3)))**2,
[1],
colors='red',)
plt.axis('equal')
plt.show()
#Introduction into Pandas
#Data Structures
import pandas as pd
S = pd.Series([11, 28, 72, 3, 5, 8])
S
print(S.index)
print(S.values)
#
fruits = ['apples', 'oranges', 'cherries', 'pears']
quantities = [20, 33, 52, 10]
S = pd.Series(quantities, index=fruits)
S
#If we add two series with the same indices, we get a new series with the same index and the
correponding values will be added:
x=(10<20)? 30:40
print(x)
x=30 if 100<20 else 40
print(x)
a="behala"
b="behala"
print(id(a))
print(id(b))
print(a is not b)
import math
print(math.sqrt(4))
list1=[10,20,30]
list2=[10,20,30]
print(id(list1))
print(id(list2))
print(list1 is list2)
print(list1==list2)
#2input("Enter 2 nos").split()
a,b=[float(x)for x in input("Enter 2 float nos").split(',')]
print(a*b)
x=eval("10+34+78")
print(x)
print("10+20")
x=eval(input("Enter data"))
print(type(x))
print(x)
for x1 in x:
print(x1)
name="Debosi"
salary=75000
boyfrnd="??"
print("Hello {} your sal is {} and boyfrnd {} is waiting".format(name,salary,boyfrnd))
for i in range(4):
for j in range(4):
print("i={} and j={}".format(i,j))
num=int(input("Enter number"))
for i in range(1,num+1):
for j in range(1,i+1):
print(num-j,end=" ")
print()
for a in range(1,num+1):
for k in range(1,num+1-a):
print(num-k,end=" ")
print()
'''
#s=[0123456789]
s=input("Enter main string")
subs=input("Enter sub string")
flag=False
pos=-1
count=0
n=len(s)
while True:
pos=s.find(subs,pos+1,n)
if pos==-1:
break
print("Found at index",pos)
flag=True
count=count+1
if flag==False:
print("not found")
print("Number of occurance",count)
class Student:
def __init__(self,rollno,name):
self.roll=rollno
self.nm=name
def talk(self):
roll=99
print("My name is ",self.nm)
print("Roll no is ",self.roll)
print("Roll no is ",roll)
s=Student(100,'Sunny')
print(s.nm)
print(s.roll)
s.talk()
s1=Student(200,'Bunny')
s1.talk()
class Student:
cname='ABC'
def __init__(self,name,rollno):
self.name=name
self.rollno=rollno
s1=Student('Jhargram',101)
s2=Student('Gopiballavpur',102)
print(s1.name,s1.rollno,Student.cname)
print(s2.name,s2.rollno,Student.cname)
Instance Method************************************************
class Student:
def __init__(self,name,rollno):
self.name=name
self.rollno=rollno
print(self.name)
print(self.rollno)
def info(self):
self.marks=19
x=60
print(self.marks)
print(x)
s1=Student('Durga',101)
s1.info()
s1.age=12
print(s1.__dict__)
Static*****************************************************
class Test:
x=10
def __init__(self):
Test.y=20
def m1(self):
Test.c=30
@classmethod
def m2(cls):
cls.d=40
Test.e=50
@staticmethod
def m3():
Test.f=60
Test.g=70
t=Test()
t.m1()
Test.m2()
Test.m3()
print(Test.__dict__)
class Test:
x=10
def __init__(self):
Test.x=20
def m1(self):
print(Test.x)
print(self.x)
@classmethod
def m2(cls):
cls.x=30
Test.x=40
@staticmethod
def m3():
Test.x=50
t=Test()
t.m1()
Global Method*******************************************
x=999 #global
class Test:
def m1(self):
global x
x=888
print(x)
def m2(self):
print(x)
t=Test()
t.m1()
t.m2()
print(x)
Test.x=60
print(Test.x)
print(t.x)
Overloading**************************************
class Test:
def sum(self,a=None,b=None,c=None):
if a!=None and b!=None and c!=None:
print('The sum of 3 number is', a+b+c)
elif a!=None and b!=None:
print('The sum of 2 number is', a+b)
else:
print('Provide 2 or 3 arguments')
t=Test()
t.sum(10,20,30)
t.sum(10,20)
t.sum(10)
Operator Overloading******************************
class Book:
def __init__(self,pages):
self.pages=pages
def __add__(self,other):
return self.pages+other.pages
def __sub__(self,other):
return self.pages-other.pages
b1=Book(100)
b2=Book(200)
print(b1+b2)
print(b1-b2)
#"""
class Book:
def __init__(self,pages1):
self.pages1=pages1
def __str__(self):
return 'The number of pages'+str(self.pages1)
def __add__(self,other1):
total=self.pages1+other1.pages1
b=Book(total)
return b
b1=Book(100)
b2=Book(200)
b3=Book(300)
b4=Book(400)
print(b1+b2+b3+b4)
#print(b1+b2*b3+b4)
"""
"""
class Student:
def __init__(self,name,marks):
self.name=name
self.marks=marks
def __lt__(self,other):
return self.marks<other.marks
s1=Student('Durga',100)
s2=Student('Ravi',200)
print(s1<s2)
print(s2<s1)
class Employee:
def __init__(self,name,salary):
self.name=name
self.salary=salary
def __mul__(self,other):
return self.salary*other.days
class TimeSheet:
def __init__(self,name,days):
self.name=name
self.days=days
def __mul__(self,other):
return self.days*other.salary
e=Employee('Durga',500)
t=TimeSheet('Durga',25)
print('This month salary',e*t)
print('This month salary',t*e)
Lab 8 Inheritance
Inheritance**********************************************
class Person:
def __str__(self):
return self.firstname + " " + self.lastname
class Employee(Person):
x = Person("Marge", "Simpson")
y = Employee("Homer", "Simpson", "1007")
print(x)
print(y)
Super*************************************************
"""
Created on Tue Oct 22 07:48:04 2019
@author: USER
"""
"""
class Person:
def __init__(self,name,age):
self.name=name
self.age=age
def display(self):
print('Name', self.name)
print('Age',self.age)
class Student(Person):
def __init__(self, name, age, rollno,marks):
super().__init__(name,age)
self.rollno=rollno
self.marks=marks
def display(self):
super().display()
print('Rollno', self.rollno)
print('Marks',self.marks)
class Teacher(Person):
def __init__(self,name,age,salary,subject):
super().__init__(name,age)
self.salary=salary
self.subject=subject
def display(self):
super().display()
print('Salary', self.salary)
print('subject',self.subject)
s=Student('Ravi',23,101,90)
t=Teacher('Durga',62,1000,'Python')
s.display()
t.display()
"""
"""
class A:
def m1(self):
print('A class method')
class B(A):
def m1(self):
print('B class method')
class C(B):
def m1(self):
print('C class method')
class D(C):
def m1(self):
print('D class method')
class E(D):
def m1(self):
C.m1(self)
e=E()
e.m1()
"""
"""
class A:
def m1(self):
print('A class method')
class B(A):
def m1(self):
print('B class method')
class C(B):
def m1(self):
print('C class method')
class D(C):
def m1(self):
print('D class method')
class E(D):
def m1(self):
super(C,self).m1()
e=E()
e.m1()
"""
class P:
a=10
def __init__(self):
self.b=20
class C(P):
def m1(self):
print(super().a)
#print(super().b)
c=C()
c.m1()
"""
"""
class P:
def __init__(self):
print('Parent class constructor')
def m1(self):
print('Parent instance method')
@classmethod
def m2(cls):
print('Parent class method')
@staticmethod
def m3():
print('parent static method')
class C(P):
def __init__(self):
super().__init__()
super().m1()
super().m2()
super().m3()
c=C()
@classmethod
@staticmethod
def method1(self):
super.m1()
super.m2()
super.m3()
c=C()
c.method1()
"""
Lab 9 Threading
Threading*******************************************************
What Is a Thread?
A thread is a separate flow of execution. This means that your program will have two things
happening at once. But for most Python 3 implementations the different threads do not actually
execute at the same time: they merely appear to.
It’s tempting to think of threading as having two (or more) different processors running on your
program, each one doing an independent task at the same time. That’s almost right. The threads may
be running on different processors, but they will only be running one at a time.
Getting multiple tasks running simultaneously requires a non-standard implementation of Python,
writing some of your code in a different language, or using multiprocessing which comes with some
extra overhead.
Because of the way CPython implementation of Python works, threading may not speed up all tasks.
This is due to interactions with the GIL that essentially limit one Python thread to run at a time.
Tasks that spend much of their time waiting for external events are generally good candidates for
threading. Problems that require heavy CPU computation and spend little time waiting for external
events might not run faster at all.
This is true for code written in Python and running on the standard CPython implementation. If your
threads are written in C they have the ability to release the GIL and run concurrently. If you are running
on a different Python implementation, check with the documentation too see how it handles threads.
If you are running a standard Python implementation, writing in only Python, and have a CPU-bound
problem, you should check out the multiprocessing module instead.
Architecting your program to use threading can also provide gains in design clarity. Most of the
examples you’ll learn about in this tutorial are not necessarily going to run faster because they use
threads. Using threading in them helps to make the design cleaner and easier to reason about.
So, let’s stop talking about threading and start using it!
Starting a Thread
Now that you’ve got an idea of what a thread is, let’s learn how to make one. The Python standard
library provides threading, which contains most of the primitives you’ll see in this article. Thread, in
this module, nicely encapsulates threads, providing a clean interface to work with them.
Link: https://realpython.com/intro-to-python-threading/
Program 1
import time
#from threading import *
def doubles(numbers):
for n in numbers:
time.sleep(1)
print('Double value',2*n)
def squares(numbers):
for n in numbers:
time.sleep(1)
print('Square value',n*n)
numbers=[1,2,3,4,5,6]
begintime=time.time()
doubles(numbers)
squares(numbers)
endtime=time.time()
print('The total time taken: ',endtime-begintime)
"""
join() a Thread
Daemon threads are handy, but what about when you want to wait for a thread to stop? What about
when you want to do that and not exit your program?
To tell one thread to wait for another thread to finish, you call .join(). If you uncomment that line, the
main thread will pause and wait for the thread x to complete running.
Program 2
from threading import *
import time
def doubles(numbers):
for n in numbers:
time.sleep(1)
print('Double value',2*n)
def squares(numbers):
for n in numbers:
time.sleep(1)
print('Square value',n*n)
numbers=[1,2,3,4,5,6]
begintime=time.time()
t1=Thread(target=doubles,args=(numbers,))
t2=Thread(target=squares,args=(numbers,))
t1.start()
t2.start()
t1.join()
t2.join()
endtime=time.time()
print('The total time taken: ',endtime-begintime)
"""
Program 3
from threading import *
print(current_thread().getName())
current_thread().setName('Durga') #current_thread().name='Durga'
print(current_thread().getName())
"""
Program 4
from threading import *
def test():
print('Child thread ')
t=Thread(target=test)
t.start()
print('Main thread identification number ',current_thread().ident)
print('Child thread identification number ',t.ident)
"""
Program 5
from threading import *
import time
def display():
print(current_thread().name,'.....started')
time.sleep(2)
print('The number of active thread:',active_count())
t1=Thread(target=display,name='ChildThread-1')
t2=Thread(target=display,name='ChildThread-2')
t3=Thread(target=display,name='ChildThread-3')
t1.start()
t2.start()
t3.start()
print('The number of active thread: ',active_count())
time.sleep(10)
print('The number of active thread:',active_count())
"""
Program 6
from threading import *
import time
def display():
print(current_thread().name,'....started')
time.sleep(3)
print(current_thread().name,'....ended')
print('The number of active thread: ',active_count())
t1=Thread(target=display,name='ChildThread-1')
t2=Thread(target=display,name='ChildThread-2')
t3=Thread(target=display,name='ChildThread-3')
t1.start()
t2.start()
t3.start()
l=enumerate()
for t in l:
print('Thread name',t.name)
print('Thread identification number',t.ident)
"""
Program 7
from threading import *
import time
def display():
print(current_thread().name,'....started')
time.sleep(3)
print(current_thread().name,'....ended')
t1=Thread(target=display,name='ChildThread-1')
t2=Thread(target=display,name='ChildThread-2')
t1.start()
t2.start()
print(t1.name,'is alive:',t1.isAlive())
print(t2.name,'is alive:',t2.isAlive())
time.sleep(4)
print(t1.name,'is alive:',t1.isAlive())
print(t2.name,'is alive:',t2.isAlive())
"""
Program 8
from threading import *
import time
def display():
for i in range(10):
print('Child thread')
#time.sleep(2)
t=Thread(target=display)
t.start()
t.join()
for i in range(10):
print('Main thread')
"""
Daemon Threads
A daemon is a process that runs in the background.
Python threading has a more specific meaning for daemon. A daemon thread will shut down
immediately when the program exits. One way to think about these definitions is to consider
the daemon thread a thread that runs in the background without worrying about shutting it down.
If a program is running Threads that are not daemons, then the program will wait for those threads to
complete before it terminates. Threads that are daemons, however, are just killed wherever they are
when the program is exiting.
Program 9
from threading import *
mt=current_thread()
print(mt.isDeamon())
print(mt.daemon)
Program 10
from threading import *
mt=current_thread()
print(mt.isDeamon())
print(mt.daemon)
mt.setDaemon(True)
"""
Program 11
from threading import *
def job1():
print('Executed by child thread')
t=Thread(target=job1)
print(t.isDaemon())
t.setDaemon(True)
print(t.isDaemon())
"""
Lab 10
Exception Handling*************************************************************************
A Python program terminates as soon as it encounters an error. In Python, an error can be a syntax
error or an exception. In this article, you will see what an exception is and how it differs from a syntax
error.
>>> print( 0 / 0 ))
File "<stdin>", line 1
print( 0 / 0 ))
^
SyntaxError: invalid syntax
The arrow indicates where the parser ran into the syntax error. In this example, there was one bracket
too many. Remove it and run your code again:
>>> print( 0 / 0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
This time, you ran into an exception error. This type of error occurs whenever syntactically correct
Python code results in an error. The last line of the message indicated what type of exception error
you ran into.
Instead of showing the message exception error, Python details what type of exception error was
encountered. In this case, it was a ZeroDivisionError. Python comes with various built-in exceptions as
well as the possibility to create self-defined exceptions.
Raising an Exception
We can use raise to throw an exception if a condition occurs. The statement can be complemented
with a custom exception.
If you want to throw an error when a certain condition occurs using raise, you could go about it like
this:
x = 10
if x > 5:
raise Exception('x should not exceed 5. The value of x was: {}'.format(x))
When you run this code, the output will be the following:
● A try clause is executed up until the point where the first exception is encountered.
● Inside the except clause, or the exception handler, you determine how the program responds
to the exception.
● You can anticipate multiple exceptions and differentiate how the program should respond to
them.
● Avoid using bare except clauses.
Program 1
try:
print(10/0)
except ZeroDivisionError:
print('Hello')
Program 2
try:
print(10/0)
except ZeroDivisionError:
print(10/2)
'''
Program 3
try:
print(10/0)
except ZeroDivisionError as msg:
print('Type of exception',type(msg))
print('Type of exception',msg.__class__)
print('Type of exception',msg.__class__.__name__)
print(msg)
'''
Program 4
try:
x=int(input('Enter first number'))
y=int(input('Enter second number'))
print('The result',x/y)
except ZeroDivisionError:
print('Cant divide by zero')
except ValueError:
print('Please provide int value only')
'''
Program 5
try:
print(10/0)
except ZeroDivisionError:
print('Zero div error')
except ArithmeticError:
print('Arithmetic Error')
'''
Program 6
try:
print(10/0)
except ArithmeticError:
print('Arithmetic Error')
except ZeroDivisionError:
print('Zero div error')
'''
Program 7
try:
x=int(input('Enter first number'))
y=int(input('Enter second number'))
print('The result',x/y)
except (ZeroDivisionError,ValueError) as msg:
print('raised exception',msg.__class__.__name__)
print('Description ofexception',msg)
print('Please provide valid input only')
'''
Program 8
try:
print(10/0)
except ZeroDivisionError:
print('Zero div error')
except:
print('default exception')
'''
Program 9
try:
print(10/0)
except:
print('default exception')
except ZeroDivisionError:
print('Zero div error')
#SyntaxError: default 'except:' must be last
'''
The else Clause
In Python, using the else statement, you can instruct a program to execute a certain block of code
only in the absence of exceptions.
Program 10
try:
print('try')
print(10/0)
except ZeroDivisionError:
print('Except')
finally:
print('finally')
'''
Program 11
try:
print('try')
print(10/0)
except ValueError:
print('Except')
finally:
print('finally')
ZeroDivisionError: division by zero
try
finally
'''
The OS module in python provides functions for interacting with the operating system. OS, comes
under Python’s standard utility modules. This module provides a portable way of using operating
system dependent functionality. The *os* and *os.path* modules include many functions to interact
with the file system.
https://www.geeksforgeeks.org/os-module-python-examples/
1. os.name: This function gives the name of the operating system dependent module
imported. The following names have currently been registered: ‘posix’, ‘nt’, ‘os2’, ‘ce’,
‘java’ and ‘riscos’
2. os.getcwd(): Function os.getcwd(), returns the Current Working Directory(CWD) of the
file used to execute the code, can vary from system to system.
3. os.error: All functions in this module raise OSError in the case of invalid or
inaccessible file names and paths, or other arguments that have the correct type, but are
not accepted by the operating system. os.error is an alias for built-in OSError exception.
4. os.popen(): This method opens a pipe to or from command. The return value can be
read or written depending on whether mode is ‘r’ or ‘w’.
5. os.close(): Close file descriptor fd. A file opened using open(), can be closed by
close()only. But file opened through os.popen(), can be closed with close() or os.close().
If we try closing a file opened with open(), using os.close(), Python would throw
TypeError.
6. os.rename(): A file old.txt can be renamed to new.txt, using the function os.rename().
The name of the file changes only if, the file exists and user has sufficient privilege
permission to change the file.
Program 12
import os
try:
print('try')
os._exit(0)
except ValueError:
print('except')
finally:
print('finally')
'''
Program 13
try:
print('Outer try block')
try:
print('Inner tryblock')
print(10/0)
except ZeroDivisionError:
print('Inner Except Block')
finally:
print('Inner Finally block')
except:
print('Outer except block')
finally:
print('Outer finally block')
'''
Program 14
try:
print('Outer try block')
try:
print('Inner tryblock')
print(10/0)
except ValueError:
print('Inner Except Block')
finally:
print('Inner Finally block')
except:
print('Outer except block')
finally:
print('Outer finally block')
'''
Program 15
try:
print('try')
except:
print('except')
else:
print('else')
finally:
print('finally')
'''
Program 16
try:
print('try')
print(10/0)
except:
print('except')
else:
print('else')
finally:
print('finally')
'''
Program 17
f=None
try:
f=open('abc.txt','r')
except:
print('Some problem while open')
else:
print('File opened successfully')
print('The content of the file')
print('*', *30)
print(f.read())
finally:
if f is not None:
f.close()
'''
User Defined Exception
Python throws errors and exceptions, when there is a code gone wrong, which may cause program to
stop abruptly. Python also provides exception handling method with the help of try-except. Some of
the standard exceptions which are most frequent include IndexError, ImportError, IOError,
ZeroDivisionError, TypeError and FileNotFoundError. A user can create his own error using exception
class.
https://www.geeksforgeeks.org/user-defined-exceptions-python-examples/
Program 18
class TooYoungException(Exception):
def __init__(self,msg):
self.msg=msg
class TooOldException(Exception):
def __init__(self,msg):
self.msg=msg
age=int(input('Enter Age'))
if age>60:
raise TooOldException('Too Old')
elif age<18:
raise TooYoungException('Too Young')
else:
print('You will be informed soon')
t.m1(10.5)
t.m1(10+20j)
import sys
assert ('linux' in sys.platform), "This code runs on Linux only."
If you run this code on a Linux machine, the assertion passes. If you were to run this code on a
Windows machine, the outcome of the assertion would be False and the result would be the following:
Traceback (most recent call last):
File "<input>", line 2, in <module>
AssertionError: This code runs on Linux only.
In this example, throwing an AssertionError exception is the last thing that the program will do.
After seeing the difference between syntax errors and exceptions, you learned about various ways to
raise, catch, and handle exceptions in Python. In this article, you saw the following options:
● raise allows you to throw an exception at any time.
● assert enables you to verify if a certain condition is met and throw an exception if it isn’t.
● In the try clause, all statements are executed until an exception is encountered.
● except is used to catch and handle the exception(s) that are encountered in the try clause.
● else lets you code sections that should run only when no exceptions are encountered in the try
clause.
● finally enables you to execute sections of code that should always run, with or without any
previously encountered exceptions.
Lab 11
File Handling*************************************************************************
'''
Python too supports file handling and allows users to handle files i.e., to read and write files, along
with many other file handling options, to operate on files. The concept of file handling has stretched
over various other languages, but the implementation is either complicated or lengthy, but alike other
concepts of Python, this concept here is also easy and short. Python treats file differently as text or
binary and this is important. Each line of code includes a sequence of characters and they form text
file. Each line of a file is terminated with a special character, called the EOL or End of Line characters
like comma {,} or newline character. It ends the current line and tells the interpreter a new one has
begun. Let’s start with Reading and Writing files.
Working of open() function
We use open () function in Python to open a file in read or write mode. As explained above, open ( )
will return a file object. To return a file object we use open() function along with two arguments, that
accepts file name and the mode, whether to read or write. So, the syntax being: open(filename, mode).
There are three kinds of mode, that Python provides and how files can be opened:
● “ r “, for reading.
● “ w “, for writing.
● “ a “, for appending.
● “ r+ “, for both reading and writing
Python file method tell() returns the current position of the file read/write pointer within the file.
Python file method seek() sets the file's current position at the offset. The whence argument is
optional and defaults to 0, which means absolute file positioning, other values are 1 which means
seek relative to the current position and 2 means seek relative to the file's end.
There is no return value. Note that if the file is opened for appending using either 'a' or 'a+', any seek()
operations will be undone at the next write.
If the file is only opened for writing in append mode using 'a', this method is essentially a no-op, but it
remains useful for files opened in append mode with reading enabled (mode 'a+').
If the file is opened in text mode using 't', only offsets returned by tell() are legal. Use of other offsets
causes undefined behavior.
Program 1
f=open('abc.txt','r')
print(f.tell())
print(f.read(3))
print(f.tell())
'''
Program 2
data='All students are stupid'
f=open('abc.txt', 'w')
f.write(data)
with open('abc.txt','r+') as f:
text=f.read()
print(text)
print('The last position is',f.tell())
f.seek(17)
print('Current pos',f.tell())
f.write('horrible!!!')
f.seek(1000)
print('Current pos',f.tell())
f.write('dumb')
print(f.tell())
'''
Program 3
import os
fname=input('enter the file name')
if os.path.isfile(fname):
print('File exists',fname)
f=open(fname,'r')
print(f.read())
else:
print('dont exist')
'''
Program 4
import os
fname=input('enter the file name')
if os.path.isfile(fname):
print('File exists',fname)
f=open(fname,'r')
lcount=wcount=ccount=0
for line in f:
lcount=lcount+1
words=line.split()
wcount=wcount+len(words)
ccount=ccount+len(line)
print(lcount)
print(wcount)
'''
Program 5
import csv
f=open('student.csv','r')
r=csv.reader(f)
data=list(r)
#print(data)
for row in data:
for column in row:
print(column,'\t',end='')
print()
'''
import csv
with open('student.csv','w',newline='') as f:
w=csv.writer(f)
w.writerow(['Name','Roll','Marks','Addr'])
while True:
name=input('Nter student name')
rno=int(input('Nter roll no'))
marks=int(input('nter marks'))
addr=input('nter address')
w.writerow([name,rno,marks,addr])
option=input('Do u wantto continue[yes|no:]')
if option.lower()=='no':
break
print('successful')
Lab 12
Garbage Collection**********************************************************************
The values of your program’s objects are stored in memory for quick access. In many programming
languages, a variable in your program code is simply a pointer to the address of the object in memory.
When a variable is used in a program, the process will read the value from memory and operate on it.
In early programming languages, developers were responsible for all memory management in their
programs. This meant before creating a list or an object, you first needed to allocate the memory for
your variable. After you were done with your variable, you then needed to deallocate it to “free” that
memory for other users.
Forgetting to free your memory. If you don’t free your memory when you’re done using it, it can result
in memory leaks. This can lead to your program using too much memory over time. For long-running
applications, this can cause serious problems.
Freeing your memory too soon. The second type of problem consists of freeing your memory while
it’s still in use. This can cause your program to crash if it tries to access a value in memory that
doesn’t exist, or it can corrupt your data. A variable that refers to memory that has been freed is called
a dangling pointer.
These problems were undesirable, and so newer languages added automatic memory management.
There are a few different methods for automatic memory management, but one of the more popular
ones uses reference counting. With reference counting, the runtime keeps track of all of the references
to an object. When an object has zero references to it, it’s unusable by the program code and thus able
to be deleted.
For programmers, automatic memory management adds a number of benefits. It’s faster to develop
programs without thinking about low-level memory details. Further, it can help avoid costly memory
leaks or dangerous dangling pointers.
However, automatic memory management comes at a cost. Your program will need to use additional
memory and computation to track all of its references. What’s more, many programming languages
with automatic memory management use a “stop-the-world” process for garbage collection where all
execution stops while the garbage collector looks for and deletes objects to be collected.
With the advances in computer processing from Moore’s law and the larger amounts of RAM in newer
computers, the benefits of automatic memory management usually outweigh the downsides. Thus,
most modern programming languages like Java, Python, and Golang use automatic memory
management.
For long-running applications where performance is critical, some languages still have manual
memory management. The classic example of this is C++. We also see manual memory
management in Objective-C, the language used for macOS and iOS. For newer languages, Rust uses
manual memory management.
Now that we know about memory management and garbage collection in general, let’s get more
specific about how garbage collection works in Python.
Program 1
import gc
print(gc.isenabled())
gc.disable()
print(gc.isenabled())
gc.enable()
print(gc.isenabled())
Program 2
import time
class Test:
def __init__(self):
print('Object initialization')
def __del__(self):
print('Fulfilling last wish')
t=Test()
time.sleep(10)
print('End of application')
'''
Program 3
import time
class Test:
def __init__(self):
print('Object initialization')
def __del__(self):
print('Fulfilling last wish')
t=Test()
t=None
time.sleep(5)
print('End of application')
'''
Program 4
import time
class Test:
def __init__(self):
print('Object initialization')
def __del__(self):
print('Fulfilling last wish')
t=Test()
t=None
print(t)
'''
Program 5
import time
class Test:
def __init__(self):
print('Object initialization')
def __del__(self):
print('Fulfilling last wish')
t=Test()
del t
print(t)
Miscellaneous
Pattern Generation**********************************************************************
def pypart(n):
# printing stars
print("* ",end="")
# Driver Code
n=5
pypart(n)
"""
def pypart2(n):
# number of spaces
k=n
# printing stars
print("* ", end="")
Inner
Class**********************************************************************************************
class Person:
def __init__(self):
self.name='Satrajit'
self.dob=self.DOB()#object creation
def display(self):
print('Name ',self.name)
self.dob.display()
class DOB:
def __init__(self):
self.dd=15
self.mm=8
self.yyyy=1947
def display(self):
print('DOB={}/{}/{}'.format(self.dd,self.mm,self.yyyy))
p=Person()
p.display()