Python Variable Types - Python Variable Scope | W3schools

Variables store values at reserved memory locations. When a variable is created, a space in memory is allocated to it to store the value. The value can be either an input from a user or fixed depending on the code. Depending on the variable’s data type the interpreter allocates memory to it and stores the value. Different data types like integers, characters, decimals etc. can be stored in a variable. Since Python is an object-oriented language, the variables need not be declared before assigning them to a value. Each and every variable is considered as an object in Python.

Also Read: Android progress Bar

Assigning Values to Variables

When a value is assigned to a variable in Python then declaration automatically happens. Explicit declaration of variables is not required in Python. To assign the values use the equal sign ‘=’. The operand on the left of the ‘=’ operator is considered the name of the value and the operand on the right is considered its value and stored in that variable. For example:

counter = 100          # An integer assignment

miles   = 1000.0       # A floating point

name    = “John”       # A string

print counter

print miles

print name


In the above example 100, 1000.0, and ‘John’ are the values assigned to the variables named counter, miles, and name respectively. The output of the above code is:




Multiple Assignment

A single value can be assigned to multiple variables simultaneously as shown in the example below:

a = b = c = 1

In the above assignment, three variables are created with names a, b, c and are assigned a common value of 1. The variables are also stored in the same memory location. Multiple values can also be assigned to multiple variables. For example:

a,b,c = 1,2,”john”

In this example values 1, 2 are assigned to variables to a and b respectively. A string object is assigned to the variable c containing the value “john”. 

Standard Data Types

Data can be stored in multiple data types in Python. For example, a number can be stored as a numeric value or an address can be stored as an alphanumeric character. There are many standard data types in Python that can be used to define variables and the operations that can be performed on them. 

Below is the list of the standard data types in Python:

1.      Numbers

2.      String

3.      List

4.      Tuple

5.      Dictionary


Python Numbers

The data types that store numeric values are called numbers. When a numeric or integer value is assigned to a variable then a numeric object is created. For example:

var1 = 1

var2 = 10

the reference for a number object can be deleted by using the del statement. The syntax for using the del statement is:

del var1[,var2[,var3[….,varN]]]]

Single or multiple objects can also be deleted by using the del statement. For example:

del var

del var_a, var_b

The different numerical types supported by python are:

1.      int (signed integers)

2.      long (long integers)

3.      float (floating point real values)

4.      complex (complex numbers) 


Some example of numbers are:


































In Python, a lowercase l can be used with long, but it is advised to use the uppercase alphabet ‘L’ to avoid any confusion with the numeric 1. Python will display the long integers with an uppercase L. The complex number has an ordered pair of floating numbers denoted by x + yj, where y and x are real numbers and j depicts the imaginary unit.


Python Strings

Strings can be defined in Python with a continuous set of characters defined within quotation marks. These quotation quotes can be either pair of double or single quotes. The subset of strings can be extracted using slice operator ([] and [:]) with indexes beginning with 0 from the front and -1 at the end. To concatenate the string a plus (+) sign can be used and the asterisk (*) is used as a repetition operator. For example:

str = ‘Hello World!’

print str          # Prints a complete string

print str[0]       # Prints first character of the string

print str[2:5]     # Prints characters starting from 3rd to 5th

print str[2:]      # Prints string starting from 3rd character

print str * 2      # Prints string two times

print str + “TEST” # Prints concatenated string

The above commands give the below output:

Hello World!



llo World!

Hello World!Hello World!

Hello World!TEST


Python Lists

The list is a very versatile data type in Python. It contains values separated by commas enclosed within square brackets ([]). The lists are similar to arrays to a certain extent. The one major difference between an array and a list is that the items in an array should be of the same data type, whereas the list supports storing of different data types. Similar to strings the values stored in any list can be extracted using slice operator ([] and [:]) with indexes beginning with 0 from the front and -1 at the end. To concatenate the lists a plus (+) sign can be used and the asterisk (*) is used as a repetition operator. For example:

list = [ ‘abcd’, 786 , 2.23, ‘john’, 70.2 ]

tinylist = [123, ‘john’]

print list          # Prints complete list

print list[0]       # Prints first element of the list

print list[1:3]     # Prints elements starting from 2nd till 3rd

print list[2:]      # Prints elements starting from the 3rd element

print tinylist * 2  # Prints list two times

print list + tinylist # Prints concatenated lists

The above set of code gives the below output:

[‘abcd’, 786, 2.23, ‘john’, 70.200000000000003]


[786, 2.23]

[2.23, ‘john’, 70.200000000000003]

[123, ‘john’, 123, ‘john’]

[‘abcd’, 786, 2.23, ‘john’, 70.200000000000003, 123, ‘john’]

Python Tuples

A tuple is another sequential data type like lists that are of great use in Python. It consists of a number of values that are separated by commas. These values should be enclosed in parenthesis ( ( ) ) for Python to recognize them as tuples.

The major difference between a list and a tuple is that a List is enclosed within brackets ( [] ) and their size can be changed but on the other hand in a tuple, the values are enclosed in parenthesis ( ( ) ) and its values cannot be updated. For example:

tuple = ( ‘abcd’, 786 , 2.23, ‘john’, 70.2  )

tinytuple = (123, ‘john’)

print tuple           # Prints complete list

print tuple[0]        # Prints first element of the list

print tuple[1:3]      # Prints elements starting from 2nd till 3rd

print tuple[2:]       # Prints elements starting from the 3rd element

print tinytuple * 2   # Prints list two times

print tuple + tinytuple # Prints concatenated lists

Running the above commands give the below output:

(‘abcd’, 786, 2.23, ‘john’, 70.200000000000003)


(786, 2.23)

(2.23, ‘john’, 70.200000000000003)

(123, ‘john’, 123, ‘john’)

(‘abcd’, 786, 2.23, ‘john’, 70.200000000000003, 123, ‘john’)

Since tuples cannot be updated, the code shown below will throw an error when executed:

tuple = ( ‘abcd’, 786 , 2.23, ‘john’, 70.2  )

list = [ ‘abcd’, 786 , 2.23, ‘john’, 70.2  ]

tuple[2] = 1000    # Invalid syntax with tuple

list[2] = 1000     # Valid syntax with list


Python Dictionary

The dictionaries in Python are similar to hash tables. They work as Pearl’s associative arrays or hashes and have key-value pairs stored in them. The dictionary can store any Python data type and the values can be any arbitrary object in Python. Dictionaries are enclosed in curly braces ({ }) and the values are accessed or assigned by using square braces ( [] ). Dictionaries have unordered elements defined in them. For example:

dict = {}

dict[‘one’] = “This is one”

dict[2]     = “This is two”

tinydict = {‘name’: ‘john’,’code’:6734, ‘dept’: ‘sales’}

print dict[‘one’]       # Prints value for ‘one’ key

print dict[2]           # Prints value for 2 key

print tinydict          # Prints complete dictionary

print tinydict.keys()   # Prints all the keys

print tinydict.values() # Prints all the values

The above set of code gives the below output:

This is one

This is two

{‘dept’: ‘sales’, ‘code’: 6734, ‘name’: ‘john’}

[‘dept’, ‘code’, ‘name’]

[‘sales’, 6734, ‘john’]

Data Type Conversion

While performing operations in the code, there might be a need to change data types sometimes. Using the name of the required function will help you to change the data type. There are a lot of built-in functions in Python that can be used to convert one data type to another. Below are the details:

1.      int(x [,base]): converts x into an integer. Base indicates if x is a string.

2.      long(x [,base]): converts x into a long integer. Base indicates if x is a string.

3.      float(x): converts x into a floating-point number.

4.      complex(real [,imag]): defines a complex number.

5.      str(x): converts x into a string.

6.      repr(x): converts x into an expression string.

7.      eval(str): string is evaluated and an object is returned.

8.      tuple(x): converts x into a tuple.

9.      list(x): converts x into a list.

10.  set(x): converts x into a set.

11.  dict(d): creates a dictionary, d should be a sequence of key-value tuples.

12.  frozenset(x): converts x to a frozen set.

13.  chr(x): converts x into a character.

14.  unichr(x): converts x into a Unicode character.

15.  ord(x): converts a single character to its integer value.

16.  hex(x): converts x into a hexadecimal string.

17.  oct(x): converts x into an octal string.


You now have a good understanding of data types in Python. The above details about the use of variables and their types should get you started with basic coding in Python. Happy Coding!