Datatypes in python
In Python, every value has a datatype, which specifies the type of data that it represents. Understanding datatypes is essential for writing Python programs that work correctly and efficiently.
Python has several built-in datatypes, including:
- Numeric Datatypes: Used for representing numbers. This includes integers, floating-point numbers, and complex numbers.
- Sequence Datatypes: Used for representing sequences of values. This includes lists, tuples, and range objects.
- Mapping Datatypes: Used for representing mappings between keys and values. This includes dictionaries.
- Set Datatypes: Used for representing sets of unique values. This includes sets and frozen sets.
-
Boolean Datatype:
Used for representing boolean values. This includes
True
andFalse
. -
NoneType Datatype:
Used for representing the absence of a value. This includes
None
.
Let's take a closer look at some of these datatypes:
Numeric Datatypes
Python has three built-in numeric datatypes:
-
int:
Used for representing integers. For example,
42
and-100
are integers. -
float:
Used for representing floating-point numbers. For example,
3.14
and-1.0
are floating-point numbers. -
complex:
Used for representing complex numbers. For example,
3 + 4j
and1 - 2j
are complex numbers.
Sequence Datatypes
Python has three built-in sequence datatypes:
-
list:
Used for representing sequences of values. Lists are mutable, which means that you can change their contents after they are created. For example,
[1, 2, 3]
and["apple", "banana", "cherry"]
are lists. -
tuple:
Used for representing sequences of values. Tuples are immutable, which means that you cannot change their contents after they are created. For example,
(1, 2, 3)
and("apple", "banana", "cherry")
are tuples. -
range:
Used for representing sequences of numbers. For example,
range(1, 10)
represents the sequence of numbers from 1 to 9.
Mapping Datatype
Python has one built-in mapping datatype:
-
dict:
Used for representing mappings between keys and values. For example,
{"name": "John", "age": 42}
is a dictionary with two key-value pairs.
# Example program that demonstrates datatypes
# Numeric datatypes
x = 42
y = 3.14
z = 3 + 4j
# Sequence datatypes
a = [1, 2, 3]
b = ("apple", "orange")
# Mapping datatype
c = {"name": "John", "age": 42}
# Boolean datatype
d = True
# NoneType datatype
e = None
# Printing datatype of variables
print(type(x)) # Output: <class 'int'>
print(type(y)) # Output: <class 'float'>
print(type(z)) # Output: <class 'complex'>
print(type(a)) # Output: <class 'list'>
print(type(b)) # Output: <class 'tuple'>
print(type(c)) # Output: <class 'dict'>
print(type(d)) # Output: <class 'bool'>
print(type(e)) # Output: <class 'NoneType'>
In the example above, we create variables with different datatypes and print their types using the
type()
function.
Understanding datatypes in Python is important for writing code that works correctly and efficiently. By knowing what datatypes are available and how to use them, you can write programs that are easier to read, maintain, and debug.