Apr 15-18, 2019 Vikrant Patil
These notes are available online at http://notes.pipal.in/2019/arcesium_basic_aug/day1.html
© Pipal Academy LLP
We will be using python 3 (>= 3.0) from anaconda for this training. You can download it from
800000/12
30 + 45
45 -35
34 * 15
2**100
integers **bold**
2 + 3 #addition
2 -3 #substraction
3*4 # multiplication
3/5 #foating point division
5//2 #integer division
5%2 #mod
2 ** 10 #power operator
5/2
5//2
2**100 #the size of integer is taken care by python. it supports really big numbers
2**10000
floating point numbers , decimal numbers
2.5**4
3.6 /3
3.6//3
2 + 3*2 + 4**45*(2+1)
string
"arcesium"
2
'arcesium'
"arcesium's location"
'he said,"I am fine!"'
"\""
"star"*5
"*"*5
"hello" "python"
expression on right hand side is executed, result is computed and stored in memory.
a name is given to this computed result, which is mentioned on lft hand side of = operator
name = 2 + 4*(3-45)
x = 1
x
y = 3**5
y
z = x+y
x = 34
Example
a = 2**5
a
b = a
b
a
a = 3**5
a
b
**strings**
strings
star = "*"
fivestar = star*5
fivestar
print("Hello World!")
print(a)
print(a+b)
oneline = "This is just one liner"
print(oneline)
import this
poem = """
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
"""
print(poem)
multiplelines ="one\ntwo\nthree"
print(multiplelines)
multiplelines1 ="""
one
two
three
"""
print(multiplelines1)
multiplelines
print(multiplelines)
empty = ""
empty
print(empty)
lists
numbers = [1, 2, 3, 5, 8, 13]
nums = [12,13,14,15,16,11]
numbers[0] # zeroth item
numbers[1] # 1st item
numbers[3]
numbers[-1] # last item
numbers[-2] # second last
s = ['p','y','t','h','o','n']
s
s[0]
s[-1]
name = "arcesium"
name[0]
name[-1]
-> 0 1 2 3 4 5
P Y T H O N
-6 -5 -4 -3 -2 -1 <--
pre = "Hello"
post = "World"
"hello""world"
pre post
pre, post
pre + post
ones = [1,1,1,1,1]
ones * 2
ones + nums
table = [ones, ones, ones]
table
s
table = [ones, s, ones, s]
table
table[0]
table[-1]
s
p = "python"
s[0]
p[0]
p[-1]
s[-1]
s
p
person = {"name":"Alice", "age":14, "email":"alice@wonder.land"}
bse_sheet = [["company","ticker","value"],["Hindustan Computers","hcl",400.0],["Tata Motors","tata",80.3]]
nse_sheet = [["company","ticker","value"],["Hindustan Computers","hcl",500.0],["Tata Motors","tata",75.3]]
stocks = {"bse":bse_sheet, "nse":nse_sheet}
person['email']
stocks['bse']
person['name']
person['age']
a = 10
numbers = [a, 2*a, 3*a, 4*a, 5*a, 6*a, a*(a+1)]
numbers
person[0]
person['email']
stocks['bse'][1] # get 1st row from bse sheet
stocks['bse'][1][1] # get 1st item from 1st row of bse sheet
nums = [1, 1, 1, 1, 0]
nums
nums1 = (1, 1, 1, 1)
nums[0] = 0
nums
nums1[0] = 0
nums1
tuples are similar to lists in all way except that they are immutable (means can not be changed once created)
nums1[0]
nums1[-1]
a = 5
b = 3
a,b
x = a,b
x
x, y = 2,3
x
y
y,x = x,y
x
y
{1, 1, 1, 2, 3, 2, 1, 3}
s = {1, 1, 1, 2, 3, 2, 1, 3}
s
s[0]
2 in s # is 2 in set s?
5 in s # is 5 in set s?
x
y
x == y # check if x is equal to y?
x != y # check if x is not equal to y?
x is 3
x is not 3
"email" in person
"address" in person
person
person['email'] == "alice@wonder.land"
nums
1 in nums
5 in nums
emptydict = {}
type(emptydict)
emptylist = []
emptystring = ""
nothing = None
nothing
print(nothing)
print(nothing)
print(1,2,3)
len(nums)
nums
language = "Python"
len(language)
len(person)
person
primes = {2,3,5,7,11,13}
len(primes)
6 in primes
t = (1,2,3,4,5,6)
len(t)
len(person['name'])
int("2")
int("23232343235435")
float("2.4")
str(545)
str([1,2,3,4])
str("3.45")
sf = "4.5"
sf + 5.6
float(sf) + 5.6
sf + str(5.6)
probems
2**1002**100
s = str(2**100)
s
len(s)
len(str(2**100)) # inner function will be called first , then outer
sum([1, 2, 3, 4])
list("python")
list((2,3,4,5))
nums
t
list(t)
t
numbers = [1,2,3,4,5,6,7]
range(10)
list(range(10))
dict([("x",1),("y",2)])
list(range(5))
list(range(5,20))
list(range(5,20,3))
d = dict([("x",1),("y",2)])
d['x']
when to use which bracket?
All kinds of access is thorugh []
items = list(range(5))
items[0]
person['name']
t
t[0]
While creating lists , we use square bracket[]
l = [1,1,0,0,1]
While creating dictionary , we use {}
d = {"one":1,
"two":2,
"three":3,
"four":4
}
d
d['one'] # access is square bracket
While creating set , we use {}
s = {1,2,3,4}
While creating tuple , we use ()
t = (1,2,2,1,1)
special = (1,)
special
len(special)
While calling functions, we use () after function name
len(special)
len(d)
# defining a function
def add(a, b):
c = a + b
return c
x
x
add
add(5,6)
x
x = add(5,6)
def sumofsquares(a, b):
sa = a*a
sb = b*b
return sa + sb
def sumofsquares1(a, b):
return a**2 + b **2
sumofsquares
sumofsquares1
sumofsquares(3,4)
def len(x):
return 5
len(person)
len([1,2,3,4,5,6,7,8,9])
del len
len(person)
def len_(x):
return 5
len(person)
len_(34)
def squareroot(x):
return x**(1/2)
def genericroot(x, r):
return x**(1/r)
genericroot(4,2)
squareroot(4)
problems
count_digits which will take integer as an argument and return number of digits in that integer
>>> count_digits(1000)
4
>>> count_digits(2**100)
31
def count_digits(n):
return len(str(n))
count_digits(2**100)
def square(x):
return x*x
def square1(x):
print(x*x)
square(5)
square1(5)
square(square(5))
square1(square1(5))
def func():
pass
func()
r = func()
print(r)
print(square(5))
def add(x,y):
return x+y
add
f = add
print(f)
print(add)
add(3,4)
f(3,4)
def sumofsquares(a, b):
return sqauare(a) + square(b)
def cube(x):
return x*x*x
def sumofcubes(a,b):
return cube(a) + cube(b)
def sumof(a, b, f):
return f(a) + f(b)
sumof(3, 4, square)
sumof(3, 4, cube)
sumofcubes(3, 4)
max([23,23,1,2,23,12,45,67,34])
words = ["one","two", "three","four","five"]
max(words)
max(words, key=len)
stocks = [
("Infosys", 'infy', 1500.4, 50),
('Tata Motors', 'tata', 203.5, 3.0),
('Capgemini', 'cpgm', 500.0, 15.0),
('Spotyfy', 'sptf', 1600.0, -20.0),
('Simple Solutions','sls', 1200.0, 10.0)
]
max(stocks)
def get_stockvalue(row):
return row[2]
def get_gain(row):
return row[3]
max(stocks, key=get_stockvalue)
max(stocks, key=get_gain)
min(stocks, key=get_gain)
min(stocks, key=get_stockvalue)
help(filter)
def greater_than1000(row):
return row[2]>1000
2 > 3
2 >= 2
list(filter(greater_than1000, stocks))
sorted(words)
words
sorted(words, reverse=True)
sorted(words, key=len, reverse=True)
sorted(words, key=len)
Reference Python Practice Book