Thursday, February 14, 2019

Python Basics : Introduction and Data Types


Python is a popular, general-purpose high level programming language created by Guido van Rossum and developed by the Python Software Foundation. Python works on different platforms and has a simple syntax similar to english language. Being an interpreted language helps developers in prototyping quickly. It is a flexible language and it can be treated in a procedural, object-oriented or a functional way. Python is easily among the top programming languages according to github language trends.

Data Types in Python

A datatype represents the type of data stored into a variable or memory. The datatypes which are already available in Python language are called Built-in datatypes. The datatypes which can be created by the programmers are called User-defined datatypes.

Built-in datatypes

There are five types of built-in datatypes :

  • None Type
    • In Python, the 'None' datatype represents an object that does not contain any value. 
    • It is used inside a function as a default value of the arguments. Meaning, when calling a function, if no value is passed, then the default value is assumed to be 'None'. 
    • In Boolean expressions, 'None' datatype represents 'False'.
  • Numeric Types
    • The Numeric data type represents numbers and is further classified into the below sub types:
      • int : This datatype represents an integer number. It does not contain any decimal point. In Python, there is no limit for the size of an int datatype.
      • float : The float datatype represents floating point numbers and contains a decimal point.
      • complex : It contains a complex number of the form 'a+bi', where a and b may contain integers or floats.
      • bool : The bool datatype represents boolean values. There are only two boolean values True or False and are internally represented as 1 and 0 respectively.
  • Sequences
    • A sequence represents a group of elements or items.
    • There are six types of sequences in Python :
      • str : In Python, str represents a string datatype. A string is a group of characters.
      • bytes : A byte datatype represents a group of byte numbers just like an array. A byte number is any positive integer from 0 to 255. An important property of byte array is that, we cannot modify or edit any element in the bytes type array. bytes() function is used to convert a list into bytes array.
      • bytearray : The bytearray datatype is similar to bytes datatype except that a bytes type array cannot be modified, whereas a bytearray type array can be modified. bytearray() function is used to convert a list into bytearray datatype.
      • list : A list represents a group of elements just like an array but unlike arrays, lists are capable of storing different types of elements. Lists can also grow dynamically in memory. Lists are represented using square brackets [].
      • tuple : A tuple is like a read-only list. It also contains a group of elements which can be of different type. The elements in a tuple are enclosed in parantheses (). Whereas list elements can be modified, it is not possible to modify the tuple elements.
      • range : The range datatype represents a sequence of numbers and the numbers in the range are not modifiable.  To create a range of numbers we can use the range() function. It is generally used for repeating a loop for a specific number of times.
  • Sets 
    • A set is an unordered collection of elements and the order of the set as entered is not maintained. A set also does not accept duplicate elements. 
    • There are two sub types of sets :
      • set datatype : A set is created by entering the elements separated by commas inside curly braces {}. set() function may also be used to create a set. Since sets are unordered, we cannot retrieve the elements using indexing or slicing operations. The update() method is used to add an element and the remove() method is used to remove a particular element from the set.
      • frozenset datatype : The frozenset datatype is the same as the set datatype except that the elements in a frozenset cannot be modified, hence update() and remove() methods will not work on frozenset datatype. We can create a frozenset by passing a set to frozenset() function.
  • Mappings
    • It represents a group of elements in the form of key value pairs so that when the key is given, we can retrieve the value associated with it. The key value pairs should be separated by a colon (:) and every pair should be separated by a comma (,) and enclosed inside curly brackets {}.
    • We can retrieve the value upon giving the key. For e.g., dict_name[key].
    • We can use keys() and values() method to retrieve only keys and values respectively.
    • We can use the del module to delete a key and the corresponding value. For e.g., del dict_name[key]
Literals in Python

A literal is a constant value that is stored into a variable in a program. In an assignment operation, like
'a' is the variable into which the constant value '10' is stored. The value '10' is the 'literal' here. Since 10 indicates an integer value, it is called an 'integer literal'.

The following types of literals are there in Python :

  • Numeric literals
    • These literals represent numbers. The different types of numeric literals available in Python are Integer, Float, Hexadecimal, Octal, Binary and Complex.
  • Boolean literals
    • Boolean literals are the True and False values stored into a bool type variable.
  • String literals
    • A group of characters is called a string literal and these literals are enclosed in single quotes (') or double quotes (") or triple quotes (''' or """). In Python, there is no difference between a single and double quoted string.
User defined datatype

The datatypes which are created by the programmers are called 'user-defined' datatypes like array, class or modules.


A constant is similar to a variable but its value cannot be modified in the course of program execution. In languages like C and Java, defining constants is possible, however, its not possible in Python. We can indicate that a variable is a constant by writing its name in all capital letters, but its value can be modified.

Identifiers and Reserved words

An identifier is a name that is given to a variable or function or class and it can include letters, numbers and underscore character (_). Identifiers should always start with a non-numeric character. Python is a case sensitive programming language and hence identifiers with the same name but different case sensitivity are treated as separate by Python.

Reserved words are the words that are already reserved for some particular purpose in Python language. Examples include and, del, in, continue etc.


  • To determine the datatype of a variable or object, we can use the type() function. 
  • Every datatype is treated as an object internally by Python. Every datatype, function, method, class, module, lists, sets, etc are all objects in Python.
  • Python does not have a char datatype to represent individual characters, unlike in programming languages like C or Java. To access individual characters in a string, we have to use the index or the position number.