Lecture Materials

Class Announcements

We've posted information about the diagnostic and some practice material on this handout. Please make sure to have downloaded and installed BlueBook, our exam platform.

Questions & Answers


Q: Hey CS106a Team,

A1:  hi!


Q: what will be the format of the diagnostic?

A1:  3 short questions. 1 karel. the practice will be exactly the same. check it out!

A2:  We’ll be posting materials about it immediately after class.


Q: Will the questions be similar to the assignments?

A1:  shorter!

A2:  They’ll be smaller scale


Q: is there a grade attached to it?

A1:  feedback, not a grade

A2:  we will give you a score, and it is part of your final grade calculation. But that is not the point of the diagnostic. The point is to give you feedback. And if you do well, great. If you dont do as well as you hoped thats fine too, we will use it as a way to help you figure out what you should focus on learning

A3:  (To be clear, it constitutes 15% of your grade for the class but given that the grading scheme is S/NC, that means it’s relatively low pressure!)


Q: Where is the link to the python guide mentioned in class?

A1:  On the right sidebar of the course page


Q: can return not take an argument ?

A1:  it just takes an “expression” after the word “return”.

A2:  It’s not a function, but you can return a value


Q: Is there a minimum score we are aiming for to get a S in the class?

A1:  its “mastery” so you have to do reasonably well on all parts. If you are in danger of not passing we will let you know. You just need to try your best


Q: *Sorry, truncated question. Hey CS106a Team, I was trying to define separate functions and their variables for the homework and was running into the error of variables not defined. How do we apply/keep variable values (like if you've evaluated 'sum' to the int 3) between functions? I did the workaround of just writing one function in 'main', but for style I thought it would be better to decompose into separate shorter functions?

A1:  Also, questions like this are better asked on Ed or at office hours/the LaIR

A2:  parameters are your solution!


Q: Can constants take user input?

A1:  not traditionally. Constants should be fixed


Q: How does def factorial know what n is?

A1:  Whoever calls the factorial function passes a value for n in as a parameter


Q: wait, do i and factorial appear on the same line?

A1:  Yep, they print with a space in between them


Q: Is it ok programming style to have variables of the same name in different functions?

A1:  Yep, provided that name is descriptive enough

A2:  yes! its common


Q: Can you have a variable visible to all functions that is changed by those functions, or does that only work with constants?

A1:  its possible, but considered very bad style because it leads to so many bugs


Q: What would you do if you wanna get answer until infinity?

A1:  float(‘inf’) is infinity so you could put it in your range function, but you’d be there for a while!

A2:  while True:


Q: Is it still ok to use +’s to get multiple string objects out of a print line? is that good style?

A1:  Either is fine


Q: Is it good style to change the variable names when defining a new function? Or is it okay to give it the same name so you don’t have too many variables?

A1:  Either is fine! Basically, a variable should have a name that describes how it will be used in that function.

A2:  i often use the same name. As such its really important to know your scope rules!


Q: Can you use any arbitrary name for the counting variable in a for loop?

A1:  Yep, but i is conventional


Q: why is i 0 and not 1 again?

A1:  its a convention! But its really useful for things we will do later in the course


Q: Does the loop not carry out just that one time? if I said for i in range(1), would the body of the loop not occur exactly once?

A1:  It would


Q: are n and i both parameters?

A1:  n is a parameter, i is a local variable.. but they both behave like variables only visible in this stack frame


Q: can we use everything that’s on the python reader for the assignment ? Ans should we know it for the monday test ?

A1:  you only need to know up until todays class


Q: for i in range only works for numbers, right? that is why he did not call “int”, right?

A1:  right! so the value was already an int.


Q: why does result = 1 ?

A1:  It starts off as 1 for the multiplication


Q: Can you return multiple variables? Say my function has variables: a = 5 b = 4 Is 'return a, b' valid?

A1:  yes! Its a little advanced. But you got it right


Q: if this function needed a constant, would we enter it under 'def factorial (n) : '

A1:  Constants should be declared outside of any function


Q: Just clarifying the point Mehran is trying to make about the information flow: When factorial(i) is called in the print statement within main(), the information that is actually passed into factorial() is not i, but the value of i within main?

A1:  Yes!


Q: What does it mean to use a parameter in a function versus using a local variable?

A1:  A parameter is information given to a function and a local variable is a variable created inside the function.


Q: Somewhat unrelated to lecture (but relevant to the p-set), how would you terminate a program currently running an infinite loop in Pycharm?

A1:  Ctrl + C in the terminal


Q: How does the computer call the function? Does it do it first then run through everything, or simutaneously?

A1:  Coould you clarify what you mean?

A2:  it does so step-by-step just like mehran is showing. but it does those steps very fast :)


Q: What was the terminology "stacked frame" used to signify?

A1:  ‘Stack frame’ - it’s a collection of all the variables inside a function.


Q: Sorry I mean: is for i in range(1, 1) essentially equivalent to for i in range(0)? Bc like 1-1 is 0?

A1:  Yep


Q: could you just use a diff variable in one of them, like for x in range(3) so it’s less confusing?

A1:  you could, but “i” is a convention. You will get really used to “scope” rules and it will become truly comfortable

A2:  Yes, but i is pretty conventional


Q: Can a user input a parameter?

A1:  Yep, using the input() function

A2:  you have to do that in two steps: x = input(‘asdfsf’) # now you can pass x as a parameter


Q: What was the stack frame, again?

A1:  A collection of all the variables in a function


Q: If you define a variable like i at the top of the program like a constant, can you reuse it inside a function as well?

A1:  It’s not quite that simple so I’d recommend against it


Q: is the diff between a constant and a variable is that the constant is assigned outside the function?

A1:  A constant is just a variable whose value shouldn’t change


Q: can we have global variables that are not constants?

A1:  no! dont do it!!! for the children…

A2:  Yes, but it’s really bad style because it leads to a lot of bugs


Q: so in a for loop i will take a different value every time for loop runs?

A1:  Yep


Q: can you use a different letter than i in a forloop?

A1:  Yes, it’s just a variable so you can give it any name


Q: thank you

A1:  np


Q: what does -m mean?

A1:  short for module… its running the doctest module

A2:  Don’t worry about it too much but it basically means to use the ‘doctest’ module which Python comes with


Q: can you define doc tests outside the def function() ?

A1:  Nope

A2:  (Well, yes, we won’t talk about them in 106A!)


Q: How do I save a value of a variable from a previous program run and use that same value on a new run? For example, I run the program once. x initializes from 0 and I put it in a function. The function makes x = 5. In the next run, however, I don’t want x = 0 and update x to 5 before putting it in the function. How do I do this?

A1:  you would have to use a “file” which is something we will cover later in the class

A2:  By program run, do you mean starting the program from scratch each time, or just calling a function again?


Q: what is the purpose of a doctest again?

A1:  Just to specify what the function should return on some inputs as a way of testing your programs

A2:  make sure your function is healthy!


Q: so in other words, the docttest is basically to confirm the answers the program will give, right?

A1:  rock on


Q: '@Brahm start/run the program from scratch :)

A1:  yep, files! We will cover those later

A2:  You’d have to save the value to a file - we’ll get there soon


Q: does the order of the functions matter in passing parameter values?

A1:  The order of the parameters should match

A2:  the order of “definitions” doesn’t matter. the order of “calling” certainly doesn’t. The order of “parameters” matters


Q: Does the doctest need to be in the comment or can it be outside?

A1:  It needs to be inside the comment


Q: what’s the difference between a test and an item?

A1:  Same thing


Q: How do you assign a name to the portion of the code with the doctest?

A1:  The function already has a name


Q: How does doctest know what the function is expecting?

A1:  It doesn’t - you do!


Q: what does the -m stand for?

A1:  module :) its the doctest module

A2:  Module - it means ‘use the doctest program in Python’


Q: Oh I see, you specify it under the function call in the doctest. So: >>>factorial(3) 6 <- this is me telling the function what to expect?

A1:  Yep!


Q: Can we go further than doctest, by getting back an actual evaluation of just a single function? That might be faster than commenting out everything else in your main

A1:  You can add a doctest with some random expected value - it’ll show you what it outputs when it fails!


Q: Should we delete the doctest in the comments once we are done testing it?

A1:  no keep it there! Its great style (makes it easier to understand the function) and as mehran said its good for regression testing (you changed the code… does it still work?)


Q: Is it bad style to refer to x by the same name in the add_five function?

A1:  no its normal to reuse names.. but you have to understand exactly how the control flow works!


Q: What would happen if you returned x +=5 ?

A1:  >>> def add_five(x): ... return x += 5 File "<stdin>", line 2 return x += 5 ^ SyntaxError: invalid syntax

A2:  x += 5 isn’t an expression, so you can’t return it!


Q: who is running the doctest? is it the same as writing a boolean for the expected value or does doctest function like wrap around and run my program like a shell?

A1:  Python has a program called ‘doctest’ built in. That program is running the doctests you write.


Q: Can we initialize a varaible by writing "x += 5"? Will this this result in an error or with a new variable x with the value 5?

A1:  >>> def add_five(x): ... return x += 5 File "<stdin>", line 2 return x += 5 ^ SyntaxError: invalid syntax


Q: why do we need the str(x) in the print function?

A1:  To convert it to a string before concatenating it


Q: Do we have to add “return x”?

A1:  yes!


Q: Are we able to return more than one piece of information? like returning two values back to the main?

A1:  Not yet!


Q: why does it have to be x=add_five(x) rather than just add_five(x)?

A1:  mehran is explaining now!


Q: can you explain why we still put str to print the final result, please?

A1:  you cant add a string and an int. You have to first convert the int to a string…


Q: Are for and while loops considered functions?

A1:  Nope


Q: What is best practice for variables that are useful across function? When should we pass them from one function to another via parameters and when should we define them as global variables?

A1:  Parameters! Global variables are basically never a good idea


Q: Isn’t it less confusing to not use x as the varibale for both the main() and the add_five function?

A1:  it gets very natural to follow “scope” i dont look outside a function when reading variables :)

A2:  Yes, but sometimes the name should be the same for both functions


Q: How do you know the order of parameters? Is it the same way they are appear in the main function or the order of how they appear in the function with a parameter?

A1:  The order they appear in the def line


Q: Thanks! What’s would be an example of a time that the name should be the same for both functions?

A1:  In some more complex functions, there are some pretty conventional names we give variables of specific types


Q: how do you pass by not value?

A1:  We’ll get there in a few weeks :)

A2:  But short answer, Python makes the call, not you


Q: in nested functions, are variables in the parent function available in the nested one?

A1:  Yes, if they’re declared before the nested function, but unless you have a *really* good reason, you shouldn’t use nested functions.


Q: Will there ever be a function that doesn’t have return (with or without a variable behind return, doesn’t matter)?

A1:  Yes! All your Karel functions and functions that print things, for example :)


Q: Is it ok to use concepts from the course reader in our assignment that we have not discussed yet in class? For example: The "try except" command or lists? If we are not supposed to use them in the basic assignments, can we use them in the extensions?

A1:  You shouldn’t need them in basic assignments (and might be an indication of poor style if you’re overcomplicating things), but you can use them in extensions


Q: wait, how do you follow logic? lleft to right or ors before and?

A1:  and before or, and then left to right


Q: Can you explain again why earlier in the lecture, the code had to be x=add_five(x) rather than just add_five(x)?

A1:  it makes a copy of x… doesnt get the original variable


Q: Quick question regarding the honor code and assignments. The Honor Code PDF says that "It is an act of plagiarism to submit work that is copied or derived from the work of others and submitted as your own." Would the following scenario be allowed: I remembered the "indirect" command from my work in Excel, which is the same as "eval" in python. Would it be fine to google "excel indirect equivalent in python", look up the definition of the function and use it in an extension?

A1:  thats not plagiarism :-). Plagiarism would be more like… “how do you program khansole academy”


Q: WIll this code be posted after class?

A1:  It’s already on the lecture page


Q: why would you use an elif vs. an or statement within an if statement? Still not clear on the exact use of an elif…?

A1:  elif and if inside else are the same thing, but elif looks a little cleaner


Q: Is there a reason why this program was really well formatted to look and behave like a real calendar, with days of the week and everything, but has “Month #1” instead of January? Is there some particular difficulty with writing out the month? Thinking about this as well in the compute interest homework problem

A1:  it wouldnt be too hard. just write a function that takes in the month number and returns the month name!


Q: where can i access calender.py?

A1:  You can download it from the lecture page


Q: Can you explain what end = “” does?

A1:  Basically, print doesn’t stick a newline at the end so the next thing you print shows up on the same line.

A2:  makes the value equal to the emptry-string!


Q: what exactly does the end="" do again?

A1:  Print doesn’t add a new line

A2:  oh that makes it so that it doesn’t add a new-line after printing


Q: So does this have the same effect as print(“”)?

A1:  Not quite. Basically, it makes it so that print doesn’t stick a newline at the end so the next thing you print shows up on the same line.


Q: Are we expected to build something like this from scratch? This program was kinda compicated…

A1:  not now, but you will get there :)


Q: How would you assign the names of the months for this program? Just like a print equal to month1, and so on?

A1:  it wouldnt be too hard. just write a function that takes in the month number and returns the month name!


Q: Are we expected to be able to create at this point a program as complex as that of the calendar?

A1:  live answered


Q: Could you explain please again what print(“”) does?

A1:  Just prints a blank line!


Q: will we have access to the calendar.py program after lecture ?

A1:  live answered


Q: Can you explain the end==

A1:  live answered


Q: To assign a constant, do you basically just do like you would assign a variable but outside main?

A1:  live answered


Q: If I am missing next Monday lecture, what should I do?

A1:  There isn’t lecture - it’s just the diagnostic


Q: And you said more information about the diagnostic will be posted after class, right?

A1:  Already up!