160 likes | 235 Views
Student Q and As from 5.1 – 5.7, 5.11. Students of CS104, Fall 2013 (and me). Modulus operator. Q : What does 10 % 3 produce? A: 1 (10 / 3 is 3 remainder 1) Q: What does 112 % 10 produce? A: 2 Q: What does 112 % 100 produce? A: 12 Q: What does x % 2 == 0 mean ?
E N D
Student Q and As from 5.1 – 5.7, 5.11 Students of CS104, Fall 2013 (and me)
Modulus operator Q: What does 10 % 3 produce? A: 1 (10 / 3 is 3 remainder 1) Q: What does 112 % 10 produce? A: 2 Q: What does 112 % 100 produce? A: 12 Q: What does x % 2 == 0 mean? A: That comparison is True if x is even.
Modulus Usefulness Q: When would this modulus operator (%) be useful? A: Suppose you have defined a function turnLeftDegrees(deg) and someone calls it, sending in a value of 363. How far should you turn? What if it is called with 723? If you take do this, you won’t be spinning your wheels… deg = deg % 360 # don’t do extra circles
Practice Q: What are the results of these expressions?: glick = 33 % 7 blick = 33 / 7 flick = 330 % 70 plick = 330 % 100 A: 5, 4, 50, 30
Relational operators Q: What 6 relational operators are there, for comparing two numbers, say, x and y? A: x < y x > y x <= y x >= y x == y x != y
Relational operators Q: What 6 relational operators are there, for comparing two numbers, say, x and y? A: x < y x > y x <= y x >= y x == y x != y
Result of a relational operator? Q: Operators produce results, right? 3 + 4 produces 7. 3 / 4 produces 0. x – y produces the value x with y subtracted. What values can relational operators produce? (e.g., x == y, x <= y, etc.) A: Boolean values: either True or False. (Named after George Boole)
not operator Q: When is this useful? A: Sometimes it is just really nice to use not instead of not using it. Or, if you have a function that returns a boolean value. Suppose we have a function isEven(): if not isEven(num): print str(num) + “is odd” You could writeif isEven(num) == False: etc.
Format of an if statement Q: What is the general format of an if statement? A: if <boolean expression> : <body of code> e.g.: if x > y: print x, “is greater than”, y
Format of an if-else statement Q: What is the general format of an if-elif statement? A: if <boolean expression> : <body of code> else: <body of code>
Format of an if-elif statement Q: What is the general format of an if-elif statement? A: if <boolean expression> : <body of code> elif <boolean expression> : <body of code> (elif part can be repeated, and optional else clause on end)
Example of use Suppose you have a function called turnDegrees(deg) which takes a positive or negative value in deg. You want to turnLeft() if deg is positive, turnRight() if negative, and do nothing if 0. How would you write this code? if deg > 0: # this code in turnDegrees() turnLeft(deg) elif deg < 0: turnRight(-1 * deg)
Example of if elif elif … else Write code to print out the letter grade for a student. > 90 is A, 80 – 89 is B, etc. Value is held in variable grade. if grade > 90: print ‘A’ elif grade > 80: print ‘B’ elif grade > 70: print ‘C’ elif grade > 60: print ‘D’ else: print ‘F’
How about this? Q: What does this code do? # get value from user into variable grade if grade >= 90: print "A” if grade >= 80 : print "B” else: print "F” A: if grade >= 90, prints out A and B (on 2 lines); prints out B if grade between 80 and 90; prints F for grades lower than 80
Nested conditional Q: Write code that prints the word “odd” or “even” depending on the value of variable anInt, and also prints “greater than 10” if the odd number has a value greater than 10. A: if anInt% 2 == 0 : print"even” else: print"odd” if anInt> 10: print"greater than 10”
Keyboard input • Built-in function raw_input(somestr) prints out somestr and waits for user to type stuff in and hit <enter>. It returns what was typed – a string. • If you want the result to be an int, you have to convert it. Same for float, etc. • That’s all there is to it.