Python Data Types
Data types in Python represent the type of data that can be stored and manipulated in a program. Python has several built-in data types, including:
- Integers (
int
) - Floats (
float
) - Strings (
str
) - Lists (
list
) - Tuples (
tuple
) - Dictionaries (
dict
) - Sets (
set
) - Boolean (
bool
) - NoneType (
None
)
1. Integers
An integer is a whole number, positive or negative, without decimals.
Example:
x = 5
y = -10
z = 0
2. Floats
A float is a floating-point number, representing real numbers with a decimal point.
Example:
x = 3.14
y = -0.5
3. Strings
A string is a sequence of characters, enclosed in single or double quotes.
Example:
name = "Alice"
message = 'Hello, World!'
4. Lists
A list is a collection of items, ordered and mutable. Lists are created using square brackets ([]
).
Example:
numbers = [1, 2, 3, 4, 5]
fruits = ['apple', 'banana', 'orange']
5. Tuples
A tuple is a collection of items, ordered and immutable. Tuples are created using parentheses (()
).
Example:
point = (10, 20)
colors = ('red', 'green', 'blue')
6. Dictionaries
A dictionary is a collection of key-value pairs, unordered and mutable. Dictionaries are created using curly braces ({}
) with key-value pairs separated by commas (,
).
Example:
person = {'name': 'Alice', 'age': 30, 'city': 'New York'}
7. Sets
A set is a collection of unique items, unordered and mutable. Sets are created using curly braces ({}
).
Example:
numbers = {1, 2, 3, 4, 5}
letters = {'a', 'b', 'c', 'd'}
8. Boolean
Boolean represents the truth values True
and False
.
Example:
is_active = True
is_valid = False
9. NoneType
NoneType represents the absence of a value or a null value.
Example:
result = None
Conclusion
Python has a rich set of built-in data types that allow you to represent and manipulate different types of data in your programs. Understanding these data types is essential for writing Python programs effectively.