How to write a text file from jypyter notebook! %%file filename
%%file hello.py
"""This is python file written from jupyter notebook
"""
import sys
def say_hello(name):
print("Hello", name.capitalize())
name = sys.argv[1]
say_hello(name)
Overwriting hello.py
How to run command from jupyter notebook!
!python hello.py pccoe
!python hello.py pccoe
Hello Pccoe
import hello
Hello -f
%%file fib.py
import sys
def fib(n):
"""Find nth fibonacci number
"""
curr, prev = 1, 1
count = 1
while count < n:
curr, prev = curr+prev, curr
count += 1
return curr
n = int(sys.argv[1])
print(fib(n))
Overwriting fib.py
!python fib.py 10
89
import fib
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-25-7d018bf75fbf> in <module> ----> 1 import fib ~/trainings/2021/pccoe-python-ml/fib.py in <module> 12 return curr 13 ---> 14 n = int(sys.argv[1]) 15 print(fib(n)) ValueError: invalid literal for int() with base 10: '-f'
def fib(n):
"""Find nth fibonacci number
"""
curr, prev = 1, 1
count = 1
while count < n:
curr, prev = curr+prev, curr
count += 1
return curr
%%file fib1.py
import sys
def fib(n):
"""Find nth fibonacci number
"""
curr, prev = 1, 1
count = 1
while count < n:
curr, prev = curr+prev, curr
count += 1
return curr
print(__name__)
#n = int(sys.argv[1])
#print(fib(n))
Writing fib1.py
!python fib1.py
__main__
import fib1
fib1
%%file fib2.py
import sys
def fib(n):
"""Find nth fibonacci number
"""
curr, prev = 1, 1
count = 1
while count < n:
curr, prev = curr+prev, curr
count += 1
return curr
if __name__ == "__main__":
n = int(sys.argv[1])
print(fib(n))
Writing fib2.py
!python fib2.py 15
987
import fib2
fib2.fib(10)
89
problems
!python mysum.py 1 2 3 4 5
15
>>> import mysum
>>>
numbers = [1, 2, 3, 4, 5, 6]
numbers[1:] # drop first
[2, 3, 4, 5, 6]
numbers[:3] # take first three
[1, 2, 3]
%%file mysum.py
import sys
def mysum(*args):
return sum(args)
if __name__ == "__main__":
mysum(sys.argv[1:])
Overwriting mysum.py
!python mysum.py 1 2 3 4
Traceback (most recent call last):
File "mysum.py", line 8, in <module>
mysum(sys.argv[1:])
File "mysum.py", line 4, in mysum
return sum(args)
TypeError: unsupported operand type(s) for +: 'int' and 'list'
%%file mysum.py
import sys
def mysum(*args):
"""this expects variable number of arguments!
"""
return sum(args)
def to_int(strnums):
return [int(strn) for strn in strnums]
if __name__ == "__main__":
mysum(*to_int(sys.argv[1:]))
Overwriting mysum.py
!python mysum.py 1 2 3 4 5 6 7
%%file mysum.py
import sys
def mysum(*args):
"""this expects variable number of arguments!
"""
return sum(args)
def to_int(strnums):
return [int(strn) for strn in strnums]
if __name__ == "__main__":
r = mysum(*to_int(sys.argv[1:]))
print(r)
Overwriting mysum.py
!python mysum.py 1 2 3 4 5
15
import mysum
mysum.mysum(1, 2, 3, 4)
10
for i in range(1, 11):
print(i, i**2, i**3)
1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000
for i in range(1, 11):
print(str(i).rjust(2), str(i**2).rjust(3), str(i**3).rjust(4))
1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000
"Answer to {} is {}".format("life", 42)
'Answer to life is 42'
"Answer to {1} is {0}".format("life", 42)
'Answer to 42 is life'
"Wizard of {place} is {name}".format(place="oz", name="python")
'Wizard of oz is python'
template = """
<html>
<header>
{HEADER}
</header>
<body>
{BODY}
</body>
</html>
"""
BODY = """
Hi all,
Welcome to my website. This is
my first blog entry on my website.
Thank you for visiting.
Please write you comments below
"""
print(template.format(HEADER="My first blog", BODY=BODY))
<html> <header> My first blog </header> <body> Hi all, Welcome to my website. This is my first blog entry on my website. Thank you for visiting. Please write you comments below </body> </html>
print(template.format(HEADER="My first blog", BODY=BODY))
<html> <header> My first blog </header> <body> Hi all, Welcome to my website. This is my first blog entry on my website. Thank you for visiting. Please write you comments below </body> </html>
for i in range(1, 11):
print("{num} {sqr} {cube}".format(num=i, sqr=i*i, cube=i**3))
1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000
for i in range(1, 11):
print("{num:2} {sqr:3} {cube:4}".format(num=i, sqr=i*i, cube=i**3))
1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000
for i in range(1, 11):
print("{num:{w}} {sqr:{w}} {cube:{w}}".format(num=i, sqr=i*i, cube=i**3, w=4))
1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000
problem
>>> pretty_print(6)
*
* *
* * *
* * * *
* * * * *
* * * * * *