Aug 10-14, 2020 Vikrant Patil
These notes are available online at http://notes.pipal.in/2020/arcesium_finop_batch1_module1/day1.html
© Pipal Academy LLP
Day 1 | Day 2 | Day 3 | Day 4 | Day 5
We will be using jupyter hub from http://lab1.pipal.in for this training.
multiply two and five and write down result in your notebook
Esc + M -> converts the cell into markdown
Esc + y -> convertes the cell back to code cell
Shift + enter - > execute the current cell
Try this out!
### Header3
## Header2
# Header1
If you type this in markdown cell you should see similar to this
42 * 42
Very primitive way of looking at Programming is manipulating numbers and text. To do this python has got some primitives, which we will explore now
42 + 42
42 -42
42*2
5 /2
5 // 2
2 ** 5 # 2 raised to 5
5 % 2 # remainder when divided by 2
Python has two types of numeric data, integers and real numbers. real numbers are represented by float in python
1.1 + 1.1
1.0 - 0.1
1.8 *2.0
5.0 / 2
5.0 // 2.0
2.0 ** 5
These symbols +, -, *, **, /, // are called operators. When there are multiple operators in same statement , there a priority to which one will be executed first
2 + 4*5
operator priority
** 1
% // / * 2
+ - 3
For example have alook at complicated arithmatic expression in python
2 ** 5%5/2*7
32%5/2*7
2/2*7
1*7
7
Round braces help us to modify the precedence of execution.
(7+2)*3
7+2*3
Now have a look at text data in python. Anything between two " or ' is called at string. And it is text data representation in python
"Hello this is text"
'hello this is text'
Question Does division operator return integer when integer divided integer?
No, It returns float always.
2/2
4/4
2**5
Python supports multiline text too. As we will see later python supports writing human friendly code writing! ANything that starts with triple quotes " or ' and ends with triple quote is multiline string.
"""This is first line of my poem
This is second line
And this is the last one"""
'''This is another text
Which has got multiple line
yet another line'''
This character \n is called as new line character. There are ohter characters too which are special like new line. They are
represented by escape sequence.
escape char meaning
\n new line
\t tab
\\ \
Strings work with some operators as well
"*"
"*"*5
"hello" + "world!"
Problem
Use python to convert asset value, 20345.5 , originally in EUR to INR
Problem
Compound interest is calculated using formula P(1+r/n)^nt , P is principle amount, r is nominal rate of interest per year. n is the number of times in a year the interest is compounded and t denotes number of years. Use python compound interest for principle amount of 26780, rate of interest 7%, interst is compunded quarterly, and amount is invested for 5 years.
88.35*20345.5
26780*(1+0.07/4)**(4*5)
As of now we saw arithmatic operators , now let's have a look at an operator called as assignment operator =. It works like this. Suppose you write a statement which involves assignment operator
x = 2 + 3
The stetement on right hand side is computed first, the result is stored in python's memory. Then there is something called as namespace, in this namespace a new name x is created. and then this name is linked to the memory loction that we already have for the result.
x
x
Question
How do you delete x? can we delete 5 stored on x?
you can delete x using statement del x , this will delete only name x form namespace. it will not delete 5. When to delete 5 is decide by python, user can not control.
del x
x
10
5 # literal
x = 5
x # variable
Be careful with string literals
vikrant = 10
"vikrant" # this is literal and not a variable
"vikrant"
vikrant
python training = 10
What are the rules by which variable name can be denifed?
2x = 3
x2 = 3
square of two = 2*2
square_of_two = 2*2
square_of_two
a = 10
a, b = 10, 20
a
b
problem
Have a look at following statements.
x = 10
y = x
x = x+ 10
What will be values y after this?
problem
Have a look at following statements.
x = 10
y = x
y = 25
What will be values x after this?
Let's do more with strings
s = "Hello"
s[0]
s[1]
s[3]
s[4]
s[5]
s[-1]
p y t h o n
-> 0 1 2 3 4 5
-6 -5 -4 -3 -2 -1 <-----
Other than basic data types we freequently need some data which need puting basic data in a sequence. Lists and tuples are such highe level datatypes which allows us to put items in a sequence.
[1, 1, 1]
numbers = [1, 2, 3, 4]
words = ["one", "two", "three", "four"]
mixed = [1, "one", 2, "two", 3, "three", 4, "four"]
numbers[0]
numbers[-1]
numbers[3]
mixed[-1]
list_inside_list = [[1, 2, 1], ["one", "two", "three"]]
list_inside_list[0]
list_inside_list[0][-1]
words
words[0] = "zero"
words
words[-1] = "last"
words
Just like strings lists can also work with some basic operators
words + words
words + numbers
[0]*5
Sibling of list is tuple. Very similar to lists, but can not be modified once created.
color = (0, 0, 1)
color[0]
color[-1]
color[0] = 1
color + color
color * 3
Lists and tuples allows us to save data by index. Dictionary is another higher level data type which allows us to save items by name.
scorebynames = {"rupali":20, "alice":19, "Elsa":18, "kavita":20}
scorebynames[0]
scorebynames['alice']
scorebynames["Elsa"]
scorebynames['seema']
scorebynames['seema'] = 20
scorebynames
stock = {"name":"IBM", "open":123, "high":126, "low":120, "close":123.5}
stock['name']
stock['open']
stock['close']
True
False
Functions are abstraction to combine multiple staements together and save it for later use.
name = "Rupali"
len(name)
len(name)
len(stock)
stock
point = (0 , 0, 0)
len(point)
len(numbers)
numbers
len can be used to find number items in any collections including string(number of characters)
name
type(name)
type(numbers)
type(point)
type(stock)
type(1)
type(1.2)
Lets try to convert string to numbers and the other way
str(42)
int(' 23')
int('42')
float("23.4")
max function allows us to find maximum number from a list/tuple
max([2, 1, 3, 1, 45, 24, 23])
min([2, 1, 3, 1, 45, 24, 23])
sum([2, 1, 3, 1, 45, 24, 23])
Question
What if list contains strings/words? what will max return?
It will return last item if items are arranged in ASCII order (similar to dictionary but slightly different )
words = ["B", "b","A", "a", "C", "c"]
max(words)
min(words)
problem
Use python to find total income of a person whic has five income sources giving income of 123330, 250000, 45555, 232130, 11123
problem
Find out how many digits are there in 2**42
problem
Using python functions find out highest income source in above example.
problem
Will this work?
sum(["a","b","c","d"])
sum([1, 2, 3, 4])
sum(["a","b","c","d"])
sum?
sum([1, 1, 1, 1], 5)
sum(["a","b","c","d"], start="")
^
incomes = [123330, 250000, 45555, 232130, 11123]
total_income = sum(incomes)
total_income
max_income = max(incomes)
max_income
2**42
len(2) # len works only on collections!
strvalue = str(2**42)
len(strvalue)
len(str(2**42))
len(str(4398046511104)
len('4398046511104')
13
digits = [0 , 1, 2, 3, 4, 5, 6, 7, 8, 9]
digits[2:8:2] #start at 2 end at 8 (exluded) with step of 2
digits[2:8:3]
If any of the parameter in slicing rule is not given , then default is taken.
digits[2:6] # be default step is taken as 1
digits[2:] # defualt end is taken as end of list
digits[:5] # start is not given , zero is taken as start
digits[:4] ## take first 4 items
digits[3:] ## drop first 3 items
digits[::] # copy the list
digits[::-1] ## reverse
print("Hello World")
name = "Rahul"
name
print(name)
print("hello", name, "How are you doing?" )
x = input("Give value of x:")
print(x)
sorted([3, 2, 1, 4, 1, 1])
print("hello"+"world")
print("hello", "world", sep="")
print("hello", "world", sep="-")
print("hello", name, "How are you doing?", sep="-")
x = input()
type(x)
nums = [3, 2, 1, 4, 1, 1]
sorted(nums)
nums
sorted(nums, reverse=True)
print("hello", "world", "-")
print("hello", "world", sep = "-")
sorted(nums, True)
sorted(nums, reverse=True)