Q&A
Q: do we need to download the Python? or only use the scholl website?
A: You can do the entire course without downloading Python. If you want access after teh course, we recommend downloading the Anaconda Distribution (this is part of the first Coding Lab)
Q: I know you said we wouldn’t use them, but what are raw cells? and when would they be useful?
A: These are used to store text that should not be executed by the notebook kernel. What this means is that the code inside is not “run.” In Python world, these are most helpful when writing Sphinx documentation (something we won’t be covering in this course).
Q: Why are there pound signs in front of text in a code cell?
A: These are code comments - they are ignored by the computer and are written by humans for humans to explain the code to come.
Q: is it possible to add cells inbetween already existing cells in the notebook?
A: Yup - the plus sign icon at top adds a cell. The keyboard shortcuts to do the same are ‘a’ for adding a cell above and ‘b’ for adding a cell below.
Q: what about other math operations like division or multiplication?
A: We’ll discuss that today! (In the operators notes)
Q: Why do we need to start a variable with a lowercase letter
A: You don’t have to but it is best practice in Python - will discuss this today, but briefly, snake case (all lowercase with undersscores between) is the fastest for our brains to process, and since we use variables most frequently, we use snake case for them.
Q: When is the due date for signing up for exams?
A: Technically, 10min before the exam. Realistically though, the sooner the better so you can secure a slot/time you like before they fill up.
Q: How do you make a kernel continuously run? I wanna watch the print number increase into infinity if it is possible.
A: It will eventually shut down because the size of the file will exceed the limit/memory I’ve allocated to each notebook.
Q: Will you ever demonstrate how to up change font sizes and font colors :)????
A: I won’t, but I’m making a note to add a Piazza post for those interested :)
Course Announcements
Due this week:
VQ3 due Wednesday
CL1 due Friday (labs start tomorrow!)
Notes:
A1 has been released (discuss: notebook, LLM focus)
Office Hours listed on Canvas Homepage
GenAI Activity: Summary & Discussion
18.7% of students correctly defined a function
Used GenAI (24%; ChatGPT most common): ~50.0% correct
Did NOT use GenAI (76%): ~8.9% correct
Prompting Strategies:
More Effective:
Step-by-step guidance (N=195)
Conceptual explanation (i.e. “What is a function?”) (N=77)
Asking for an example (N=64)
Ask for practice problems (N=39)
Less Effective:
Asking for direct answer (N=128)
Additional Prompting Strategies:
Check my work/give feedback
Specific debugging questions
Explain my code (line-by-line; at the level of a 10 y/o; why is your’s different)
“Give me a hint” or “Do not display any code”
How could I test this function?
Is there a better approach?
What are some common mistakes beginners made?
Summarize the most important points about this topic
Variables#
Review
Code Style
Variables Activity
Declaring Variables Cheat Sheet#
Names are always on the left of the
=, values are always on the rightNames are case sensitive
Variables must start with letters (or underscores)
After that, they can include numbers
They cannot include special characters (like &, *, #, etc)
Python doesn’t care what you name your variables
Humans do care. Pick names that describe the data / value that they store
Python is dynamically typed - variable type is determined on creation; every variable has a type
Reserved Words#
There are 33 words that are not allowed to be used for variable assignment in Python 3.6.
False |
None |
True |
and |
as |
assert |
break |
class |
continue |
def |
del |
elif |
else |
except |
finally |
for |
from |
global |
if |
import |
in |
is |
lambda |
nonlocal |
not |
or |
pass |
raise |
return |
try |
while |
with |
yield |
# you will get an error if you try to assign a variable to one of these words
try = 6
a = 6
Not all equal signs are created equal#
In mathematics: = refers to equality (as a statement of truth).
In coding: = refers to assignment. == refers to equality
In your head, when you are reading code like
x = 3
you should say it to yourself as:
“we assign the value 3 to the variable x”
Math: What is x?
\(y = 10x + 2\)
Code: What is x?
x = x + 1
Code Style#
Code style considerations do NOT affect the functionality of your code, but DO affect readability.
Get in good habits now!
For variable assignment:
we use a single space around assignment operator
=we use snake_case for variable names (All lowercase, underscores between words)
use informative variable names…something that tells you a bit about what’s being stored
Ideal:
my_variable = 6Avoid:
MyVariable=6
Activity time: Variables#
Complete all questions in this Google Form (https://forms.gle/BiAxKqbdPJK7422g7) and then click submit.
You are encouraged to:
Talk with your neighbor
Try the questions out in your notebok to check your understanding
If you have questions, I’m happy to come over as we work on this!
Mutable vs Immutable#
The variable types we’ve talked about today are all immutable. This means they cannot be altered after they’re created.
immutable_string = 'COGS18 is the best!'
immutable_string[4]
# cannot change part of the string after creation
immutable_string[4] = '0'
Python does have mutable types. We’ll talk about these soon!
Indentation#
Just a brief word on indentation.
Python does care about whitespace.
You will get an error if Python runs into unanticipated whitespace.
a = 1
b = a
print(b)
There are times when indentation will be required and expected. We’ll discuss these in future lectures.