SOPH 303 The Digital World Term 3 Spring 2013 Notes for Week 1. Sessions 2 and 3 Revised: January 28, 2013 Aditya Mathur Head of Pillar Information Systems Technology and Design Singapore University of Technology and Design West Lafayette, IN, USA
Learning Objectives Week 1. Sessions 2 and 31/30/2013 Learn to use IDLE to write and execute Python programs Learn to use Python as a calculator: variables, arithmetic operations Understand basic object types: int, float, string, complex Learn how to import libraries Learn to print formatted and unformatted data Learn to use basic mathematical operations
Python installation Download Python if you have not yet done so Week 1. Sessions 2 and 31/30/2013 Source for download: Go to: http: //epd-free.enthought.com/?Download=Download+EPD+Free Click on EPD Free-Windows or EPD-Free Mac depending on your laptop When the download is complete click on the downloaded file and follow instructions to install.
Python installation: check Week 1. Sessions 2 and 31/30/2013 Go to the Enthought directory and click on IDLE. You should see a window with the message: Python |EPD_free (32-bit)| (default, Apr , 11:28:34) [GCC (Apple Inc. build 5493)] on darwin Type "copyright", "credits" or "license()" for more information. and a prompt like this: >>>
Python installation: Another check Week 1. Sessions 2 and 31/30/2013 Type: >>>import matplotlib You should see the prompt >>> indicating that matplotlib exists. More on this later.
Python: A simple exercise Week 1. Sessions 2 and 31/30/2013 Click on IDLE or just type idle in a terminal window. You should see a new window with the prompt >>> Go to the course web site and click on Sample Python Programs. Click on BasicExamplesWeek1 and then on simpleCalculator.py. Open a new window from the File menu in the Python window. Copy the code you just opened and paste in the new window.
Python: Program execution Week 1. Sessions 2 and 31/30/2013 Enter Function f5 in the new window you just opened. The program you just copied will be executed and the results will be in the Python window. Correlate the results with each line of the program to understand it. Look carefully at the comments in the program. After a small set of Python statements there will be a few simple exercises that you need to solve on your own. Once you have understood the entire program start working on problems in Problem Set 1.
Python: Types Week 1. Sessions 2 and 31/30/2013 Set of valuesSet of Operations x x x x x a b c
Python: Primitive types: int, long Week 1. Sessions 2 and 31/30/ Set of integers Set of arithmetic operators * % sys.maxint: sys.maxint-1: / // Long integers: unlimited precision Set of bitwise operators | << & >> ^ ~
Python: Primitive types: float Week 1. Sessions 2 and 31/30/ Set of floating point numbers E4 inf -inf nan Set of Operations (sample) + - * > == >= max: e+308 min: e-308 Epsilon: e-16
Python: Primitive types: complex Week 1. Sessions 2 and 31/30/2013 Set of complex numbers j 1+2j 1.3j Set of Operations (sample) + - * > == >= j
Python: Primitive types: boolean Week 1. Sessions 2 and 31/30/2013 Set of logical valuesSet of (logical) Operations (sample) and True False or not
Python: Names Week 1. Sessions 2 and 31/30/2013 Used to denote variables, classes, objects Contain characters; must start with a letter, or an underscore (_) sign or an underscore. Examples: height, area1, Dog, _great_ Length unlimited, case sensitive. Dog and dog are different names. Our convention: All class names begin with an uppercase letter; all other names begin with a lower case letter.
Python: Constants Week 1. Sessions 2 and 31/30/2013 A constant is something that cannot change during program execution. It is an immutable object. Examples: Integer constants: 0, 1, -1, +24, 29, , O14, 0x1B. 0b1011 Floating point constants: 0.0, e28, Boolean constants: True, False String constants:,, Hi!, Alice in Wonderland
Python: Variables Week 1. Sessions 2 and 31/30/2013 A variable is something whose value may change during program execution. Example: int numStudents; denotes the number of students whose grads have been processed. Its value changes as each students grade is processed by a grade processing program. Every variable has a name; its type is the type of its value. Example: hurricaneCategory=2; The name is hurricaneCategory and its type is int. The type of a variable may change when its value changes. Example: a=1 [Type of a is int] A=1.0 [Type of a changed to float]
Python: Strings and operations Week 1. Sessions 2 and 31/30/2013 A string is any sequence of Unicode characters enclosed inside double or single quotes. Examples: ``Digital world, Digital world, ``, a=Digital b= world c=a+b The value of c is Digital world Several other formatting operations are available.
Python: Expressions Week 1. Sessions 2 and 31/30/2013 Expressions are used to compute something. x=1; y=2; z=3.0 x*y+z; // Arithmetic expression, type of value float x<y; // Boolean expression, results in boolean value firstName=Mary, lastName= Jones; firstName+ +lastName; // Results in a String
Python: Assignment Week 1. Sessions 2 and 31/30/2013 An assignment statement allows assigning the value of an expression to a variable. p=x*y+z; // p gets the value of x*y+z q=x<y; // q gets the value of x<y firstName=Mary, lastName= Jones; name= firstName+ +lastName;
Week 1. Sessions 2 and Hope you enjoyed this week! Week 1. Sessions 2 and 31/30/2013