Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

OSC python programming pot
MIỄN PHÍ
Số trang
121
Kích thước
358.9 KB
Định dạng
PDF
Lượt xem
1552

OSC python programming pot

Nội dung xem thử

Mô tả chi tiết

Python Programming

Science & Technology

Support Group

20-21 February 2007

Python Programming

Instructor

Peter G. Carswell

Supercomputer Resource Specialist

Ohio Supercomputer Center

[email protected]

(614) 292-1091

Python Programming 3

Table of Contents

• Introduction

• The “Not-So-Full” Monty

– Mechanics of using Python

– Variables, Data Types and Operators

– Programming statements

– Python Functions

– Using Python Modules

– Everything is an Object

– Classes and Objects

– Operator Overloading

– Constructors

Python Programming 4

What is Python?

• NOT an acronym (Thank goodness!). Named after Monty Python

• A compiled/interpreted mid-level language

– Not only used for scripting tasks

• Extremely useful for a huge variety of programming tasks (Modules)

• Easy to “glue” with other languages (C, C++, Java …)

• Under the hood: Object Oriented

• Commonly in use: Versions 2.3 and 2.4

– Use python –V to see the version

• Python is portable

• Python is free!

• Home Page: www.python.org

Python Programming 5

Basic Operation

• Python is both an interpreted and a compiled language

• When run, the program is first read and “compiled” in memory

– Not a true compilation to native machine instructions

– Python source code converted to byte code (platform-independent)

– Some optimizations are performed, e.g.

• eliminating unreachable code

• reducing constant expressions

• loading library definitions

• Second stage is line-by-line execution via the interpreter PVM (Python Virtual

Machine)

– analogous to Java VM

• Much faster than fully interpreted languages, such as the shell

• No object code

– But byte code saved in a file called prog.pyc

Python Programming

Running Python

Python Programming 7

To use Python: Interactive

• Type python in a shell window and start typing in commands at the Python

prompt >>>. The results of the commands will be seen immediately.

• Workshop examples will be for Python on Unix/Linux system.

[piv-login1]% python

Python 2.2.3 (#1, Feb 2 2005, 12:20:51)

[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-49)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>>

>>> print "Hello World! (with respect)“ #Basic output command

Hello World! (with respect)

>>> a=34.5 # No variable type declaration

>>> a # To see contents of variable, type its name

34.5

>>> a*5.6

193.2

>>> z=a/33.3

>>> print '%7.2f' %(z) #Formatting Output

1.04

>>> ^D #How to exit the Python session

$

Python Programming 8

Ways to use Python: Shell Scripts

• A shell script is just a series of python commands entered line-by-line into a file.

By typing the name of the shell script after the python executable, all the

commands will be run in order.

• By convention files composed of Python commands have the suffix py. Let’s say

all the commands on the previous slide have been put into a file rabbit.py.

Here’s how it would be run:

Notice that only the text directly written out to stdout will appear on your monitor

$ python rabbit.py

Hello World! (with

respect)

1.04

$

Python Programming 9

Ways to use Python: Executable Scripts

• Say you want to run your Python script directly at the system prompt. (Just

like an operating system command). This requires two steps

– Make the first line in the rabbit.py file

#!<full pathname of Python executable>

– Second, give rabbit.py executable permission

$ chmod u+x rabbit.py

• Now the rabbit file looks like this:

• To run the commands in the rabbit.py file, just type its name at the system

prompt

#!/usr/local/bin/python

print "Hello World! (with respect)"

a=34.5

a

a*5.6

z=a/33.3

print '%7.2f' %(z)

$ rabbit.py

Hello World! (with respect)

1.04

Python Programming

Variables, Data Types and Operators

Python Programming 11

Variables

• Pick any name you want as long as it begins with a letter or underscore

• Don’t have to declare the variable name or type

• Variable “comes into existence” when it is assigned a value

• Case-sensitive

• Actually much more to variable assignments. Fascinating approach when

we get to objects ….

Python Programming 12

Data Types

• Python has five built-in, core data types. Listed here in the order in which they

will be covered

Numbers

Strings

Lists

Dictionaries

Tuples

Tải ngay đi em, còn do dự, trời tối mất!