Python for beginners: Variables in Python

2018-07-02
Carton image to show variables as an introduction to Python

Python is one of the most widely used programming languages today thanks in large part to its easy syntax and various applications. In this article, we will discuss the types of variables in Python and where each can be used.


Unlike other programming languages, Python has no command for declaring a variable. Instead, all you have to do is assign a value to the variable and move on. The variable is automatically declared and allocated a space in the computer’s memory. Variables in Python do not need a special type to be declared either. In fact, the variable’s type can be changed after declaration.


Variables syntax:

A variable in Python can have a short name that consists of as one or more characters. For example, you could call your variable x or y, or you could use a more descriptive name like area or perimeter.


There are some rules for defining variables in Python. A variable’s name must always begin with either a letter or an underscore. After that, it can contain any number of characters and/or underscores. Variables are case sensitive, so typing age creates a different variable than typing Age.


If you want to write the value of a variable to the screen, you can use the command “print”.


Variables’ types:

There are three numeric types of variables in Python:

  • int for integer numbers: An integer is a whole number, positive or negative, without decimals. It can have unlimited length provided the computer’s memory can handle the value.

  • float for decimal numbers: A float is a number, positive or negative, containing one or more decimals.

  • complex for imaginary numbers: A complex number is written using a "j" as the imaginary part:

x = 1

y = 2.8

z = 1j


Getting variable type:

You can find the type of any variable at any time by using the type() function. This is important for Python since, unlike in other languages, you cannot see the type of variable that has been declared. For example, in other languages you may see a line that says: int x =10. In Python, however, the names of variable types names are used as a type casting tool. Type casting is when you want to specify that a variable must have a certain type.


Casting in Python is done using the following functions:

  • Int() creates an integer number from the original variable. If you cast a float variable into an integer, the computer will round down to the closest whole number.

  • Float() creates a float number from the original variable. It adds a decimal point and zeros after the number to be further used in decimal operations.

  • String() creates a string representation of a variable.


See how easy it is? Try it out with the short exercise below.

What will be the type of the variable resulting from the following operation: (x = 1 + 2.8). If you are not sure, register with RoboGarden now and try to find the answers there there.


Python Series Introduction to Python

相关的博客

Start Python coding

Let's start python coding with RoboGarden learning journey.

Got an invite