Python Data Types

Python is a dynamically typed language, which means that variables don’t have a specific data type assigned to them until runtime. However, there are several built-in data types that you can use to define variables and objects in your code. Here are some of the most commonly used Python data types:

  1. Integers (int): These are whole numbers (positive, negative, or zero) without any decimal points.
  2. Floats (float): These are numbers with decimal points.
  3. Strings (str): These are sequences of characters (letters, numbers, symbols, etc.) enclosed in quotes (single or double).
  4. Booleans (bool): These are values that are either True or False. They’re often used in conditional statements to determine the flow of the program.
  5. Lists (list): These are ordered collections of values, which can be of any data type. Lists are defined using square brackets and commas.
  6. Tuples (tuple): These are similar to lists, but they’re immutable (i.e., their values cannot be changed once they’re defined). Tuples are defined using parentheses and commas.
  7. Dictionaries (dict): These are unordered collections of key-value pairs. The keys must be unique, and they’re used to retrieve the corresponding values. Dictionaries are defined using curly braces and colons.
  8. Sets (set): These are unordered collections of unique values. Sets are defined using curly braces.

In addition to these built-in data types, Python also supports user-defined classes and objects, which can be used to define more complex data structures and behaviors.

Examples of Python data types

Integer:

x = 42
print(type(x)) # Output: <class 'int'>

Float:

y = 3.14
print(type(y)) # Output: <class 'float'>

String:

name = "Alice"
print(type(name)) # Output: <class 'str'>

Boolean:

is_valid = True
print(type(is_valid)) # Output: <class 'bool'>

List:

my_list = [1, 2, 3, "apple", "orange"]
print(type(my_list)) # Output: <class 'list'>

Tuple:

my_tuple = (1, 2, 3, "apple", "orange")
print(type(my_tuple)) # Output: <class 'tuple'>

Dictionary:

my_dict = {"name": "Alice", "age": 25, "is_valid": True}
print(type(my_dict)) # Output: <class 'dict'>

Set:

my_set = {"apple", "orange", "banana"}
print(type(my_set)) # Output: <class 'set'>