Mapping mathematical formulae to Python-1

Download Report

Transcript Mapping mathematical formulae to Python-1

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
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 use basic mathematical operations
Learn to print formatted and unformatted data
1/30/2013
10.009.2013. Week 1. Sessions 2 and 3
2
Python installation
Download Python if you have not yet done so.
Source for download:
Go to:
http: //epd-free.enthought.com/?Download=Download+EPD+Free+7.3-2
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.
1/30/2013
10.009.2013. Week 1. Sessions 2 and 3
3
Python installation: check
Go to the Enthought directory and click on IDLE.
You should see a window with the message:
Python 2.7.3 |EPD_free 7.3-2 (32-bit)| (default, Apr 12 2012,
11:28:34)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "copyright", "credits" or "license()" for more information.
and a prompt like this:
>>>
1/30/2013
10.009.2013. Week 1. Sessions 2 and 3
4
Python installation: Another check
Type:
>>>import matplotlib
You should see the prompt
>>>
indicating that matplotlib exists. More on this later.
1/30/2013
10.009.2013. Week 1. Sessions 2 and 3
5
Python: A simple exercise
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.
1/30/2013
10.009.2013. Week 1. Sessions 2 and 3
6
Python: Program execution
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.
1/30/2013
10.009.2013. Week 1. Sessions 2 and 3
7
Python: Types
Set of values
x
Set of Operations
a
b
1/30/2013
10.009.2013. Week 1. Sessions 2 and 3
c
8
Python: Primitive types: int, long
Set of integers
Set of arithmetic
operators
2010
//
-14
180
/
+
12
*
%
Set of bitwise
operators
|
<<
~
^
&
>>
1751
sys.maxint: 9223372036854775807
Long integers: unlimited precision
1/30/2013
-sys.maxint-1: -9223372036854775808
10.009.2013. Week 1. Sessions 2 and 3
9
Python: Primitive types: float
Set of floating point numbers
Set of Operations
(sample)
2010.98135
12.77
+
inf
3.14
-inf
.2010E4
180.0
-
==
*
>=
>
nan
-1751.0
max: 1.7976931348623157e+308
Epsilon: 2.220446049250313e-16
min: 2.2250738585072014e-308
1/30/2013
10.009.2013. Week 1. Sessions 2 and 3
10
Python: Primitive types: complex
Set of complex numbers
Set of Operations
(sample)
1+2j
+
12.77+3j
1.3j
-5
==
*
>=
>
12.77-3j
1/30/2013
10.009.2013. Week 1. Sessions 2 and 3
11
Python: Primitive types: boolean
Set of logical values
Set of (logical) Operations
(sample)
and
True
or
not
False
1/30/2013
10.009.2013. Week 1. Sessions 2 and 3
12
Python: Names
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.
1/30/2013
10.009.2013. Week 1. Sessions 2 and 3
13
Python: Constants
A constant is something that cannot change during program execution. It
is an immutable object.
Examples:
Integer constants: 0, 1, -1, +24, 29, 300009998, O14, 0x1B. 0b1011
Floating point constants: 0.0, -2.345e28, -0.000976512
Boolean constants: True, False
String constants: “”, “ ”, “Hi!”, “Alice in Wonderland”
1/30/2013
10.009.2013. Week 1. Sessions 2 and 3
14
Python: Variables
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 student’s 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]
1/30/2013
10.009.2013. Week 1. Sessions 2 and 3
15
Python: Strings and operations
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.
1/30/2013
10.009.2013. Week 1. Sessions 2 and 3
16
Python: Expressions
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
1/30/2013
10.009.2013. Week 1. Sessions 2 and 3
17
Python: Assignment
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;
1/30/2013
10.009.2013. Week 1. Sessions 2 and 3
18
Week 1. Sessions 2 and 3. 2013
Hope you enjoyed this week!
1/30/2013
10.009.2013. Week 1. Sessions 2 and 3
19