Python - Data Types

Python – Data Types

Data

Any raw fact & figure can be considered as Data. It can either be a character or a string, a number, or a Boolean or a collection of different data. Basically data alone does not make much of a sense until it is processed into a piece of meaningful information.

In Python, we store the data into the variables. And Python itself can determine its type and store it into a suitable container of its type. So now, let’s look at what are Data-Types.

Data-Types

Data-Types are basically categories into which the data can be categorized. Now Python supports various different types of Data-Types. They can be further divided to understand better with the help of the following chart.

We can know the data-type of a variable using ‘type’ keyword. Let’s have a look at that

  '''
  Knowing the Data-Types of Declared Variables
  '''
  #string
  a = 'Python'
  print(a)
  print(type(a))

  #integer - int
  b = 25
  print(b)
  print(type(b))

  #float
  c = 32.2
  print(c)
  print(type(c))

  #list
  d = ['a', 'b', 1, 2]
  print(d)
  print(type(d))

  #tuple
  e = ('a', 'b', 1, 2)
  print(e)
  print(type(e))

  #set
  f = {1, 2, 3}
  print(f)
  print(type(f))

  #dictionary - dict
  g = {'a': 'apple', 'b': 'ball', 'c': 'cat'}
  print(g)
  print(type(g))

Data Type Conversion

We can also convert data from one data-type to another. This is basically called data conversion. There are different keyword to different data types. We will understand this with the help of the following example.

  '''
  Converting different Data-Types
  '''
  #declaring string  
  a = '5'
  print(type(a))

  #converting to int
  a = int(a)
  print(type(a))

  #converting to float
  a = float(a)
  print(type(a))
  
  #declaring list
  b = [1, 2, 3, 4, 5]
  print(type(b))
 
  #converting to tuple
  b = tuple(b)
  print(type(b))
 
  #converting to set
  b = set(b)
  print(type(b))
 

Get started with Python: Strings

Follow us on:

Source: Platforuma

Leave a Comment

Your email address will not be published. Required fields are marked *

EXTRA ₹999 OFF on Lock-Down Live Learning!

Join Now!
X myStickymenu