Download value

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction to
Programming with Python
Languages
• Some influential ones:
– FORTRAN
• science / engineering
– COBOL
• business data
– LISP
• logic and AI
– BASIC
• a simple language
2
Python
• Created in 1991 by Guido van Rossum (now at Google)
– Named for Monty Python
• Useful as a scripting language
– script: A program meant for use in small/medium projects
• Used by:
– Google, Yahoo!, Youtube
– Many Linux distributions
– Games and apps (e.g. Eve Online)
3
Installing Python
Windows:
• Download Python from
http://www.python.org
• Install Python.
• Run Idle from the Start Menu.
Mac OS X:
• Python is already installed.
• Open a terminal and run python
or run Idle from Finder.
Linux:
• Chances are you already have
Python installed. To check, run
python from the terminal.
• If not, install from your
distribution's package system.
Note: For step by step installation
instructions, see the course web site.
4
Interpreted Languages
• interpreted
– Not compiled like many other languages (Java, C, C++)
– Code is written and then directly executed by an interpreter
– Type commands into interpreter and see immediate results
Java:
Python:
Code
Compiler
Code
Runtime
Environment
Computer
Interpreter
Computer
5
The Python Interpreter
• Allows you to type commands one-at-a-time and see results
• A great way to explore Python's syntax
6
Basic Programs and
Numeric Data
The print Statement
• A Python program's code is just written directly into a file
print "text"
(a blank line)
print
hello.py
1
print
"Hello,
world!"
swallows.py
1
2
3
4
print "Hello, world!"
print
print "Suppose two swallows carry it together."
print "African or European swallow?"
8
Comments
• Syntax:
# comment text (one line)
swallows2.py
1
2
3
4
5
6
# Suzy Student, CSE 142, Fall 2097
# This program prints important messages.
print "Hello, world!"
print
# blank line
print "Suppose two swallows \"carry\" it together."
print 'African or "European" swallows?'
9
Expressions
• expression: A value or operation(s) to compute a value.
Example:
1 + 4 * 3
• Arithmetic operators:
– + - * /
– **
– %
add, subtract/negate, multiply, divide
exponentiate
modulus, a.k.a. remainder
• precedence: Order in which operations are computed.
– * / % **
have a higher precedence than + -
1 + 3 * 4
is 13
(1 + 3) * 4 is 16
10
Integer division
• When we divide integers with / , the quotient is an integer.
3
4 ) 14
12
2
• 35 / 5 is 7
• 84 / 10 is 8
52
27 ) 1425
135
75
54
21
• The % operator computes a remainder from integer division.
3
4 ) 14
12
2
43
5 ) 218
20
18
15
3
11
Variables
• variable: A named piece of memory that stores a value.
• assignment: Stores a value into a variable.
– Syntax:
name = expression
– Examples:
x
x = 5
gpa = 3.14
5
gpa
3.14
– A variable can be used in expressions.
x + 4 is 9
12
•
•
•
Variables
A variable is a named place in the memory where a
programmer can store data and later retrieve the data using
the variable “name”
Programmers get to choose the names of the variables
You can change the contents of a variable in a later
statement
x = 12.2
y = 14
x = 100
x 12.2 100
y 14
13
Python Variable Name Rules
•
Can consist of letters, numbers, or underscores (but cannot
start with a number)
•
Case Sensitive
•
Good:
spam
•
Bad:
23spam
•
Different:
eggs spam23
_speed
#sign var.12
spam Spam SPAM
14
Data Types
• type: A category or set of data values.
– Constrains the operations that can be performed on the data
– Examples: integer, real number, text string
• Python is relaxed about types.
– A variable's type does not need to be declared.
– A variable can change types as a program is running.
Value Python type
42
int
3.14
float
"ni!" str
15
Some Data Types in Python
•
Integer (Examples: 0, 12, 5, -5)
•
Float (Examples: 4.5, 3.99, 0.1 )
•
String (Examples: “Hi”, “Hello”, “Hi there!”)
•
Boolean (Examples: True, False)
•
List (Example: [ “hi”, “there”, “you” ] )
•
Tuple (Example: ( 4, 2, 7, 3) )
16
Statements
x=2
x=x+2
print x
Variable
Operator
Assignment Statement
Assignment with expression
Print statement
Constant
Reserved Word
17
Assignment Statements
•
•
We assign a value to a variable using the assignment
statement (=)
An assignment statement consists of an expression on the
right hand side and a variable to store the result
x = 3.9 * x * ( 1 - x )
18
Numeric Expressions
•
•
•
Because of the lack of
mathematical symbols on
computer keyboards - we use
“computer-speak” to express the
classic math operations
Asterisk is multiplication
Exponentiation (raise to a power)
looks different from in math.
+
Addition
-
Subtraction
*
Multiplication
/
Division
**
Power
%
Remainder
19
Parameters
• parameter: A value supplied to a command as you run it.
– Syntax:
command ( value )
command ( value, value, ..., value )
• Example:
print sqrt(25)
print sqrt(15 + 10 * 10 + 6)
x = 5
print sqrt(x + sqrt(16))
20
Integer Division
•
•
Integer division truncates
Floating point division
produces floating point
numbers
>>> print 10 / 2
5
>>> print 9 / 2
4
>>> print 99 / 100
0
>>> print 10.0 / 2.0
5.0
>>> print 99.0 / 100.0
0.99
This changes in Python 3.0
21
Math commands
Function name
abs(value)
Description
Constant
Description
absolute value
e
2.7182818...
ceil(value)
rounds up
pi
3.1415926...
cos(value)
cosine, in radians
floor(value)
rounds down
log10(value)
logarithm, base 10
max(value1, value2)
larger of two values
min(value1, value2)
smaller of two values
round(value)
nearest whole number
sin(value)
sine, in radians
sqrt(value)
square root
• To use these commands, place this line atop your program:
from math import *
22
input
• input : Reads a number from the user's keyboard.
– You can store the result of input into a variable.
– Example:
age = input("How old are you? ")
print "Your age is", age
print "You have", 65 - age, "years until retirement"
Output:
How old are you? 53
Your age is 53
You have 12 years until retirement
• Exercise: Modify the restaurant program to ask the user
how much of a tip to leave.
23
Numeric Types
•
Numeric
•
•
•
•
•
int: equivalent to C’s long int in 2.x
but unlimited in 3.x.
float: equivalent to C’s doubles.
long: unlimited in 2.x and
unavailable in 3.x.
complex: complex numbers.
Supported operations include
constructors (i.e. int(3)), arithmetic,
negation, modulus, absolute value,
exponentiation, etc.
$ python
>>> 3 + 2
5
>>> 18 % 5
3
>>> abs(-7)
7
>>> float(9)
9.0
>>> int(5.3)
5
>>> complex(1,2)
(1+2j)
>>> 2 ** 8
256
24
Exercise
Write a program to prompt the user for hours and
rate per hour to compute gross pay.
Enter Hours: 35
Enter Rate: 2.75
Pay: 96.25
25