Variables


Basic Data Types

There are four simple data types in python that are important for this course:

These stand for integer, floating-point number, string, and boolean respectively. These data types are known as primitives. These store single pieces of data and are the building blocks of everything we will create in code. Primitive data types can be combined together to construct more complicated compound data types.

integers and floats

The int and float types are used for numerical data.

int values can only hold positive or negative whole numbers: ~~~ {.python} 23 0 -110 ~~~

float values can hold positive or negative whole numbers and decimal values: ~~~ {.python} 20.20 0.123 110.0 ~~~

strings

The str type is used for textual data. All strings are enclosed in quotes. Anything inside of these quotes is part of the string.

Two strings can also be concatenated to be one string!

Examples of string literals: ~~~ {.python} “Hello, World” “COMP110” “42” “I am” + “5” + " years old!" ~~~

Note: “42” is treated as a string since it’s in double quotes. This is NOT the same as the int 42.

booleans

The bool or boolean type is used for true and false. A boolean can be either true or false - that’s all folks!

Examples of boolean literals (Note the capital letters!): ~~~ {.python} True False ~~~

Setting Up Variables

Variables are used to store, change and access data which makes them one of our most powerful tools. Each variable has its own name and holds a specific type of data. Before you use a variable, you must declare it in your program!

Declaration

When you declare a variable, you’re stating that the identifier (the variable’s name) will refer to a value of a specific data type.

Variable names have no spaces and can only use letters, numbers, and underscores.

If a variable name involves a phrase or multiple words, we often use something called snake case in which words are separated by underscores. This is not required but it can make your variable names easier to read.

Example of camel case:

You cannot declare more than one variable with the same name within the same scope, and you cannot access a variable outside of its scope. Further, no variable can be used before it is declared.

Example of declaration:

When you declare a variable without initializing it in the same statement, you must designate the variable’s type.

Initialization

When you assign a value to a variable for the first time, you are giving it an initial value. This is called initializing the variable. Here’s what this looks like:

The type of the value you assign to the variable must match the type you specified when you declared the variable.

Example of initialization:

You may also declare and initialize a variable in the same statement: ~~~ {.python} my_favorite_number: number = 110 # declaration & initialization ~~~

Assignment

After you declare and initialize a variable, you can still change its value. All you have to do is assign a new value to that variable. When you change a variable’s value, you are reassigning a new value to that variable. A variable’s value is the value most recently assigned to that variable.

Example with my_favorite_number: