Transcript python

Hvordan kører kurset?
• Jeg taler en smule
• I programmerer en masse
• Læs stoffet før undervisningen
• Løs opgaver under og efter undervisningen
• Kurset afsluttes med et obligatorisk projekt
Intro: Java/Python Differences
Java
Python
Compiled:
Interpreted:
javac MyClass.java
java MyClass
python MyProgram.py
Blocks surrounded by { }:
Blocks indented:
if (a==1) {
b=5; }
if a==1:
b=5
Statically typed:
Dynamically typed:
int a; String s;
a = 2;
s = “Julius Caesar”
a = 2
s = “Julius Caesar”
s = 4
- see http://www.daimi.au.dk/~chili/PBI/pythonForJavaProgrammers.htm
2
First Program in Python: Printing a Line of Text
• Python code
– # symbol: a single line comment (like // in java)
– print function: sends a stream of text to be output
• Executing code
– Saved in a file
• Type code into a .py file and save it (e.g. someName.py)
• Type python someName.py
– Interactively
• Type python in the command line: runs the python
interpreter
• Type in code statements one by one
3
# Fig. 2.1: fig02_01.py
# Printing a line of text in Python.
print "Welcome to Python!"
Comments
Prints out the line of text
Welcome to Python!
Fig. 2.2
1
2
3
4
Code saved in a file
# Fig. 2.5: fig02_05.py
# Printing multiple lines with a single statement.
print "Welcome\nto\n\nPython!"
Welcome
to
Python!
The \n is used to make the
text appear on the next line
Python 2.2b2 (#26, Nov 16 2001, 11:44:11) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print "Welcome to Python!"
Welcome to Python!
>>> ^D
Fig. 2.2 Interactive mode. (Python interpreter software Copyright ©2001 Python
Software Foundation.)
5
Functions, variables
• The raw_input function
– Used to retrieve data from the user
• The int function
– Used to convert strings to integers
• The float function
– Used to convert strings to floating point numbers
6
1
2
3
4
5
6
7
8
9
10
11
12
13
# Fig. 2.7: fig02_07.py
# Simple addition program.
# prompt user for input
integer1 = raw_input( "Enter first integer:\n" ) # read string
integer1 = int( integer1 )
# convert string to integer
Prompts the user to
enter an integer value
integer2 = raw_input( "Enter second integer:\n" ) # read string
integer2 = int( integer2 )
# convert string to integer
sum = integer1 + integer2
# compute and assign sum
print "Sum is", sum
# print sum
Enter first integer:
45
Enter second integer:
72
Sum is 117
Converts the string value
into an integer value
Adds up and then prints out
the sum of the two numbers
More on variables
integer1
"45"
Memory location showing value of the variable (after integer1
name bound to the value.
= raw_input(..)
) and the
"45"
integer1
After integer1
45
= int( integer1 )
8
integer1 = raw_input( "Enter first integer:\n" ) # read a string
print "integer1: ", id( integer1 ), type( integer1 ), integer1
integer1 = int( integer1 )
# convert the string to an integer
print "integer1: ", id( integer1 ), type( integer1 ), integer1
Enter first integer:
5
integer1:
7956744 <type 'str'> 5
integer1:
7637688 <type 'int'> 5
Notice in the output that
after the conversion the
value is the same but the
type and id have changed
Prints the id, type and
value before and after
the variable is converted
into an integer
Accidentally adding strings
Python 2.2b2 (#26, Nov 16 2001, 11:44:11) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> value1 = raw_input( "Enter an integer: " )
Enter an integer: 2
>>> value2 = raw_input( "Enter an integer: " )
Enter an integer: 4
>>> print value1 + value2
24
Fig. 2.8 Adding values from raw_input (incorrectly) without converting to
integers (the result should be 6).
10
2.6 Arithmetic
• Symbols
–
–
–
–
* = multiply
/ = divide
% = modulus
** = exponential (a**4 means a*a*a*a)
– // = floor division
• Order
– blahblah.. Just put in the extra parentheses.
(1+3**3+10/5)*2-5 = 55
((1+(3**3)+(10/5))*2)-5 = 55
11
The % formatting operator
# Fig. 2.19: fig02_19.py
# String formatting.
Integer
integerValue = 4237
print "Integer ", integerValue
print "Decimal integer %d" % integerValue
print "Hexadecimal integer %x\n" % integerValue
4237
Decimal integer 4237
Hexadecimal integer 108d
floatValue = 123456.789
print "Float", floatValue
print "Default float %f" % floatValue
print "Default exponential %e\n" % floatValue
Float 123456.789
Default float 123456.789000
Default exponential 1.234568e+05
print "Right justify integer (%8d)" % integerValue
print "Left justify integer (%-8d)\n" % integerValue
print "Force eight digits in integer %.8d" % integerValue
print "Five digits after decimal in float %.5f" % floatValue
Right justify integer (
Left justify integer
(4237
4237)
)
stringValue = "String formatting"
print "Fifteen and five characters allowed in string:"
print "(%.15s) (%.5s)" % ( stringValue, stringValue )
Force eight digits in integer 00004237
Five digits after decimal in float 123456.78900
Fifteen and five characters allowed in string:
(String formatti) (Strin)
Easy to put variable values inside strings
# String formatting.
name = “Arthur”
grade = 11
print “Dear %s, your grade is %d, congratulations!” %(name, grade)
Dear Arthur, your grade is 11, congratulations!
i_guess_your_number.py
Note
•% operator
•while 1 / break
•Indentation
•if statement
14
i_guess_your_number.py output
15
Indentation
– Used to delimit blocks of code (instead of {})
– The indentation must be the same in each block
– There is no rule for the number of spaces but 3 or 4
gives good readability
Wrong
indentation
16
Expression values
• Expressions may have integer values
• 0 is false, non-0 is true.
>>> if 0:
...
print "0 is true"
... else:
...
print "0 is false"
...
0 is false
>>> if 1:
...
print "non-zero is true"
...
non-zero is true
>>> if -1:
...
print "non-zero is true"
...
non-zero is true
>>> print 2 < 3
False
17
Example programs
Programs from book, e.g. chapter 2, can be found here:
DAIMI Unix system:
~chili/PBI/Programs_from_book/ch02/
Web:
http://www.daimi.au.dk/~chili/PBI/Programs_from_book/ch02/
Other example programs can be found here:
DAIMI Unix system:
~chili/PBI/ExamplePrograms/
Web:
http://www.daimi.au.dk/~chili/PBI/exampleprograms.html
18