python meetup 2
DESCRIPTION
NTNUTRANSCRIPT
![Page 1: Python meetup 2](https://reader030.vdocuments.site/reader030/viewer/2022013108/555822cbd8b42a25588b4c61/html5/thumbnails/1.jpg)
Python meetup -2Vic Yang
![Page 2: Python meetup 2](https://reader030.vdocuments.site/reader030/viewer/2022013108/555822cbd8b42a25588b4c61/html5/thumbnails/2.jpg)
How to run a program?
Python Virtual Machine
![Page 3: Python meetup 2](https://reader030.vdocuments.site/reader030/viewer/2022013108/555822cbd8b42a25588b4c61/html5/thumbnails/3.jpg)
Program Execution
In interactive shell:
print ‘hello world’ # hello world
print 2 ** 100# 1267650600228229491496703205376
In command line:
python hello.py
![Page 4: Python meetup 2](https://reader030.vdocuments.site/reader030/viewer/2022013108/555822cbd8b42a25588b4c61/html5/thumbnails/4.jpg)
Python Virtual Machine.py VS. .pyc
1. .py is the source code of Python
2. .pyc is the byte-compiled code of .py
3. .pyc can be executed in PVM
![Page 5: Python meetup 2](https://reader030.vdocuments.site/reader030/viewer/2022013108/555822cbd8b42a25588b4c61/html5/thumbnails/5.jpg)
Python Virtual Machine
.py .pyc PVM
source byte code execution
PVM is a huge loop that executes our code one by one.
![Page 6: Python meetup 2](https://reader030.vdocuments.site/reader030/viewer/2022013108/555822cbd8b42a25588b4c61/html5/thumbnails/6.jpg)
.pyc
A file contains byte-code interpreted from .py
Boost the execution time.
Rebuild automatically if .py changed.
The text in .pyc file is not binary code.
![Page 7: Python meetup 2](https://reader030.vdocuments.site/reader030/viewer/2022013108/555822cbd8b42a25588b4c61/html5/thumbnails/7.jpg)
Python Implementation
CPython - ported from ANSI C, standard implementation.
Jython - compiled Python code to Java byte-code and executed on JVM
IronPython - similar as Jython implemented
![Page 8: Python meetup 2](https://reader030.vdocuments.site/reader030/viewer/2022013108/555822cbd8b42a25588b4c61/html5/thumbnails/8.jpg)
Input commands
![Page 9: Python meetup 2](https://reader030.vdocuments.site/reader030/viewer/2022013108/555822cbd8b42a25588b4c61/html5/thumbnails/9.jpg)
Inputstandard input
a = “hello world”
print a
a = raw_input()
print a
sys.argv[1]
import sys
print sys.argv[1]
> python script1.py
script1.py
![Page 10: Python meetup 2](https://reader030.vdocuments.site/reader030/viewer/2022013108/555822cbd8b42a25588b4c61/html5/thumbnails/10.jpg)
Module import and reloadscript4.py
print ‘hello world’
print 2 ** 100
title = ‘The Meaning of life’
IDLE
>>>import script4
>>>reload(script4)
>>>print script4.title
![Page 11: Python meetup 2](https://reader030.vdocuments.site/reader030/viewer/2022013108/555822cbd8b42a25588b4c61/html5/thumbnails/11.jpg)
Module import and reloadthreenames.py
a = ‘dead’
b = ‘parrot’
c = ‘sketch’
print a, b ,c
IDLE
>>>import three names
>>>threenames.b,
threenames.c
>>>from threenames import a, b, c
![Page 12: Python meetup 2](https://reader030.vdocuments.site/reader030/viewer/2022013108/555822cbd8b42a25588b4c61/html5/thumbnails/12.jpg)
Practice
![Page 13: Python meetup 2](https://reader030.vdocuments.site/reader030/viewer/2022013108/555822cbd8b42a25588b4c61/html5/thumbnails/13.jpg)
PracticeFibonacci sequence
CheckIO
![Page 14: Python meetup 2](https://reader030.vdocuments.site/reader030/viewer/2022013108/555822cbd8b42a25588b4c61/html5/thumbnails/14.jpg)
Fibonacci sequencedef fib():
x = 0
y = 1
while 1:
yield x
x, y = y, x + y
!
g = fib()
for i in range(9):
print g.next()