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: Hello, I am having problems with submitting my karel assignment, when trying to upload the folder it gives me an error. How can I fix this problem? Should I zip the assigment?

A1:  Instead of uploading a folder, you should upload the .py files in which you wrote code! The specific files are listed at the end of the assignment handout.


Q: how do we submit assignments?

A1:  Check out the “Submitting Assignments” handout on the website


Q: Is there another way we can get help on the karel assignment if we’re really stuck if the queue for laIR is full?

A1:  Mehran has office hours after class today and Chris has office hours tomorrow morning! You can also email your section leader with other questions!


Q: For midpoint karel, does it only have to work in the files with midpoint in the name, or does it also have to work on 3x5, for example?

A1:  It should work on any world :). But its optional. So anything is good


Q: Where can I find what files I need to submit for Assignment 1? I can't find a list at the end of the Assignment handout. Thanks!

A1:  The four .py files that you wrote code in, midpoint if you wrote that progra, and Extension.py if you wrote one!


Q: Do functions like turn_right() and turn_around() need to have comments on the pre and post conditions?

A1:  It is nice to have but not necessary.


Q: any suggestions for how to start checkerboard karel?

A1:  Think about what is consistently repeated in all of the different worlds. Can you find any patterns?


Q: if we made some progress on an Extra credit assignment should we submit it even if its not complete?

A1:  Sure!


Q: can we use other libraries and variables for extension karel? or should we just use things in the karel reader even for the extension?

A1:  Only things in the karel reader for the extension.


Q: do we write comments for while/for loops or just for defining functions?

A1:  You can include them for while/for loops if you think that would be helpful. But they are not necessary.


Q: I was curious about what is the correct style for writing comments for your function. Are there specific phrases we should aim to use in writing comments or if the comments generally describe waht the function does is that ok?

A1:  Check out Chris and Mehran’s comments for code they wrote in lecture last week! They have great examples for how to write comments.


Q: Can you also store integar as float? and if so why would you sometimes choose to store as int rather than float

A1:  Come chat offline about this! But exactly what Mehran is saying right now. Floats are approximations and not all integers can be correctly approximated as floats.


Q: The Python Reader states we should use 4 spaces instead of tab for Python styling. Is that necessary?

A1:  It is best practice.


Q: Which lecture should we review if we want guidance on how to write comments in our code?

A1:  Any of the lectures from last week!


Q: What are the sections like? Is it just Q&A or is there material? Where do we get the zoom links for sections?

A1:  There is material! You can check out the handout for this week in the sections tab on the website. You can find the zoom link for your section at this link: https://cs198.stanford.edu/cs198/auth/section/ViewSections.aspx?class=1


Q: Are there spaces between num1, the sign, and num2?

A1:  Yes but that is stylistic preference.


Q: Can grad students take this C/NC? Or only for letter grades?

A1:  I believe grad students can take this C/NC as well. But please check with your advisor to ensure that taking this grade C/NC will still satisfy any necessary requirements.


Q: Do num1 and num2 have to be integers to use //? Or can they be floats?

A1:  They can be floats.


Q: For the Karel assignment, are we allowed to incorporate multiple conditions (such as 2 or 3) in our if statements using”and”?

A1:  Yes!


Q: Does python follow order of operations?

A1:  We will go over that now!


Q: could you say num3 = -num1 + num2 and get -3?

A1:  Yes!


Q: does negation work for integers and floats?

A1:  yes!


Q: does division always evaluate to a float?

A1:  Yes! Unless you use // which is integer division.


Q: What does the unary mean in ‘negation’(unary)?

A1:  It means to change the sign from positive to negative or negative to positive.


Q: can you write something like (2)(3) to multiply 2*3?

A1:  No, you need the * operator for multiplication.


Q: Is the homework due at the end of Friday or the start of Friday?

A1:  It is due at 1pm PDT on Friday.


Q: Why are subtraction and negation the same character?

A1:  Not sure exaclty why, but they are closely related so perhaps that is why.


Q: How does the code behave when attempting to divide with a repeating decimal quotient (e.g., num3 = 5/3)

A1:  First the code would evaluate num3 as an approximation of 5/3. Then it would use that value for num3.


Q: I’m a little confused, what does the % operation do?

A1:  That is the modulus function. It gives the remainder of the first number divided by the second. So 11%2 would be 1 because 11/2 is 5 remainder 1.


Q: Why can’t we represent 0.9 as it is? I didn’t understand

A1:  Python stores floating points as approximations and not all real numbers have exact approximations, which results in small roundoff errors.


Q: so for the arthimatic that gives us 0.89999999.. we cant really predict an exact answer? bcoz each computer may generate a different number? How to make it give 0.9 instead? Any rounding function we could use?

A1:  There is a function called “round” which can help if you need it.


Q: What if we have 2+ operators?

A1:  You mean like x = 3* x + 5? Then it will follow order of precedence


Q: Are the ones taught today the only Python operators or are there more? If there are more, can we use them in our assignments?

A1:  There are a few more, which you can use (but dont have to)


Q: If we went to the LAIR, do we need to write that when we submit our assignment?

A1:  no!


Q: Can we revisit how come 1.9 - 1 ≠ 0.9?

A1:  its because of how floating point numbers are represented. If you are interested here is a resource that goes super deep: https://docs.python.org/2/tutorial/floatingpoint.html. But for cs106a its sufficient to know: its hard to perfectly represent floats


Q: Then, is 0.9 an exception or is there many more real numbers which cannot be aproximated? and how de we know if python can approximate a number or not?

A1:  Many more! Generally I assume that for floats, the number is not exact


Q: how do we know to put float or int before the input?

A1:  You get to decide! Do you want the user to enter an integer or can they enter a decimal point?


Q: what's the shortkey to pull up your py file.py? windows

A1:  live answered


Q: Where can we find the code posted for each lecture?

A1:  Under the lectures tab, there is a page for each lecture where you can download the lecture code.


Q: So the shorthand of x = 3*x + 5 would be…?

A1:  You cannot do this in shorthand.


Q: if we put int before the input, will the program only produce int answers? or is there no correlation

A1:  When you ask for input, the user (you in this case) will type in the answer. If you type in a number with a decimal, your program will throw an error saying that you must enter an integer.


Q: If we have more than one operator and we wanted to use the shortcuts how could we?

A1:  You can only use the shortcuts with one operator at a time.


Q: Will we be expected to use numpy for this class?

A1:  we will introduce it, but we focus more on ideas than on libraries :)


Q: Could we control the number of digits after the decimal place? i.e. if we want the result to be 2.00 instead of 2.

A1:  Yes, you can use the round() function.


Q: Does Python have let & var types of variables? Or do we just use styling to indicate constants from variables?

A1:  style!


Q: If we define variables outside of a function, can all functions access them?

A1:  yes. You should only do this for constants!


Q: Is there a built in Pi in Python?

A1:  Yes! There is one in the math library


Q: Does Pytjon ensure that we cant change valiue of a constant? Like how Java does? or is it programmer’s responsibility to not update it?

A1:  It is the programmer’s responsibiity not to update it.


Q: Is there a way to apply a variable to the entire program? Like, do I define it in or before main?

A1:  You should avoid this at all costs. It leads to mayham later on. Fight the dark side

A2:  Yes, but you should not do this in 106A. You should only define constants outside of any functions.


Q: are constants defined in functions like main() or any other function we define, or can they be defined outside of functions

A1:  They are usually defined outside of functions.


Q: Does that mean we should generally write constants at the beginning of our code for better legibility and easier access?

A1:  yes!


Q: So for labeling functions use lower case letters and for labeling constants use upper case letters?

A1:  yes!


Q: When would one use commas to put things together in print function vs addition signs?

A1:  You can use commas when you are printing variables of a different type. To use addition signs, the variables must be the same type.


Q: can you import multiple libraries for a single program?

A1:  Yes!


Q: so variables must be assigned within every function?

A1:  yes, that is the way of the light side


Q: is there a master import statement?

A1:  no! There are way too many libraries :)


Q: why are math.pi and math.e not capitalized for being constants?

A1:  Its because they were defined in 1990 before python decided on its current “style” format! We are stuck with that inconsistency forever. Great question!


Q: If a constant is defined in a function, is it defined in the rest of the program?

A1:  no, sadly. So we define them at the top :)


Q: when will next assignment be posted and due?

A1:  Check here: http://web.stanford.edu/class/cs106a/schedule.html. Friday!


Q: In the average problem done before, the answer was 6.5 which is a float. If I put int(input) at the top instead of float(input), would it still have given me the float final answer?

A1:  as long as the user inputs an integer!


Q: Is there an import statement like import java.io.*

A1:  that is what import math does (it brings in all of math)


Q: When should we use float instead of int and vice versa?

A1:  Use a float when you need to have decimal points. Use ints when you do not need any decimal points.


Q: Why computer is not true random?

A1:  What is truly random? Computers are deterministic machines


Q: How does python have libraries? Did someone code up a math library for example with different mathematical concepts and now it is open-sourced?

A1:  yes! Infact math is core python and all of python is open sourced


Q: For the karel assignments, should we have been using 4 spaces instead of tab?

A1:  Either is fine for us. I prefer spaces, but reasonable people have used tabs.


Q: what’s the difference between random uniform and random randint?

A1:  Randint returns an integer whereas uniform returns a float.


Q: So random.uniform(min, max) is not inclusive of min & max?

A1:  It is inclusive of both.


Q: do we have to be familiar with functions from the math and random libraries for future diagnostics?

A1:  You will only have to be familiar with functions we have presented in class / asked you to use on assignments. But in short, you will have to be familiar with some of them.


Q: is random.uniform inclusive?

A1:  Yes!


Q: What is a seed again?

A1:  It is a way to always produce the same sequence of random numbers.


Q: is num_sides an example of a constant?

A1:  Yes!


Q: Why is NUM_SIDES a constant even though it’s not converting anything?

A1:  Because the value remains constant throughout the entire program


Q: what does the seed do?

A1:  It allows you to generate the same sequence of random numbers which can help debug your programs that use random numbers!


Q: What is the point of random.seed(1)?

A1:  live answered


Q: Is CS106A just Python?

A1:  Yes!


Q: For random.randit() and random.seed(), can the min and max values be floats or do they have to be integers?

A1:  They have to be integers.


Q: what is the difference between seed(1) and seed(2), like what is the significance of the number in parentheses

A1:  live answered


Q: Can we find the slides on the cs106 website?

A1:  Under the lectures tab there is a page for each lecture. On that page you can find the pdf of the lecture slides.


Q: What happens if it is random.seed(2) instead of random.seed(1)

A1:  live answered


Q: how do you know what value to set random.seed to?

A1:  live answered


Q: if there is a seed inside one function, does the seed apply to other functions as well or is it restricted like variables?

A1:  live answered


Q: why are we having die1=10?

A1:  live answered


Q: why are global variables bad practice?

A1:  live answered


Q: What exactly are the diagnostics?

A1:  They are questions that will be similar to the ones you solve in section. You will have one hour to complete it.


Q: what’s the point of putting str(die1)) instead of just die1

A1:  live answered


Q: How did we print total of two dice?

A1:  live answered


Q: what if we set seed to say 23, would it still behave the same?

A1:  It would generate a different random sequence.


Q: so what if you wanted to change the dice variable through a differnet function? is there a way to do that without using global variable which you said is bad style?

A1:  live answered


Q: I’m having trouble understanding how this kind of coding is similar/dissimilar to Karel?

A1:  live answered


Q: what was the point of saying die1 in the main function?

A1:  live answered


Q: When is the next assignment coming out?

A1:  Friday!


Q: After we have assigned our variable in main() and we would have our lower-level new function where we assign that variable with a different number, have we ‘overwritten’ our variable or declared a completely new one? Could we have used the variable from main() in our lower-level function?

A1:  live answered


Q: can you explain briefly again why floats are not always exact and an answer will be something like 0.89999 instead of 0.9?

A1:  its because of how floating point numbers are represented. If you are interested here is a resource that goes super deep: https://docs.python.org/2/tutorial/floatingpoint.html. But for cs106a its sufficient to know: its hard to perfectly represent floats


Q: Why cant computer generate truely random numbers?

A1:  They use an algorithm that simulates randomness.


Q: How can we make the rolldice function change the value of die1 in the main fuction?

A1:  live answered


Q: How do we fix the die rolling program? So that main() doesn’t print 10?

A1:  live answered


Q: will the diangnositics be during section or on our own?

A1:  It will be during class time. If you cannot make that work, email Juliette (jwoodrow@stanford.edu)


Q: Hi Juliette! I saw that you have individual and group office hours - can we drop in, or should we schedule in advance? Thanks!

A1:  You can just drop in!!


Q: So do you only use global variables for constants?

A1:  Exactly.


Q: Is MidPoint Karel a requirement of the assignment or extra credit?

A1:  Extra credit!