menu COMPSCI 2120/9642/DIGIHUM 2220 1.0 documentation

CS 2120: Topic 2

Videos for this week:

Part 1: Background

Part 2: Coding

Welcome back! This week we’ll cover a bit of background theory and get into variables, operations, printing to the console, and a few other things.

Before we start..

Do you have Python running on your computer? If not, see “Getting set up for CS 2120


Background (Theory)

What’s a program?

  • You can think of it as a recipe for the computer to do something that you want it to do

  • More formal: A sequence of instructions that specifies exactly how to perform a computation

What’s debugging?

  • Fixing your errors: syntax, runtime, or semantic

Languages

  • What’s the difference between a “natural language” and a “formal language”?

  • Why is ambiguity a problem for a formal language?

Remark

Is the following statement true or false? This statement is false.

Background (Python)


Sanity Check

Write a Python program that prints a message to the console.

Values

  • Values are things that a program manipulates.
    • Strings: “abcdef”

    • Integers: 7, 42, 97

    • Floating-point numbers: 3.792, 0.00005

  • To a computer, the integer 1 is not necessarily the same thing as the floating point number 1.0… because they have different types

  • Python will guess at the type of your value if you don’t tell it.

  • Can I ask Python to tell me its guess for the type of a value?
    >>> type(12)
    <type 'int'>
    >>> type('Toronto Blue Jays')
    <type 'str'>
    >>> type(3.75)
    <type 'float'>
    

Variables

  • Variables let you store values in a labelled (named) location

  • You store values into variables by using the assignment operator =
    >>> a=5
    >>> m='I like variables'
    
  • The ‘=’ symbol here corresponds to variable assignment… it doesn’t really mean the same thing as the ‘=’ sign in math.

  • In math when we write ‘a = 5’ we mean that ‘5’ and ‘a’ are equivalent.

  • In Python when we write
    >>> a=5
    
  • … we’re telling the Python interpreter to create a variable named a and store the value 5 in it.

What can you do with variables?

  • Anything you can do with values.

  • For example, we can add variables:
    >>> a = 5
    >>> b = 7
    >>> a+b
    12
    >>> b=5
    >>> a+b
    10
    

Choosing variable names

  • Some rules for Python..
    • start the name with a letter of the alphabet or an underscore (“_”)

    • a variable name can’t start with a number

    • a variable name can only be made up of alphanumeric characters and underscores

    • variable names are case-sensitive

Try this

Create two variables, named maple and leafs, and set them to 19 and 67, respectively. Try adding them. What happened?

Statements

  • A statement tells Python to “do something”.
    • for example, printing to the screen (i.e. print("a"))

  • it is an instruction that can be executed by the interpreter.

Expressions

  • An expression is a combination of:
    • values (e.g., 5)

    • variables (e.g., whale)

    • operators (e.g., +)

    • Example:

    >>> leafs * 2 + 7
    141
    

Operators

  • Operators are symbols that tell Python to perform computations on expressions.
    • +, -, \*, /

Order of Operations

  • Python follows the usual order of operations (B-E-D-M-A-S).

  • You can use () to customize your expressions:
    >>> 2 + 5 * 2
    12
    >>> (2 + 5) * 2
    14
    

Are operators just for numbers?

  • No

  • they work on strings, for example

Try this

Try mixing strings and integers with various operators.

Ways to run Python

  • The engine which runs Python (the interpreter) can run in 2 ways:
    • immediate mode: you type a line of code and the interpreter responds with an output (like the code samples on this page)

    • script mode: you type all of the code out (i.e. in a PyCharm script), run it, the interpreter responds with all of its output in the console window

For next class