Python Data Types


In this chapter, we will learn about Python Data Types

What are Data Types ?


Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. 

Python has several built-in data types that are used to store and manipulate data. The following are the most commonly used data types in Python:

Numeric data types


Numeric data types are used to represent numerical values in Python. Python has three built-in numeric data types:

a) Int: Integers are whole numbers, positive, negative or zero, without a decimal point. They can be of any length.

Example:

x = 10
y = -3 
z = 0

 

b) Float: Floating-point numbers are decimal numbers with a fractional component. They are represented using the keyword "float".

Example:

x = 3.14 
y = -2.5
z = 0.0

 

c) Complex: Complex numbers are represented using the keyword "complex". They are used to represent numbers with both real and imaginary components.

Example:

x = 2 + 3j 
y = -1 - 2j

 

String Data Type


Strings are used to represent text in Python. They are a sequence of characters enclosed in single or double quotes.

Example:

x = "Hello World!"
y = 'Python is awesome'

 

Strings can be concatenated using the "+" operator.

Example:

x = "Hello" + " " + "World!" 

# x will be "Hello World!"

 

Strings can also be indexed and sliced to access specific characters or substrings.

Example:

x = "Hello World!"
print(x[0]) # Output: H
print(x[1:5]) # Output: ello

 

Boolean Data Type


Boolean data type is used to represent true or false values. In Python, the keyword "True" represents true and "False" represents false.

Example:

x = True
y = False

 

List Data Type


Lists are used to store a collection of items in Python. They are a mutable sequence of values enclosed in square brackets, separated by commas.

Example:

x = [1, 2, 3, 4]
y = ['apple', 'banana', 'cherry']

 

Tuple Data Type


Tuples are similar to lists, but they are immutable. They are a sequence of values enclosed in parentheses, separated by commas.

Example: 

x = (1, 2, 3)
y = ('apple', 'banana', 'cherry')


Set Data Type


Sets are used to store a collection of unique elements in Python. They are an unordered collection of values enclosed in curly braces, separated by commas.

Example:

x = {1, 2, 3, 4}
y = {'apple', 'banana', 'cherry'}

 

Dictionary Data Type


Dictionaries are used to store key-value pairs in Python. They are an unordered collection of key-value pairs enclosed in curly braces, separated by commas.

Example:

x = {'name': 'John', 'age': 30, 'city': 'New York'}

Dictionaries can be accessed using keys.

Example:

x = {'name': 'John', 'age': 30, 'city': 'New York'}
print(x['name']) # Output: John

 

Summary


Data types are fundamental to any programming language, and Python is no exception.

In Python, there are several built-in data types, including numeric data types, string data type, boolean data type, list data type, tuple data type, set data type, and dictionary data type.

Understanding these data types and their operations is crucial for any Python programmer.

By mastering these data types, you can write efficient and effective Python code to solve a wide range of programming problems.
 

© 2022-2023 All rights reserved.