<jupyter_start><jupyter_text>**Course Announcements**Due Dates:- **CL2** due Wednesday (11:59 PM)- **A1** now available; due next Mon 1/17 (11:59 PM)    - originally a typo in Q10, instructions referred to `student_b` twice, third one should be `student_c` (`assert`s would make it clear we wanted `student_c`Notes:- Office Hours Start today!- Waitlist: I've reached out to staff- Anaconda demo- **CL1** has been (re-)graded- Pre-course survey EC has been posted; responses to survey sent **Pre-Course Survey Summary**- N=347 (338 unique)- 70.1% of the class has never programmed before- Common hobbies/activities: Surfing, Netflix/shows, cooking/baking, spots/working out, music/kpop, work - Common futures: Psychology, Neuroscience, Physician, Happy, Content, Employed- Common note: Nervous/anxious scared, but excited and/or willing to work hard **Q&A**Q: What's the difference between print() and just typing the variable name by itself to display the output?   A: For our purposes they're very similar. One small difference is that `print()` will *always* print out whatever is in the parentheses; Typing the variable by itself will only show what the variable stores if it is the last thing in the cell.Q: Do the numbers on the side (that appear once you run the code) show up for you (the grader)? Like if I make a mistake and have to run the code multiple times so now I have a large number or something, is that bad?  A: We can see them when you submit. However, it doesn't matter to me how long it takes you to understand...just that you understand! So, definitely do not worry about/stress over this!Q: How are hidden test created? What functions are employed? Does it require some new libraries? I have heard of hidden tests in coding assignments before but have never "seen" one and see how it is implemented to check a piece of code.   A: Occassionally I'll import new libraries. But, the hidden tests are typically just additional `assert` statements, like the ones you can see in your assignments. That said, when assignments are graded, you'll see all assert statements, hidden ones included...so after A1 is graded, you'll have a better sense for the hidden tests!Q: do the hidden tests effect anything more than just checking our work? Is the verify button basically the same thing?  A: The validate button only checks that you passed the *visible* tests. And, nope, hidden tests are just there to check your answers.Q: How would you be able to fix it if you accidentally delete a cell? Is there a way to start over an assignment if needed?  A: You can use the "Undo Delete Cell" from the Edit menu at top. To completely restart an assignment, rename the folder where the assignment is stored...and then you'll be able to fetch a fresh copy of the assignment from the assignments tab on datahub.Q: I want to know what each color means when we encode. Sometimes it’s red, green, and mostly black and I am sure other colors will come up.   A: I'll point these out as we go! For now, variables you create are black. Functions that exist in python are green. Reserved keywords are bold and green. Red is typically to point out an issue/something you want to fix.Q: If we want to write notes on jupyterhub, how do we change the color of the notes?  A: https://campuswire.com/c/G9193CB28/feed/10  Q: What is the purpose of the raise NotImplemented(error)?  A: To indicate that this is where you're supposed to put your code. You should delete this line and replace it with your code.Q: Do we need an underscore for a variable name if it is only one word. Example "flowers"  A: NoQ: Why does python not have variable types?   A: It does, but it is a dynamically-typed programming language.Q: How can we use Anaconda to access the course content, since it's not necessarily connected to UCSD's datahub? Would we download each individual page from the course website and upload it to our own Jupyter notebook?  A: Downloading from website would be the best way unless you're familiar with git/GitHub.  Operators- assignment- math- logic- comparison- identity- membership Operators are special symbols in Python that carry out arithmetic or logical computation.  Assignment Operator Python uses = for assignment.<jupyter_code>my_var = 1<jupyter_output><empty_output><jupyter_text>Math Operators- `+`, `-`, `*`, `/` for addition, subtraction, multiplication, & division- `**` for exponentiation & `%` for modulus (remainder)- `//` for floor division (integer division) Python uses the mathematical operators +, -, *, / for 'sum', 'substract', 'multiply', and 'divide', repsectively.<jupyter_code>print(2 + 3)
div_result = 4 / 2 
print(div_result)
type(div_result)<jupyter_output><empty_output><jupyter_text>Order of OperationsMathematical operators follow the rules for order of operations. - follow the rules for order of operations.- parentheses specify which order you want to occur first<jupyter_code>order_operations = 3 + 16 / 2
print(order_operations)<jupyter_output><empty_output><jupyter_text>To specify that you want the addition to occur first, you would use parentheses.<jupyter_code>specify_operations = (3 + 16) / 2
print(specify_operations)<jupyter_output><empty_output><jupyter_text>Clicker Question 1What would be the value stored in `my_value`? Note: Best to think about it before running the code to ensure you understand.<jupyter_code>my_value = (3 + 2) + 16 / (4 / 2) 
my_value<jupyter_output><empty_output><jupyter_text>- A) 7.0- B) 10.5- C) 13.0- D) 20.0- E) Produces an error  More Math Python also has ** for exponentiation and % for remainder (called modulus). These also return numbers.<jupyter_code># 2 to the power 3
2 ** 3
# remainder of 17 divided by 7
17 % 7<jupyter_output><empty_output><jupyter_text>Clicker Question 2What would be the value stored in `remainder`?<jupyter_code>remainder = 16 % 5
remainder<jupyter_output><empty_output><jupyter_text>- A) 0- B) 1- C) 3- D) 3.2- E) Produces an error  Clicker Question 3What would be the value stored in `modulo_time`?<jupyter_code>modulo_time = 4 * 2 % 5
modulo_time<jupyter_output><empty_output><jupyter_text>- A) 0- B) 1- C) 3- D) 3.2- E) Produces an error  RemainderHow to get Python to tell you the integer and the remainder when dividing?<jupyter_code>a = 17 // 7
b = 17 % 7

print(a, 'remainder', b)<jupyter_output><empty_output><jupyter_text>`//` is an operator for floor division (integer division)  Logical (Boolean) operators- use Boolean logic- logical operators: `and`, `or`, and `not` Booleans are named after the British mathematician - George Boole. He first formulated Boolean algebra, which are a set of rules for how to reason with and combine these values. This is the basis of all modern computer logic. Python has and, or and not for boolean logic. These operators often return booleans, but can return any Python value. - `and` : True if both are true- `or` : True if at least one is true- `not` : True only if false<jupyter_code>True and True
True or True
True and not False
not False
# two nots cancel one another out
not (not True)<jupyter_output><empty_output><jupyter_text>Capitalization matters<jupyter_code># this will give you an error
# 'TRUE' is not a boolean
# 'True' is
TRUE and TRUE<jupyter_output><empty_output><jupyter_text>Clicker Question 4How will the following boolean expression evaluate:<jupyter_code>(6 < 10) and (4 == 4)<jupyter_output><empty_output><jupyter_text>- A) True- B) False- C) None- D) This code will fail  Comparison Operators Python has comparison operators ==, !=, , >, , and >= for value comparisons. These operators return booleans. - `==` : values are equal- `!=` : values are not equal- `<` : value on left is less than value or right- `>` : value on left is greater than value on right- `<=` : value on left is less than *or equal to* value on right- `>=` : value on left is greater than or equal to value on the right<jupyter_code>a = 12
b = 13
a > b
True == True
True != False
'aa' == 'aa'
12 <= 13<jupyter_output><empty_output><jupyter_text>Clicker Question 5Assume you're writing a videogame that will only slay the dragon only if our magic lightsabre sword is charged to 90 or higher and we have 100 or more energy units in our protective shield. Start with the code in the following cell. Replace `---` with operators or values that will evaluate to `True` when the cell is run (and slay the dragon!).- A) I did it!- B) I tried but am stuck.- C) I'm unsure where to start<jupyter_code>## EDIT CODE HERE
sword_charge = ---
shield_energy = ---

(sword_charge ---) and (shield_energy ---)<jupyter_output><empty_output><jupyter_text>Identity Operators Identity operators are used to check if two values (or variables) are located on the same part of the memory. Python uses is and is not to compare identity. These operators return booleans. - `is` : True if both refer to the same object- `is not` : True if they do not refer to the same object<jupyter_code>a = 927
b = a
c = 927
print(a is b)
print(c is a)<jupyter_output><empty_output><jupyter_text>Two variables that are equal does **not** imply that they are identical. If we wanted that second statement to evaluate as `True` we could use `is not`...<jupyter_code># make a True statement
print(c is not a)
# testing for value equality
a == b == c<jupyter_output><empty_output><jupyter_text>Clicker Question 6Using the variables provded below and identity operators replace `---` with code such that `true_variable` will return `True` and `false_variable` will return `False`. - A) I did it!- B) I tried but am stuck.- C) I'm unsure where to start<jupyter_code>z = 5
x = '5'
c = 'Hello'
d = c
e = [1, 2, 3]
f = [1, 2, 3]

# EDIT CODE HERE
true_variable = ---
false_variable = ---

print(true_variable, false_variable)<jupyter_output><empty_output><jupyter_text>Delving Deeper: Identity Operators A **new object** is created each time we have a variable that makes reference to it, but there are *few notable exceptions*:- some simple strings- Integers between -5 and 256 (inclusive)- empty immutable containers (e.g. tuples) - we'll get to these laterWhile these may *seem* random, they exist for memory optimization in Python implementation.  Shorter and less complex strings are "interned" (share the same space in memory).The rules behind this are a bit fuzzy, so we'll just go through a few examples here. But, if you want to read more about string interning and how Python handles this, you can read more [here](http://guilload.com/python-string-interning/).<jupyter_code>simple_string = 'string'
simple_string2 = 'string'
simple_string is simple_string2
print(id(simple_string), id(simple_string2))
longer_string = 'really long string that just keeps going'
longer_string2 = 'really long string that just keeps going'
longer_string is longer_string2
print(id(longer_string), id(longer_string2))
d = 5
e = 5
print(id(d), id(e))
print(d is e)<jupyter_output><empty_output><jupyter_text>Python implementation front loads an array of integers between -5 to 256, so these objects *already exist*.<jupyter_code># Python doesn't create a new object here
j = 5
k = 5
l = 'Hello'
m = 'Hello'

true_variable_integer = j is k
true_variable_string = l is m

print(true_variable_integer, true_variable_string)
# Python DOES create a new object here
n = 975 #greater than 256
o = 975
p = 'Hello!' #that exclamation point makes it more complex
q = 'Hello!'

false_variable_integer = n is o
false_variable_string = p is q

print(false_variable_integer, false_variable_string)<jupyter_output><empty_output><jupyter_text>Clicker Question 7Using the variables provded below and identity operators replace `---` with code such that `true_variable` will return `True` and `false_variable` will return `False`. - A) I did it!- B) I tried but am stuck.- C) I'm unsure where to start<jupyter_code>a = 5
b = 5
c = b
d = 'Hello!'
e = 'Hello!'
f = 567
g = 567

# EDIT CODE HERE
true_variable = ---
false_variable = ---

print(true_variable, false_variable)<jupyter_output><empty_output><jupyter_text>Membership Operators Python uses in and not in to compare membership. These operators return booleans. Membership operators are used to check whether a value or variable is found in a sequence.Here, we'll just be checking for value membership in strings. But, we'll discuss lists, tuples, sets, and dictionaries soon. - `in` : True if value is found in the sequence- `not in` : True if value is not found in the sequence<jupyter_code>x = 'I love COGS18!'
print('l' in x)
print('L' in x)
print('COGS' in x)
print('CSOG' in x)
print(' ' in x)<jupyter_output><empty_output><jupyter_text>String Concatenation Operators sometimes do different things on different types of variables. For example, + on strings does concatenation.<jupyter_code>'COGS' + ' 18'
'a' + 'b' + 'c'<jupyter_output><empty_output><jupyter_text>Chaining Operators Operators and variables can also be chained together into arbitrarily complex expressions.<jupyter_code># Note that you can use parentheses to chunk sections
(13 % 7 >= 7) and ('COGS' + '18' == 'COGS18')
(13 % 7 >= 7)
('COGS' + '18' == 'COGS18')<jupyter_output><empty_output><jupyter_text>Clicker Question 8How will the following expression evaluate:<jupyter_code>2**2 >= 4 and 13%3 > 1<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08665105386416862, 'fraction_numerical': 0.014958072070710886, 'mean_word_length': 4.836860670194003, 'pattern_counts': {'":': 0, '<': 101, '<?xml version=': 0, '>': 106, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '24', 'dir': 'jupyter-structured-clean-dedup', 'id': '192168', 'license': 'MIT', 'n_tokens_mistral': 4094, 'n_tokens_neox': 3841, 'n_words': 1831, 'path': '_build/html/_sources/materials/04-Operators.ipynb', 'repo_name': 'COGS18/COGS18.github.io', 'size': '13237'}