2. Values, Types, Variables, Print, Input

2.1. Welcome back

Let’s get started right away…

Quick Activity

Did you try anything interesting with Python yet?

Heads up: Before we get to the fun stuff, we’ve got to cover the basics. I understand that the basics aren’t super awesome. Don’t worry, we’ll get there. But we can’t get there without the basics.

Activity

  • What is computation? What is a computer? What is programming?

  • Who can name different kinds of computers ?
    • What is a program?

  • Explain to a partner how you would go about making breakfast in the morning.

2.2. Languages

In this course we will use the programming language Python.

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

  • Why is ambiguity so important to natural language?

  • Why is ambiguity deadly for a formal language?

Activity

Do you think there is a limit to what I can describe with a formal language?

Can I describe anything? Any computation?

HINT: Is the following statement true or false: “This statement is false.”

2.3. Terminology

  • Hardware/Software

  • IO

  • Processors

  • Hard Drive

  • RAM

  • Binary

2.4. Okay, we’re done with the background, let’s get on with the real stuff

Activity

Write a (single-line) Python program that prints a witty message, of your choice.

2.5. print

  • Print is a function that allows us to print out information to the screen

  • Print might end up being your best friend

  • Get used to writing it

2.6. Values

  • Values are things that a program manipulates

    • Strings: “abcdef”

    • Integers: 7, 42, 97

    • Floating-point numbers: 3.792, 0.000000000005

  • These values are called literals

    • like, 1 is literally 1

  • Notice how I described the type of each value along with the value itself

    • Strings

    • Integers

    • Float

  • Computers are exceptionally stupid. You must be completely explicit about everything

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

    • They actually have different meaning

    • They even technically have different physical representations inside the computer too!

  • Many of the errors you will make in programming result from mixing types inappropriately

  • Some languages (e.g., C, Fortran, Java) are very militant about types. You have to be totally explicit about them

  • Python is a little more relaxed. You can be explicit, but you don’t have to be. Python will guess if you don’t tell it

  • Upside: less to worry about and less clutter in your code

  • Can I ask Python to tell me its guess for the type of a value?
    >>> print(type(12))
    <class 'int'>
    
    >>> print(type('Witty remark'))
    <class 'str'>
    
    >>> print(type(3.75))
    <class 'float'>
    
    >>> print(type(type(1.1)))
    <class 'type'>
    
  • It’s kinda’ easy to tell the type of a value isn’t it?
    • Most of the time… but this will bite you… trust me!

Activity

Write a single line program to print out the integer 1. Now write a single line program to print out the string 1. Can you tell the difference by looking at the output?

2.7. Variables

  • Probably the most important feature of a procedural programming language.

  • If you’re going to pay attention only once this term… now’s the time.

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

  • You store values into variables by using the assignment operator =
    >>> a=5
    >>> m='Variables are fun'
    
  • For historical reasons, we’re stuck with the ‘=’ symbol for assignment, but 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 as they exist. We’re not asking to change anything; we’re making a statement of fact.

  • In Python when we write
    >>> a=5
    
  • … we’re saying “Hey, Python interpreter! Create a variable named a and store the value 5 in it. This isn’t a statement of fact, it’s an order!

    • a is 5 now

    • a is not a literal though, it’s a variable

    • Wait, what? Literal, variable?

      • It’s simple

      • If I say print(5) python will print out the literal 5

      • If I say print(a), where a was assigned to 5, python will print out the variable a which has the value 5

2.8. 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
    
  • This seems pretty straightforward now, but it’s this ability to store results that will let us do all the cool stuff later.

Activity

  • Assign various values of types string, integer and float to variables.

  • Try adding variables of the same type. What happens?

  • Try adding variables of different types. What happens?

  • Try the assignment 5=a. What happens?

  • Figure out how to display the current contents of a variable.

2.9. Choosing variable names

  • You can use whatever you want, within a few restrictions set by the language.
    • Python wants variable names that begin with a letter of the alphabet and limits what non-alphanumeric characters you can use

  • A good choice is a variable name that is descriptive of what the variable is meant to contain.
    • good: density

    • less good: d

    • bad: definitely_not_density

Activity

Create two variables, named number_1 and number_2, set them to 20 and 23 respectively, then add them.

  • What happened?

2.10. input

  • So we saw how to out print out the contents of a variable

  • Is there a way to read in a value and put it into a variable?

  • YES!

  • Let’s type this
    >>> my_value = input('give me a value: ')
    
  • The string between the parentheses is what will be displayed to the user
    • We can leave it blank too, but nothing will be printed out (this is important for Kattis)
      >>> my_value = input()
      
  • The program will wait for the user to enter a value

  • After a value is entered, it will be stored in the variable myValue

Activity

  • Read in some value into the computer.

  • Print out the value you inputted.

  • What is the type of the value? How can I test this?

  • What if we want it to be an int?

    >>> my_value = input('give me a value: ')
    >>> my_value = int(my_value)
    

or

>>> my_value = int(input('give me a value: '))
  • We can actually use this idea to convert types.
    • int will convert something to an int

    • str will convert something to a string

    • float will convert something to a float

but…

>>> int('hi')
ValueError: invalid literal for int() with base 10: 'hi'

So it will only work if it’s a valid thing to ask

2.11. Statements

  • A statement is an order to Python: “do something

  • An instruction that can be executed by Python

  • You type in the statement into the interpreter, press Enter, and Python does what you asked (or at least tries to)

  • If you type a series of statements into Colab and press run, Python does what you asked (or, again, at least tries to)

  • Some statements produce immediate output, some just change things ‘behind the scenes’

  • We’ve already been using assignment statements (=), prints, inputs, and there are A LOT more

2.12. Expressions

  • An expression is, roughly, a thing that can be crunched down to a value.

  • More precisely, an expression is a combination of:
    • literal values (e.g., 5)

    • variables (e.g., my_variable)

    • operators (e.g., +)
      >>> my_variable = 87
      >>> print(leppard * 2 + 7)
      181
      

2.13. Operators

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

Activity

Generate expressions to:

2.14. Are operators just for numbers?

  • Nope! Values of all sorts have operators that work on ‘em.

Activity

  • Experiment with the operators you know on strings (instead of just integers).

  • Which ones work? What do they do?

  • Try mixing strings and integers with various operators. What happens there?

2.15. Doing sequences of things

  • So far we’ve just been entering one line at a time into the Python.

  • That’s not going to scale very well for most of the stuff we want to do…

  • You can store an (arbitrarily long) series of statements in Colab (or in a file), and then ask Python to run that file for you.

  • Python will execute each line of the file, in order, as if you’d typed them in.

Activity

Consider the sentence Learning programming is fun. Write a program that stores each word of that sentence in it’s own variable, and then prints the whole sentence to the screen, using only a single print statement.

2.16. For next class