Python has several built-in data types, which are commonly used in Python programming. Here are some of the most common ones:
Numbers
Python supports three types of numbers - integer, float, and complex.
- Integer: These are whole numbers, positive or negative, without decimals. Example:
5
,-3
,0
- Float: These are real numbers with a decimal point. Example:
3.14
,-0.01
,9.0
- Complex: These are numbers with a real and imaginary component. Example:
3+4j
,5j
# Examples of number data types
x = 5 # integer
y = 3.14 # float
z = 3+4j # complex
Strings
Strings in Python are sequences of characters. They are created by enclosing characters in quotes. Python treats single quotes and double quotes as the same.
# Examples of string data type
str1 = "Hello, World!" # string with double quotes
str2 = 'Hello, Python!' # string with single quotes
Lists
A list in Python is a collection of items. Lists are ordered, changeable, and allow duplicate values. Lists are written with square brackets.
# Example of list data type
my_list = ["apple", "banana", "cherry"]
Tuples
A tuple in Python is similar to a list. The difference between the two is that tuples are immutable, which means we cannot change the elements of a tuple once it is assigned.
# Example of tuple data type
my_tuple = ("apple", "banana", "cherry")
Dictionaries
A dictionary in Python is an unordered collection of items. Each item of a dictionary has a key/value pair.
# Example of dictionary data type
my_dict = {"name": "John", "age": 30, "city": "New York"}
Booleans
In Python, Booleans represent one of two values: True
or False
.
# Examples of boolean data type
is_true = True
is_false = False