Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 2 Intro: Java/Python Differences Dynamically typed: a = 2 s = “Julius Caesar” s = 4 Statically typed: int a; String s; a = 2; s = “Julius Caesar” Blocks indented: if a==1: b=5 Blocks surrounded by { }: if (a==1) { b=5; } Interpreted: python MyProgram.py Compiled: javac MyClass.java java MyClass PythonJava - see http://www.daimi.au.dk/~chili/PBI/pythonForJavaProgrammers.htm

3 3 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

4 # Fig. 2.1: fig02_01.py # Printing a line of text in Python. print "Welcome to Python!" Welcome to Python! Comments Prints out the line of text Fig. 2.2Code saved in a file 1 # Fig. 2.5: fig02_05.py 2 # Printing multiple lines with a single statement. 3 4 print "Welcome\nto\n\nPython!" Welcome to Python! The \n is used to make the text appear on the next line

5 5 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.2Interactive mode. (Python interpreter software Copyright ©2001 Python Software Foundation.)

6 6 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

7 1 # Fig. 2.7: fig02_07.py 2 # Simple addition program. 3 4 # prompt user for input 5 integer1 = raw_input( "Enter first integer:\n" ) # read string 6 integer1 = int( integer1 ) # convert string to integer 7 8 integer2 = raw_input( "Enter second integer:\n" ) # read string 9 integer2 = int( integer2 ) # convert string to integer 10 11 sum = integer1 + integer2 # compute and assign sum 12 13 print "Sum is", sum # print sum Enter first integer: 45 Enter second integer: 72 Sum is 117 Prompts the user to enter an integer value Converts the string value into an integer value Adds up and then prints out the sum of the two numbers

8 8 More on variables "45"integer1 "45" 45 Memory location showing value of the variable (after integer1 = raw_input(..) ) and the name bound to the value. After integer1 = int( integer1 )

9 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 5 integer1: 7637688 5 Prints the id, type and value before and after the variable is converted into an integer Notice in the output that after the conversion the value is the same but the type and id have changed

10 10 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.8Adding values from raw_input (incorrectly) without converting to integers (the result should be 6).

11 11 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

12 # Fig. 2.19: fig02_19.py # String formatting. integerValue = 4237 print "Integer ", integerValue print "Decimal integer %d" % integerValue print "Hexadecimal integer %x\n" % integerValue floatValue = 123456.789 print "Float", floatValue print "Default float %f" % floatValue print "Default exponential %e\n" % floatValue 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 stringValue = "String formatting" print "Fifteen and five characters allowed in string:" print "(%.15s) (%.5s)" % ( stringValue, stringValue ) Integer 4237 Decimal integer 4237 Hexadecimal integer 108d Float 123456.789 Default float 123456.789000 Default exponential 1.234568e+05 Right justify integer ( 4237) Left justify integer (4237 ) Force eight digits in integer 00004237 Five digits after decimal in float 123456.78900 Fifteen and five characters allowed in string: (String formatti) (Strin) The % formatting operator

13 # String formatting. name = “Arthur” grade = 11 print “Dear %s, your grade is %d, congratulations!” %(name, grade) Dear Arthur, your grade is 11, congratulations! Easy to put variable values inside strings

14 14 i_guess_your_number.py Note % operator while 1 / break Indentation if statement

15 15 i_guess_your_number.py output

16 16 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

17 17 Expression values >>> 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 Expressions may have integer values 0 is false, non-0 is true.

18 18 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


Download ppt "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."

Similar presentations


Ads by Google