In Python, data types are used to categorize and represent different types of values that you can manipulate in your programs. Understanding data types is essential because they determine how you can use and operate on your data. Python provides a variety of built-in data types, and here are some of the most common ones:
My Python codes: https://masterwithhamza@bitbucket.org/hamxaflutterapps/mypythoncodes.git
Integer (
int
)Represents whole numbers (positive or negative) without a decimal point.
Example:
42
,-10
,0
Float (
float
)Represents real numbers with a decimal point.
Example:
3.14
,-0.5
,2.0
String (
str
)Represents sequences of characters enclosed in single (' ') or double (" ") quotes.
Example:
"Hello, Python!"
,'12345'
,"Data Types"
Boolean (
bool
)Represents the truth values
True
orFalse
.Used for making decisions and logical operations.
Example:
True
,False
List
An ordered collection of items that can be of different data types.
Enclosed in square brackets
[]
.Example:
[1, 2, 3]
,['apple', 'banana', 'cherry']
Tuple
Similar to lists, but they are immutable (cannot be changed after creation).
Enclosed in parentheses
()
.Example:
(1, 2, 3)
,('John', 25)
Dictionary (
dict
)A collection of key-value pairs.
Enclosed in curly braces
{}
.Example:
{'name': 'Alice', 'age': 30, 'city': 'New York'}
Set
An unordered collection of unique elements.
Enclosed in curly braces
{}
or created using theset()
constructor.Example:
{1, 2, 3}
,set([2, 2, 3, 3, 4])
NoneType (
None
)Represents the absence of a value or a null value.
Often used to indicate that a variable has no assigned value.
Complex (
complex
)Represents complex numbers with both real and imaginary parts.
Written as
a + bj
, wherea
andb
are real numbers, andj
represents the square root of -1.Example:
3 + 4j
,-2.5 - 1.7j
These data types are the building blocks of Python programs, and you can perform various operations on them, such as arithmetic operations on numbers, concatenation and manipulation of strings, indexing and slicing of sequences (lists, tuples, strings), and more.
You can also create your custom data types using classes, but understanding and using these built-in data types is fundamental for any Python programmer. Additionally, Python is a dynamically typed language, which means you don't need to declare the data type of a variable explicitly; it is inferred at runtime based on the value assigned to it.