Intro to Programming with Variables


Lesson Objectives

After this lesson, you will be able to…

  • Create and re-assign numerical and string variables.
  • Use numerical operators.
  • Print complex variable structures.

Discussion: How Many Chocolates?

Think about a box of chocolates.

How do you know how many chocolates are in it?

What if I ask you again tomorrow?


Chocolates vs. Python

Should you count each time? Or should you get a label? “Contains 15 chocolates.”

What if you come across that same scenario in programming?

(It’s a delicious program!)


Variables Label Everything

The same way, in Python, we can use labels to save information.

Instead, we could tell Python:

Labels in Python are called variables.


Variable

Variables…

  • Are boxes that can hold all kinds of information for you.
  • Make it easier to store and re-use values.
  • Are the most basic piece of code.

This is very helpful for things that change.

How many cupcakes have you eaten today?

In a program, where do you think variables could be helpful?


Variable Syntax

To use a variable, we simply announce that we want to use it. How?

Simply write it (a.k.a declare it).

Now, you can print it.

Side note: See that underscore? We’ll look at variable naming in a second!


Try It: Declaring a Variable

Try it yourself - run the code below!


Python: Naming Conventions

You can name a variable anything you’d like. …But is it a good idea?


Discussion: Common Naming Mistakes

You don’t need to be meaningful, but you should try.

Do you think there are any variable names that could actually break your code?


Naming Conventions: Mistakes and Syntax

Some common naming mistakes:

  • Case sensitivity (CUPCAKES_IVE_EATEN and cupcakes_ive_eaten are not the same!)
  • No spaces or punctuation (“cupcakes i’ve eaten” isn’t allowed)
    • This is invalid syntax
    • Use snake_case: lowercase_letters_with_underscores (it’s in the official Python style guide)

Let’s Break Things!

Try using different cases (“CUPCAKES”) and spaces (“cupcakes ive eaten”) to declare variables, and see what happens:


Discussion: Changing Values

What if, later, you eat more cupcakes? Now, this is wrong.

What do you think we need to do?


Discussion: Reassigning Variables

In the example below, what do you think the output of the code is?


Try it: Reassigning Variables

  • Declare a variable and assign it to the number 6
  • Print the value
  • Reassign the variable
  • Print it again!

Mathematical Operators

Math works on numerical variables, too!

  • The +, -, * (multiply), and / (divide) operators work just like they do with regular math.

Math Between Variables

We can also perform math between variables!

Try declaring more variables and doing math with them:


Math On The Same Variable

You can reassign a variable using that very same variable.


You Do: Mathematical Operators

Try to code the below:


What Math Can We Do?

So, what are some operators are available to us in Python?

Operator Example Description
+ 5 + 14 Addition
- 7 - 4 Subtraction
* 3 * 2 Multiplication
/ 10 / 5 Division
** 10**3 Exponents
% 10 % 4 Modulus: Like division, but gives remainder instead of quotient

Math Crash Course: Exponents

An exponent multiplies a number by itself. In regular math, it’s written 103.

Try making an exponent!


Discussion: Even or Odd

Is 6 even or odd?

Is 7 even or odd?

How do you think a computer knows?


Mathematical Operators: Modulus

Modulus operator shows the remainder of a division problem.

  • 4 % 2: 4 / 2 is 2, with no remainder. 4 % 2 = 0.
  • 5 % 2: 5 / 2 is 2, with a remainder of 1. 5 % 2 = 1.

What do you think the following prints out?


Try Modulus!

Try finding the modulus of 15 and various numbers, like 15 % 2.


Modulus: Evens and Odds

Let’s look at some even and odd examples.


Reassigning Variables with Operators

If I asked you to take a number and add 7 to it, you could do:

How?

  • The right side of the equals sign is evaluated first.
  • 9 + 7 happens before the reassignment.

Reassignment Shorthand

This is okay:

But this is better:


We Do: Reassignment Shorthand

Let’s try this.


You Do: Numerical Reassignment

In the empty environment below, follow the prompts:


Introducing Strings

A character is:

  • Anything on your keyboard , such as a letter or a number.
  • “Apple” is five characters: a, p, p, l, e.
  • Spaces count! (they’re on the keyboard!)

A string is:

  • A complete list of characters.
  • “Apple”
  • “Chocolate Cupcake”
  • This entire sentence: “Hello, you are 1 of a kind!”

Strings in Python

You tell Python that your variable will hold a string using quotation marks.


We Do: Declaring Strings

Let’s practice.

  1. Declare a variable called name and assign it the value Marty
  2. Declare a variable called car and assign it the value Delorean
  3. Declare a variable called speed and assign it the value 88mph
  4. Print out these variables

Strings in Python

What do you think happens below?

Guess before running it!

Make sure that, when you want a numerical value, you do not use quotes!


String Concatenation

+ on:

  • Numerical variables adds (5 + 5 = 10)
  • String variables concatenate ("Doc" + "Brown" = "DocBrown")
    • Pssst: Pronunciation tip: con-CAT-en-ATE

We Do: Spaces in Concatenation

Try adding this below:

We expect the sentence to be Marty is driving his Delorean 88mph. Is that what we got?


Adding Spaces

Let’s fix this, by adding in the spaces ourselves:


Printing Strings

When printing, commas also create spaces.

Change your code below to:

This only works for printing, not for concatenation.


Let’s Practice: Print Statements


Some Common Mistakes: 1

Do you think this will run? If yes, what does it print?


Some Common Mistakes: 2

How about this? Does it run? If so, what does it print?


Some Common Mistakes: 3

How about this? Does it run? If so, what does it print?


Some Common Mistakes: 4

One last question. What does this do?


Summary

We learned a lot today!

  • We created, used, and re-assigned number and string variables.
  • We used the numerical operators + - / * // %
  • We did some complex stuff with the print function!

Congrats! You’ve finished your first programming lesson!


Additional Resources