Link Search Menu Expand Document

Variables, Data Types, Syntax in Python

Variables in Python

The variables are the storage banks of any datatype. They make sure to reserve the requested memory for data to use it later in the program. Many programming languages use various sets of rules when it comes to declaring variables. On the other hand, there is no such convention used in python.

In python, variables do not need any specified data type declared with them to store the items. Instead, they can store numerous data types just by assigning them with one. However, there are few restrictions when it comes to giving variables a name.

Python variables are case sensitive and can never start with a number. Moreover, variables can only start with a letter or underscore. Also, these can contain alphanumeric characters.

Examples of Variables

#declaring variables in python
print(a)          //output: 42
print(b)        //output: Hello
print(c)        //output: Hello

The variable examples in the table above demonstrate how to store int and str type of data in a variable. The critical point here is that no variable has data type mentioned before it, such as a and b. But it still does not produce any syntax errors because, being a user-friendly language,  python recognizes the type of data by default. Furthermore, the programmers can use either single or double quotes to store the string type variable.

Multiple Assignments

As the python language does not require any explicit declaration of the variables, the users can use the equal “=” operator to assign any value to a variable and declare them if they do not already exist. Therefore, if the user modifies some variable’s value later after its initialization, the variable changes everywhere in the code. For instance, the value of “a” in the coding example below. When the user changes its value to “2”, he is changing it everywhere in the program. Below is the sample code to demonstrate it:

a =0
a  = 2
print(a)   # output: 2

Variable References

In python, variables are the references that point to some object. Therefore, it always passes the values by reference. So, if the user assigns one variable to another and changes the value of the new variable, it changes the value of the original variable as well. When a user assigns one variable to another, python does not create a new object for the new variable, but the new variable also points to the same object as the old variable. Therefore, changes in the object via either of the variables reflect in both the variables. Following is the sample code to demonstrate it:

a = [9,3,3]
b = a          #assigning the value of a to b
b[0] =1       #changing the value of zeroth index of “b”
print(a)       #output will be [1,3,3]

Chained Assignments

Like other higher-level languages, python does not require its users to assign the variables separately. For instance, In some scenarios, the users may want to assign the same values to all or several variables, and assigning the values one by one can be a hassle. So, they can do it by using the equal (=) sign with all the variables in a single line and writing the value at the end to assign it to all the variables. Following is the sample code to demonstrate it:

x= y= z= 10

Data Types in Python

Like other programming languages, python can also store various types of data. There are many built-in data types used in python. These are mainly integers, strings, characters, Boolean, and floating-point data types. The users can recognize these data types by some conventional keywords such as int and float for numbers, str for String, bool for Boolean, set of Set. Similarly, list, tuple, and range for order or sequence of data. Additionally, python also supports binary data types, which could be recognized by keywords bytes or byte array.

All in all, everything in python has a specified data type assigned to it. These data types are mainly related to the classes they belong to, as explained earlier. The piece of code given below depicts various data types assigned to variables.

Examples of Data Types

#Various data types in Python example
a = 672 #here data type is int
b = ["six", "seven", "two"] #here data type is list
c = 67.2 #here data type is float
d = “Hello” #here data type is str
e = False #here data type is bool
f = bytearray(10) #here data type is bytearray

Moreover, there is a function in python to differentiate these numerous types. Using type() function can explain what data type the input variable holds. The table below exhibits the use of this function:

#Code to find the type of data stored in a variable

Furthermore, it not compulsory in python to specify a variable data type. But these can be assigned by specifying their type before assigning the data like typecasting. Below is an example of such code:

#simple code to assign a data type to a variable
v = int(672)
y = range(10)
w = tuple((“name”, “age”, “gender”))


The programming language’s syntax is a set of rules and principles to define how a programmer can write the program in that particular language. These rules define perfectly structured sentences, expressions, symbols, and their combinations. The programming languages are languages like any other language and have their own grammar or syntaxes. Therefore, the quality and correctness of syntax are essential in writing the code. It makes the code clean and readable by other programmers and compiler.


A compiler is a unique program that works as a translator between computers and programmers. It reads the code and translates it to the machine language. In this way, the computer understands the user’s instructions and performs the assigned tasks accordingly. That is why it is crucial to use the correct grammar to be translated and understood entirely.

Python Language Syntax

Python language is highly readable and user friendly and doesn’t require the user to know many rules. However, there are some critical syntax rules, such as:

  • Newline terminates the current line.
  • It does not consider blank lines.
  • It is case sensitive. False and false are not the same words in this language.
  • The program completely ignores the comments. There are just for the ease of the developer. The comments begin with the '#' symbol until the end of the line. So, the next python code instruction should be in the next line.
  • Put the semicolon(;) at the end of the previous instructions if the user wants to write multiple instructions in a single line.
  • It has some reserved words, such as False, True, return, class, continue, and while, which the user cannot use as variables.
  • Indentation matters. Therefore the code blocks should be perfectly aligned using spaces or tabs.
  • Use a backslash (\) as a logical line breaker to write a single line of code in multiple lines or join numerous code lines. For instance, sometimes the code lines are too long that it is convenient to write them in separate lines. However, in python language, the newline serves as a terminator. Therefore, the users can use the backslash character to connect the code lines.
  • The variable names can contain lower and upper case alphabets, zero to nine (0-9) numerical digits, and a unique character, i.e., underscore (_). However, the variable names can never start with the numerical digit. Furthermore, as the language is case sensitive, “age” and “Age” are not the same variable names.

Other useful articles:

Back to top

© , Learn Python 101 — All Rights Reserved - Terms of Use - Privacy Policy