Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python Programming Lecture II. Getting Started with Python When you start idle Python prompt should appear looking something like this: Python 2.3 (#46,

Similar presentations


Presentation on theme: "Python Programming Lecture II. Getting Started with Python When you start idle Python prompt should appear looking something like this: Python 2.3 (#46,"— Presentation transcript:

1 Python Programming Lecture II

2 Getting Started with Python When you start idle Python prompt should appear looking something like this: Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>

3 Error Messages Error messages are nothing to be scared of, read them carefully, they usually give a clue as to why you got them. But it's only a clue... if in doubt check the lines immediately before the reported line.

4 Printing Strings >>> print 'Hello there!' The print command is the way to get Python to display its results to you. In this case it is printing the sequence of characters H,e,l,l,o,,t,h,e,r,e,!. Such a sequence of characters is known in programming circles as a string of characters or a character string or just a plain string. You signify a string by surrounding it in quotes. In Python you can use either single quotes(as above) or double quotes: "a string ". This allows you to include one type of quote within a string which is surrounded by the other type - useful for apostrophes:

5 Printing Numbers >>>print 6 + 5 It's not just characters that can be printed: Here we have printed the result of an arithmetic operation - we added six and five. Python recognized the numbers as such and the plus sign and did the sum for us. It then printed the result. We can use other arithmetic operators: –subtract(-) –multiply (*) –divide (/)

6 Parentheses We can combine multiple expressions like this: >>> print ((8 * 4) + (7 - 3)) / (2 + 4) Note the use of parentheses: always evaluate items inside parentheses first. Precedence of operators: some operators are “more binding” than others

7 Integer and Real Numbers Integers are whole numbers Real numbers include the spaces between the whole numbers, and are usually written with a decimal point. Note that >>> print 5/2 gives an integer answer (=2) This is because Python sees that the numbers are whole numbers and assumes you want to keep them that way. How does it see they are whole numbers?

8 Decimal Results If you want decimal fractions as a result simply write one number as a decimal: >>> print 5/2.0 2.5 Python sees the 2.0 and realizes that we are happy dealing with fractions (referred to as real numbers or floating point in computing parlance), so it responds with a fractional result.

9 Data Types We have now seen how to print results including strings, integers, and real numbers. Each of these is a different type of data, or datatype characterised by: –a distinctive notation; –a distinctive kind of value; –a distinctive set of operations for manipulating the values. Often we need to print expressions of mixed type.

10 Printing Mixed Expressions Here we can print a mixed expression by giving print an argument list i.e. several arguments of different types separated by commas >>>print ‘The total is ‘, 23+45 This is fine provided that the result reflects exactly the order of the arguments This is not always the case. I may wish to intersperse the numbers inside the string, e.g. the sum of 3 and 3 is 6 One cumbersome way to do this is as before >>>print ‘the sum of ‘, 3, ‘and’, 3, ‘is ‘, 6

11 Format String A format string offers a more flexible solution >>>print “the sum of %d and %d is %d”% (3,3,6) Format strings support different datatypes: %d decimal integer %s string >>> print “%s %d %s %d %s %d” % (“the sum of”,3,’and’,3,’is’,6) There are many other datatypes available.

12 Import When you start Python there are a bunch of commands available to you called built-ins Python can extend the list of commands available by incorporating extension modules >>> import sys The command import makes available all the items defined in a “module” called sys. We can use those items by adopting the following notation module-name.item-name()

13 Parentheses >>> sys.exit() This command will normally cause the system to exit (provided the sys module has been loaded first). Note the use of parantheses to indicate that the object is a function (in this case the list of arguments is empty).

14 Summary Printing strings Printing numerical expressions Integer and real numbers Format Strings Import


Download ppt "Python Programming Lecture II. Getting Started with Python When you start idle Python prompt should appear looking something like this: Python 2.3 (#46,"

Similar presentations


Ads by Google