Python Datatypes

Introduction

Variables are used to store data and the data they store is related to some specific type like text, number, or sequence, in any programming language. Python Datatypes are similar to most of the other programming languages.

Python also has predefined datatypes.

During the declaration of any variable, we do not need to specify it’s a data type in Python, as Python is a dynamically-typed language and Python automatically specifies the datatype.

Data-Types

Data-TypeCategories
1. Text/stringstr
2. Numberint, float, complex
3. Sequencelist, tuple
4. Mappingdict
5. Setset, frozenset
6. Booleanbool
7. Binarybytes, bytearray, memoryview

type() in Python

Python uses “type()” to determine the type of any datatype, see the example below–

Example

x = 2
y = "copyassignment.com"
z = ["list", 1]
print(type(x))
print(type(y))
print(type(z))

Output

<class 'int'>
<class 'str'>
<class 'list'>

Type Casting

Typecasting or type conversion is the conversion of a variable from one data type to another e.g. int to float or float to int.

Example

var1 = 20.3
var2 = int(var1)    # to int type
var3 = complex(var1)    # to complex type
var4 = str(var1)    # to str type
print(var1, "Type is", type(var1))
print(var2, "Type is", type(var2))
print(var3, "Type is", type(var3))
print(var4, "Type is", type(var4))

Output

20.3 Type is <class 'float'>
20 Type is <class 'int'>
(20.3+0j) Type is <class 'complex'>
20.3 Type is <class 'str'>

From the next lecture, we will start learning all data types in detail.


Also Read:

10 Useful Python One-Liners

In this tutorial, we will learn about the best and most powerful Python One-Liners ranging from beginner’s level to advanced level that too with examples for better understanding. Using Python One-Liners in your programs will actually make your code more readable and precise. What exactly are Python One-Liners? One-liners in simple language are single-line codes…

Continue Reading 10 Useful Python One-Liners

20 Python Projects for Resume

About 20 Python Projects for Resume Today, in this article on 20 Python Projects for Resume, we’ll expose you to some of the top beginners to advanced python projects for resumes. In 2022, your best investment will be learning Python through practical projects. Therefore, working on actual Python projects is essential if you wish to…

Continue Reading 20 Python Projects for Resume