This is our Part 1 of the Mastering Python for Blockchain series. I will write separate parts for the Mastering Python for Blockchain series in the coming months. This Part covers data objects, data types, data values, variables and related concepts in Python. I have started with a brief introduction to the language, its applications and prominent features.

I will suggest you to download the latest version of Python which will help you to practice coding and compiling programs. In case you do not have Python installed in your system you can use this link to online run your Python codes.

I also suggest you to visit the article here to learn the basic concepts of Blockchain.

Mastering Python for Blockchain-Introduction

Python is a popular high level language, with human friendly syntax and symbols. Hence it is easy to learn, although it has a wide range of usage.

Few areas of Python applications are:

  • Blockchain development
  • Web Development
  • Machine Learning applications
  • Blockchains and
  • Other cutting edge Software development technologies

Basic Features of Python

The list below bullets some of the basic features of Python.

  • Python is one of the most popular and widely used programming language because of its easy and human friendly syntax, comparatively lesser lines of code and better readability by using indentation.
  • Python allows programming in Object-Oriented as well as Procedural paradigms.
  • Python is used by almost all the tech-giants like Google, Amazon, Facebook, Instagram, Dropbox, Uber, etc.
  • Python has a large collection of libraries which comes handy with the language.

Mastering Python–>Data in Python


The computer memory is analogous to a series of shelves. Each shelve has a number of slots for storing data. Each slot is one byte (8 bits) wide and are numbered from 0 to the end.

A Typical Computer Memory

Now Python keeps any data value-Booleans, Integers, Floats, Strings, data structures, functions and programs in the computer memory as Objects. In the computer memory analogy we discussed above, Objects are made by combining different number of bytes to form boxes of different sizes.

An object contains at least the following,

  • A data type which defines what it can do. For example Integer, Boolean, Floating Point, Complex, etc.
  • A unique id which distinguishes it from other Objects. It is a particular location in the computer’s memory assigned to the Object.
  • A data value consistent with its type. If we think of the Object as a box then the value resides inside the box.
  • A reference count which tracks how many times the Object has been used
An Object created in the Computer’s Memory

Mutable and Immutable Objects

A data type can be mutable as well as immutable. An Object which contains a mutable data type like Set, can have the value inside changed. Whereas an Object which contains an immutable data type like integer, cannot have the value inside changed.

We can think of an immutable Object as a sealed box with transparent sides. We can see the data value inside but we cannot change it.

Whereas a mutable Object is not sealed and has transparent sides. You can see the value inside and can also change it.

Note: Python is strongly typed, i.e. the type of an Object cannot change although the value inside it can change.

NameData Type in PythonMutable(Yes/No)Examples
BooleanboolnoTrue, False
Floating Pointfloatno2.34, 2.3e5
Complexcomplexno3j, 5+3j. 6y+4
Text Stringstrno‘alas’, “attack”
Listlistyes[‘alas’, ‘attack’]
Byte Arraybytearrayyesbytearray(…)
Frozen Setfrozensetnofrozenset([‘Elsa’, ‘Otto’])
Dictionarydictyes{‘bingo’: ‘bore’: ‘alpha’: ‘nitin’}
Data Types in Python

Literal Values

Data in Python is either expressed as a literal value or a variable.

For now just understand literal values as 2, 3, 456, 189, etc. for integers (series of digits); 4.6, 4.5ef6, etc. for floats (having a decimal); text strings are surrounded by quotes. Let us learn more about the basic data types in Python, i.e. Booleans, Integers and Floats. These are like atoms of building blocks in Python. They combine to create complex data types like Lists and Dictionaries.


  • The only values for the data type Boolean in Python are True and False.
  • The special function bool() helps to convert any data type to a Boolean.
    We will discuss functions later. For now you just need to know that functions have a name, zero or more comma-separated input arguments surrounded by parenthesis and zero or more return values.
    The Boolean function bool() takes any data value and returns its data equivalent.

Try running the below code.

Basically non-zero numbers are considered True, whereas zero is considered False.

We will see the actual usage of Booleans later.

Note: Remember, print() is Python’s built-in function to print things, normally to your screen.


  • Integers as in mathematics are Whole numbers, positive and negative.
    Ex: 1, -2, 100,-1000, etc.

Some exceptions:
(1) In Python you cannot have a zero followed by a digit between 1 to 9. Ex: 09 will give you an exception in Python.
Try the below:

This will give you the exception

(2) You cannot use commas in Integers.
Ex: 1,00,000,090
But you can use underscore (_) in Python to give spaces between digits.
Actually, you can put underscores anywhere after the first digit; they’re just ignored:

Integer Operations
OperatorDescriptionExampleResult (of Example)
/Floating Point Division7/23.5
//Integer (Truncating) Division7//23
%Modulus (remainder)7%31
Integer Operations

After execution the result comes as

Precedence in Integer Operations

In what order will you carry out the below integer operations.


If the order of your operations is first addition (3+4) and then multiplication (4*5) then the final result comes out to be 35. Whereas when you carry out multiplication first (4*5) and then addition(3+4) then the final result comes out as 23.

The question is which one among the two is correct. So in Python to make it easy, we use parenthesis to group our code in the order in which we intend our operations to be carried out.


So, carry out multiplication first and then addition. Final result comes 23.

Another example,


Result is 5. First the operation 7%2 is carried out. Then the result is multiplied by 5.

Type Conversions
Conversion to Integer
  • int() function is used to convert any data type in Python to its integer equivalent.
  • Ex:

The result comes out as 1 and 0 respectively.

  • The int() function takes one input argument and returns one value, the integer-ized equivalent of the input argument. This will keep the whole number and discard any fractional part.
Conversion to Boolean
  • The bool() function takes any input argument as gives the Boolean equivalent.

The results will be True, False and True, respectively.


  • As we saw Integers are whole numbers (in negative as well). Floats have decimal points.
    Ex: 5.2, 5.3, 6.72, etc.
  • Floats can also include a decimal integer after the letter e.

The result


  • Floats are handled similarly to integers: you can use the operators (+, –, *, /, //, **, and %) and the divmod() function
  • Use float() function to convert any data type to its Float equivalent.

The results are respectively


In Python, variables are the names assigned to data values in the Computer’s memory, which you can use in your program. Some important points.

  • Variables should only contain
    • Lowercase letters from a to z
    • Uppercase letters from A to Z
    • Underscore_
    • Digits from 0 to 9
  • Variables must begin with a letter or underscore and cannot start with a digit
    Hence, 0abc is not a valid variable name
    Whereas a, abc, a_b are all valid variable names
  • Variables are case sensitive, meaning thing is different from THING or Thing.
  • They cannot be Python’s reserved words or keywords

Python’s reserved words are:

Keywords in Python
Assignment of variable

In Python a value is assigned to a variable by using the = sign

Let us see a small Python program

Here the variable x has been assigned a value 6. In the second line, the variable y has been assigned a value that is 4 added to x. Now x already has a value of 6, hence the value of y becomes 10. Hence when you run the program the out comes as 10.

If however the value of x was not defined, the program would have given an exception (error).

Run the above program and you will get an error, as the x was not assigned a value before using it. You will see an error like below.

Variables are just names, not places

This is a crucial point in Python about variables. Variables are just names or references assigned to a data value in the computer’s memory which are stored in other locations of the computer memory.

You can even assign two variables the same data value.

Contrary to other static languages a variable in Python has no defined type. In static languages you need to define the type of the variable first before using it.

Note: Python uses the type() function to find out the data type of any data (variable or literal value).
Ex: type(5)
<class 'int'>

Let us now have a quick look on what exactly is happening when the below code runs.

  • Python created an integer object with value 6.
  • Assigns a variable x to the object with value 6.
  • Incremented the reference count of the object with value 6.
  • Creates another integer object with value 13.
  • Subtracted the integer object with value 13 by the value assigned to the variable x.
  • Created an integer object with the value that comes after subtracting 13 object with variable x.
  • Assigns a variable y to the new integer object.
  • Incremented the reference count of the new object that y refers to.
  • Looked up the value that y refers to and prints it.
Assigning multiple variables to the same Object

As discussed above, the same Object can be assigned multiple variables.

In the example code above the Object containing integer value 5 is assigned to variable x as well as y. This means x and y both are tagged to the same Object with value 5.


Leave a Reply