CSCI 161

Course Notes

  • 1. Introduction and Output
  • 2. Print, Values, Variables, Types
    • 2.1. What Is A Program/Algorithm?
    • 2.2. Print
    • 2.3. Values And Types
    • 2.4. Variables
      • 2.4.1. Using Variables
      • 2.4.2. Naming Variables
      • 2.4.3. Constants
    • 2.5. For Next Topic
  • 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. 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.3. 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 errors you’ll encounter will come 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'>

  • 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.4. 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.

This also means that assignment captures the value at that moment in time, not the relationship between variables. Consider the following:

1x = 1
2y = x + 2
3x = 9
4print(y)

The value of y is still 3, not 11. When y = x + 2 was executed, Python evaluated the right side to 3 and stored that value in y. Changing x afterwards has no effect on y.

2.4.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.4.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.4.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.14
2another_bill = 4.99 * 1.14
  • This is correct and works fine, however

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

    • What if the government 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.14
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.14)

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

  • Constants are not intended to change once set

    • 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.5. For Next Topic

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