Data Types in Python

futura 5 Explore and Read Our Blogs Written By Our Insutry Experts Learn From KSR Data Vizon

Data types are used to define the type of data or value stored in a variable. They determine how the data is stored, processed, and manipulated in a program. Based on the value provided, the data type will be assigned automatically in Python.

To check the type of data it is: use type(data) -> A Predefined function

Python contains the following inbuilt data types

  1. Numeric:
    • Integer (int) : Whole numbers (eg: 0,1,2….)
    • Float (float) : Decimals numbers(eg: 10.2,11.0,2.222…)
    • Complex: Number is in the form of Real and imaginary  (20+30j)
  2. Boolean (bool):
    • True
    • False
  3. None Type: None
  4. Sequential types:
    • String
    • List
    • Tuple
    • Range
    • Set
    • Dictionary

Types of Data types:

Numeric Datatypes

Integer (int):

Used to represent whole numbers (non-decimal values)

Example:

a = 10
print(type(a)) # Output: class <int>

Float (float):

Used to represent decimal values (point values).

Example:

f = 1.2234
print(type(f)) # Output: class <float>

Bool datatype:

  • This data type is used to represent boolean values.
  • The only possible values for this data type are True and False.
  • Internally, Python represents True as 1 and False as 0.
t = True
type(t)  # bool

Example:

a = 100
b = 200
c = a < b
print(c)  # Output: True

# Demonstrating arithmetic with boolean values
True + True    # Output: 2
True - False   # Output: 1

None datatype:

The None data type represents an object with no defined value. If an object has no value, we can assign it the None type.

a = None
print(a) # None
print(type(a)) # Output : <class ‘NoneType’>

Sequential data types

String (str):

  • str is a short form representation of String.
  • It is a sequence of characters enclosed in either single quotes (‘KSR’) or double quotes (”KSR”) or triple quotes (” ” ” KSR” ” ”) or (”’ KSR ”’).
  • It is immutable in nature (means once created can’t change it by using code)
  • It allows duplicates
  • We will discuss string in more detail in the upcoming chapter on string data type.
  • All types of quotes are used to represent single-string literals. (mostly single & double quotes used).

Example:

s = 'KSR Datavizon'
s = ''KSR Datavizon''

Triple single or double quotes are used to represent multiple lines of string literals.

Example:

s = '''KSR 
      Datavizon
      Python Class '''

s = '' '' ''KSR 
      Datavizon
      Python Class '' '' ''

List

  • A list is a data structure in Python that stores a group of values.
  • A group of data enclosed in square brackets [] and each value separated with a comma ‘ , ‘.
  • It is mutable in nature (means once created we can change it by using code)
  • It allows duplicates
  • We will discuss lists in more detail in the upcoming chapter on list data type.

Example:

lst=[10, 20, 30, 40]
print(lst)  # output: [10,20,30,40]
print(type(lst)) # Output: <class 'list'>

Tuple

  • A tuple data type stores a group of values and is enclosed by parenthesis.
  • It is immutable in nature and allows duplicates.
  • We will cover tuples in more detail in the upcoming chapter on tuple data type.

Example:

t=(1, 2, 3, 4)
print(t) # Output: (1,2,3,4)
print(type(t))  # Output: <class 'tuple'>

Range:

  • The range data type represents a sequence of numbers.
  • It is immutable in nature and duplicates are not allowed.
  • It is commonly used in loops and other places that need a sequence of numbers
  • While it doesn’t store all the numbers in memory like other data types, it creates an object that represents the sequence.

Syntax:

  1. range(stop): Generates numbers from 0 to stop – 1.
  2. range(start, stop): Generates numbers from start to stop – 1.
  3. range(start, stop, step): Generates numbers from start to stop – 1, incrementing by step.

Example 1: Basic Range

for i in range(5):
    print(i)

Output:
1
2
3
4
5

This generates numbers from 0 to 4.

Example 2: Range with Start and Stop

for i in range(2, 6):
    print(i)

Output:
2
3
4
5

This generates numbers from 2 to 5.

Example 3: Range with Step

for i in range(1, 10, 2):
    print(i)

Output:
1
3
5
7
9

This generates numbers ranging from 1 to 9, incrementing by 2.

Example 4: Using range to Create a List

numbers = list(range(5))
print(numbers)

Output: [0, 1, 2, 3, 4]

Here, range(5) is used to generate a list of numbers from 0 to 4.

Set:

  • A set data type stores a collection of unique values within curly braces {}.
  • Sets are unordered and do not allow duplicate values.
  • It is mutable in nature.
  • We will cover the set in more detail in the upcoming chapter on set data structures.

Example 1: Basic Set Creation

my_set = {1, 2, 3, 4, 5}
print(my_set)

Output: {1, 2, 3, 4, 5}

Example 2: Creating a Set with Duplicate Values

my_set = {1, 2, 3, 4, 4,2}
print(my_set)

Output: {1, 2, 3, 4}

Example 3: Using a Set with Mixed Data Types

my_set = {1, 'hello', 3.14}
print(my_set)

Output: {1, 'hello', 3.14}

Dictionary (dict):

  • A dictionary data type stores a collection of key-value pairs within curly braces {}.
  • Each key in a dictionary must be unique, and it maps to a specific value.
  • It is mutable in nature.
  • We will cover the dict in more detail in the upcoming chapter on dict data structures.

Example 1: Basic Dictionary Creation

d = {'name': 'Rohit', 'age': 30, 'city': 'New York'}
print(d)

Output: {'name': 'Rohit', 'age': 30, 'city': 'New York'}

Here, d is a dictionary with keys ‘name’, ‘age’, and ‘city’, each associated with a corresponding value.

Example 2: Accessing Values

print(d['name'])  # Output: Rohit
print(d['age'])   # Output: 30

Values are accessed using their keys.

Example 3: Adding or Updating Key-Value Pairs

d['email'] = 'rohit@example.com'  # Adding a new key-value pair
d['age'] = 32                     # Updating an existing key-value pair
print(d)

Output: {'name': 'Rohit', 'age': 32, 'city': 'New York', 'email': 'rohit@example.com'}

Here, a new key-value pair is added, and an existing key’s value is updated.

Conclusion

Understanding data types is fundamental in Python as they dictate how data is stored, manipulated, and used in operations. Python offers a rich set of data types to handle various kinds of data, making it versatile for different programming needs.

Knowledge Check

data types in python
Data Analytics with Power Bi and Fabric
Could Data Engineer
Data Analytics With Power Bi Fabic
AWS Data Engineering with Snowflake
Azure Data Engineering
Azure & Fabric for Power bi
Full Stack Power Bi
Subscribe to our channel & Don’t miss any update on trending technologies

Kick Start Your Career With Our Data Job

Master Fullstack Power BI – SQL, Power BI, Azure Cloud & Fabric Tools
Master in Data Science With Generative AI Transform Data into Business Solutions
Master Azure Data Engineering – Build Scalable Solutions for Big Data
Master AWS Data Engineering with Snowflake: Build Scalable Data Solutions
Transform Your Productivity With Low Code Technology: Master the Microsoft Power Platform

Social Media channels

► KSR Datavizon Website :- https://www.datavizon.com
► KSR Datavizon LinkedIn :- https://www.linkedin.com/company/datavizon/
► KSR Datavizon You tube :- https://www.youtube.com/c/KSRDatavizon
► KSR Datavizon Twitter :- https://twitter.com/ksrdatavizon
► KSR Datavizon Instagram :- https://www.instagram.com/ksr_datavision
► KSR Datavizon Face book :- https://www.facebook.com/KSRConsultingServices
► KSR Datavizon Playstore :- https://play.google.com/store/apps/details?id=com.datavizon.courses&hl=en-IN

Related Posts

Leave a Reply

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