This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Starting up Python ECS 10
09/29 From From START menu, pick “all programs”, then Python Pick Pick the “IDLE” option IDLE
IDLE IDLE is an interpreter interpreter Responds Responds to input line-by-line line-byIf If you input data, it tells you back what it thinks it is. So So with numbers, it acts like a calculator Remainder
0,1,2,3… 0,1,2,3… and -1,-2,-3…. are integers 1,-2,integers 7/3 integer 7/3 is integer division 7%2 7%2 = 1 % gives the remainder when 7 is divided by 2 gives remainder when (7/2)*2 (7/2)*2 + (7%2) = 7 Floating point numbers
7.0, 7.0, 2.0,0.0006, 7.34 – floating point numbers 7.0/2.0 7.0/2.0 = 3.5 – floating point division If If either number is floating point, so is the answer – so 7.0/3 produces the value 3.5 0/3 Floating point is not exact
>>> 8.0/3.0 2.6666666666666665 This This is weird…why? Computer Computer numbers have a fixed number of decimal places Exact Exact results with floating point numbers would have an infinite number of decimal places: Example: 8.0/3.0 has the value 2.666666……. 1 Data types
Integers Integers and floating point numbers are two different data types in Python data in Can Can do different things with them. 3/2 diff 3/2 is different from 3.0/2 3.0 3.0 is different from 3 Strings
Another Another data type – for text. ‘A ‘A rose is a rose.’ is a string. string “9.0/7.0” “9.0/7.0” is also a string, because it’s in quotes Without Without the quotes, Python does not understand that it is a string. Strings Strings in English Errors
Lots Lots of things you do will cause errors Something Something Python doesn’t understand It It trys to tell you what it doesn’t like, but in this case it is more like, ‘huh?’ lik ‘h It It didn’t understand that we meant to type a string and forgot the quotes. I thinks we were trying to type Python thinks commands. Python Python commands
IDLE IDLE does Python commands immediately print print is a Python command >>> print 'A rose is a rose.' A rose is a rose. Print
Can Can print out any type of data (integer, floating point number, string). Can Can put multiple things in the same print command command. ….Printing out the text in the string.
>>> 'A rose is a rose.' 'A rose is a rose.’ …Responding to data (a string). 2 Making a program
Do Do something more complicated Remember Remember and repeat a bunch of commands A program
A program is a list of statements in a file program Python Python executes the statements one by one, just as if you had typed them in. It does them in the order in which they appear in the file. Your Your program
Uses Uses print Uses Uses some arithmetic? Variables
>>> x = 2.0 >>> x = x+3.0 >>> x 5.0 x is a variable is variable “x “x = 2.0” is called an assignment statement assignment statement Variable Variable on left-hand side gets value on rightleftrighthand side. Pronounce Pronounce this “x gets 2.0” or “x becomes 2.0” Variables
>>> x = 2.0 >>> x = x+3.0 >>> x 5.0 Assignment Assignment statement puts a data value into variable x, it is not itself a data value or an expression. IDLE IDLE does not type a value back after an assignment statement the way it does after an expression. 3 ...
View Full Document
- Winter '07