Example: biology

Python Basics - Loyola University Chicago

Python DotyAugust 27, 2008 Contents1 What is Python ? .. Installation and documentation .. 42 Getting Running Python as a calculator .. Quitting the interpreter .. Loading commands from the library .. Defining functions .. Files .. Testing code .. Scripts .. 813 Python Comments .. Numbers and other data types .. Thetypefunction .. Strings .. Lists and tuples .. Therangefunction .. Boolean values .. Expressions .. Operators .. Variables and assignment .. Decisions .. Loops .. loops .. ,continue, andpass.

Python is a good choice for mathematical calculations, since we can write code quickly, test it easily, and its syntax is similar to the way mathematical ideas are expressed in the mathematical literature.

Tags:

  Python

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of Python Basics - Loyola University Chicago

1 Python DotyAugust 27, 2008 Contents1 What is Python ? .. Installation and documentation .. 42 Getting Running Python as a calculator .. Quitting the interpreter .. Loading commands from the library .. Defining functions .. Files .. Testing code .. Scripts .. 813 Python Comments .. Numbers and other data types .. Thetypefunction .. Strings .. Lists and tuples .. Therangefunction .. Boolean values .. Expressions .. Operators .. Variables and assignment .. Decisions .. Loops .. loops .. ,continue, andpass.

2 Lists .. Length of a list; empty list .. Sublists (slicing) .. Joining two lists .. List methods .. Strings .. 192 What is Python ? Python is a powerful modern computer programming bears some similarities toFortran, one of the earliest programming languages, but it is much more powerful than allows you to use variables without declaring them ( , it determines types implicitly),and it relies on indentation as a control structure. You are not forced to define classes in Python (unlike Java) but you are free to do so when was developed by Guido van Rossum, and it is free software.

3 Free as in free beer, in thatyou can obtain Python without spending any money. But Pythonis also free in other importantways, for example you are free to copy it as many times as you like, and free to study the sourcecode, and make changes to it. There is a worldwide movement behind the idea of free software,initiated in 1983 by Richard document focuses on learning Python for the purpose of doing mathematical assume the reader has some knowledge of basic mathematics, but we try not to assume anyprevious exposure to computer programming, although some such exposure would certainly behelpful.

4 Python is a good choice for mathematical calculations, since we can write code quickly, testit easily, and its syntax is similar to the way mathematical ideas are expressed in the mathematicalliterature. By learning Python you will also be learning a major tool used by many web Installation and documentationIf you use Mac OS X or Linux, then Python should already be installed on your computer bydefault. If not, you can download the latest version by visiting the Python home page, you will also find loads of documentation and other useful information. Windows users canalso download Python at this website.

5 Don t forget this website; it is your first point of referencefor all things Python . You will find there, for example, reference [1], the excellentPython Tutorialby Guido van Rossum. You may find it useful to read along in the Tutorial as a supplement tothis Getting Running Python as a calculatorThe easiest way to get started is to run Python as aninterpreter, which behaves similar to theway one would use a calculator. In the interpreter, you type acommand, and Python producesthe answer. Then you type another command, which again produes an answer, and so OS X or Linux, to start the Python interpreter is as simple as typing the commandpythonon the command line in a terminal shell.

6 In Windows, assumingthat Python has already been1 See or formore , you need to find Python in the appropriate menu. Windows users may choose to runPython in a command shell ( , a DOS window) where it will behave very similarly to Linux orOS all three operating systems (Linux, OS X, Windows) thereis also an integrated developmentenvironment for Python namedIDLE. If interested, you may download and install this on your help on getting started with IDLE ~dyoo/ Python /idle_intOnce Python starts running in interpreter mode, usingIDLEor a command shell, it produces aprompt, which waits for your input.

7 For example, this is whatI get when I start Python in acommand shell on my Linux box:doty@brauer:~% pythonPython (r252:60911 , Apr 21 2008, 11:12:42)[GCC (Ubuntu -2ubuntu7)] on linux2 Type "help", "copyright", "credits" or "license" for moreinformation.>>>where the three symbols>>>indicates the prompt awaiting my experiment, using the Python interpreter as a calculator. Be assured that you cannot harmanything, so play with Python as much as you like. For example:>>> 2*10242048>>> 3+4+916>>> 2**1001267650600228229401496703205376 LIn the above, we first asked for the product of 2 and 1024, then we asked for the sum of 3, 4, and 9and finally we asked for the value of 2100.

8 Note that multiplication in Python is represented by ,addition by +, and exponents by**; you will need to remember this syntax. The L appended tothe last answer is there to indicate that this is alonginteger; more on this later. It is also worthnoting that Python does arbitrary precision integer arithmetic, by default:>>> 2**1000107150860718626732094842504906000 1810561404811705533607443750388370351051 1249361224931983788156958581275946729175 5314682518714528569231404359845775746985 7480393456777482423098542107460506237114 1877954182153046474983581941267398767559 1655439460770629145711964776865421676604 29831652624386837205668069376 LHere is another example, where we print a table of perfect squares:>>> for n in [1,2,3,4,5,6].

9 Print n** Python and IDLE should be already preinstalled on all Loyola Windows illustrates several points. First, the expression [1,2,3,4,5,6] is a list, and we print the valuesofn2fornvarying over the list. If we prefer, we can print horizontally instead of vertically:>>> for n in [1,2,3,4,5,6]:.. print n**2,..1 4 9 16 25 36simply by adding a comma at the end of the print command, whichtells Pythonnotto move toa new line before the next last two examples are examples of acompoundcommand, where the command is dividedover two lines (or more). That is why you the second line instead of the usual>>>,which is the interpreter s way of telling us it awaits the rest of the command.

10 On the third linewe entered nothing, in order to tell the interpreter that thecommand was complete at the secondline. Also notice thecolonat the end of the first line, and theindentationin the second line. Bothare required in compound Python Quitting the interpreterIn a terminal you can quit a Python session by CTRL-D. (Hold down the CTRL key while pressingthe D key.) In IDLE you can also quit from the the interpreter gets stuck in an infinite loop, you can quitthe current execution by Loading commands from the libraryPython has a very extensive library of commands, documentedin thePython Library ReferenceManual[2].


Related search queries