Coding Lab 1: Variables & Operators#

Welcome to the first coding lab!

CodingLab labs are meant to be interactive - so, feel free to find another person to work together with on your CodingLab notebooks.

For this lab, it’s really important that you’re comfortable with all of the tools we introduce here, as we’ll be using them throughout the quarter. So, while you should feel free to consult with your classmates, you’ll want to be sure you carry out each part on your own.

If you have a question about how something works / what something does - try it out, and see what happens! If you get stuck, consult your classmates. If you’re still stuck, your instructional staff are there to help!

Reminders:#

  • PLEASE DO NOT CHANGE THE NAME OF THIS FILE.

  • PLEASE DO NOT COPY & PASTE OR DELETE CELLS INLCUDED IN THE ASSIGNMENT. (Adding new cells is allowed!)

Whatever you do, please DO NOT RENAME CODINGLAB OR ASSIGNMENT NOTEBOOK FILES!

Part 0: Jupyter#

This is a Jupyter Notebook! They are a very helpful learning tool because they allow plain text (like this!) and code (coming up!) to be combined in a single document.

The notes presented in lecture are Jupyter Notebooks. Your CodingLabs will be in Jupyter Notebooks. And, your assignments will be completed in Jupyter Notebooks. So, you’ll want to get very comfortable with working within a notebook.

Cells#

The operational unit of the notebook is the cell.

Cells are primarily either text (Markdown) or code.

If you click on this cell, you should see in the menu above that it says “Markdown”

YOUR TURN: Add a new cell#

Single click on this cell.

Then, click the ‘+’ icon on the toolbar at the top to add a new cell below this one.

The cell you just added above is a code cell by default. You can tell by the In [ ]: to the left of the cell and the fact that the drop-down box above says “Code”

Use that drop-down menu to change the type of that cell you just created to be a text cell. Type the following in that cell “Learning to program in Python makes me …” and finish the sentence with how you feel about learning to program in python.

YOUR TURN: Editing Text Cells#

To edit the text in this cell, double-click on it.

Add information below about yourself to get practice with editing text cells.

Name: Shannon Ellis
PID: A1234567
College: ERC
Major: Cognitive Science

Markdown#

As discussed in lecture, these cells are formatted using Markdown syntax.

Edit the text in the cell below so that it has the formatting specified by the text.

For example, if the text said:

This sentence is bold.

You would edit the text so that it was bold:

This sentence is bold.

Note that to see the formatting changes, you’ll have to run the cell using the Run icon above (or more simply use ‘Shift + Enter’ on your keyboard.

YOUR TURN: Edit this text#

This is a heading level 4 (H4)#

This sentence is italicized.

Below is a bulleted list:

  • bullet item 1

  • bullet item 2

  • bullet item 3

Below is a numbered list:

  • list item 1

  • list item 2

  • list item 3

This sentence is bold AND italic.

Part 1: Variables#

Variables are used in Python to store values.

Defining Variables#

Declare an int, float, string, and boolean. Fill them with with any values you want.

Call them my_int, my_float, my_string and my_boolean.

### BEGIN SOLUTION
# specific values will differ
my_int = 12
my_float = 56.3
my_string = 'hello'
my_boolean = True
### END SOLUTION

It’s often best to check your thinking/work before running the provided assert cells, to ensure. For example, here you may want to use the following cells to check the types of the variables you write are as expected. We’ve showed how you may do this for your first variable created. You should check the other variables you created in the cell provided

type(my_int)
## check the others here
print(type(my_float), type(my_string), type(my_boolean))

You’ll see a lot of assert test cells throughout this course - most often in exams and assignments, but sometimes in coding labs. These are meant to ensure you’re on the right track!

When you see assert followed by a variable name, that is Python checking to ensure that variable exists. If you mistyped the variable name above (when defining the variable name), you would get an assertion error when running the cell below. The assertion error will point to which assert led the error to be raised. It’s then your job to go back to your code and figure out why the assert test failed!

If you’re on the right track, there will be no output - or as you’ll hear us say, these will “pass siltently”. This doesn’t guarantee your answer is right…but it let’s you know you’re on the right track!

assert my_int
assert my_float
assert my_string
assert my_boolean # if you put False above, this assert would fail. That's not your fault. Poor instructions.

Note that we could also use assert statements to test the type. If the type of my_int is an integer (int), this cell will “pass silently” (meaning give no output). If it raised an asserttion error, we would know we had done something wrong above.

# test that my_int is an int
assert type(my_int) == int

Part 2: Operators & Comparisons#

Operators are used in Python to do things with variables.

Operator Questions#

First - explore using Python operators. Make sure you can add and subtract numbers, concatenate strings, do boolean comparisons, etc.

## Do some exploring!

### BEGIN SOLUTION
# possible explorations
# Add & subtract numbers
print(1 + 2)
print(2 - 2)

# Concatenate string
print('ab' + 'cd')

# Boolean comparisons
print(True and True)
print(False or True)
### END SOLUTION

Once you have explored using operators, try to answer some questions using them.

Let’s start with an example: is the multiplication of 13 and 15 greater than 200?

13 * 15 > 200

Similar to that example, try to answer the following questions using Python variables & comparisons. You can assign the output to whatever variable name you like:

  • Is the remainder of dividing 323 by 13 greater than 6?

  • Is 7 to the power of 3 greater than or equal to 300?

  • Is 49 squared greater than 2500 or (boolean comparison) is 14 to the power of 3 greater than 2500

  • Is 49 modulus 9 equal to 67 modulus 9 and (boolean comparison) is the sum 49 & 67 modulus 9 greater than 7

### BEGIN SOLUTION
323 % 13 > 6
7 **3 >= 300
49**2 > 2500 or 14**3 > 2500
49%9 == 67%9 and (49 + 67) % 9 > 7
### END SOLUTION

Operator Challenges#

The following are more challenging operator related questions. If they seem too tricky for now, just skip ahead to the next section.

Some more comparisons, with variable assignment:

  • Set the variable comp_1 to the result of whether 17 squared is not the same as 867 divided by 3

  • Set the variable comp_2 to the result of whether 4^3 is less than 3^4 and also greater than the remainder of dividing 1234 by 99

Bonus: both of the above questions can be written in (at least) two ways.

If you found an answer to them, see if you can update the construction (by changing at least one operator) to answer the question in a different way.

Note: x^y indicates ‘x to the power of y’

### BEGIN SOLUTION
comp_1 = not 17**2 == 867/3
comp_1 = 17**2 != 867/3

comp_2 = (3**4) > (4**3) > (1234 % 99)
comp_2 = ((3**4) > (4**3)) and ((4**3)) > (1234 % 99)
### END SOLUTION

Operator Explorations#

For each of the following questions, start by writing out your guess on the outcome.

Then, test out each one by writing out some code.

Finally, once you have answered the question using code, add a comment in markdown with the answer.

Questions:

  • If you divide a float by an int, what is the type of the outcome? What about dividing an int by a float?

  • What happens if you multiply a string with a number?

  • The + sign works if you for adding numbers and for concatenating strings. Can you use + with a number and string in the same operation?

  • Since you can use + and * with strings, what happens if you try to substract two strings using -?b

### BEGIN SOLUTION
# possible explorations

# Dividing with a float and an int, in either order, returns a float
print(type(12.0/2))
print(type(12/2.0))

# Multiplying a string by a number repeats that string number times
'hodor ' * 8

# No, this code (when uncommented) fails. You cannot use '+' with a number and a string
# 12 + 'str'

# No, this code fails (when uncommented). You cannot use '-' with strings
# 'abc' - 'a' 
### END SOLUTION

Part 3: Academic Integrity#

Navigate to the following pages:

Read and reflect on the content there. Then, respond to the following situations with your own thoughts in the cell provided after each situation using Markdown text.

Consider 1) if the action was a violation of academic integrity in COGS 18 and 2) if it was, what do you think the consequence should be.

Situation 1

A COGS 18 student is working on an assignment and is particularly stuck on one question. They go back through the notes and just can’t figure it out. They look at the staff office hours schedule and realize that they won’t be able to make it to any staff office hours this week, so they decide to ask a classmate for help. Their classmate agrees to meet. During this meeting they discuss where the stuck student is confused. The classmate helps the stuck student through the question. The stuck student writes the code during this process for the assignment and then explains the code they’ve just written back to their helpful classmate. The stuck student thanks their classmate for their help.

Prof’s thoughts: This is 100% OK! In COGS 18, you’re allowed and encouraged to help one another. In fact, when we explain things to others, we solidfy our own understanding, so both students are getting a lot out of this interaction!

Situation 2

A COGS 18 student is working on an assignment. During this, they realize they’re running out of time before the submission deadline and still have two questions left they just can’t figure out. They text their friend who is in the class asking for the answers to those two questions. Their friend (who has already submitted their assignment), wanting to help out, sends their code along. The struggling student changes the code a little bit and submits his assignment.

Prof’s thoughts: This is NOT ok. It is never ok to ask a classmate for their answers - be it on a coding lab, assignment, or exam. While you can look at and discuss one another’s code on labs and assignments, there is a difference between that and just giving someone your answer. This could lead to consequences for both the student asking and the student who provided the answers.

Also, note that this would be more problematic if this were an exam and not an assignment, as exams are not allowed to be discussed with anyone.

Situation 3

A COGS 18 student is taking the take-home portion of their exam. During this, they realize they just can’t figure out two questions while taking the exam. They copy the question from the exam into ChatGPT and out pops an answer. Because they’re pressed for time, they quickly copy the code provided into their exam and submit their exam.

Prof’s thoughts: This one may seem tough, but hopefully after I explain, it will clear things up as to why this is NOT ok. In this case, this code would NOT be code you wrote/came up with and thus you cannot use it on your exam directly. So, can you use ChatGPT (or other LLMs) when working on assignments/labs/take-home exams? Yes! But, you must understand and test the code. You need to be sure you really understand it and could explain it to someone if asked. The best way to do this is to avoid asking questions straight from course materials (i.e. avoid copying in the entire question) and instead ask ChatGPT to explain parts of the question or concepts that you do not yet understand. Then, think about the output and explanation, test it out, and incorporate what you’ve learned into your response.

Optional: Anaconda#

As noted above, assignments will be completed on Datahub. However, the second part of the course involves completing a project. This can be completed on datahub, but you’ll likely want to complete this on your local computer. To do so, you’ll need Python downloaded on your computer.

Additionally, after this course, if you want to have the tools we use here, you’ll want to have them installed on your own computer.

To ensure that this is possible, we ask that you download the Anaconda Distribution now. This is available for mac, windows,and linux operating systems. However, note that if you have a linux-based machine, you may run into a few hurdles upon installation.

The Anaconda distribution is:

a package manager, an environment manager, a Python/R data science distribution, and a collection of over 1,500+ open source packages. Anaconda is free and easy to install, and it offers free community support.”

This provides you with a local copy of Jupyter and access to commonly-used python packages.

The End!#

This is the end of the first Coding Lab!

Be sure you’ve made a concerted effort to complete all the tasks specified in this lab. Then, go ahead and submit on datahub!

If you fail to click "Submit", we will not have access to or be able to grade your assignment. ALWAYS REMEMBER TO CLICK SUBMIT.