Aug 17-21, 2020 Vikrant Patil
These notes are available online at http://notes.pipal.in/2020/arcesium_finop_batch2/module1-day1.html
© Pipal Academy LLP
Day 1 | Day 2 | Day 3 | Day 4 | Day 5
We will be using jupyter hub from http://lab2.pipal.in for this training.
esc + m -> covert the cell to markdown
esc +y -> convert back the cell to code cell
shift + enter -> execute the cell
# Header1
## Header2
### Header3
3 + 4
multiply two and three and write down results in your notebook
2 + 3
sum([1, 2, 3, 4 ,5])
dom
print("hello")
Very primitive way looking at programming is manipulating numbers and text! To handle numbers and text we need to have some basic operations supported as given below
42 + 42
42 - 42
42 * 2 # multiplication
5 / 2 # real division
5 // 2 ## integer division
2 ** 5 ### 2 raised to power 5
5 % 2 ## remainder when 5 divided by 2
Numerci data is divided in two parts, integers and real numbers.
1.1 + 1.1
1.0 - 0.1
1.0 * 2
5.0 / 2
5.0 // 2.0
2.0 ** 5
The basic arithmatic opeartions we are doing using something called opeartors. These symbols, operators have got some priority
1+2**5%5/2*7
Opeartor priority
** 1
% // / * 2
+ - 3
operators with same priority , the one that comes first from left to right (in the statement) will get high priority.
1+2**5%5/2*7
1+32%5/2*7
1 + 2 /2 *7
1 + 1 * 7
1 + 7
8
If we want to change the priority?
7 + 2*3
(7+2)*3
Now lets look at text data
"Hello this is text data"
'Hello this is also text data'
Python also has multiline string. multiline string starts with 3 quotes, double or single anything is fine
"""This is first line of my poem
And this is second
and lst one
"""
\n is special character called newline char. all special characters are represented \ escape character
=========== ====
escape char mean
=========== ====
\n new line
\t tab
\\ \
text data also supports some operators
"hello" + "world"
"*"*5
problems
2(2+3)
2*(2+3)
26780(1+0.07/4)**(4*5)
26780*(1+0.07/4)**(4*5)
We saw arithmatic operators. Now lets look at one interesting operator = , assignment operator.
x = 10
This statement results in creation of object which is result on right hand side, 10 (integer). It stores the object in python's memory. Python has got namespace. In the current namespce it will create a name called x. Then it will create a link between this name x and the memory location where it stored 10. As a result, whenever you refer x! it goes through the link and fetches the the value
x
x = 10
y = 20
x = y ## both will refer to same location which will have value of 20
x
y
x = 20
x = y
x = 22
y
10
x
Be carefull with string literals
vikrant = 10
vikrant
"vikrant" # litteral string
"vikrant"
vikrant
What can be used as variable name!
this_is_variable = 10
this_also_232 = 232
invalid variable name = 12
invalid_variable_name = "value"
Assignment operator allows multiple assignments at a time
a, b = 56, 57
x, y, z = 1, 2, 3
x
y
z
x, y, z
think about it
x = 10
y = x
x = x+ 10
x = 10
y = x
y = 25
Now that we can store things in variables lets do more fun with text
s = "hello"
s[0] # 0th char in the string
s[4]
s[-1] # last character
indices in python work like this
+---+---+---+---+---+---+
| p | y | t | h | o | n |
+---+---+---+---+---+---+
--> 0 1 2 3 4 5
-6 -5 -4 -3 -2 -1 <----
p = "python"
p[0]
[-6]
p[1]
p[-5]
Other than basic datatype we also feel need of colectiing basic datatypes to form array or sequence of serially arranged items. List is varsatile high level data type which allows us to keep anu number of items squentially.
[1, 1, 1]
ones = [1, 1, 1]
You can save items of any type together
numbers = [1, 2, 3, 4]
words = ["hello", "some", "wise", "words"]
mixed = [1, "one", 2, "two", ones]
mixed
[['a', 'b', 'c'], [1, 1, 1]]
mixed[0]
mixed
mixed[0]
mixed[1]
mixed[2]
mixed[3]
mixed[4]
mixed[-1]
mixed[-1][1]
words
words[4]
words[3]
Just like strings lists also support few operators
[1, 1]*3
words
words*2
words*3
"one" + "two"
[0, 0, 0 ] + [1, 1]
empty = []
[0] # a list with only one item in it which is 0
Question How/when lists are used?
Lets say I want to send say hello to every participant in this class!
participant = ["akash", "hasneet", "aseet", "deepak", "saurabh"]
Similar to lists, there is another datatype called tuple. It is sibling of list.
color = (0, 0, 256) # round brackets
color[0]
color[-1]
color + color
color*5
participant[0]
participant[0] = "dhaval"
participant
color[0]
color
color[0] = 256 # tuples once created can not be modified
s
s[0] = "c"
In list you save things by sequence, i.e localtion is identity.
lets say we want to store ages of persons with known name. Disctionay store key and value.
age = {"rupali":20, "alice":16, "maya":18, "kavya":21}
age['rupali']
age['alice']
age['kavya']
age["rupali"] = 18
age
Dictionary can have only unique keys. SO anything that can be unique can be kept as key. Values can be anything.
scores = {"x":23, "y":22, "z":23, "m":21, "n":22}
scores
scores['x']
scores['z']
age['seema']
age
age['seema'] = 23
age['seema']
stock = {"name":"IMB", "open":123, "high":125, "low":120, "close":123.5}
stock['name']
stock['open']
stock['close']
l = [1, 1, 2] # creating we use square bracket
t = ( 1, 2, 3) # creating , we use round bracket
d = {"one":1 , "two": 2,"three": 3} # creating , we use curly bracket
l[0] # access is doone with []
t[0]
d['one']
stock = {"name":"IBM",
"open":"123", #string ... this comment
"high":125,
"low" :120,
"close":123.5
}
stock["open"] # this gives string/text not a number
stock['low'] # this gives, number
{"a":1 #this is a , "b":2}
{"a":1 , "b":2} #this is a as 1 and b as 2
True
False
Till now we are executing only some statements. But sometimes we need to make use of multiple statements. Then we might wan to resuse those statement. We might use same statemanets to compute items with different parameters. This is handled by making functions by putting the statements together. We will start with python's built in functions
name= "Rupali" # collection of characters
numbers = [1, 1, 2, 2, 1] # collections of numbers
point = (0, 0, 1) # collection of some numbers
stock = {"name":"IMB", "open":123, "high":125, "low":120, "close":123.5} # collections of some key value pairs
len(name) # lenght string/text
len(numbers) # lenght of list
len(point) # length of tuple
len(stock) # length of dictionary
name
type(name)
type(numbers)
type(point)
type(stock)
type(1)
type(1.2)
twentythree = "23"
int(twentythree) # converts string
int("42")
int(" 42 ")
str(42)
float("23.3")
max([23, 12, 23, 12, 34, 67, 34, 34])
min([23, 12, 23, 12, 34, 67, 34, 34])
words = ["one", "two", "three", "four", "five"]
max(words) # gives max by ASCCI order ... A-Z ..a-z
sum([12, 23, 1, 1, 1, 1])
max([23, 12, 23, 12, 34, 67, 34, 34])
str(max([23, 12, 23, 12, 34, 67, 34, 34]))# nested function call
str(67) # inner function will be executed first
'67'
problem
sum(["a","b","c","d"])
Solution 1
incomes = [123330, 250000, 45555, 232130, 11123]
sum(incomes)
Solution 2
42**42
42
p = 42**42
p
str(p)
len(str(42**42))
len(p) # length works only on sequences like str, list, tuple, dict
len(str(p))
Solution 3
max(incomes)
Solution 4
["a", "b", "c", "d"]
'a' +'b'
sum(['a', 'b','c','d'])
0 + "a"
sum([1, 2, 3])
subset of list can be accessed using list slicing
digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
digits[2:8:2]
lst[start:end:step]
digits[1:8:3] # start at index 1 , stop at index 8 (excluded..that means stop at 7), at steps of 3
rules to take default in list slicing
digits[:5] # take first 5
digits[2:] # drop first two
digits[::2] # take alternate items
digits[::-1] # reverse the list
digits
it is possible to generate complicated slicings using -ve numbers for start, stop and end. but that will make your code unredable. complicated to debug. Here are few useful and well accepted slicings , just make use of them.
import math
if you want to install jupyter-notebook on your system with python already installed. use following command from cmd
python3 -m pip install jupyter
then go to cmd and execute
jupyter notebook