DEV Community

Cover image for Python3 Programming - Exercise 1 - Variables
Michael Otu
Michael Otu

Posted on • Updated on

Python3 Programming - Exercise 1 - Variables

Creating a variable

A variable is a placeholder ( a name, a label, a tag) given to an address ( a memory location) of a value in memory.
You can think of a variable as a box, which has a name. This box will have a value. Whenever we reference ( call) this variable ( name of the address), we then get the value stored at that address.
To create a variable and assign it a value, all we have to think about is an equation.
Eg: x = 2 . x is the variable ( the placeholder) that points to the address of the value, 2. So 2 is the value. A single ( one) equal-to sign, =, is known as the assignment operator. It takes what is on the right ( the value) and assigns ( puts) it into what is on the left ( the variable).
In Python, the data type of a value is controlled dynamically ( meaning as the program runs) so we don't have to worry about it ( memory management).

More examples:

variable assignment

All these are examples of variable assignment ( variable creation) of different data types.

Rules

  • variable names can start with a letter or an underscore
  • followed by a letter, an underscore or a number
  • a variable name can not begin with a number or dot or special symbols (+, -, /, *, and others)
  • a variable name can not have white-space characters (space, newline, tab, and others)

Practicals

  • Create ten variables and assign them any values suitable ( perhaps, as above).

Summary:

  • Variable represents the name given to the memory address.
  • Calling the variable returns the value
  • The value stored in a variable can change anytime - that's why we call it a variable
  • To create a variable, think about an equation, LHS = RHS. LSH is the variable_name, RHS is the value. Eg: full name = "John Doe"

Top comments (0)