1. Trang chủ
  2. » Công Nghệ Thông Tin

OSC python programming pot

121 353 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 121
Dung lượng 358,93 KB

Nội dung

Python Programming Science & Technology Support Group 20-21 February 2007 Python Programming Instructor Peter G. Carswell Supercomputer Resource Specialist Ohio Supercomputer Center pete@osc.edu (614) 292-1091 3Python Programming 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 4Python Programming What is Python? • NOT an acronym (Thank goodness!). Named after Monty Python • A compiled/interpreted mid-level language – Not only used for scripting tasks •Extremelyuseful 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 5Python Programming 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 7Python Programming 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 $ 8Python Programming 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 $ 9Python Programming 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 [...]... objects … Python Programming 11 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 Python Programming 12 Numbers in Python Type Examples Decimal Integers 10 -235 Octal and Hexadecimal Integers 034723 0x3DE56A Long Integers (unlimited size) 777888207468890L Floating-Point 3.45 Complex 6.2+3J Python Programming. .. y Addition, Subtraction x-y Python Programming 14 Operator Precedence & Parenthetical Override $ python Python 2.2.3 (#1, Oct 26 2004, 17:11:32) [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-47)] on linux2 Type "help", "copyright", "credits" or "license" for more information >>> a=40 >>> b=3 >>> c=75 >>> a*b+c 195 >>> a*(b+c) 3120 >>> $ Python Programming 15 Python Division Python 2.2.3 (#1, Oct 26 2004,... well 3.0 >>> divmod(9,4) # Returns (x/y,x%y) tuple (2,1) $ Python Programming 16 Alternative Integer Bases >>> num=4578 >>> int(num) 4578 >>> int(67.345) 67 >>> round(67.899) 68.0 >>> oct(num) '010742' >>> hex(num) '0x11e2' >>> #Truncating Conversion #Can “round-up” #Octal literals have leading 0 #Hex literals have leading 0x Python Programming 17 Python Long Integers • Long integers marked with a trailing... twice We will see later that some Python variables can are quite large – This shortened syntax will cause the compiler to automatically pick the optimized technique for performing the combined operations This advantage will have a greater effect on the more sophisticated data types to come • Note for C/C++ users: no increment/ decrement operators (++, ) in Python Python Programming 21 “Augmented” Assignment... Truth shall set you free >>> Python Programming 25 String Operators Expression What Happens str1 + str2 Concatenation str *n Replication (n is an integer) n*str str[i] String element with index i str[i:j] Substring of consecutive elements (i to j-1) len(str) Number of characters in a string max(str) Maximum element (ASCII value) min(str) Minimum element (ASCII value) Python Programming 26 Working with... print rur[-3] # Negative index => relative to end 3 # of string [ length(rur)+i ] >>> print rur[-11:-4] Droid Python Programming 27 Finish theWork >>> print len(rur) 19 >>> print max(rur) t >>> print min(rur) # Number of elements # ASCII code for ‘t’= 116 # ASCII code for (space) ‘ ‘= 32 >>> Python Programming 28 Strings playing well with others … • • • The addition symbol ‘+’ is overloaded: it can add... wait for input Clever way of encouraging prompts Prompts are optional, though All input entered will be treated as a string by your Python program Must use the conversion commands on the previous slide for other types Python Programming 33 The read.py Script #!/usr/bin /python year=raw_input("Please enter a year:") year=int(year) #convert input string to int ten=year+10 print "A decade later will be",... r=(x**2+y**2)**0.5 print "Distance from origin is", r Python Programming 34 An Input Session $ read.py Please enter a year:1963 A decade later will be 1973 Enter x coord: 3.0 Enter y coord: 4.0 Distance from origin is 5.0 $ read.py Please enter a year:2260 A decade later will be 2270 Enter x coord: 13.4 Enter y coord: 56.2 Distance from origin is 57.7754273026 Python Programming 35 More Refined Input • There is... variable from the variable_list Example: print ”Out %15s %5d %10.2f\n” % (a,b,c) Control string – List of items to be printed See printf man page for further details >> man printf Python Programming 32 Interlude: Can we provide input to Python programs? • • • Yes The most basic of reading commands is called (aptly enough) raw_input() When raw_input() is executed, the program stops and waits for input from... precision In this #case ~15 decimal places (8 byte word) >>> print power 1.23114441334 >>> "%e" % power '1.231144e+00' >>> "%1.5f" % power '1.23114' >>> Python Programming 19 Complex Numbers • A feature* of some implementations of complex numbers in python is that the coefficient must be represented in decimal format, i.e 5.0j not 5j *feature is a term used by programmers to make excuses for programs . 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 5Python Programming Basic Operation • Python is both an interpreted. prog.pyc Python Programming Running Python 7Python Programming To use Python: Interactive • Type python in a shell window and start typing in commands at the Python prompt >>>. The results. Center pete @osc. edu (614) 292-1091 3Python Programming Table of Contents • Introduction • The “Not-So-Full” Monty – Mechanics of using Python – Variables, Data Types and Operators – Programming

Ngày đăng: 02/08/2014, 10:21

TỪ KHÓA LIÊN QUAN