Python Logo

Data Types


Python datatypes are used to classify the different types of data that can be stored in Python variables. There are six basic datatypes in Python:

  • Integers (int): Integers are whole numbers, both positive and negative. For example, 5, -10, and 1234567890 are all integers.
  • Floats (float): Floats are real numbers with a decimal point. For example, 3.14, -20.5, and 1.234567890e9 are all floats.
  • Booleans (bool): Booleans are logical values that can be either True or False. For example, the statement "5 is greater than 2" is True, while the statement "5 is less than 2" is False.
  • Strings (str): Strings are sequences of characters. For example, "Hello, World!", "This is a string.", and "1234567890" are all strings.
  • Lists (list): Lists are mutable sequences of data. This means that the data in a list can be changed. For example, the list [1, 2, 3, 4, 5] contains the numbers 1 through 5. You can add, remove, or change the items in the list at any time.
  • Tuples (tuple): Tuples are immutable sequences of data. This means that the data in a tuple cannot be changed. For example, the tuple (1, 2, 3, 4, 5) contains the same numbers as the list [1, 2, 3, 4, 5], but you cannot add, remove, or change the items in the tuple.

Here are some examples of how to use Python datatypes:

 

Python

# Integers
my_integer = 5
print(my_integer)

# Floats
my_float = 3.14
print(my_float)

# Booleans
my_boolean = True
print(my_boolean)

# Strings
my_string = "Hello, World!"
print(my_string)

# Lists
my_list = [1, 2, 3, 4, 5]
print(my_list)

# Tuples
my_tuple = (1, 2, 3, 4, 5)
print(my_tuple)
 

Output:

 

5
3.14
True
Hello, World!
[1, 2, 3, 4, 5]
(1, 2, 3, 4, 5)
 

Python datatypes are a fundamental part of Python programming. Understanding how to use datatypes correctly is essential for writing efficient and readable Python code.