COMP1701-004

fall 2023

lec-03

Down the Road

any A1 questions?

speaking of questions

continuing to speak of questions

onlinequestions.org

202304170103

RECALL

Let's do a brain dump: write down everything you can remember - words, phrases, pictures, whatever - about things we covered in lecture and lab last week.

You've got 1 minute.

let's talk about these things today:

hardware & the measly 7 things

software

Hello, Python (on a Codespace)

printing stuffs

variables

Last week, we spent some time up here, solving simple problems by making algorithms for people to follow.

This week, we'll spend time down here, learning how to write algorithms in Python, a language that a computer's hardware can understand (with some help).

let's talk hardware

c. curtis

This is a very high level view of hardware, from a programmer's point of view

hardware

c. curtis

Inputs

c. curtis

Outputs

hardware

c. curtis

Main Memory

hardware

c. curtis

Secondary Storage

hardware

c. curtis

CPU

CPU

What can a CPU actually do?

7 measly things!

  1. read from an input device

  2. write to an output device

  3. read a value from storage

  4. write a value to storage

  5. "do math"

  6. branch

  7. loop (i.e. repeated branching)

WAIT...REALLY???

all possible because of 7 measly operations

we tell the hardware to do these 7 things by writing software

the code that we write doesn't "talk" to the CPU directly

software

a wee problem

// Hey there - I'm written in C++

#include <iostream>
using namespace std;

int main() {
	cout << "Greetings, citizen." << endl;
}
// Hey there - I'm written in Java

class Greeting {
	
    public static void main (String[] args) {
    	System.out.println("Greetings, citizen.");
    }
}
# Hey there - I'm written in Python

print("Greetings, citizen.")

high-level languages

# Hey there - I'm written in Python

print("Greetings, citizen.")

source code

various tools & processes

get ready to code

I'll be using this for lecture code demos...

new kid in town on our GitHub org page

...but IMO, during lecture you should just pay attention and predict

getting ready to code

the REPL

  1. go to the Terminal
  2. enter python
  3. do the Python incantations you wish
  4. exit() or Ctrl + d when done

Demo dat, Pratt.

running a source file

  1. make a new script file (w/ .py extension)
  2. open that file in the editor
  3. enter the Python incantations you wish
  4. run the script (there's at least 3 different ways to do this!)

Demo dat, Pratt.

getting ready to code

Should we use the REPL or run scripts?

Yup.

RECALL

Are these Python instructions being directly interpreted by the computer's CPU?

let's print() some stuffs

What can a CPU do again?

  • read from an input device

  • write to an output device

  • read a value from storage

  • write a value to storage

  • "do math"

  • branch

  • loop (repeated branching)

let's do some of this

printing stuffs

In Python, we tell the CPU we want to output to the terminal by using

print()

Demo dat, Pratt.

(Yes, it's weird that we use "print", because no paper is involved. Sigh.)

printing stuffs

let's RT(f)M a bit

ouch

printing stuffs

print('this','that')

print('this','that' sep='*')

print()

print('this')
      
print('that')
      
print('this',end='...')
      
print('that')

Demo dat, Pratt.

predict!

printing stuffs

what's up with that funky \n?

printing stuffs

\n is an example - probably the most commonly used example - of an escape sequence

print("foo\nfoo")

print("foo\tfoo")

print("foo\bfoo")

print("\u2639")

print("\u26FA") 

printing stuffs

You can totally print numbers, too 

print(3)

print(3.14) # yes to decimals

print(-400) # negs are ok

print(02)  # !! nope

print($120.12) # not really a number

printing stuffs

try this challenge

4825 Mount Royal Gate
Calgary, AB

T3E 4K2

Write the following address 2 ways:

  1. with multiple prints
  2. with one print

printing stuffs

try this challenge

Name       Nickname
Jordan      "JP"
Cassie       50¢

Write the following table.

anyone have some time after lecture today?

RECALL

lec-03

By Jordan Pratt

lec-03

hardware | software | Python plunge | print()

  • 202