How to create a numeric variable in Python Pro Preview

To create a variable, it needs to have a name and a value that it stores within itself. The name of the variable can be any word, but when a program has many variables, it gets difficult to keep track of which variable does what. This is why it's good practice in programing to name variables according to what they do.

To access the full video please subscribe to FLLCasts.com

Subscribe

  • #1547
  • 27 Feb 2020

How to write a variable name

The rule of naming variables is to have at least two words so that we know what is in the variable and why. The names of the variables are written with an underline separation to make it easier to read (the underline is similar to an empty space). 

This variable naming standard is known as snake_case, since the words flow like snake vertebrae.

Example:

Let's look at an example of creating three variables with the names "motor_speed", "motor_time" and "waiting_time":

motor_speed = 100
motor_time = 200
waiting_time = 300

Where variables are created

VS-Code creates its programs with the comment "# Create your objects here.", This is in order to build a habit in programmers, to create their variables before their programs. The only thing that is written before the variables are the entered libraries that you will use in your code.

How to use variables

When we assign a numeric value to a variable, we can use its name instead of a number.

Example:

Let's look at an example in which we create and use the variable "motor_speed" to set the speed of the motors:

# Create your objects here. 
ev3 = EV3Brick()

motor_speed = 500

# Write your program here. 
Motor(Port.B).run_time(motor_speed, 3000, Stop.COAST, False)
Motor(Port.C).run_time(motor_speed, 3000, Stop.COAST)

Courses and lessons with this Tutorial

This Tutorial is used in the following courses and lessons

Image for Python with LEGO Mindstorms EV3 - Level 1
  • 74
  • 28:18
  • 114
Image for Lesson 4 - Strange Bot
  • 7
  • 5
  • 11
  • 3d_rotation 1