Data Types in Python

Data types are fundamental building blocks in programming that define the type of data a variable can hold. Python supports various data types, each with its own characteristics and uses. In this topic, we'll explore Python's data types comprehensively, covering everything from basic types to more advanced ones.

Basic Data Types

Data Types in Python

Integers

Integers are whole numbers, positive or negative, without any decimal point.

				
					# Example of integers
x = 10
y = -5
				
			

Boolean

The Boolean data type has two possible values: True and False. It is commonly used in conditional statements and logical operations.

				
					# Assigning Boolean values
is_python_fun = True
is_sky_green = False

# Using Booleans in conditional statements
if is_python_fun:
    print("Python is fun!")
else:
    print("Python is not fun!")

# Boolean expressions
a = 10
b = 20

print(a > b)  # False
print(a < b)  # True
print(bool(0))  # False
print(bool(1))  # True
print(bool(""))  # False (empty string)
print(bool("Hello"))  # True (non-empty string)

				
			

Floats

Floats (floating-point numbers) are numbers with a decimal point or numbers in exponential form.

				
					# Example of floats
pi = 3.14
temperature = 98.6
				
			

Strings

Strings are sequences of characters enclosed in single quotes (”) or double quotes (“”).

				
					# Example of strings
name = "Alice"
message = 'Hello, Python!'
				
			

Sequence Data Types

Lists

Lists are ordered collections of items, which can be of different data types. They are mutable, meaning you can change their elements after they are created.

				
					# Example of lists
my_list = [1, 2, 3, 'apple', 'banana']
				
			

Tuples

Tuples are similar to lists but are immutable, meaning you cannot change their elements after they are created.

				
					# Example of tuples
my_tuple = (1, 2, 3, 'apple', 'banana')

				
			

Mapping Data Types

Dictionaries

Dictionaries are unordered collections of key-value pairs. They are mutable and can store elements of different data types.

				
					# Example of dictionaries
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
				
			

Set Data Types

Sets

Sets are unordered collections of unique elements. They do not allow duplicate elements.

				
					# Example of sets
my_set = {1, 2, 3, 4, 5}
				
			

Understanding data types is crucial in Python programming as it allows you to work with different types of data efficiently. In this topic, we've covered the basic data types like integers, floats, and strings, as well as sequence types like lists and tuples, mapping types like dictionaries, and set types like sets. By mastering these data types, you'll be well-equipped to handle a wide range of programming tasks in Python. Happy Coding!❤️

Table of Contents