A Python Tour: Just a Brief Introduction CS 303e: Elements of Computers and Programming "The only way to learn a new programming language is by writing programs in it." -- B. Kernighan and D. Ritchie "Computers are good at following instructions, but not at reading your mind." -- D. Knuth Program: n. A magic spell cast over a computer allowing it to turn one's input into error messages. tr. v. To engage in a pastime similar to banging one's head against a wall, but with fewer opportunities for reward.
Useful Definitions Programming Language: A language that is designed to express instructions that can be carried out by a computer Program: A detailed set of instructions written in a programming language for a computer to perform Program Execution ("running" a program): The computer performs the program's instructions Machine language: The low-level language of a computer that consists of each computation's encoding as a sequence of 0s and 1s. In machine language, a program could look like this: 0110111100001010 1111011110101111 0000111101010111... A sequence of 0s and 1s (called bits) corresponds to operations such as Add, Store, Get Instruction, etc. Each computer (CPU) has its own machine language.
What is Python? A high-level language developed by Guido van Rossum in the Netherlands in the late 1980s. Released in 1991. Named after Monty Python Twice received recognition as the language with the largest growth in popularity for the year (2007, 2010) Clean, concise syntax o Easy to learn o Can create powerful programs quickly Design is compact o Language constructs are easy to learn o Easy to remember
How Does Python Work? Python is interpreted: o Each command is translated to machine language and executed on the fly The other type is compiled: o All commands are translated into machine language o The resulting program is stored in a new file o Then the machine language program is executed
How Python Works Python uses an interpreter, which is a program that translates each command in your program into machine language and executes it. Here's what you do: o Type your source code (Python) in a file (that ends in extension.py). o Invoke the Python interpreter (either from the command line or using IDLE) to translate your instructions into machine language and execute them. o Or you may also use Python interactively, and enter one command at a time: piglet.cs.utexas.edu:eberlein> python Python 2.5.4 (r254:67916, Jan 16 2009, 16:27:43) [GCC 4.2.2] on linux2 Type "help", "copyright", "credits" or "license" for more information >>>> print "welcome to cs 303e" welcome to cs 303e >>>> for i in range(3):... print i... 0 1 2 >>>>
Comments in a Python Program Explanations of code purpose in English Indicated by the # character Ignored by the interpreter The style guide for this class requires comments: o At the beginning of a module o At the beginning of a function o Before a chunk of related statements that carry out some unified action Example: # python program example def main(): # Welcome everyone to CS 303e print "Welcome to CS 303e!" Question: What output do you expect from the above Python program? A. Welcome to CS 303e! B. "Welcome to CS 303e!" C. welcome everyone to CS 303e
Repetition in Python In programs, we may want the computer to repeat an instruction or set of instructions more than once To do that, we use loops, also known as repetition structures Examples with a Loop: for i in [1, 2, 3]: print i # indentation is important! Output: 1 2 3 for i in range(3): print i Output: 0 1 2
Decisions in Python We may want the computer to perform certain instructions only under certain conditions. Test for the condition (to see if it is true) If condition is met, perform the action If condition not met, skip the action Example: i = 5 if i < 10: print i, "is less than 10"
Expected Output def main(): for i in [2, 4, 6]: print i What is the output? A. 1 B. 2 C. i 2 4 i 3 6 i
Python Program Structure A source file my contain function definitions, which are a collection of Python statements that will be executed together when the function is invoked. Function definitions start like this: def myfunctionname(): Statements that are part of the function (or a loop body) must be indented (say 4 spaces) underneath the above line - this indentation tells the Python interpreter which statements are part of your function. Functions are not executed until you call them. It is common to include a function called main which is executed first. A Python program can contain statements which are not part of any function. A comment is included after a # sign and is ignored by the python interpreter.
An Example: Some Source Code in Python def main(): # definition of main function print "hello" print "world" print "hello", "world" mynum = 13 print mynum*mynum # call the main function main() Output: 169 hello world hello world
Python Tour: Things to Remember Whitespace (blank spaces) matters o Statements inside functions/loops/conditionals must be indented Tells interpreter which statements belong inside Two spaces too few, six probably too many be consistent IDLE indents automatically Common to include the main function
Announcements Apply for your lab account today (if you have not already) use the link on the CS 303e website called "Lab Accounts". This week: no discussion section meetings on Friday Next week only: discussion sections meet in PAI 5.38 on Friday (at your regular time) o Your TA will be there and available to help with project 1 o TA will be at a computer with a sign introduce yourself, ask for help o If you have already finished project 1, you can skip discussion section (next week only)