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-Type | Categories |
---|---|
1. Text/string | str |
2. Number | int, float, complex |
3. Sequence | list, tuple |
4. Mapping | dict |
5. Set | set, frozenset |
6. Boolean | bool |
7. Binary | bytes, 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 Tkinter Projects for Beginners
Introduction Python has become one of the fastest-growing programming languages, and its applications are also increasing every day. Tkinter Projects for Beginners are one of the best ways to learn the basics of Python. Tkinter is said to be one of the most straightforward modules to start learning Python as well. This will help absolute…
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…
100+ Java Projects for Beginners 2023
Are you looking for interesting Java Projects ideas and 100+ Java Projects for beginners with source code? If yes, the you have come to right place because in this article we will show you best 100+ Java project ideas and topics for beginners with source code. We recommend you bookmark this page before you lost it as…
15 Common Coding Mistakes by Beginners
The most common mistakes that beginners while learning to code are 1. Switching between multiple languages 2. No roadmap 3. Lone wolf programming
15 Deep Learning Projects for Final year
Introduction In this tutorial, we are going to learn about Deep Learning Projects for Final year students. It contains all the beginner, intermediate and advanced level project ideas as well as an understanding of what is deep learning and the applications of deep learning. What is Deep Learning? Deep learning is basically the subset of…
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…