Data Types in Python

Python data types define the kind of value a variable can store and determine the operations you can perform on that value. Because Python is dynamically typed, you do not need to declare a variable’s data type explicitly. Instead, Python automatically determines the type based on the assigned value. Understanding Python data types helps you write cleaner, more efficient, and error-free programs.

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

Python contains the following built-in data types

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

Note:

# 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 (which means once created, it can’t be changed by using code)
  • It allows duplicates
  • We will discuss strings 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 (meaning 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)
print(type(lst))

Output:

[10,20,30,40]
<class 'list'>

Tuple

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

Example:

t=(1, 2, 3, 4)
print(t)
print(type(t))

Output:

(1,2,3,4)
<class 'tuple'>

Range

  • It 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): gives numbers from 0 to stop – 1.
  2. range(start, stop): gives numbers from start to stop – 1.
  3. range(start, stop, step): gives 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) generates 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'])
print(d['age'])

Output:

Rohit
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.

Frequently Asked Questions

What are Python data types?

Python data types classify the type of value stored in a variable, such as integers, strings, lists, dictionaries, and sets.

How many built-in data types does Python have?

Python provides built-in categories including numeric, text, sequence, mapping, set, boolean, binary, and NoneType.

What is the difference between mutable and immutable data types?

Mutable data types can be modified after creation, while immutable data types cannot.

Which function checks the data type in Python?

You can use the type() function or isinstance() to check a variable’s data type.

Conclusion

Python data types are the foundation of every Python program. By understanding numeric, string, sequence, mapping, set, boolean, binary, and NoneType values, you can choose the right structure for your data and write more efficient, maintainable code. Practice working with different data types, learn when to use mutable or immutable objects, and explore type conversion to build a strong Python programming foundation.

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 *