Variables

You can take the value of something - whether it’s a number or whatever, and store it in a variable for use later. You use an = to do that.

name = "Dennis"

The following will print 4.

cats = 4
print(cats)

If you put quotes around it, though, Python thinks you’re talking about a word. The following will print cats.

cats = 4
print("cats")

Variable Naming Tips

  • Be sure to use descriptive names like age not x
  • Also, use underscores for multiple words like number_of_cats not numberofcats or numberOfCats

Printing

The most useful thing you’ll ever do in Python is print! print out information to the user, print out variables that you’d like to look up.

In Python 2, you could do print "blah blah", but in Python 3 you need to do print("blah blah"). You’ll find a lot of Python 2 code on the internet, so be careful.

Print a string

print("Hello world!")

Print something from a variable

name = "Justine"
print(name)

Print the result of some math*

print(4 * 2)

You CAN add together strings when printing, although it will show hellothere without a space

print("hello" + "there")

You CAN’T do this, you’ll get an error about combining strings and integers

print("hello" + 5)

To combine multiple things when printing, you SHOULD do this (it will also add a space)

print("hello", name)
print("hello", 5)

You can use commas to print a million things, more or less

print("hello", name, 5, 5, name, "blah")

Arithemetic

Addition

4 + 2

Subtraction

4 - 2

Multiplication

4 * 2

Division

4 / 2