<jupyter_start><jupyter_text>Class 4A: Introduction to Programming in PythonWe will begin at 2:00 PM! Until then, feel free to use the chat to socialize, and enjoy the music!             Photo by <NAME> from Pexels     September 27, 2021     <NAME>  Class Outline:- 1st hour - Introduction to Python    - Announcements (2 mins)    - Introduction (3 mins)    - Writing and Running Code (15 min)    - Interpreting Code (15 min)    - Review and Recap (5 min)      Learning Objectives - Look at some lines of code and predict what the output will be. - Convert an English sentence into code. - Recognize the order specific lines of code need to be run to get the desired output. - Imagine how programming can be useful to your life!  Part 1: Introduction (5 mins)  Part 2: Writing and Running code (15 mins)  Using Python to do Math| Task                 | Symbol ||---------------------------|--------|| Addition                  | `+`    || Subtraction               | `-`    || Multiplication            | `*`    || Division                  | `/`    || Square, cube, ...         | `**`  || Squareroot, Cuberoot, ... | `**`   || Trigonometry (sin,cos,tan)| Later...|  Demo (side-by-side)  Addition<jupyter_code>4 + 5 +2
# Subtraction

4-5
# Multiplication
4*5

# Division

10/2
# Square, cube, ... 

2**3
# Squareroot, Cuberoot, ...

4**(1/2)<jupyter_output><empty_output><jupyter_text>Assigning *numbers* to variables- You can assign numbers to a "variable".- You can think of a variable as a "container" that represents what you assigned to it- There are some rules about "valid" names for variables, we'll talk about the details later    - Rule 1: Can't start the name of a variable with a number!    - General guideline for now: just use a combination of words and numbers  Demo (side-by-side)<jupyter_code># Two numbers

num1 = 4000000000000
num2 = 8

print(num1,num2)
# Multiply numbers together

num1*num2<jupyter_output><empty_output><jupyter_text>Assigning *words* to variables- You can also assign words and sentences to variables!- Surround anything that's not a number with double quotes " and "<jupyter_code>mysentence = "This is a whole sentence with a list of sports, swimming, tennis, badminton"

print(mysentence)<jupyter_output>This is a whole sentence with a list of sports, swimming, tennis, badminton<jupyter_text>Using Python to work with wordsPython has some nifty "functions" to work with words and sentences.Here's a table summarizing some interesting ones, we'll keep adding to this as the term goes on.| Task | Function || ---- | -------- || Make everything upper-case | `upper()` || Make everything lower-case | `lower()` || Capitalize first letter of every word | `title()` || Count letters or sequences | `count()` |  Demo (side-by-side)<jupyter_code>mysentence
# split on a comma 

mysentence.split(',')
# make upper case

mysentence.upper()
# make lower case 

mysentence.lower() 
# count the times "i" occurs
mysentence.count('i')   
# count two characters: hi
mysentence.count('hi')
mysentence2 = "     Hello.     World .....     "
mysentence2
mysentence2.strip(' ')
mysentence2.replace(' ','^')<jupyter_output><empty_output><jupyter_text>Part 3: Interpreting code (15 mins)  Q1: Interpret CodeLook at the following code chunk, can you predict what the output will be?```some_numbers = [1, 50, 40, 75, 400, 1000]for i in some_numbers:    print(i*5)``` **A. Prints 6 random numbers.** **B. Prints the 6 numbers in `some_numbers`.** **C. Prints the 6 numbers in `some_numbers` multiplied by 5.**  **D. I don't know.**<jupyter_code># Let's try it! 

some_numbers = [1, 50, 40, 75, 400, 1000]

for i in some_numbers:
    print(i*5)<jupyter_output>5
250
200
375
2000
5000<jupyter_text>Q2: Interpret CodeLook at the following code chunk, can you predict what the output will be?```some_numbers = [1, 50, 40, 75, 400, 1000]for i in some_numbers:        if i > 50:        print(i/5)    else:        print(i)``` **A. Prints the 6 numbers in `some_numbers`.** **B. Prints the number in `some_numbers` if it is less than or equal to 50, otherwise prints the number divided by 5.** **C. Prints the number in `some_numbers` if it is greater than 50, otherwise prints the number divided by 5.** **D. I don't know.**<jupyter_code># Let's try it!

some_numbers = [1, 50, 40, 75, 400, 1000]

for i in some_numbers:

    if i > 50:
        print(i/5)
    else:
        print(i)<jupyter_output>1
50
40
15.0
80.0
200.0<jupyter_text>Q3: Interpret CodeLook at the following code chunk, can you predict what the output will be?```some_numbers = [1, 50, 40, 75, 400, 1000]def process_number(number):     return (number**2)/10for i in some_numbers:        if i > 50:        print(process_number(i))``` **A. Prints the number in `some_numbers` if it is greater than 50, otherwise prints nothing.** **B. Prints the output of the `process_number()` function applied to `some_numbers`.** **C. Prints the output of the `process_number()` function if the original number is greater than 50, otherwise prints nothing.** **D. I don't know.**<jupyter_code># Let's try it!

some_numbers = [1, 50, 40, 75, 400, 1000]

def process_number(number): 
    return (number**2)/10

for i in some_numbers:    
    if i > 50:
        print(process_number(i))<jupyter_output>562.5
16000.0
100000.0<jupyter_text>Q4: Order matters!    Suppose you are asked to complete the following operation:> Take the number 5, square it, subtract 2, and then multiply the result by 10Does the order of the operations you do matter? Yes!<jupyter_code># Let's try it:
((5**2) -2)*10
# Here is the same operation as above but in multiple lines

number = 5
number = number**2  
number = number - 2
number = number * 10

print(number)<jupyter_output>230<jupyter_text>Q5: Parson's problemA Parson's Problem is one where you are given all the lines of code to solve the problem, but they are jumbled and it's up to you to get the right order.A student would like to get this as the final output of some code that they are writing:```3 is smaller than, or equal to 10.4 is smaller than, or equal to 10.5 is smaller than, or equal to 10.6 is smaller than, or equal to 10.7 is smaller than, or equal to 10.8 is smaller than, or equal to 10.9 is smaller than, or equal to 10.10 is smaller than, or equal to 10.11 is bigger than 10!12 is bigger than 10!13 is bigger than 10!14 is bigger than 10!15 is bigger than 10!``` Here are ALL the lines of code they will need to use, but they are scrambled in the wrong order. Can you produce the desired output? *Hint: Pay attention to the indents!*```my_numbers = [3,4,5,6,7,8,9,10,11,12,13,14,15]for i in my_numbers:      if i > 10:        print(i,'is smaller than, or equal to 10.')    else:        print(i,'is bigger than 10!')```<jupyter_code>my_numbers = [3,4,5,6,7,8,9,10,11,12,13,14,15]
for i in my_numbers:  
    if i > 10:
        print(i,'is bigger than 10!')
    else: 
        print(i,'is smaller than, or equal to 10.')<jupyter_output>3 is smaller than, or equal to 10.
4 is smaller than, or equal to 10.
5 is smaller than, or equal to 10.
6 is smaller than, or equal to 10.
7 is smaller than, or equal to 10.
8 is smaller than, or equal to 10.
9 is smaller than, or equal to 10.
10 is smaller than, or equal to 10.
11 is bigger than 10!
12 is bigger than 10!
13 is bigger than 10!
14 is bigger than 10!
15 is bigger than 10!
{'fraction_non_alphanumeric': 0.11050551846300585, 'fraction_numerical': 0.05382204660035427, 'mean_word_length': 3.5618396519577376, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '491501', 'license': 'MIT', 'n_tokens_mistral': 2630, 'n_tokens_neox': 2260, 'n_words': 1005, 'path': 'notes/week04/Class4A/Class4A.ipynb', 'repo_name': 'ubco-cmps/data301_course', 'size': '7356'}