cs 177 week 4 recitation slides

25
CS 177 Week 4 Recitation Slides Variables, Files and Functions

Upload: lana-ramsey

Post on 03-Jan-2016

26 views

Category:

Documents


2 download

DESCRIPTION

CS 177 Week 4 Recitation Slides. Variables, Files and Functions. Announcements. ANY QUESTIONS?. Table of content. Variables Files Functions What is a function? Why to use a function?. Are names for objects that have values in main memory. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 177 Week 4 Recitation Slides

CS 177 Week 4 Recitation Slides

Variables, Files and Functions

Page 2: CS 177 Week 4 Recitation Slides

Announcements

Page 3: CS 177 Week 4 Recitation Slides

ANY QUESTIONS?

Page 4: CS 177 Week 4 Recitation Slides

Table of content

• Variables • Files • Functions

o What is a function? o Why to use a function?

Page 5: CS 177 Week 4 Recitation Slides

Variables

• Are names for objects that have values in main memory.

• We define variables by assigning values to names: Example: >>>a = 9 >>>b = 13.54 >>>c = “Computer”

• Variables can change their values during the program.

• It’s important to choose good names that describes the variable. Example: firstName, courseNo, etc.

Page 6: CS 177 Week 4 Recitation Slides

Assignment Statement

Example:X = 10

• Variable is also called Left Hand Side.• Value is also called Right Hand Side.

Variable Value

Page 7: CS 177 Week 4 Recitation Slides

Assignment Statement

• The value can be: a number, a string of characters, but also a variable, or an expression.

• Example (decimal integer numbers): X = 10 Y = X Y = X + 5 Z = Y

Variable Value

Page 8: CS 177 Week 4 Recitation Slides

Example 1

What is the output of the following statements:>>> X = 10>>> Y= 20>>> result = X+ Y>>> print result>>> Y= 100>>> print x + 10>>> print x>>> print y

X 10 Y 20100

result

output302010100

30

Page 9: CS 177 Week 4 Recitation Slides

Exercise 1

>>> name = “John”>>> price = 9.99>>> total = price / 3.0>>> name = “Jane”>>>print name>>>print price>>>print total

3.33total

9.99pricename

Jane9.993.33

output

JohnJane

Page 10: CS 177 Week 4 Recitation Slides

Exercise 2

>>> var1 = “Purdue”>>> var2 = “University”>>>print var1>>>print var2>>>var2 = var1>>>print var1>>>print var2

“Purdue”var1 var2

PurdueUniversityPurduePurdue

output

“University”

“Purdue”

Page 11: CS 177 Week 4 Recitation Slides

What about …

>>> X + 2 = 15>>> Y = “Hello” + 10

Tips:• Left Hand Side should always be a variable• You can’t perform operations (such as sum) on

two values of different types (numeric and string)

Page 12: CS 177 Week 4 Recitation Slides

Files

• Are collection of larger objects and values (byte) on the hard disk of the computer.

• Files can contain text, picture, sound, video, etc.

• Files have names• File names have suffixes

(.jpg, .wav, .mp3, and so forth)

Page 13: CS 177 Week 4 Recitation Slides

Functions

• Collection of instructions that perform a task as:o Printing your name and course.o Calculating the average of set of numbers.o Editing a picture or video.

Page 14: CS 177 Week 4 Recitation Slides

Functions

• Like in algebra, a function is a kind of “box” into which you put one value and out comes another. We represent (“denote”) a function by a name (in math we use f or F).

FoutputInput

Page 15: CS 177 Week 4 Recitation Slides

Why to use a function?

• If we define a function to perform a task, then we will write it once then we can use it (or call it) many times.

Page 16: CS 177 Week 4 Recitation Slides

How to write functions?

def functionName(): statement #1 statement #2 …

Page 17: CS 177 Week 4 Recitation Slides

• def SayHello: print(“Hello world!”)

print(“--From Python”)

Note: 1. Never forget the colon(:)

2. Align the statements in one function

Page 18: CS 177 Week 4 Recitation Slides

• No input arguments, no variable returned

• def Greet():    print("Hello Jack")    print("Hello Tom")def main():    Greet()

Page 19: CS 177 Week 4 Recitation Slides

• Multiple arguments, returns one result

• def Average(a, b):    return (a+b)/2def main():    ave = Average(3, 4)

Page 20: CS 177 Week 4 Recitation Slides

• Returns more than one variable

• def getabc():    a = "Hello"    b = "World"    c = "!"    return a,b,cdef main():    a, b, c = getabc()

Page 21: CS 177 Week 4 Recitation Slides

• def Sum(a, b, c):    return (a+b+c)def Greet(name, GPA):    print("Hello", name)    print("You have a GPA of ", GPA)def Div(a, b):    return a/bdef Mul(a, b):    return a*b

main()

Page 22: CS 177 Week 4 Recitation Slides

• # assign values to variables

G1 = 4    G2 = 3.7    G3 = 3.7    C1 = 3    C2 = 1    C3 = 2    name = "Tom“

Page 23: CS 177 Week 4 Recitation Slides

•     GPA = Div(Sum(Mul(G1, C1), Mul(G2, C2), Mul(G3, C3)), Sum(C1, C2, C3))    Greet(name, GPA)        input()

• The output of Mul() is input of Sum()

• The output of Sum() is the input of Div()

Page 24: CS 177 Week 4 Recitation Slides

What can go wrong?

• If your parrot is dead, consider this:o Did you use the exact same names (case, spelling)?o All the lines in the block must be indented,

and indented the same amount.o Variables in the command area don’t exist in your functions, and

variables in your functions don’t exist in the command area.o The computer can’t read your mind.

It will only do exactly what you tell it to do. In fact, programs always “work,” but maybe not how you

intended!

Page 25: CS 177 Week 4 Recitation Slides

Final QUESTIONS???