This preview shows pages 1–4. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Engineering Computation: Tutorial 01 Problem Solving with Computing, using the Python Language Questions from Week 00? Programming Exercises from Zelle textbook 1 pg. 22 # 1 2 pg. 23 # 2 5 (you will need to have read Chapter 1 to attempt these). 2 Friendly note: practice, practice, practice ... You learn to drive by driving. You learn to play the guitar by playing the guitar. You learn to play hockey by playing hockey. You learn to program by programming. So try not to spend all your time driving, playing guitar and playing hockey. Make some room for Python :) 3 During the teaching session, you should start IDLE and try to replicate the code samples. Quick Review Calculate 14.0 to the 6th power. 4 Whats in your toolbox? 1 Python print statement 2 String literals 3 Basic calculator 4 Integer division 5 Todays Topics: 1 Variables 2 Variable types 3 Assigning values to variables 4 Some built in functions: type() id() str() int() float() abs() round() range() 5 Some math library functions 6 Straightline (nonbranching) programs. 7 Introduction to conditionals (branching programs) 6 Variables Introduction to Program Variables: jarmodel need to calculate values that represent physical entities like: velocity acceleration time other data items manipulate and store data. a variable is bound to a value as if saying this is x. >>> x = 5 >>> 7 How to name Variables Variable naming conventions 1 Variable names cannot start with a number. 2 Extremely short or very long variable names can be hard to work with in certain contexts. 3 Use names of an appropriate length that are meaningful . For example: thisIsMyVeryLongNameForAVariable = "bad" , would be annoying. a=5 is not very meaningful. What is "a" ? 4 Python keywords (e.g. print, for, in ) cannot be used as variable names. 5 Variable names are part of your programs documentation. 8 Example 1 What does this code do? >>> a = 4 >>> b = 91 >>> c = 78 >>> d = 65 >>> e = 81 >>> f = b >>> f = f + c >>> f = f + d >>> f = f + e >>> g = f / a >>> Its not intuitively obvious what this program does. It can be improved easily by giving the variables useful names. 9 Example 1 What does this code do? >>> eng1d04 = 91 >>> eng1c03 = 78 >>> eng1p03 = 65 >>> chem1e03 = 81 >>> numCourses = 4 >>> sum = eng1d04 >>> sum = sum + eng1c03 >>> sum = sum + eng1p03 >>> sum = sum + chem1e03 >>> average = sum/numCourses >>> There is still room for improvement in the code that calculates the average. What can you do to make it better? 10 Example 1 nice work! >>> eng1d04 = 91 >>> eng1c03 = 78 >>> eng1p03 = 65 >>> chem1e03 = 81 >>> numCourses = 4 >>> average = ( eng1d04 + eng1c03 + eng1p03 + chem1e03 )/ numCourses >>> average 78 >>> 11 Variables: number types int : plain integers (32 bits of precision) >>> 3 long : long integers (unlimited precision) >>> 3L float : floating point numbers (computer approximation of real numbers; precision depends on the machine.) >>> 3.0 complex : complex numbers (with a real and imaginary part)...
View
Full
Document
This document was uploaded on 09/14/2011.
 Spring '10

Click to edit the document details