general computer science for engineers cisc 106 lecture 04 dr. john cavazos computer and information...

13
General Computer Science General Computer Science for Engineers for Engineers CISC 106 CISC 106 Lecture 04 Lecture 04 Dr. John Cavazos Computer and Information Sciences 09/10/2010

Upload: aubrey-mccarthy

Post on 02-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

General Computer Science General Computer Science for Engineersfor Engineers

CISC 106CISC 106Lecture 04Lecture 04

Dr. John CavazosComputer and Information Sciences

09/10/2010

Course OverviewCourse Overview• Explain lab00.py on computer• Show it running. Add errors.

• Atomic data and variables• Sample Function• Function composition

Atomic Data : Numbers and Atomic Data : Numbers and StringsStrings- Numbers : floats, integers

Can use “type” to find out the data type of something.

type(5) # inttype (5.0) # float

- Strings Some examples : “hello” and ‘hello’

type(“hello”) # str

“hello” + “world” # plus symbol concatenates two strings

Atomic Data (cont’d)Atomic Data (cont’d)# Can mix and match numbers>>> 3 + 5.08.0

# Cannot mix numbers and strings“hello” + 3

# Builtin convert between typesstr(100)

int(“5”)

VariablesVariables

Concept:

A variable is a name that represents a value stored in the computer’s memory.

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Variables (cont’d)Variables (cont’d)

The age variable references the value 25

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Variables Naming RulesVariables Naming Rules

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Variable Naming Variable Naming ConventionsConventions

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Same Variables NamesSame Variables Names

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

circleArea functioncircleArea functiondef circleArea(radius) : “””

computes the area of a circle

radius – number return – number “””

PI = 3.1415 # constant variable (use caps) return PI * radius ** 2

assertEqual(circleArea(4), 50.264)

Now, what if we want to Now, what if we want to calculate area of a ringcalculate area of a ringA ring of two concentric circleswe can use our circleArea

function

= -

Area of a ringArea of a ringpi * (r1) ** 2 – pi * (r2) ** 2 first circle second circle

circleArea(r1) - circleArea(r2)

Note: Can create a function called ringArea that takes two parameters and calls circleArea twice!

ringArea functionringArea functiondef ringArea(r1, r2) : “””

calculates area of ring given outer r1 and inner r2

r1 – number r2 – number return – number “””

return circleArea(r1) – circleArea(r2)