Presentation is loading. Please wait.

Presentation is loading. Please wait.

Student Q and As from 5.1 – 5.7, 5.11 Students of CS104, Fall 2013 (and me)

Similar presentations


Presentation on theme: "Student Q and As from 5.1 – 5.7, 5.11 Students of CS104, Fall 2013 (and me)"— Presentation transcript:

1 Student Q and As from 5.1 – 5.7, 5.11 Students of CS104, Fall 2013 (and me)

2 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.

3 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

4 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

5 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

6 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

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

8 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 write if isEven(num) == False: etc.

9 Format of an if statement Q: What is the general format of an if statement? A: if : e.g.: if x > y: print x, “is greater than”, y

10 Format of an if-else statement Q: What is the general format of an if-elif statement? A: if : else:

11 Format of an if-elif statement Q: What is the general format of an if-elif statement? A: if : elif : (elif part can be repeated, and optional else clause on end)

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

13 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’

14 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

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

16 Keyboard input Built-in function raw_input(somestr) prints out somestr and waits for user to type stuff in and hit. 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.


Download ppt "Student Q and As from 5.1 – 5.7, 5.11 Students of CS104, Fall 2013 (and me)"

Similar presentations


Ads by Google