Lecture Materials

Class Announcements

Sections start today!


Learning Goals

The goal of today's class is for you to be able to track through more complex Arithmetic in Python.

Questions & Answers


Q: wait what button did he hit to make that command into one line?

A1:  ‘Enter’ - he’s on powerpoint :) You just need to type it manually!


Q: Hi, does this work for numbers of all digits?

A1:  These operators? Yes


Q: Can it give a negative ? (ex. num3=num2-num1)

A1:  Yep


Q: Where could we get more information on what's being done in Peru? Fellow Peruvian here wanting to get involved..

A1:  Shoot us (in particular Mehran) an email and we can get you some resources!


Q: Will you get a float even if there’s an integer answer?

A1:  If you combine a float and an integer, you’ll get an integer


Q: Does 5/1 give a float also? Even though it would be an integer answer?

A1:  Actually, quick correction: 5/1 is also a float (it gives you 5.0)

A2:  That would be an int, but 5/1.0 or 5.0/1 would be a float!


Q: Can you not use ^ to raise to a power?

A1:  Can you not use ^ to raise to a power?

A2:  That does something different in Python!

A3:  No, that does something different in Python :)


Q: how is the second subtraction sign different from the first?

A1:  The first is subtraction and the other is negating a number (multiplying by -1)


Q: how does python know you want negation rather than subtraction if they both use the “-“ symbol?

A1:  If it’s between two numbers, it’s subtraction - if it’s before the first number, it’s negation. To use both, we’d do something like 42 - (-100)


Q: Can you explain the 5 % 2 operation? What does the 1 mean in this case?

A1:  It’s the remainder when you didivde 5 by 2!

A2:  Can you explain the 5 % 2 operation? What does the 1 mean in this case?


Q: I understand how it is functionally different, but how is it different in terms of how you type it into the computer?

A1:  It’s not, same character :)


Q: is there a function for the integer portions of a float in phython ?

A1:  there is a function called “floor” which drops everything after the decimal place (though the result is still type float)


Q: Why is it always a float when dividing by two ints? Just curious

A1:  Dividing an int by an int doesn’t always give you a whole number (i.e. 7/2) but all the other operations do!


Q: when you type num1 + 7 , does python assume that 7 is an integer? (or any number you type)

A1:  Yep, provided you don’t surround it with quotes (which would make it a string) or something


Q: Will float() also work on number strings?

A1:  Will float() also work on number strings?

A2:  Yep - float(“4.2”) will give you back 4.2


Q: When you assign a variable (i.e. 9), how does python know if it’s an int or a string?

A1:  live answered


Q: what happens if you write num1=float(num1)? Did we replace or created a new value? And if we created a new value, how do we call for the original num1?

A1:  We’d replace the contents of the num1 box. You wouldn’t be able to access the whole value.


Q: where is the new value of num1 stored?

A1:  Whatever is on the left hand side of the equals sign


Q: what's the difference between a float and a string?

A1:  A string represents text and is surrounded by quotes


Q: so int(value) is the same thing as floor(value) except that int is an integer and floor is a float ?

A1:  Floor isn’t a function in Python!


Q: is there a function to round values? Might be useful for sorting through data…

A1:  There’s a function called round!


Q: Can we properly round floats? Like we are just chopping off the decimal point but could we also go from like 1.9 to 2

A1:  There’s a function called round!


Q: If we were to assign a varible to a word, will python automatically know that this is a string or do we always have to put quotation marks around the word?

A1:  Nope! If you say str = banter, it will look for a variable called banter so you need to say str = “banter”


Q: Can you write num += 1 as num++?

A1:  Not in Python


Q: Sorry but I still don’t understand why num3-1 would be .899999 instead of just .9 ??

A1:  Python isn’t able to represent *every* decimal number precisely, so it often has to resort to whatever is closest! Come chat in office hours or post on Ed if you’re interested in why


Q: Does the expression num1 = num1 + 1 , mean that num1 then becomes num1 = 6?

A1:  Assuming num1 is 5 initially, yes


Q: can you define your own shorthands

A1:  ish :) We’ll get there a little later in the class


Q: why do we need float in the average program ? int would have worked too right ?

A1:  well then if you tried to average 2.3 and 4.5 it wouldn’t work :)


Q: So converting from float to integer will NOT round the number? For example, 4.7 would turn into 4?

A1:  Yep!


Q: Is there a round, ceil, and or floor function?

A1:  They’re called round, math.ceil and math.floor

A2:  all three!


Q: Is there anything we can do in the way of rounding/some code we can input to get python to truncate the answer and represent it nicely or do we just have to suffer with .899999999

A1:  There are, but it’s a little messy - post on Ed or come to office hours if you’re interested!

A2:  we suffer… only way is to use a string representation (which is hard to work with) or i once saw someone represent fractions by storing the numerator and denominator as separate variables… wild world…


Q: How do set a certain number of sig figs? whats the max default anyway?

A1:  It’s a little messy - post on Ed or come to OH if you’re interested! There isn’t really a ‘default’

A2:  Its a good question. As you noticed mehran didn’t chose to go too deep into the standard. It does get complex — and you have so much to learn anyways. Floats are limited by their “precision” not by the number of significant figures. Its wild


Q: Why isn’t python accurate in terms of the division?

A1:  Basically, there are a finite number of decimal numbers that Python can represent precisely so it sometimes needs to just show you whichever decimal is closest to the correct value.


Q: Is there a textbook of some sort that has all of this info like the Karel reader but for python?

A1:  Yes, we have some handouts - I’ll post those later today


Q: are there any constants built into python?

A1:  Yes! We’ll talk about how to use those a little later this quarter.


Q: What's the defualt number of decimal places that a float returns? Like if I did 22/7 how many decimal places will be returned?

A1:  There isn’t really one - when you try to print it, it truncates after a few but it’s really just a question of how it’s represented internally.

A2:  no. floats always have many decimal points. The limitation is is in precision, not on decimal points


Q: Are any constants built into python? Or do they all need to be entered by the user?

A1:  Yes! We’ll get to how to access them later on this quarter.


Q: Is PI built into python or do you as the programmer have to assign it a value?

A1:  It is, but we’ll talk about how to access it later!


Q: Is there any difference between a constant and a variable except for the all-caps?

A1:  Nope


Q: To just to make sure, “magic numbers” are like 0 and 1?

A1:  They’re numbers that just randomly appear in your code without any explanation of why they have that value


Q: what is “feet” defined as in that example?

A1:  It’s just a float variable


Q: re: Vincent Xie's question Variables vs Constants, don't Variables only apply to the specific function you apply them to? we discussed Scope of Variables?

A1:  Oh, good point! I meant that the way they store values is the same. Yes, constants are available everywhere.


Q: is the math library built into python or the IDE? or do we have to download it separately?

A1:  is the math library built into python or the IDE? or do we have to download it separately?

A2:  It’s built in!

A3:  python! no download necessary


Q: is there a way to check if a function or constant is in the math library?

A1:  https://docs.python.org/3/library/math.html is the definitive reference


Q: so these commands cannot be used if math is not imported?

A1:  Yep


Q: would you define a constant above or below the import math

A1:  Before import math and above the functions


Q: Can we print an integer or float without first casting it as a string?

A1:  Provided you’re not adding it to a string, yes


Q: Question? def math.pi(): PI=3.1415

A1:  defining the constant should go outside any function


Q: where is math being imported from? why do we have to explicitly import it instead of being “built in” ?

A1:  It’s built in to Python, but we want it to be available in this particular program


Q: so when you download a library, you do library.something to acess that object in the library?

A1:  Yes!


Q: can you only print string-type things? Like you couldn’t print num1 as an integer?

A1:  You can, but only because if you print(7), Python automatically converts the 7 to a string before you print. If you add it to a string, you need to manually do it.


Q: Why don’t computers have “true” randomness?

A1:  its really hard to know what true randomness is! one of the world experts is a prof here Percis Diacanosis. The gold standard comes from background radiation from space and some super specific computer have random numbers based on space!

A2:  Great question! It turns out this is an incredibly difficult thing to do unless you have access to an actually random process (like universal background radiation or radioactive decay). This is because computers are totally deterministic machines.


Q: isn’t uniform for uniform distribution?

A1:  Yep


Q: How do you multiply random.random() to get a wider range?

A1:  You get a number between 0 and 1. If you want to get a number between 0 and 10, just multiply the random number by 10 :)


Q: How is Mehran running py rolldice.py so quickly? is there a shortcut to avoid writing down the whole file name?

A1:  live answered


Q: I’m a little confused as to why you would want to seed multipe times

A1:  Basically, so that the random numbers you get each time are the same, which is useful as you test


Q: What happens when you increase the value of seed?

A1:  Nothing predictable - it’s just that the random numbers you get back are different.


Q: what does 1 in the random.seed() mean?

A1:  Just the input to the random number generation process


Q: can you random.seed(random.random())?

A1:  You can, but that wouldn’t be very helpful :)


Q: Can you explain what the random.seed(x) function does again?

A1:  Basically, random number generation is a process that starts with some initial value that is different very time. Random.seed(x) sets that value to be equal to x at the beginning, so the random numbers you get back are the same.


Q: What does random.uniform do?

A1:  random.uniform(a, b) returns a random number between a and b. All numbers are uniformly likely to be returned.


Q: what is "import" for ?

A1:  To make the random library available in this program


Q: Shouldn’t PEP-8 require Mehran to have two speces in between his functions?

A1:  Yes - we’re not tremendously fussy about that right now.


Q: do you always put a 1 in the seed(1)

A1:  You can put any number in there, it doesn’t really matter


Q: So typing random.seed(1) will seed any program?

A1:  Yep


Q: “Just the input to the random number generation process” but what happens if we enter 2?

A1:  2 is input into the process and you get a different bunch of random numbers back! If you stick around after Q&A we can talk about it in more detail


Q: Is there a way to call the die1 variable from main() in the roll_die() fcn?

A1:  Not yet :) Stick around for a few lectures!


Q: So if die 1 is in roll_dice and roll_dice is in main, can die_1 be used in main and vice versa?

A1:  die1 in those two functions are separate variables


Q: is it good practice to reuse the variable name? Why would I use die1 twice?

A1:  sometimes! It makes it clear that they are the same conceptually


Q: could you use a for i in range for roll_dice?

A1:  yes well talk more about that


Q: what is the purpose of defining die1 in main?

A1:  Just to show you that it’s separate from die1 in roll_dice


Q: could you do for i in range (3): roll dice ?

A1:  yes!


Q: what is the 1 in the parentheses next to NUM_SIDES

A1:  its the minimum value for the random number


Q: stylistically, why might you define a function before main()?

A1:  stylistically, why might you define a function before main()?

A2:  i think its good practice to put main first..


Q: if we want to keep a better style, is it better not to use the same variable name across different functions?

A1:  Not really! The variable name should communicate its purpose in that function


Q: Is there any way to change colors for "die1" in Main vs "die1" in any random function in order to make the scope of the variable visually obvious?

A1:  that would be sweet. But no, that is pycharm to decide


Q: why the different color “10”?

A1:  to make it clear its a string


Q: So what is the point of stating die1 in the main function?

A1:  Just to show you that it’s not the same as the one in roll_dice!


Q: so is it a bad idea to have the same name in different functions?

A1:  no its very common. Its hard to think of new names all the time


Q: Does it matter where and when you define a constant? Does it have to be under main function?

A1:  thats the standard!


Q: whats the point of die1 in main()?

A1:  pedagogy


Q: How do you make is so that the output of the functions that are called *do* affect/ update your variables in "main"?

A1:  good question. Well talk about parameters and returns on monday


Q: Is there a way to comment out a lot of lines of code at once?

A1:  teach you


Q: what was the purpose of defining die1 as 10 in main?

A1:  live answered