Quiz 0 - Frequently Asked Questions


Style and Format of the Quiz

Will this be Synchronous or Asynchronous and how much time will I have?

The quiz is Synchronous, meaning it will be on Tuesday 9/8, 4:45-6:00 PM Eastern Time.

International students who opted into the 4:45 AM Eastern Time quizzing will begin theirs via the INTL Gradescope account at 08:45 GMT 9/9/2020 and submit by 10:00AM GMT.

Am I allowed to use notes, VS Code, and/or the Internet?

Yes to notes, anything on the course site, and VS Code… you are encouraged to try things out in the REPL!

No to using the Internet. While you may think there is no way for us to check whether the Internet was used, it is obvious to tell what methods to program coding were taught in class versus something that was pulled from the Internet. Using obscure methods from the Internet either way will probably get points taken off anyway for not following the conventions we use in class.

Will I need to hand-write anything?

Nope! All will be done in gradescope.

What topics should I know for the quiz?

  • Data Types
  • Expressions
  • Relational and Logical Operators
  • Variables and Constants
  • Inputting and Type Conversion
  • Functions
  • If, Then, Else Structure
  • While Loops

Please take time and practice the syntax for all of these!

What is the breakdown of question styles?

Primarily we will be asking you to apply the concepts from above to come up with programs similar to what you have been working on in the exercises. Meaning there will not be any true/false or multiple choice, but there will be code writing with an emphasis on functions.

Variables

How important is declaring the type? It seems so unnecessary…

While Python technically does allow for variables to be declared without types, it is important to get used to the concept now before you move on in programming and it ends up mattering with other languages. Another key reason is that it helps you keep track of potential errors that can come later on.

For example, if you try concatenating two variables and get an error, it would help to go back and see what types you declared those variables as to see what you need to convert. Plus, it’s as simple as:

[name]: [type]

You don’t want to lose any points because you didn’t want to declare the variable with its type :)

Where and when should I be declaring variables?

It is always a good habit to declare variables when in doubt. Not only will it help you reduce redundancy in typing a literal string or number value over and over again, but it also is nice to assign a meaningful name (which will be discussed below) so that way you understand what the purpose of the variable is.

There have been occasions however where in functions, people declare variables to just reassign other variables and function parameters (essentially creating copies). You do NOT need to do this as the purpose of the original variable or function parameter is exactly to store that value to reuse later!

When it comes to simple expressions such as the concatenating of strings for printing (like in ex01), it is not necessary to assign each of the literal string phrases to a variable. For example, for hype machine, it is not necessary to do:

amazing: str = " is amazing!"
                print(name + amazing)

But for cluster funk, when you were using the " - Total: " string four times, it could be helpful to do:

total: str = " - Total: "

… and use that total variable when printing your lines! Not necessary, but you see how it reduces the redundancy!

When building a string letter by letter, especially when we didn’t know the length like we did in ex03, it is super helpful to declare a variable for what you intend to output from the start, and then add on to that you build each letter! You will see in future exercises too that it is helpful to declare:

result: str = ""
                
                # or
                
                result: int = 0

… just so you have them at the start, and then you can add to this however you need, depending on what your function is doing, and return this output variable at the very last line!

How do I name variables and what does it mean to be meaningful?

Always use snake case when declaring a variable:

example_string: str = "Hello"

There are many ways to be meaningful without having to have a mouthful for your variable names. If I wanted to declare a variable for the best basketball player:

best_player: str = "Coby White"

Or if I wanted to list that players highest scoring game in a season:

high_score: int = 35

Something that at least gives some specificity to what your variable is trying to store is super helpful to understanding your own code!

User Inputs and Conversion

If you are looking for a user to enter a string value, it is simple! All you need to decide is the variable name you want to store it in and the prompt (which are literally whatever you want as long as it is not a Python keyword or number), which will go within quotes and within the parentheses of input().

variable_name: str = input("Enter a word: ")

If you are looking for a user to enter an integer or decimal, the issue is that the default type of calling the input() is a str. We therefore have to convert the type of the result of input!

There are many ways to achieve this. First, you could store the str input in a variable and then its conversion in a separate variable:

str_name: str = input("Enter an integer: ")
                int_name: int = int(str_name)
str_name: str = input("Enter an decimal: ")
                float_name: float = float(str_name)

Or, you can compose nested function calls:

int_name: int = int(input("Enter an integer: "))
                float_name: float = float(input("Enter a decimal: "))

Operators

Relational

  • less than: <
  • less than or equal to: <=
  • greater than: >
  • greater than or equal to: >=
  • equal to: ==
  • not equal to !=

Logical

  • or , | - used when at least one of the conditions need to be True
  • and , & - used when all conditions need to be True
  • not - used when you want to reverse a boolean value or expression from True to False or False to True

Functions

How do I write a function (syntax)?

def name(parameter: type, parameter: type, ...) -> type:
                    ...

While syntax is something easy to get if you are using VS Code during the quiz, Python tends to not flag off any missing types, so it’s important that you take note of these.

With regard to parameters, please keep in mind that these are essentially variables that are available to use throughout your function! Do NOT define a new variable that simply assigns the parameter to it (unless a copy is necessary, which we will see later). There is no point in cluttering your function with another variable that holds the exact same value as a parameter!

How do I write a function (to achieve a purpose)?

We will often tell you the skeletal components your function needs - the function name, the parameter types, the return type, along with exactly what you are trying to accomplish with the function. Please pay attention to those instructions as you can be very stuck on how to implement a function without understanding the basic building blocks we give to start you off.

Once you get the function definition written (the first line that is outlined in the previous question), now it’s time to for the fun part! We have to actually think about what we do with the input (the parameters) in order to attain the output (the return value).

The key aspect of implementing functions is thinking about what you’re doing in non-coding terms. If I ask you to return the length of the longest string, given 3 parameters for each string, the process in English is:

Compare the lengths of the first two numbers, take the longer one, and compare that one to the third number. Once this is done, return the length of the ‘winner.’

Now we have to think what tools we know that can help us solve this. Well, for starters, we need comparison operators such as > to help us make compare two numbers. We know we have a len() function that can help us find the number of characters a string as. Lastly, we know a structure known as the if-then-else that will help us enact statements based on whether or not a condition is true.

a = len(first)  # Defining variables for length to avoid redundancy
                b = len(second)
                c = len(third)
                
                if a > b:  # Compare first two numbers
                  if a > c:  # Compare with third
                    return a
                  else:
                    return c
                else:
                  if b > c:   # Compare with third
                    return b
                  else:
                    return c

The Million Dollar Question: How do I best prepare for the quiz?

  1. Review your own notes, focusing on the topics related to the quiz and star/highlight anything you’re unsure about.
  2. Review your assignments and feedback from Gradescope, paying attention to syntax so you do not end up making simple mistakes.
  3. Do the practice exercises that we will assign, since that is essentially what the quiz will ask you to do.
  4. Email TA’s to ask about anything you are unsure about! Office hours will be open Tuesday at 10 AM so you can pop in with personal help as well.
  5. Weather is looking nice the next few days, take a relaxing and safe walk whenever you can to destress. You have been doing amazing to get to this point in the class! If you are really worried that your grade might drop due to the quiz, first off, you have other quizzes and assignments to make up for it, and second, you will know the quiz envrionment best after you have taken the first one! :)