Lecture Materials

Class Announcements

  1. Section sign ups: Sections start this week. Section assignments will be emailed out to you by Tuesday. If you didn't sign up for a section, or need to change your assignment, a late signup form will open on Tuesday evening.
  2. Early assignment start: We have covered everything you need for assignment #1. It is due on Friday. Don't leave submission to the last second.
  3. LaIR is open: See the Office Hours link on the main page for hours.


Learning Goals

The goal of today's class is for you to be able to create, use and modify basic variables. You will also be able to think through how expressions evaluate.

Questions & Answers


Q: Hi! Should we sign up for the LaIR or do we just join the Zoom call?

A1:  Check out the details on the ‘Zoom details’ handout on the course page!


Q: Hello! Is it acceptable to define our functions outside of the main in assignment 1?

A1:  Thats the best place to put them


Q: When will we be assigned our sections?

A1:  By tomorrow evening - I’ll post on the course page when they’re out.


Q: I went to the Tea Room last week 1 time - may help to set informal time/periods for people to meet 🌸

A1:  Good idea!


Q: does it matter if we define a function in a line before or after we use it?

A1:  Python doesn’t care, but you should be consistent about it.


Q: How are we judged with the descriptive names as long as we describe why we defined the function that way?

A1:  Make names succinct, but descriptive! You’ll get a better sense for this as we go through the quarter.


Q: Should we include pre and post conditions for our functions in our pset?

A1:  Yes


Q: In the Karel IDE for the website, we used # for comments. Can we use # instead of “”” in our psets?

A1:  “””””” is for multline comments and # is for single line comments


Q: re: Our First Python Program slide, are there any conventions for when to use # vs """

A1:  “””””” is for multline comments and # is for single line comments


Q: What's the point of the "py" at the beginning of the terminal line when calling a program?

A1:  ‘py’ is a program on your computer that reads through the file with the Python code you’re writing (helloworld.py) and actually executes the code!


Q: do we have to type python3 <name of program>.py every single time we want to run our code after we’ve made an adjustmenmt to the code or is there something else we can do?

A1:  you can type that, or, try hitting the up arrow while you have your cursor in the terminal


Q: Is Karel artificially intelligent?

A1:  he is truly intelligent

A2:  But he’s not an AI :) Stop by office hours if you’re itnerested in how that something works!


Q: For the homework, does Karel need to be facing the same direction as the diagrams for the end state in the assignment sheet?

A1:  We try and write in details what we expect! Its problem specific


Q: why do we have to always run our code in a specific program <name of program>? so do we need to download always a certain program to run our code eg. karel?

A1:  I’m not sure I fully understand the question so feel free to clarify but your code always needs to be written in a file with a filename you then give to Python.

A2:  if you just type “python” into your terminal it opens up a place where you can just type python!


Q: What is "int" short for ?

A1:  integer


Q: What if the user inputs a letter instead of a number?

A1:  the line where it tries to convert to an int breaks!


Q: would int stand for integer or would it allow any number?

A1:  int is a function that converts text to a number!


Q: does “int” stand for integer?

A1:  Yep


Q: Will python understand the difference between an input of “9” and an input of “nine?”

A1:  Python doesn’t know that “nine” means ‘9’, so you can’t convert ‘nine’ to a number


Q: So we can only print Strings and not integers in the print statement?

A1:  (A string is the formal name for text)

A2:  if you only print an int, it is fine. Its just that you cant add a string and an intenger, gotta convert first


Q: Is there a difference between running the program in the terminal vs simply using ^R?

A1:  Yes - we’ll get there later in the quarter!


Q: So using int(17.5) would return 18?

A1:  Python would throw a tantrum and complain that it’s not a whole number!


Q: Dumb question but is there a french version of python for example ? or like just other languages ?

A1:  good question! No its international! People around the world all write things like “def” and “int” but they would make the output in their own language


Q: Can’t we print variables too? Hence no need for str(total), simply print(total)?

A1:  thats right


Q: how does the program know to print the “enter first number” “enter second number”? we never wrote print by them

A1:  Input prints whatever you put in there before getting input from the user!


Q: what is str?

A1:  It stands for string, which is the Python name for text - don’t worry about it too much right now!


Q: So if you want double and single quotes in the text…

A1:  😱

A2:  Answered live :)


Q: What if I wanna use both in the text?

A1:  live answered


Q: what if you want to use both double quotes and an apostrophe?

A1:  live answered


Q: Is there a line breaker in python?

A1:  \n

A2:  Yes! It’s “\n” but you don’t need to worry about this right now.


Q: Can we add the single and double quote statements in the print statement? Like print("hello" + 'world')

A1:  Yep


Q: What is a deliminator? Not sure if I spelled that correctly…

A1:  ‘Delimiter’! It marks the start or end of something.


Q: can you use int(input(“Give number”))?

A1:  Yep! We’ll get to how that works later


Q: Who is “the user” in this? Is it us or another person?

A1:  Whoever is running the program


Q: does it matter if the input is a number versus a letter?

A1:  the conversion to integer will break! that is the int() part


Q: How can we print text to the next line?

A1:  Everytime you call print, it goes to a new line.


Q: Can the user type out a number? Say “nine”?

A1:  Yes, but we couldn’t convert it to a number


Q: If the user types in “a” will there be an error because it isn’t a number? Or is the int() function ok with receiving an “a”?

A1:  Nope, int will crash


Q: what if you use x == 10?

A1:  That’s checking if x has the value 5 - we’ll get there later!


Q: Can you store multiple values inside a single variable?

A1:  Nope!


Q: Which quotes do we use if we want to print a sentence with both “ “ and ‘ ‘? E.g. The teacher told me, “You spelled Karel ‘Karol’.”

A1:  There are ways to indicate that your quotes don’t delimit a string, but they’re a little beyond what we’re working with right now. Essentially, you need to type \ before the quotes.

A2:  in that case there is a special token backslash which be used to mean.. i want this next token to be an actual quote, not an end of string. eg “ this’ \” “


Q: Wondering why we can use mathematical expressions to add NUMBERS in Variables (eg x= 5 + 7), when we also said 5 is "text" and needed to convert it to a NUMBER with int?

A1:  Could you clarify what you mean?


Q: Is there any way to initialize a variable without assigning a value to the variable?

A1:  Not in Python!


Q: Can we define a variable before we use it? Like int x = 9

A1:  yes!


Q: Is = the same as -> ?

A1:  No, -> is not valid Python


Q: Can a variable first appear on the right handside? For ex. Total = Total +1, where this is the very first time total appears? If so, is total evaluated as 0?

A1:  Great question! Python would complain and give you an error if you did that.


Q: If you reuse/ reassign the varible, which value will python use? for example if you were to use this code : x=10 (code part 1 ) x=5 (code part 2) will code part 1 use x=10 and part2 will use x=5?

A1:  Yep!


Q: is total the previous total assigned?

A1:  Yes, in this case 5


Q: both those lines of code together in a program are valid? ie. you can have both “5” and “total + 1” assigned?

A1:  Yes, one comes after the other. The first would set total to 5 and the next would add 1 to it.


Q: How do you know if you picked an existing word accidentally?

A1:  PyCharm will color it a special color and you’ll get a scary red line under it! You’ll also get more familiar with these words as the quarter goes on.


Q: what is snake case?

A1:  variable_names_like_this


Q: If you define a variable within a function within main, does it get fed into the next function within main? E.g. if def main(): function1() # variable assigned here function2() # will function2() know the variable from function1()?

A1:  Nope - functions only know about their own variables (for now!)


Q: Is the object the variable (the box) or whatever value is inside?

A1:  The object is the suitcase itself, which has a 700 inside it


Q: does their sequence matter?

A1:  Whose sequence?


Q: of the num____ programs in integrating the values of the others to the 3rd

A1:  You can define the variable in whatever order you want


Q: Can you type a negative sign before the number to denote a negative number?

A1:  Yep


Q: Difference between real value and integer?

A1:  Integers can’t have decimal places


Q: int is a type and int() is a function right?

A1:  Yep! Good catch


Q: What about irrational numbers?

A1:  Good question! There are some clever ways to represent them but by in large we just use floats that are close to them i.e. pi = 3.1415


Q: why would you save a whole number like 5.0 as a real number rather than an integer?

A1:  live answered


Q: how many digits can you put after the decimal point?

A1:  An arbitrary number


Q: If you assign a variable using another variable (e.g. num_initial= 500, then later num_total = num_initial), using the luggage tag analogy would you say that a second tag is added to the suitcase or that a second identical suitcase is created? I assume it would be the second case since you are assigning rather than claiming the two variables will always be equivalent, but just wanted to check

A1:  Fantastic question! You are actually usually making a new tag but there are some nuances to this, which we’ll explore later in the quarter.

A2:  you are right!


Q: why is string 5 not equivalent to int 5?

A1:  lots of good reasons a number could be represented as a string. For example you might have a confirmation code “04384” but a number can’t start with a zero. So at times you do want a number in string form, and they do have a few differences. Good question

A2:  You can’t do math on “5” but you can on 5 :)


Q: So input always gives a string back?

A1:  always!

A2:  Yep!


Q: Is there a function that returms the type of a variable?

A1:  Yes! It’s called type() :)


Q: Does 9 go in the place of “Enter first…” or after the colon?

A1:  After the colon


Q: Do you have to end the prompt for an input command with a colon, or does it not matter?

A1:  Doesn’t matter


Q: is it possible to take in an integer rather than a string?

A1:  From input? No


Q: so why not name the integer version differently?

A1:  You could, but we don’t need the string anymore, so we might as well overwrite it.


Q: Will python happily convert between int and float? Vice versa? (e.g. if I have 5 as an int, will python be ok if it’s reassigned as a float? Or vice versa for 5.0 float trying to convert to int)

A1:  You can go from ints to floats, but floats to ints gets a little weird if there’s a decimal place


Q: eg. Brahm re: question before on strings, can we just have Input from the User be an Int or Float? Instead of a string? Or do we need a different command other than Input to make it work?

A1:  You always get a string back from input, so you’ll need to manually convert it.


Q: Is there a command to convert text into floats instead of integers?

A1:  Yes! It’s called float()


Q: why do we have to input string first and then convert to a int?

A1:  Input always gives you back a string


Q: So num1=int(num1) is now replacing the suitcase that was a string and we cannot ask the string num1 anymore without converting it back?

A1:  Yep!


Q: What if the user gave you 9.1?

A1:  The program would throw a tantrum and break because you can’t convert that to an int!


Q: So you lose the num1 string value when you replace it with the int version? Or do you have both as num1?

A1:  You lose the string value


Q: Is it possible to go directly to an integer value instead of having to go from string to integer?

A1:  No, input always gives you back a string.


Q: If we were just printing the number 26, can we keep it in integer form?

A1:  Yep!


Q: So we always have to start with input/ string?

A1:  It depends on what your program is doing but if you need user input, you need to call input and you’ll get back a string


Q: why do you have to convert an int to a str version of it in order to print?

A1:  live answered


Q: So unlike Karel, order does matter. if you are reassigning a varible it needs to be after that varible has already been created. Ex: num1 = 9 num1 = int (num1) is not the same as num1 = int(num1) num1 = 9 ?

A1:  your example is correct. In karel order matters too (move turn isn’t always the same as turn, move)


Q: will str4 still have double quotes?

A1:  Not when you print it, but internally, yes


Q: can num1 be used multiple times in the same function asking for different inputs?

A1:  Yes, but you’d lose the previousinputs


Q: if your already are definying "str= " for a value, do you need to still use " "

A1:  Can you clarify what you mean?


Q: Stylistically, where should we put comments in our code? Today’s lecture slides and the section on “Good Style” in the Assignment 1 handout put a multiline comment in different places. Or do we just have to be consistent with where we put comments?

A1:  A multiline comment at the top, but I don’t think anything else really *needs* a comment here.


Q: Can you print a number by itself without converting it into its string form?

A1:  Yep


Q: Does the + operator have multiple functions—regular addition with integers as well as concatenation with strings? Does it have any other functions with other types?

A1:  Yes! We’ll get there in due time :)


Q: Do variables need to be the same type when doing mathematical operations (i.e. can we subtract a float value from an int)?

A1:  Yes! If you do an operation on a float and an int you always get back a float


Q: so does input both display what you put inside () and take a string to put into the variable? aka does input do two things at the same time? and what if you leave () empty?

A1:  correct. If you leave it empty, it doesn’t show the user any text before asking for the input


Q: What if the user entered a decimal?

A1:  int would crash


Q: Do you need to define a function for spaces between words or does it do it automatically?

A1:  What do you mean?


Q: For the example that was give "str = "hello" ", is it necessary to write hello in " " vs hello w/o "" since you are already identifying by "str=" that is a string?

A1:  Oh, I see - you need the double quotes. Python won’t look at variable names (it always looks at RHS first)


Q: what if you type 10.5?

A1:  Same kind of crash!


Q: after int(num1), where does the contents before the colon go?

A1:  That’s not stored in num1 :)