Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
An Introduction to Python Dr. Nancy Warter-Perez April 15, 2004 Overview Overview of program development Python Basics Python Types and Operators Numbers and Arithmetic operators Strings Lists Dictionaries Input & Output Example amino acid search program Programming Workshop #1 4/15/04 Introduction to Python 2 Programming Language and Development Software In this program, we’ll use Python Development software 4/15/04 Interpretive Language IDLE python gui Pythonwin (recommended) Do your work on either the hard disk or zip disk (not floppy disk, A: drive – too slow!) Introduction to Python 3 Program Development Problem solving Problem specification Algorithm design Test by hand Implementation Code in target language Test code / debug Program 4/15/04 Introduction to Python 4 Python Basics - Comments Python comments # line comment Header comments #Description of program #Written by: #Date created: #Last Modified: 4/15/04 Introduction to Python 5 Python Basics - Variables Python variables are not “declared”. To assign a variable, just type: identifier=literal Identifiers Have the following restrictions: Must start with a letter or underscore (_) Case sensitive Must consist of only letters, numbers or underscore Must not be a reserved word (LP pg 137) Have the following conventions: All uppercase letters are used for constants Variable names are meaningful – thus, often multi-word Python specific conventions: 4/15/04 Convention 1: alignment_sequence Convention 2: AlignmentSequence Avoid _X, __X__, __X, _, (LP pg 138) Introduction to Python 6 Numbers Numbers Normal Integers –represent whole numbers Ex: 3, -7, 123, 76 Long Integers – unlimited size Ex: 9999999999999999999999L Floating-point – represent numbers with decimal places Ex: 1.2, 3.14159,3.14e-10 Octal and hexadecimal numbers Ex: O177, 0x9ff, Oxff Complex numbers Ex: 3+4j, 3.0+4.0j, 3J 4/15/04 Introduction to Python 7 Python Basics – arithmetic operations Operators Example y=5; z=3 + add x=y+z x - subract x=y–z x * multiply x=y*z x / divide x=y/z x % modulus/remainder x = y % z x 4/15/04 Introduction to Python = = = = = 8 2 15 1 2 8 Python Basics – arithmetic operations Operators << shift left >> shift right ** raise to power 4/15/04 Example y=5; z=3 x = y << 1 x = 10 x = y >> 2 x = 1 x = y ** z x = 125 Introduction to Python 9 Python Basics – Relational and Logical Operators Relational operators == !=, <> > >= < <= 4/15/04 equal not equal greater than greater than or equal less than less than or equal Logical operators and or not Introduction to Python and or not 10 Python Basics – Relational Operators Assume x = 1, y = 4, z = 14 Expression Value Interpretation x<y+z 1 True y == 2 * x + 3 0 False z <= x + y 0 False z>x 1 True x != y 1 True 4/15/04 Introduction to Python 11 Python Basics – Logical Operators Assume x = 1, y = 4, z = 14 Expression Value Interpretation x<=1 and y==3 0 False x<= 1 or y==3 1 True not (x > 1) 1 True not x > 1 0 False not (x<=1 or y==3) 0 False 4/15/04 Introduction to Python 12 Strings Enclosed in single or double quotes Ex: ‘Hello!’ , “Hello!”, “3.5”, “a”, ‘a’ Sequence of characters: mystring=“hello world!” mystring[0] -> “h” mystring[1] -> “e” mystring[2] -> “l” mystring[-1] -> “!” -1 is last, -2 next to last, etc… 4/15/04 Introduction to Python 13 String operations mystring = “Hello World!” Expression Value Purpose len(mystring) 12 number of characters in mystring “hello”+“world” “helloworld” Concatenate strings “%s world”%“hello” “hello world” Format strings (like sprintf) “world” == “hello” “world” == “world” 0 or False 1 or True Test for equality “a” < “b” “b” < “a” 1 or True 0 or False Alphabetical ordering 4/15/04 Introduction to Python 14 Strings (2) substrings can be reassigned: mystring=“spoons” mystring[5]=“!” mystring -> “spoon!” slicing: mystring[2:] -> “oon!” mystring[:3] -> “spo” #note last element is never included! mystring[1:3]-> “po” 4/15/04 Introduction to Python 15 Strings (3) “%” operator: sort of “fill in the blanks” operation: mystring=“%s has %n marbles” % (“John”,35) “blanks” mystring -> “John has 35 marbles” 4/15/04 %s %n,%i %f Values to put in blanks replace with string replace with integer replace with float Introduction to Python 16 Lists mylist=[“a”,”b”,3.58,”d”,4,0] mylist[0] mylist[2] a 3.58 Indexing mylist[-1] mylist[-2] 0 4 Negative indexing (counts from end) mylist[1:4] [“b”,3.58,”d”] Slicing (like strings) “b” in mylist “e” not in mylist 1 or True 1 or True mylist.append(8) [“a”,”b”,3.58,”d”,4,0,8] 4/15/04 Introduction to Python Add to end of list 17 Tuples Tuples – sequence of values like lists, but cannot be changed after it is created mytuple=(1,2,3,4) mytuple=(1,”a”,”bc”,3,87.2) mytuple[1]=“3” Error! 4/15/04 Used when you want to pass several variables around at once Introduction to Python 18 Dictionaries Dictionaries – map ‘keys’ to ‘values’ 4/15/04 like lists, but indices can be of any type Also, keys are in no particular order Eg: mydict={‘b’:3, ’a’:4, 75:2.85} mydict[‘b’] -> 3 mydict[75] -> 2.85 mydict[‘a’] -> 4 Introduction to Python 19 Dictionaries mydict={“r”:1,”g”:2,”y”:3.5,8.5:8,9:”nine”} mydict.keys() ['y', 8.5, 'r', 'g', 9] List of the keys mydict.values() [3.5, 8, 1, 2, 'nine'] List of the values mydict[“y”] 3.5 Value lookup mydict.has_key(“r”) True or 1 Check for keys mydict.update({“a”:75}) {8.5: 8, 'a': 75, 'r': 1, 'g': 2, 'y': 3.5, 9: 'nine'} 4/15/04 Introduction to Python Add pairs to dictionary 20 Dictionaries – other considerations Slicing not allowed Referencing invalid key is an error: >>> mydict={8.5: 8, 'a': 75, 'r': 1, 'g': 2, 'y': 3.5, 9: 'nine'} >>> mydict["red"] Traceback (most recent call last): File "<interactive input>", line 1, in ? KeyError: 'red‘ Use mydict.get(“red”) instead, it returns None if key is not found 4/15/04 Introduction to Python 21 Input/Output Function raw_input() designed to read a line of input from the user 1 optional argument: string to prompt user If int or float desired, simply convert string: int(mystring)->convert to int (if possible) float(mystring)->convert to float (if possible) >>> mystr=raw_input("Enter a string:") Enter a string:Hello World! >>> mystr 'Hello World!' 4/15/04 Introduction to Python 22 Output Function print Prints each argument, followed by space After all arguments, prints newline Put comma after last arg to prevent newline “add” strings to avoid spaces print “a”,”b”,”c” abc Newline! print “a”,”b”,”c”, No abc Newline! print “a”+”b”+”c” No abc spaces! 4/15/04 Introduction to Python 23 Output Example >>> print "hello","world";print "hello","again" hello world hello again >>> print "hello","world",;print "hello","again" hello world hello again >>> print "hello %s world" % "cold and cruel" hello cold and cruel world >>> print "hello","cold"+ " " + "and","cruel","world" hello cold and cruel world 4/15/04 Introduction to Python 24 Creating a Python Program Enter your program in the editor Notice that the editor has a color coding Also notice that it automatically indents 4/15/04 Comments Key words Etc… Don’t override!! – this is how python tells when block statements end! If doesn’t indent to proper location – indicates bug Introduction to Python 25 Running your Program To build your program Under File->Run… 4/15/04 Select No Debugging in the drop-down window Fix any errors, then run again Introduction to Python 26 Programming Workshop #1 Write a Python program to compute the hydrophobicity of an amino acid Program will prompt the user for an amino acid and will display the hydrophobicity 4/15/04 A m in o A c id A C D E F G H I K L M N P Q R S T V W Y H y d ro p . V A L U E 1 .8 2 .5 - 3 .5 - 3 .5 2 .8 - 0 .4 - 3 .2 4 .5 - 3 .9 3 .8 1 .9 - 3 .5 - 1 .6 - 3 .5 - 4 .5 - 0 .8 - 0 .7 4 .2 - 0 .9 - 1 .3 Introduction to Python 27