Python Data Types - AsgarTech

 Python Data Types - AsgarTech

Python a data type is a classification of the type of data that can be stored and manipulated within a program. Python has several built-in data types that can be used to represent various types of data. Here are some of the most commonly used data types in Python:


Numbers: Numbers can be of different types in Python, such as integers, floating-point numbers, and complex numbers.

Example : -

# Integers

x = 10

y = -5

z = 0


# Floating-point numbers

a = 3.14

b = -2.5


# Complex numbers

c = 2 + 3j

d = 4 - 2j


Strings: Strings are sequences of characters enclosed in single, double, or triple quotes. They are immutable, meaning that once created, their contents cannot be modified.

Example : -

# Single quotes

s1 = 'Hello, world!'


# Double quotes

s2 = "Python is great"


# Triple quotes for multiline strings

s3 = '''This is a 

multiline string'''

Lists: Lists are sequences of values that can be of different types. They are mutable, which means that you can add, remove or modify their elements.

Example : -

# Creating a list

numbers = [1, 2, 3, 4, 5]

names = ['John', 'Mary', 'Bob']

mixed = [1, 'hello', 3.14]


# Accessing list elements

print(numbers[0]) # prints 1

print(names[2]) # prints Bob


# Modifying list elements

mixed[1] = 'world'

print(mixed) # prints [1, 'world', 3.14]


# Adding elements to a list

numbers.append(6)

print(numbers) # prints [1, 2, 3, 4, 5, 6]


Tuples: Tuples are similar to lists, but they are immutable. Once created, their elements cannot be changed.

Example : -

# Creating a tuple

coordinates = (1, 2)

colors = ('red', 'green', 'blue')


# Accessing tuple elements

print(coordinates[0]) # prints 1

print(colors[2]) # prints blue


# Tuples are immutable

# coordinates[0] = 2 # TypeError: 'tuple' object does not support item assignment


Sets: Sets are unordered collections of unique elements. They can be used for various operations, such as intersection, union, and difference.

Example : -

# Creating a set

numbers = {1, 2, 3, 4, 5}

vowels = {'a', 'e', 'i', 'o', 'u'}


# Adding elements to a set

numbers.add(6)

print(numbers) # prints {1, 2, 3, 4, 5, 6}


# Set operations

even = {2, 4, 6}

print(numbers.intersection(even)) # prints {2, 4, 6}

print(numbers.union(even)) # prints {1, 2, 3, 4, 5, 6}


Dictionaries: Dictionaries are unordered collections of key-value pairs. They are used to store and retrieve data using keys instead of indices.

Example : -

# Creating a dictionary

person = {'name': 'John', 'age': 30, 'gender': 'male'}


# Accessing dictionary elements

print(person['name']) # prints John

print(person.get('age')) # prints 30


# Modifying dictionary elements

person['age'] = 40

print(person) # prints {'name': 'John', 'age': 40, 'gender': 'male'}


# Adding elements to a dictionary

person['city'] = 'New York'

print(person) # prints {'name': 'John', 'age': 40, 'gender': 'male', 'city': 'New York'}

Booleans: Booleans are a data type that can have only two possible values, True or False. They are commonly used in conditional statements and loops.

Example : -

# Boolean values

x = True

y = False


# Boolean expressions

a = 10

b = 20

print(a < b


These are some of the main data types in Python. Understanding data types is crucial in programming as it helps you manipulate data effectively and efficiently.

thanks my dears friends.

No comments: