CSCI 161

Course Notes

  • 1. Introduction and Output
  • 2. Print, Values, Variables, Types
    • 2.1. What Is A Program/Algorithm?
    • 2.2. What Is Debugging?
    • 2.3. Languages
    • 2.4. Terminology
    • 2.5. Print
    • 2.6. Values And Types
    • 2.7. Variables
      • 2.7.1. Using Variables
      • 2.7.2. Naming Variables
      • 2.7.3. Constants
    • 2.8. For Next Class
  • 3. Statements and Expressions
  • 4. Input and Changing Types
  • 5. Functions
  • 6. Propositional Logic
  • 7. Testing Your Code and Type Hints
  • 8. If/Else
  • 9. Putting Things Together
  • 10. Loops
  • 11. Strings & Objects
  • 12. Lists
  • 13. References
  • 14. Aliases & List Trivia
  • 15. Tuples, Dictionaries, and Sets
  • 16. Debugging
  • 17. File IO
  • 18. Exceptions
  • 19. Objects I — Introduction
  • 20. Objects II — More on Methods
  • 21. Objects III — Interacting Objects
  • 22. Unittest
  • 23. Objects IV — Data Structures
  • 24. Searching and Complexity
  • 25. Sorting Algorithms

Labs

  • 1. Hello World
  • 2. Functions
  • 3. Testing
  • 4. Conditionals
  • 5. Car Rental
  • 6. Loops & Linear Search
  • 7. Lists
  • 8. 2D Lists
  • 9. References & More Lists
  • 10. Objects
  • 11. Unit Testing Objects
  • 12. Data Structures

Assignments

  • 1. Density of Starbucks
  • 2. Zombie Infections
  • 3. Tic-Tac-Toe
  • 4. Country Catalogue

Getting Started

  • Getting set up for CSCI 161

Outline

  • Computer Science 161 Section 66 — Introduction to Programming
  • Course Schedule
CSCI 161
  • 2. Print, Values, Variables, Types
  • View page source

2. Print, Values, Variables, Types

Activity

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

Activity

Who can name different kinds of computers?

2.1. What Is A Program/Algorithm?

Activity

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

  • There, that’s an algorithm

    • It is a series of instructions that can be followed to achieve something

  • What kind of computer was executing this program?

2.2. What Is Debugging?

  • Most of the time your code will be wrong

    • This is true for both new and experienced programmers

  • Debugging is the process of addressing the bugs in your code

  • Realistically, expect to debug a lot

  • Remember, you will be wrong every time you run your program before you get it right

    • The point is, you will be wrong a lot more than you will be right — get used to this

Quick Activity

Have you seen any Python errors yet?

What were they?

Did you understand them?

2.3. Languages

  • What is a natural language?

  • What is a formal 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.4. Terminology

  • Hardware/Software

  • Input/Output (I/O)

  • Processors

  • Storage — Hard Disk Drive/Solid State Drive

  • Memory and Random Access Memory (RAM)

  • Binary

  • Compiling

  • Interpreter vs Integrated Development Environment (IDE)

2.5. Print

  • We already made use of print in the previous topic

Activity

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

  • 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 And Types

  • Values are things that a program manipulates

    • Strings: "abcdefg", "Hello World"

    • 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

  • To a computer, the integer 1 is not necessarily the same thing as the floating point number 1.0 or the string "1"

  • Some of the errors you will make will be a result from mixing types incorrectly

  • Some languages (e.g., C, Java) are strict about types

    • You have to be totally explicit about them

  • Python is a little more relaxed

    • Python will guess what the type is

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

    • Downside: more likely to introduce errors caused by mixing types

  • You can check the type of something in Python by using the type function

    • print(type(12)) would print out <class 'int'>

    • print(type("Hello, World")) would print out <class 'str'>

    • print(type(3.75)) would print out <class 'float'>

    • print(type(type(1.1))) would print out <class 'type'>

  • Notice that we are using two functions in the above examples

    • print

    • type

Activity

  1. Write a single line program to print out the integer 1.

  2. Now write a single line program to print out the string "1".

  3. Can you tell the difference by looking at the output?

2.7. Variables

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

  • You store values into variables by using the assignment operator — =

1a = 5
2m = "Some String"
  • In the above example, the variable a now has the value 5

  • Both the variable a and the literal 5 have the same value

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

    • If I say print(a), Python will print out the value stored in the variable a, which is 5

Warning

The = in Python has a very different meaning from what you are familiar with in math. In math, when one writes \(a = 5\), it means that \(a\) and \(5\) are equivalent as they exist — it is stating a fact.

In Python, and many other programming languages, it is not a statement about equality, but an assignment. In Python, if one writes a = 5, it means that the variable a is now storing the value 5 within it.

2.7.1. Using Variables

  • You can use variables in the same way you use literals

1print(5 + 6)
2
3a = 5
4b = 6
5print(a + b)
  • Both print function calls will print out 11

    • The first one adds the literals 5 and 6

    • The second one adds the variables a and b

Activity

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

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

  3. Try adding variables of different types. What happens?

  4. Try the assignment 5 = a. What happens?

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

2.7.2. Naming Variables

  • 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

  • There are a few other important restrictions that you may come across

    • For example, you cannot use reserved words (words that already have a specific meaning in Python)

      • def = 55 will not work since def is a reserved word

  • Two important conventions we will follow

    • Use lowercase letters

    • Separate words in the variable name with underscores (snake case)

      • some_bill

2.7.3. Constants

  • In Python, constants are just variables that we as programmers use in a special way

  • Imagine you are writing a program where you’re doing a lot of calculations with sales tax

1some_bill = 10.45 * 1.15
2another_bill = 4.99 * 1.15
  • This is clearly correct, however

    • What if someone else looks at this code and wonders what 1.15 is?

    • What if the gov changes the sales tax in the future?

  • Although there is nothing wrong with the above code, one could do the following instead

1SALES_TAX = 1.15
2some_bill = 10.45 * SALES_TAX
3another_bill = 4.99 * SALES_TAX
  • Now, just by looking at those lines of code, I know exactly what we are multiplying the numbers with

  • If the sales tax rate is ever lowered, all I need to do is change the one line of code (SALES_TAX = 1.15)

  • The naming convention for constants is all uppercase letters separate with underscores

  • The idea behind the constants are that once the value is set by you, they are not to change

    • You can change them in the code, but the code should not alter the value of SALES_TAX

  • In Python, there is nothing stopping you from changing the value other than the convention

    • In some languages, the language actually prevents the program from altering the value of a constant



2.8. For Next Class

  • Read the rest of Chapter 2 of the text

Previous Next

© Copyright 2022, James Hughes, Taras Mychaskiw, & Jean-Alexis Delamer.

Built with Sphinx using a theme provided by Read the Docs.