Top Banner
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
13

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

Jan 02, 2016

Download

Documents

Aubrey McCarthy
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: General Computer Science for Engineers CISC 106 Lecture 04 Dr. John Cavazos Computer and Information Sciences 09/10/2010.

General Computer Science General Computer Science for Engineersfor Engineers

CISC 106CISC 106Lecture 04Lecture 04

Dr. John CavazosComputer and Information Sciences

09/10/2010

Page 2: General Computer Science for Engineers CISC 106 Lecture 04 Dr. John Cavazos Computer 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

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

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

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

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”)

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

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

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

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

The age variable references the value 25

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

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

Variables Naming RulesVariables Naming Rules

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

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

Variable Naming Variable Naming ConventionsConventions

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

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

Same Variables NamesSame Variables Names

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

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

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)

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

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

= -

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

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!

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

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)