You already learned what a function is in the previous section, it's a command that you call by name, which sometimes needs some additional information to be provided between its parenthesis `()`.

Let's step back even further and talk about some of the Python language basic data types and a few fundamentals of how the Python language works.

This page will only serve as an extremely limited introduction to the Python language, and really just enough to be able to finish getting an introduction to DrawBot. I would encourage you to check out the resources for getting started with Python from Python's own documentation.

String

In your print() statement, the phrase "Hello World" was enclosed in quotes. This text within quotes is known in Python as a String.

Strings can be surrounded by double quotes or single quotes

Download: file
print("Double quotes")
print('or single quotes are okay')

This is useful if you ever need to have quote marks within your string, this code will give you an error:

Download: file
print("I think "DrawBot" is great")

You can even see by the syntax coloring in the code editor that something is wrong here — it really thinks there's a string "I think " and another string " is great" but the word DrawBot is left out of the two strings. So, just switch the outside quotes to be single quotes and you'll be fine —

Download: file
print('I think "DrawBot" is great')

Numbers

When setting the size of the canvas, you gave the size() function two numbers separated by a comma, with no quote marks. Numbers need to work differently than strings, even though sometimes they might look the same. Try this example:

Download: file
print("300")
print(300)

The first line printed a string of the characters 300, the second line printed the number. The quote marks from the string were removed for printing which made it look like a number, but it might be more clear what's going on under the hood if we try some basic math:

Download: file
print("300 + 200")
print(300 + 200)

Maybe this helps make things more clear, the first line printed 300 + 200 because the text of the string represents those 9 characters, but the second line actually added the two numbers together before printing. Very handy! And a little bit more on this later.

Variable names

Aside from the function names, the other data that we're providing (the strings and the numbers) might need to be reused in several places within your script, or their values might need to changes as the script runs. To make this easy, you can assign a variable name of your choosing to these pieces of data. Try this:

Download: file
myName = "Andy"
age = 38

Go ahead and run this script — nothing should be output. All we've done is assigned myName to be equal to "Andy" and age to be equal to 38. Now, any time in the script that we use these names their equivalent values will be used instead:

Download: file
myName = "Andy"
age = 38
print(myName)
print(age)

Let's make things more complicated, add more text to a print statement by separating the items with commas:

Download: file
myName = "Andy"
age = 38
print("My name is", myName)
print("and I am", age)

Remember though, we can do some basic math with that number. Add one more line:

Download: file
myName = "Andy"
age = 38
print("My name is", myName)
print("and I am", age)
print("Next year I will be", age + 1)

Nice!

More Basic Math

So, numbers can be added, but all of the other basic math you would expect can be applied to them as well:

Download: file
print(2 + 5)
print(100 - 1)
print(3 * 2)
print(10 / 2)

It's worth noting that in all of my examples so far, I've added spaces around the mathematical operators, but in Python this white space is optional, or you can use as much as you like. So really, these two lines are the same:

Download: file
print(2 + 5)
print(2+5)
print(2   +   5)

You can do some basic math with strings too, which might sound crazy at first but kind of makes sense:

Download: file
print("Draw" + "Bot")
print("Hello!" * 5)

It's no problem to add two strings together to combine them into one, or even multiply a string to repeat it. However, it's not possible to divide or subtract strings (because that's just crazy).

Lists

Two more basic data types for now, along with some useful things you can do with them, and then we'll get back to the drawing.

In Python, a List is an ordered sequence of items that you would call out by their index order, and a Dictionary is an unordered group of items that you would call out by their keyword name.

This example list is full of names:

Download: file
classNames = ["Limor", "Frank", "Erica"]
prit(classNames)

Printing the list gives all of the items back to you. It might be useful to ask for the items in order:

Download: file
classNames = ["Limor", "Frank", "Erica"]
print(classNames[1])

The important thing here is within the print statement, classNames[1]. In this case, we asked for only item 1 in the classNames list. You might be surprised to see Frank print out instead of Limor, that's because counting in Python (and most programming languages) starts from zero.

Are you ready for a big jump? If we have a list, we can use a for statement to fetch one item at a time out of the list and do something with it.

Download: file
classNames = ["Limor", "Frank", "Erica"]  
for name in classNames:
    print(name)
    print(name * 5)

Like the command says, for each name in classNames, take the name and do something. And that something is really every line that's tabbed in after the for loop starts.

First, it prints the name one time, then it prints the name 5 times before moving on to the next name in the list. In this example, name is another variable name that we made up, we could have called it person and this code would run identically as long as we update all of the tabbed in lines with the new variable name.

Download: file
classNames = ["Limor", "Frank", "Erica"]  
for person in classNames:
    print(person)
    print(person * 5)

And more

There's a lot more to Python than just what I've covered here, but if you're comfortable with this as a starting point we'll take some new concepts as they come in the next few sections.

This guide was first published on Dec 14, 2018. It was last updated on Dec 14, 2018.

This page (Python, the very basics) was last updated on Oct 31, 2020.