CS101 Lecture 24: Thinking in Python: Input and Output Variables and Arithmetic Aaron Stevens 28 March 2011 1 Overview/Questions Review: Programmability Why learn programming? What is a programming language? What is Python? 2 1
Compiled Language Source Code (Program) Compiler Machine Code Program Inputs Running Program Outputs A compiler takes source code, and produces executable machine code (stored in a file on disk). The machine code program is executed by the user. Advantage: speed at time of execution. 3 Interpreted Language Source Code (Program) Computer Running an Interpreter Outputs Inputs An interpreter takes source code as input, and then compiles each statement and executes it immediately. Advantage: portability 4 2
Introducing Python 5 Introducing Python We will experiment with the Python programming language. What is Python? A very high level interpreted language. Very clear and simple syntax. Runs an almost any computing device Windows, Linux/Unix, Mac OS X, OS/2, Amiga, Palm Handhelds, Nokia mobile phones, and any device which supports Java. You can download and install Python on your own computer for free. We will use the Jython Environment for Students Download from http://code.google.com/p/mediacomp-jes/ 6 3
Jython Environment for Students When we launch the JES, we will see this window: This is the editor for writing and saving programs. This is the command prompt 7 Experimenting with Python Some simple output statements, and a hint of arithmetic 8 4
Python Interpreted Mode Statements and instructions typed in Python s command prompt go away when we close the JES. Instead, we can type source code into the editor window, save it as its own file, and then open and run it whenever we want. 9 Saved Program Mode Type commands in editor window Save as a file with the extension.py Load program and call functions 10 5
11 Elements of Programs Keywords Special words reserved by the language. Keywords are used to perform basic computation functions, and are one of the building blocks of programs. Example: print # The word print is a Python keyword print Hello, world! 12 6
Elements of Programs Comments Comments are lines of source code which are ignored by the Python interpreter. Comments always begin with the # mark. Example: # The word print is a Python keyword print Hello, world! 13 Elements of Programs Functions Functions are pre-written groups of instructions that we can call upon in our programs (or at the command prompt). Example: ord(<character>), chr(<integer>) 14 7
Some JES functions Examples: pickafile() makepicture(<file>) show(<picture>) 15 Variables A variable is a spot in memory which can hold a value. The value is stored using a name we chose. We can recall it when needed to use the value, or change it to hold something else. x = 42 16 8
Elements of Programs Identifiers Programmers use names to identify modules (e.g. hello.py), functions (e.g. main), and variables (e.g. pesos) within programs. Variables Give names to values within programs 17 Identifier Naming Rules Identifiers in Python follow these rules: Must begin with a letter or underscore _ May contain letters, numbers, underscores May not contain spaces, punctuation, etc. May not be Python keywords Are case sensitive x x1 dollars dollarsandcents dollars_and_cents main 9thValue print hello there my.favorite.letter 18 9
Saved Program Mode Type commands in editor window Save as a file with the extension.py Load program and call functions 19 Elements of Programs Expressions Any statement which produces a value. Values can be numeric, text, or other types. Variables, too are expressions. Examples (each line is a valid Python statement): x = 42 name = requeststring( Enter your name: ) 20 10
Arithmetic Operators Python built-in numeric operators: + addition - subtraction * multiplication / division ** exponentiation % remainder (modulo) The order of operations is the familiar PEMDAS. 21 Arithmetic Examples Build a simple calculator program 22 11
Data Types All data stored by a computer has a type. The type determines how the computer should interpret the bits in its memory. For example, we ve already dealt with both numeric data (x = 5) and non-numeric data (name = Monty ). type(x) -- returns the data type for the variable x 23 Creating our own functions For anything more than one or two commands, we ll want to create our own functions. Example: 24 12
Function Parameters A parameter is like a changeable input to a function. Example: 25 What You Learned Today Introducing Python Jython Environment for Students Command Prompt, Source code editor Print statement, literals, variables, comments Using Functions in JES 26 13
Announcements and To Do List Download & install JES on your computer! http://code.google.com/p/mediacomp-jes/downloads/list (pick 4.3 -- Mac or Windows) Readings: The CS101 Guide to Python/JES http://www.cs.bu.edu/courses/cs101b1/jes/ Optional (free) book about Python: How to Think Like A Computer Scientist: Learning with Python Available online at: http://openbookproject.net//thinkcspy/ HW9 is due Wednesday 3/30 27 14