Skip to content

Python Datatypes

Python is a dynamically-typed language, meaning the interpreter determines the data type of a variable based on the value it holds. Here are some common data types in Python:
Integers (`int`):
    • Whole numbers without a fractional part
				
					age = 25
				
			
Floats (`float`):
    • Numbers with a decimal point or in scientific notation.
				
					age = 25
				
			
Strings (`str`):
    • Sequences of characters enclosed in single or double quotes.
				
					name = "John"
				
			
Booleans (`bool`):
    • Represents the truth values True or False.
				
					name = "John"
				
			
Lists (`list`):
    • Ordered collections of items, can contain elements of different data types.
				
					fruits = ["apple", "orange", "banana"]
				
			
Tuples (`tuple`):
    • Similar to lists, but immutable (cannot be changed after creation).
				
					coordinates = (3, 4)
				
			
Dictionaries (`dict`):
    • Key-value pairs, used to store data in a structured way.
				
					person = {"name": "Alice", "age": 30, "city": "New York"}
				
			
Sets (`set`):
    • Unordered collections of unique items.
				
					unique_numbers = {1, 2, 3, 4, 5}
				
			
None Type (`NoneType`):
    • Represents the absence of a value or a null value.
				
					result = None
				
			
Type Conversion:
You can convert between different data types using functions like int(), float(), str(), etc.
				
					# Convert a float to an integer
x = int(5.9)
print(x)  # Outputs: 5

# Convert an integer to a string
y = str(42)
print(y)  # Outputs: "42"
				
			
Checking Data Types:
You can use the type() function to check the data type of a variable.
				
					age = 25
print(type(age))  # Outputs: <class 'int'>

name = "John"
print(type(name))  # Outputs: <class 'str'>
				
			
Understanding data types is essential for effective programming in Python. It allows you to perform operations and manipulations based on the nature of the data you’re working with.