Oct 25-31, 2018 Vikrant Patil
These notes are available online at http://notes.pipal.in/2018/arcesium-basic-oct/day2.html
© Pipal Academy LLP
We will be using python 3 (>= 3.0) from anaconda for this training. You can download it from
x = 10
def func(x):
x = 2*x
print("x: inside func =", x)
return x
func(30)
print("x: outside func = ", x)
print(1, "hello", "world!", ".....",end=",")
print(2, "hello", "world!", ".....",)
func(x)
print("x: outside func=",x)
def appendone(data):
data.append(1)
return data
l = [1,2,3,4,5]
appendone(l)
print(l)
x = func(30)
print(x)
x
def xadder(y):
return x+y
xadder(20)
def xmodify():
x = x + 30
print("x: inside xmodify = ", x )
x = 50
xmodify()
print(x)
def xmodify():
global x
x = x + 30
print("x: inside xmodify = ", x )
x = 50
xmodify()
print(x)
def xmodify():
global x
x = x + 30
print("x: inside xmodify = ", x )
del x
xmodify()
print(x)
problem
make_sentence to make sentence out of list of wrods.>>> make_sentence(["Hi", "there", ",", "how", "are", "you", "doing?"])
'Hi there , how are you doing?'
>>> make_word(["p","y","t","h","o","n"])
'python'
def make_sentence(words):
return " ".join(words)
def make_word(chars):
return "".join(chars)
make_sentence(["Hello","world","pyhton"])
make_word(["p","y","t","h","o","n"])
make_word(["1","2","3"])
def make_int(digits):
strnum = make_word(digits)
return int(strnum)
x = make_int(["1","2","3"])
x
x + 2
"123" + 2
def make_adder(x):
def adder(y):
return x+y
return adder
adder5 = make_adder(5)
print(adder5)
adder5(10)
adder5(20)
adder10 = make_adder(10)
adder10(12)
adder5(12)
import os
import math as m
os.getcwd()
os.getlogin()
os.listdir()
os.listdir("/tmp/")
os.path.exists("/tmp/")
os.path.exists("c:\\")
os.path.getsize("day1.ipynb")
"\t"
os.path.abspath("day1.html")
contents = os.listdir()
contents
max(contents, key=len)
max(contents, key=os.path.getsize)
problem
def countfiles():
return len(os.listdir())
countfiles()
def countfiles(dirpath):
return len(os.listdir(dirpath))
countfiles(".")
#os.path.join("c:", "")
os.path.join("/home","vikrant", "training")
os.path.isfile("day1.html")
os.path.isdir("/tmp")
import numpy
import math
math.pi
math.sin(math.pi)
math.ceil(2.3)
os.mkdir("test")
os.listdir()
%%file mymodule.py
def hello(name):
print("Hello", name)
import mymodule
mymodule.hello("pyhton")
from mymodule import hello
hello("Arcesium")
%%file bank.py
balance = 0
def get_balance():
return balance
import bank
bank.balance
bank.get_balance()
bank.balance = 100
bank.get_balance()
import sys
%%file square.py
import sys
def square(x):
return x*x
print(sys.argv)
num = int(sys.argv[1])
sqr = square(num)
print(sqr)
!python square.py 67
%%file add.py
import sys
def add(x,y):
return x+y
a = int(sys.argv[1])
b = int(sys.argv[2])
print(add(a,b))
!python add.py 1 300
%%file echo.py
import sys
def echo(expr):
print(expr)
expr = sys.argv[1]
print(sys.argv)
echo(expr)
!python echo.py hello world
!python echo.py hello\ world
!python echo.py "hello world"
!echo hello echo this all
digits = [1,2,3,4,5,6]
digits[:]
digits[1:]
%%file echo.py
import sys
def echo(expr):
print(expr)
expr = " ".join(sys.argv[1:])
echo(expr)
!python echo.py hello echo this statement
"hel" in "hello"
"alice" in {"alice":1, "alex":2}
1 in [1,2,3,4,5]
filename = "hello.py"
if ".py" in filename:
print("python")
filename = "hello.exe"
if ".py" in filename:
print("python")
else:
print("something else")
filename = "hello.exe"
if ".py" in filename:
print("python")
elif ".exe" in filename:
print("executable")
elif ".xlsx" in filename:
print("excelsheet")
else:
pass
%%file backup.py
import sys
def delete_old(path):
print("Deleted old data")
def copy_new(src, dest):
print("Copying new data to backup location")
src = sys.argv[1]
dest = sys.argv[2]
delete_old(dest)
copy_new(src, dest)
import backup
%%file backup1.py
import sys
def delete_old(path):
print("Deleted old data")
def copy_new(src, dest):
print("Copying new data to backup location")
src = sys.argv[1]
dest = sys.argv[2]
print(__name__)
#delete_old(dest)
#copy_new(src, dest)
import backup1
!python backup1.py src dest
%%file backup2.py
import sys
def delete_old(path):
print("Deleted old data")
def copy_new(src, dest):
print("Copying new data to backup location")
if __name__ == "__main__":
src = sys.argv[1]
dest = sys.argv[2]
delete_old(dest)
copy_new(src, dest)
import backup2
!python backup2.py /home/vikrant/trainings/ /tmp/
problem
min2 to find minimum from two numbersmin3 to find minimum from threee numbers2 > 3
2 <= 3
2 == 2
def min2(x, y):
if x < y:
return x
else:
return y
def min3(x, y, z):
return min2(min2(x,y),z)
min3(34,56,1)
math.sqrt(min2(3,56) + sum([1,2,3]))
3 * 100 + 20
n = 10
while n > 0:
print(n, end=",")
n = n - 1 # n -= 1
def sum_(seq):
s = 0
pos = len(seq)
while pos > 0:
pos = pos-1
print(pos, end=",")
s = s + seq[pos]
return s
sum_([12,12,34,1,2,1])
sum([12,12,34,1,2,1])
digits = [0,1,2,3,4,5]
for digit in digits:
s = digit*digit
print(digit)
for i in digits:
print(i)
people = ["Anand", "David", "Alex", "Alia", "Elsa"]
for person in people:
print(person)
for name in people:
print(name)
for c in "some random statement":
print(c,end=",")
morepeople = ["girish", "ashok", "ankit", "aniket", "dua"]
def commonfrom(collection1, collection2):
common = []
for person in collection1:
if person in collection2:
common.append(person)
return common
commonfrom(morepeople, people)
morepeople.append("Anand")
commonfrom(morepeople, people)
!ls
problem
print(os.listdir())
%%file ls.py
import os
def ls():
for file in os.listdir():
print(file)
if __name__ == "__main__":
ls()
!python ls.py
problem
for i in range(10):
print(i)