Introduction.
Welcome back to today’s Python tutorial on ‘Python Data Types‘. In this post, you’ll learn the basics of various data types supported by Monty Python. The data types are of two types i.e. Built-in data type and User Define data type. You’ll also learn how to define a data type for any literal or variable. Let’s begin today’s Python tutorial.
Basics of Data types In Python.
Python is a high-level programming language. It supports the feature of functional and object-oriented programming languages such as JAVA. .NET. The Data type specifies the property of a variable (i.e. integer or float). A variable is like a storage box, that stores the value for further processing. Each variable linked to a memory location. In programming languages such as C, COBOL, JAVA, .NET you need to specify the data type (i.e. integer, float, etc) of each variable.
The beauty of python is that its a dynamically typed programming language and you’re not required to define the variable datatype explicitly.
In python, value is seen as a tag that is tied to some value. Refer below example:
a = 33 # Python assign a new tag.
If the variable value is changed then the new tag will be assigned and the previous tag/object would be removed by python garbage collector automatically. It means python is using memory efficiently.
a = 4 # Python will replace existing tag with the new tag.
Data type in python represents the data store in memory or variable. Python, support two types of data types:
Built-In Data Types: The data types, which is already available in a python programming language is called Built-In data types.
User-Defined Data Types: The data types, which can be created by the programmer itself are called User-defined data types.
Let’s discuss, these python data types in details with the help of a few examples.
Build-In Data type in Python.
There are primarily 5 different types of build-in datatypes in python.
-
None Type.
In Python, the “None” datatype represents an object that does not represent any value. It’s similar to the null object of another programming language. This data type is used in the function to define the default function argument value.
-
The numeric data type in python is a collection of three types i.e. Int, Float, Complex. Int or integer represents a numeric value with no decimal. Float represents numeric value with decimal. The complex represents a number that can be represented as a mathematical equation.
-
Bool Datatype
The boolean datatype another important datatype in Python. A boolean variable can have two possible value True or False. Internally these values are represented as 0 or 1. A blank string “” is treated as False.
-
A sequence in python has generally represented a group of items or elements, for example, 999 or ABC. There are basically 6 types of sequence in python i.e. str, bytes, array, list, tuple, range.
-
Sets
A python sets are an unordered group of simple elements/objects. The order of data is not maintained in the set and a set does not accept any duplicate. There are two categories of sets: set datatype, frozen set datatype.
-
Mapping
A map represents a group of elements in the form of the key-value pair so that you can fetch value based on the key. Dictionary data type in python is a perfect example of maps.
Example 1: Python Build-in datatype.
# Built-in Data Types in Python Example.
a = 12 # Integer Datatype in Python.
C = 12.22 # Floating Datatype in Python.
d = -1 + 5j # Complex Datatype in Python.
# Boolean Datatype in Python.
if(a<c):
print('Python')
Output: Python
b = 'topictrick' # String Data type in Python.
# Bytes Data type in Python.
d = [10,20,30]
e = bytes(d) # convert to into bytes type array.
print (e[0])
Output: 10
f = [10,20,'P'] # List Datatype in Python.
g = ('Python',3.7) # Tuple Datatype in Python.
r = range(10) # Range Datatype in Python.
s = {10, 20, 30} # Set Datatype in Python.
User-defined Data types in Python.
The user-define data type in python is basically custom data types which are defined by used as per program or project requirement. For example an array, a class etc.
-
Constants In Python.
A variable whose value cannot be change is termed as constant variables. These variable are generally defined as per business logic requirements.
-
Identifiers.
An identifier is a name which is given to a variable or function or class. An identifier can be a letter, number, letters. They should always start with a non-numeric character. For example – tpl11
-
Reserved Words.
Python reserved words are the words that are already reserved for some particular purpose in the Python Language. The names of these words should not be used as identifiers. The following are couple of reserved words: global, from, try, return, lambda, import, finally, class, continue, break, continue, asserts, print, raise, while, do, if etc.
Escape Character | Description |
| New line continuation. |
\ | Display a single . |
* | Display a single quote. |
“ | Display a double quote. |
b | Backspace. |
r | Enter. |
t | Horizontal tab space. |
v | Vertical tab space. |
n | New line. |
Comments in Python.
Including comments in the program is very much required from a code readability perspective and including comments in the program is mandatory and it depends on the organization/project coding practice. In Python, there are two types of comments i.e. single-line comments and multi-line comments.
Single Line Comments: These comments are generally used to write comment in single line and it its start with hash symbol (#) . The complete line is treated a comment.
# Description: Python tutorial on datatypes.
Multi line comments: In case you want use multiple line to include comment then one option is to include # in the beginning of each line. Python has provided to solution to this problem as well, you can use multi line comments. They start with triple double quotes (“””) and should be end with triple double quotes (“””). You can also use triple single quotes (”’) despite of double quotes.
''' Description: Python tutorial on Python Data Types will present a brief overview of the various built-in data type or user define data types. '''
""" Description: This program demonstrates the use of the function to calculate the sum of two numbers. """
Docstring in Python.
Now, its time to disclose some interesting facts around multi-line comments. In reality, Python only supports single-line comments. Multi-line comments are regular strings with an exception that they can extend to multiple lines.
The memory will be allocated to these string normally and if these strings are not assigned to any variable then they would be removed from the memory by the garbage collector. Thus, these multi-line strings can be used as comments.
If you use these strings as the first statement in class or function or method, then these strings would be treated as documentation strings or docstrings. It’s always recommended to avoid using multi-line comments because they occupy internal memory and it also requires interpreters processing. Following is an example of docstring in python function.
# Example of Docstings in Python.
# Function to calculate the total.
def total(a,b):
''' Function to calculate total of two numbers '''
print("Total is :", (a+b))
# Call the function.
total(10,20)
# Output:
Total is : 3
Naming Convention in Python.
All organization uses best practices to design and develop an application. You must follow these guidelines along with naming conventions while writing your code. The following are some of the important points that you should keep in mind while writing your code.
- Packages: Package names should be written in lower case. In case you use multiple words then use ‘_’ underscore.
- Module: Module name should be written in lower case. In case you use multiple words then used ‘_’ underscore.
- Classes: Each word of a class name should start with capital letters. This rule is applicable to user-defined classes.
- Global Variables: Global variable should name should be all lower letters. In case you use multiple words then use ‘_’ underscore.
- Function Name: Function name should be defined in lowercase and in case you use multiple words then use ‘_’ underscore.
- Method: Method name should be defined in lower case. If you use multiple words then use ‘_’ underscore.
- Constants: Constants name should be defined in all capital letters. If you use multiple words then use ‘_’ underscore.
Conclusion.
Finally, this para mark an end of today’s Python tutorial on Python datatype. Let’s me precisely summarized of all important points that you learned today.
A data type in python represent a variable that can store data. These data types are broadly categorized into two category i.e. Build-in and user define data types. You also learn how to define these datatypes. If you like today’s tutorial, then please share with your friends and don’t forget to share your comments.