108HL16 - Midterm Review Lecture 16, Week 6 February 14,...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Midterm Review Lecture 16, Week 6 February 14, 2011 CSC108H1S Velian Pandeliev 1 Monday, February 14, 2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Operators and Operands An operator is a symbol that indicates a simple operation (i.e. +, -, *, /) An operand is a value on which operations are performed A combination of operators and operands that evaluates to a single value is called an expression 2 Monday, February 14, 2011
Background image of page 2
Data Types in Python 3 Type Example Description int 17 integers between -2147483647 and 2147483647 long 3000000000L integers outside the range above float 3.14159 foating-point decimal e.g. 1.23 or 7.8e-20 bool False a Boolean (‘True’ or ‘False’) str "Hello!" a string o± characters (text) list [1.3, 5, "Hi"] a collection o± other values Monday, February 14, 2011
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Assignment Statement 4 A line of code that tells Python to do something is called a statement . In Python, the assignment statement is used to assign values to variables. It looks like this: variable = expression E.g. >>> x = 8 >>> x 8 Monday, February 14, 2011
Background image of page 4
Functions 5 In Python, we defne a function using the following syntax: def function_name ( par1 , par2 ...) : statement1 statement2 statement3 ... We call a function like this: function_name(arg1, arg2,. ..) Monday, February 14, 2011
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Example: A function that returns the average of two numbers def average(a,b): avg = (a + b) / 2 return avg x = average(4,6) The return Statement 5 name value 1 average (function) 4 avg 5 2 x 5 7 5 6 a 4 b 6 3 Monday, February 14, 2011
Background image of page 6
print vs return 7 print and return do very different things: print is used to display information to the user by outputting it to the screen. It can be used anywhere, as many times as is necessary, but it only gives information to the user. return is used to extract a value from a function for further use inside a program (in fact, it's the only way to extract a value from a function). It can only appear at the end of a function body and it passes information to other parts of the program, not to the user. Monday, February 14, 2011
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
vs Parameters 8 raw_input( prompt_string ) is the input equivalent of a print statement - it is used to ask the user for input. It is a statement with a very speciFc use case. Passing a value to a function as a parameter is the best and most versatile way to give a function the information that it wants. 90% of the time functions require information from other parts of the program, not from the user, and those other 10% are usually very clearly described. Monday, February 14, 2011
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 32

108HL16 - Midterm Review Lecture 16, Week 6 February 14,...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online