Class 4A: Introduction to Programming in Python¶

We will begin at 2:00 PM! Until then, feel free to use the chat to socialize, and enjoy the music!

../../../_images/programming1.jpg


Photo by Christina Morillo from Pexels
September 27, 2021
Firas Moosvi

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)¶

bus stop

Part 2: Writing and Running code (15 mins)¶

../../../_images/bus_stop.jpg

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¶

4 + 5 +2
11
# Subtraction

4-5
-1
# Multiplication
4*5
20
# Division

10/2
5.0
# Square, cube, ... 

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

4**(1/2)
2.0

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)¶

# Two numbers

num1 = 4000000000000
num2 = 8

print(num1,num2)
4000000000000 8
# Multiply numbers together

num1*num2
32000000000000

Assigning words to variables¶

  • You can also assign words and sentences to variables!

  • Surround anything that’s not a number with double quotes ” and “

mysentence = "This is a whole sentence with a list of sports, swimming, tennis, badminton"

print(mysentence)
This is a whole sentence with a list of sports, swimming, tennis, badminton

Using Python to work with words¶

Python 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)¶

mysentence
'This is a whole sentence with a list of sports, swimming, tennis, badminton'
# split on a comma 

mysentence.split(',')
['This is a whole sentence with a list of sports',
 ' swimming',
 ' tennis',
 ' badminton']
# make upper case

mysentence.upper()
'THIS IS A WHOLE SENTENCE WITH A LIST OF SPORTS, SWIMMING, TENNIS, BADMINTON'
# make lower case 

mysentence.lower() 
'this is a whole sentence with a list of sports, swimming, tennis, badminton'
# count the times "i" occurs
mysentence.count('i')   
8
# count two characters: hi
mysentence.count('hi')
1
mysentence2 = "     Hello.     World .....     "
mysentence2
'     Hello.     World .....     '
mysentence2.strip(' ')
'Hello.     World .....'
mysentence2.replace(' ','^')
'^^^^^Hello.^^^^^World^.....^^^^^'

Part 3: Interpreting code (15 mins)¶

../../../_images/bus_stop.jpg

Q1: Interpret Code¶

Look 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.

# Let's try it! 

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

for i in some_numbers:
    print(i*5)
5
250
200
375
2000
5000

Q2: Interpret Code¶

Look 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.

# 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)
1
50
40
15.0
80.0
200.0

Q3: Interpret Code¶

Look 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)/10

for 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.

# 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))
562.5
16000.0
100000.0

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 10

Does the order of the operations you do matter? Yes!

# Let's try it:
((5**2) -2)*10
230
# Here is the same operation as above but in multiple lines

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

print(number)
230

Q5: Parson’s problem¶

A 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!')
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.')
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!

Congratulations!!¶

  • You have just shown that you can program!

  • Over 75% of the course programming content will be focused on details of the things you’ve seen above:

    • Numbers and Strings

    • Loops and Conditionals

    • Functions

  • If you followed along with most of what we covered, you’re in good shape for this course

https://media.giphy.com/media/11sBLVxNs7v6WA/giphy.gif