Lokang 

Python and MySQL

Data Type

In programming, a data type is an important concept that specifies the type of data that a variable can hold. Python, like other programming languages, has a set of built-in data types. These can be categorized into several types, including:

Text Type:

  • str: Used to store a sequence of characters (text). Created by enclosing text in quotes.

Example:

x = "Hello World"

umeric Types:

  • int: Used to store integers (whole numbers).
  • float: Used to store numbers that have a decimal point.
  • complex: Used to store complex numbers.

Examples:

x = 20              # int
y = 20.5            # float
z = 1j              # complex

Sequence Types:

  • list: Used to store multiple items in a single variable. Lists are created using square brackets and are mutable (changeable).
  • tuple: Similar to lists, but the tuples are immutable (unchangeable). Tuples are created using round brackets.
  • range: A sequence type that represents a sequence of numbers. Used in for loops and other looping constructs.

Examples:

x = ["apple", "banana", "cherry"]  # list
y = ("apple", "banana", "cherry")  # tuple
z = range(6)                       # range

Mapping Type:

  • dict: A collection which is unordered, changeable, and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values.

Example:

x = {"name": "John", "age": 36}

Set Types:

  • set: A collection which is unordered and unindexed. Sets are written with curly brackets.
  • frozenset: Similar to set, but frozensets are immutable.

Example:

x = {"apple", "banana", "cherry"}  # set
y = frozenset({"apple", "banana", "cherry"})  # frozenset

Boolean Type:

  • bool: Used to represent boolean values: True or False.

Example:

x = True
y = False

Binary Types:

  • bytes: Immutable sequence of bytes.
  • bytearray: Mutable sequence of bytes.
  • memoryview: Memory view objects.

Examples:

x = b"Hello"         # bytes
y = bytearray(5)     # bytearray
z = memoryview(bytes(5))  # memoryview

None Type:

  • NoneType: This type has a single value. It is used to signify the absence of a value or a null value. It is represented by the keyword None.

Example:

x = None

Each of these data types serves a specific purpose and allows the Python interpreter to manage memory, perform optimizations, and enforce a level of type checking in your code. Understanding these types and their characteristics is essential for writing clear and efficient Python code.