Data Types In Python

Today, in this article, we will learn about Python data types and their usage. This is a very important topic because every programming language has some specific datatypes. If we work on any project, this is significant to understand what datatypes are, because if we perform any operation using variables and their datatypes in an improper way, then we get errors.

Let's start!

 

In Python, datatypes are divided into 4 types. 

  • Numeric
  • Sequence
  • Dictionary
  • None 

Now, let us talk about each and every datatype mentioned above.

Numeric

It has four types.

  • int
  • float
  • complex
  • bool

type()

 

This function always returns a class type of argument passed as a parameter.

 

Example

  1. lst=[10,25,35,45,50,60]  
  2. print(type(lst))  

Output

 

 

In the output, you can see that the type() method returns variable (list) datatype which is list type.

int

An Integer is a whole number that can have both zero, or positive and negative values but it cannot store decimal values.

 

Example

  1. a=5  
  2. print(type(a))  

Output

 

 

float

 

This datatype contains decimal values or point values.

 

Example

  1. a=5.2  
  2. print(type(a))  

Output

 

 

In the above result, the data type of variable "a" is float type. 

 

bool

 

It is a special type of datatype which returns true of false. 

 

Example

  1. a,b=6,8  
  2. print(a>b)  
  3. print(type(a>b))  

Output

 

 

 

complex

 

It contains a real part + imaginary part.

 

Example

  1. a=5+4j  
  2. print(type(a))  

Output

 

 

Coversion

 

Python covnersion is different as compared to other programming languages, like in C# we use Convert.ToInt() or .ToString() etc. In Python, it is something different.

 

Int to float 

  1. a=5  
  2. print(float(a))  

Output

 

 

float to int

  1. # float to int  
  2. a=8.5  
  3. print(int(a))  

Output

 

 

float and int into complex

  1. a=5.2  
  2. b=6  
  3. print(complex(a,b))  

Output

 

 

Now, if we want to know the actual value of True and False

  1. print(int(True))  
  2. print(int(False))  

Output 


Squence

 

It is divided into five types, as given below. 

  • List
  • Tuple
  • String 
  • Set 
  • range objects

List

 

A list is a collection of items or objects of any type that can be accessed by index. This class also provides methods to search, sort, and manipulate lists. In Python, the list is created by using Square braces([]).

  1. lst=[10,25,35,45,50,60]  
  2. print(type(lst))  

Output

 

 

 

Tuple

A tuple is a collection which is ordered and unchangeable. A tuple is similar to a list. In Python, tuples are written with round brackets. The only difference between the list and tuple is that we cannot change the elements of a tuple once it assigned whereas in a list, elements can be changed after assigned. For more details about tuples

  1. tuple=(12,12,9,15,13,20,15)  
  2. print(type(tuple))  

Output

 

 

Set

Set is a collection of items in any order. A Python set is similar to the mathematics terms like union, intersection, difference and complement. In python set is created by using curly braces({}).

  1. s={12,12,9,15,13,20}   
  2. print(type(s))  

Output

 

 

String

 

String is a collection of one or more than one character. The string type in Python is called str. For more detail see String Operation in Python

range object: It uses range().

 

The range() type returns an immutable sequence of numbers between the given start integer to the stop integer.

  1. r=range(10)  
  2. print(r)  
  3. print(list(r))  

Output 

 

 

 

Even numbers between 0-10. There are many ways to do this but we use range function. 

  1. evenNumber=list(range(2,10,2))  
  2. print("Even No: " + str(evenNumber))  

Output

 

 

Dictonary

Python dictionary is an unordered collection of items. A dictionary has a key: value pair. The items are separated by commas, and the whole thing is enclosed in curly braces({}). Keys are unique within a dictionary while values may not be, and values of the dictionary can be any type.

  1. # dictionary  
  2. dict={'Amit':'Kumar','Abhishek':'Yadav','Apress':'Pub'}  
  3. print(dict.keys())  

Output

 

 

 

  1. Get values from keys,dict={'Math':'Algebra','Science':'Physics','Apress':'Publication'}   
  2. print(dict['Apress'])  

Output

 

 

None

 

When a variable is not assigned with any values. Normally, in other languages, we use null but in Python, we use none.

 

Conclusion

 

In this article, we learned about the datatypes Python has. We saw how to find the datatype of a variable, using type(). I will explain the remaining (list, tuple, dictionary) in an upcoming article.

要查看或添加评论,请登录

ganesh kavhar的更多文章

  • Python NumPy for Data Science

    Python NumPy for Data Science

    NumPy Introduction NumPy stands for ‘Numerical Python.’ It is a package in Python to work with arrays.

  • Becoming a Better Programmer

    Becoming a Better Programmer

    A smart programmer is one who understands that his or her work is never truly done. It doesn't matter how much you…

  • Data Processing in Machine Learning

    Data Processing in Machine Learning

    ML | Understanding Data Processing Data Processing is a task of converting data from a given form to a much more usable…

  • Operator in C programing by ganesh kavhar

    Operator in C programing by ganesh kavhar

    An operator in a programming language is a symbol that tells the compiler or interpreter to perform a specific…

  • Numpy by ganesh kavhar

    Numpy by ganesh kavhar

    Python Numpy Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array…

  • Cython to Wrap Existing C Code

    Cython to Wrap Existing C Code

    What is Cython ? It is an optimizing static compiler for both the Python programming language and the extended Cython…

  • Python for Data Analysis by ganesh kavhar

    Python for Data Analysis by ganesh kavhar

    A friend recently asked this and I thought it might benefit others if published here. This is for someone new to Python…

  • Data Classes in Python | An Introduction by ganesh kavhar

    Data Classes in Python | An Introduction by ganesh kavhar

    dataclass module is introduced in Python 3.7 as a utility tool to make structured classes specially for storing data.

  • Why learning C Programming is a must?

    Why learning C Programming is a must?

    C is a procedural programming language. It was initially developed by Dennis Ritchie between 1969 and 1973.

  • String Operation in Python

    String Operation in Python

    String: Strings in an array of bytes which represent Unicode characters in python. Python does not support character…

社区洞察

其他会员也浏览了