Download presentation
Presentation is loading. Please wait.
Published byAubrey McCarthy Modified over 9 years ago
1
General Computer Science for Engineers CISC 106 Lecture 04 Dr. John Cavazos Computer and Information Sciences 09/10/2010
2
Course Overview Explain lab00.py on computer Show it running. Add errors. Atomic data and variables Sample Function Function composition
3
Atomic Data : Numbers and Strings - Numbers : floats, integers Can use “type” to find out the data type of something. type(5) # int type (5.0) # float -Strings Some examples : “hello” and ‘hello’ type(“hello”) # str “hello” + “world” # plus symbol concatenates two strings
4
Atomic Data (cont’d) # Can mix and match numbers >>> 3 + 5.0 8.0 # Cannot mix numbers and strings “hello” + 3 # Builtin convert between types str(100) int(“5”)
5
Variables 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
6
Variables (cont’d) Creating Variables with Assignment Statements Assignment Statement variable = expression >>> age = 25 where, variable name of the variable = assignment operator expression value or piece of code that results in a value The age variable references the value 25 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
7
Variables Naming Rules Can not use Python key words Can not contain spaces First character must be one of the letters a through z, A through Z, or an underscore character(_) After the first character use letters a through z, A through Z, digits 0 through 9, or underscore Uppercase and lowercase characters are distinct Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
8
Variable Naming Conventions For readability use one of the below styles for multiword variable name: Use the underscore character to represent a space gross_pay hot_dogs_sold_today camelCase naming convention grossPay hotDogsSoldToday Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
9
Same Variables Names Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
10
circleArea function def 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)
11
Now, what if we want to calculate area of a ring A ring of two concentric circles we can use our circleArea function = -
12
Area of a ring pi * (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!
13
ringArea function def ringArea(r1, r2) : “”” calculates area of ring given outer r1 and inner r2 r1 – number r2 – number return – number “”” return circleArea(r1) – circleArea(r2)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.