CMSC 201

Lab 2: A Simple Program

Variables

What are they?

A variable is a name or placeholder for information. It's an easy way to keep track of information (ie the area of a circle or the name of a student) without remembering it all ourselves. Imagine a big bucket with a name on the side (pi) and some data (3.14159) inside. Instead of remembering and typing 3.14159 every time we calculate the area of a circle, we know that 3.14159 is already in the pi "bucket" so we can save ourselves time by just using the "name" pi.

Python has several different variables types but today we will focus on two, ints and strings

  1. Int stands for integer. Remember, an integer is any number with no fractional part.
    Example: 2, -84, 0, 25, 10

  2. String stands for a "string" of characters. Strings can be anything and are represented by quotation marks.
    Example: "Madam I'm Adam", "345", "true", "pi", "3.14159"

How do I use them?

In Python variables are easy! Use a very descriptive name like numberOfBooks (NOT just b or NOB) and then assign your value.

numberOfStates = 50
studentName = "Sally"
address = "123 Beech Road"
studentAge = 19