14
Python meetup -2 Vic Yang

Python meetup 2

Embed Size (px)

DESCRIPTION

NTNU

Citation preview

Page 1: Python meetup 2

Python meetup -2Vic Yang

Page 2: Python meetup 2

How to run a program?

Python Virtual Machine

Page 3: Python meetup 2

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

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

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

.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

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

Input commands

Page 9: Python meetup 2

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

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

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

Practice

Page 13: Python meetup 2

PracticeFibonacci sequence

CheckIO

Page 14: Python meetup 2

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