GGR274 Week 1: Computation, Python, and JupyterHub#
David Liu, Ilya Musabirov
What is programming?#
A computer program is a set of instructions for a computer to execute.
Just as humans have languages like English to communicate with each other, a programming language is a language that allows humans to communicate these instructions to a computer.
Python!#
In this course, we’ll be using the Python programming language.
Why Python?#
Python is…
beginner friendly (code looks a lot like English)
lots of built-in functionality
powerful data science and data visualization libraries (e.g.
numpy
,pandas
,plotly
)very commonly used in both academia and industry
Writing Python code#
You’ve seen examples of Python code earlier this lecture. Next week, we’ll start going over the basics of Python programming, but for now, think of Python code as a sequence of very precise instructions that your computer can understand.
my_name = "Ilya"
my_age = 5 + 13
print(f"Wow {my_name}, you're only {my_age} years old? Why do they let you teach?!")
my_age = my_age + 21
print(f"Wow {my_name}, you're already {my_age} years old? You should be resting more!")
Wow Ilya, you're only 18 years old? Why do they let you teach?!
Wow Ilya, you're already 39 years old? You should be resting more!
Notebooks!#
Traditionally, Python code was written in .py
files, called Python modules.
Notebooks!#
In this course, we’ll use Jupyter notebooks to write Python code.
Notebooks let us mix:
Python code
Output of running the code
Text (including nice formatting)
Images
…and more!
Notebooks!#
Fun fact: Jupyter, the software that lets us create notebooks, is written in Python!
Do I need to download and install Python and Jupyter?#
No software installation required!#
In this course, we’ll be using JupyterHub (https://jupyter.utoronto.ca), which runs Python and Jupyter for us online.
You can login to JupyterHub from anywhere with an Internet collection
All of your files are stored “in the cloud”
Runs Python code “in the cloud”
JupyterHub Demo!#
Two ways of accessing JupyterHub:
Go to any notebook on our course website (https://uoftcompdsci.github.io/ggr274-20241/) and click on the little rocket icon:
Learning and Learning to Program. Some things to remember#
Computer programming languages are actually for people, not for computers :)
To some extent, learning a new programming language is quite like learning a new (written) human language, but … easier
Vocabulary is much, much smaller
Syntax/Grammar are much simpler
The main limitation is that the computer (of pre-GPT era) is much more limited in understanding and thus more strict
The real barrier is understanding what you want to do and explaining that in a clear non self-contradictory way
In addition to writing code, we in this course and you, as you progress, should be learning reading code, and doing that effectively might be even more crucial
Data Science tasks rarely start from a blank page
You use, assimilate, and adapt the code and ideas of others
Quite often, you are in a “menu in an exotic restaurant” situation, and that’s okay!
Why learning is difficult?#
Coding Example (AI-edited)#
Example 1:
print join " ", map { scalar reverse $_ } split / /, "Hello, this is a sample sentence.";
Example 2:
' '.join([word[::-1] for word in 'Hello, this is a sample sentence.'.split()])
' '.join([word[::-1] for word in 'Hello, this is a sample sentence.'.split()])
',olleH siht si a elpmas .ecnetnes'
Example 3:
sentence = "Hello, this is a sample sentence."
words = sentence.split()
reversed_words = []
for word in words:
reversed_word = ''
for char in word:
reversed_word = char + reversed_word
reversed_words.append(reversed_word)
Example 3 (contd)
reversed_sentence = ''
for word in reversed_words:
if reversed_sentence:
reversed_sentence += ' '
reversed_sentence += word
print(reversed_sentence)
Example 4:
# Original sentence
sentence = "Hello, this is a sample sentence."
# Split the sentence into individual words
words = sentence.split()
# Initialize an empty list to hold reversed words
reversed_words = []
Example 4 (contd)
# Iterate over each word in the sentence
for word in words:
# Initialize an empty string to build the reversed word
reversed_word = ''
# Iterate over each character in the word
for char in word:
# Prepend the character to the reversed_word, effectively reversing it
reversed_word = char + reversed_word
# Append the reversed word to the list of reversed_words
reversed_words.append(reversed_word)
Example 4 (contd 2)
# Initialize an empty string for the reversed sentence
reversed_sentence = ''
# Iterate over each reversed word
for word in reversed_words:
# If reversed_sentence is not empty, add a space (to separate words)
if reversed_sentence:
reversed_sentence += ' '
# Append the current word to the reversed_sentence
reversed_sentence += word
# Print the final reversed sentence
print(reversed_sentence)
Example 5:
Splitting the Sentence: The sentence is split into words.
Reversing Each Word: Each word is reversed using a nested loop.
Building the Reversed Sentence: The reversed words are then concatenated to form the final sentence, with spaces added between words.
Printing the Result: The reversed sentence is printed.
' '.join([word[::-1] for word in 'Hello, this is a sample sentence.'.split()])
Some Active (Self-)Learning Strategies#
Activating Prior Knowledge
Distributing Practice (not cramming!) and Interleaving (not creating interruptions!)
Elaborative rehearsal: rephrasing, finding examples, integrating with other constructs, organizing in a meaningful way
From strategies to tactics#
Reading your own code and the code of others, highlight and annotate it (notebooks are perfect for that)
Use markdown blocks to write down topic sentences and paragraphs,
Always assume you will need to reread your code and that you will you remember nothing
Dealing with the complex blocks of code you don’t understand just yet, try to write down high-level explanations in plain language
what does it do?
what does it take as an input and produce as an output?
Distributed practice and elaborative rehearsal:
Read the lab and homework at the beginning of the week. If you have time, make comments
Look at the previous homework to find useful chunks
Regularly repeat main commands, functions, and constructs in context
For example, modify a bit chunks from previous homework (What if I change 5 to 6, a number to a character, add another variable?)
Make notes on connections between terminology and Python syntax, between different ways of writing the same code