Transcript

Python meetup -2Vic Yang

How to run a program?

Python Virtual Machine

Program Execution

In interactive shell:

print ‘hello world’ # hello world

print 2 ** 100# 1267650600228229491496703205376

In command line:

python hello.py

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

Python Virtual Machine

.py .pyc PVM

source byte code execution

PVM is a huge loop that executes our code one by one.

.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.

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

Input commands

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

Module import and reloadscript4.py

print ‘hello world’

print 2 ** 100

title = ‘The Meaning of life’

IDLE

>>>import script4

>>>reload(script4)

>>>print script4.title

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

Practice

PracticeFibonacci sequence

CheckIO

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()

top related