top of page
WFU Robotics

4: Variables, Data Types, and Operations

Updated: Mar 8, 2021

Variables are the most important concept to understand for programming. They are both the containers used to hold your information and the labels used to reference it. Correct use of variables in Python allows for readable, concise code, and differentiates programs akin to works of art, from jumbled messes of numbers and and characters. Here is an example of the print statement from the last tutorial, but with the use of a variable.


my_message = "Hello World"
print(my_message)

>>Hello World

As we can see, the result is the same, but this time instead of the message passed into the print statement being outputted directly as entered, Python first evaluates the contents of the variable, my_message, and then prints the data inside.


Here we create a variable, my_message, and assign it a value using the ‘=’ operator. The creation of the variable is called variable declaration, and assigning it a value is called variable initialization. In Python, we declare variables at the same time as we initialize them. A variable’s name cannot contain certain characters, such as the space, and cannot start with a number.


If we want to update the contents of a variable, we can simply reassign its contents by using the “=” operator again:


my_message = "Hello World"
print(my_message)
my_message = "Goodbye"
print(my_message)

>>Hello World
  Goodbye

One great aspect of Python is that the data type of a variable is fluid. That means that a variable’s data type, such as string, integer, double, or array, is not determined at the declaration of that variable, but rather dynamically, when the code is compiled. So, we can reassign a variable at any point, to any type of data we want, without needing to create a new variable like in other languages:


my_message = "Hello World"
print(my_message)
my_message = 4
print(my_message)

>>Hello World
  4

It is important to know the data type of your variables, since many built-in Python functions, as well as functions in packages that we will use in robotics and machine learning, require their arguments to be certain data types to function. The basic data types are strings, chars, ints, doubles, booleans, and Python’s array-like data types: lists, tuples, and dictionaries. We will go over array-like data types in more depth later.


Ints are integers, or whole numbers. They are often used for counting and for indexing a certain element in a list or tuple. Floats, also called doubles, are their counterparts, which can contain decimal numbers for greater precision. Booleans, or bools, are a simple data type that can either be True, or False. Strings we are familiar with, and are enclosed within quotation marks. Chars, or characters, are just strings of length one.


For more information on strings, check out this link from a great Python blog:


If we don't know what data type a variable is, we can check with the type() function:


var = 4.2
print(type(var))
var = True
print(type(var))

>><class 'float'>
  <class 'bool'>

Another powerful tool in programming is the mathematical operation. Computers can perform calculations much faster than any human, and taking advantage of this fact is what allows us to create amazing programs and robots. The basic operations are addition (+), subtraction (-), division (/), multiplication (*), modulo (%), the exponent (**), and assignment (=). Aside from modulo and perhaps assignment, these are all self-explainable operations. Here they are in action:


print(4.0 / 1.2)
print(3.1 * 9)
print(0 - 3.5)
print(-800 + 4210)
print(5 ** 3)

>>3.333333333335
  27.90000000002
  -3.5
  3410
  125

Note that with division and multiplication, the values are not exactly true. This is because with floats, there is some very slight imprecision with these operations, but it is not noticeable in many cases.


Modulo (%) is a very useful operation that will perform division between two numbers, and return the remainder after division. Say we want our robot to perform a certain action after every 100 seconds, we would check that time % 100 is equal to 0, and then take that action. Here’s an example of modulo:


print(14 % 5)

>>4

Finally, we have assignment (=), which is only used to assign values to variables. Assignment can also be used to assign one variable’s value to the value of another variable. In fact, all these operations can be used with variables on the right hand side of the expression. The variable to which a value should be assigned goes on the left hand side of these equations. On the right hand side, any expression which is ultimately equivalent to some data type in Python, such as an int or an array, can be placed. The variable on the left will be assigned to the value on the right. Without assignment, a variable would simply be and empty container. Here are some examples to make this point more clearly:


var_1 = 3.4
var_2 = 15
var_3 = var_1 + var_2
print(var_3)
var_1 = var_3
print(var_1)
var_4 = var_1 % var_2
print(var_4)

>>18.4
  18.4
  3.399999999999986

Thanks for reading! The next tutorial will be on if-statements, a tool which adds greater complication, and therefore potential, to the programs we can create.



Challenge Activities:


Find how many days are left until the birthday of someone who is 12,420 days old (assuming no leap years).


Given any whole number, what is an easy way, using the arithmetic operations above, to tell if that number is even or odd?

6 views0 comments

Recent Posts

See All

Hozzászólások


bottom of page