Python Data type

Python is a dynamically typed language, which means that the data type of a variable is determined automatically based on the value assigned to it. In Python, the most commonly used data types are:

  1. Numeric Types: Numeric types include integers, floating-point numbers, and complex numbers. Integers are whole numbers, while floating-point numbers have decimal places. Complex numbers have a real and imaginary part.
    # integer type
    x = 10
    print(type(x))  # Output: <class 'int'>
    # float type
    y = 3.14
    print(type(y))  # Output: <class 'float'>
    # complex type
    z = 2 + 3j
    print(type(z))  # Output: <class 'complex'>
  2. Boolean Type: The Boolean type represents logical values True and False. Boolean values are often used in conditional statements and loops.
    # boolean type
    a = True
    print(type(a))  # Output: <class 'bool'>
    b = False
    print(type(b))  # Output: <class 'bool'>
  3. Strings: Strings are sequences of characters enclosed in single or double quotes. They are used to represent text data in Python.
    # string type
    name = "John"
    print(type(name))  # Output: <class 'str'>
    message = 'Hello, World!'
    print(type(message))  # Output: <class 'str'>
  4. Lists: Lists are ordered collections of items, which can be of any data type. They are created using square brackets [] and can be modified during the program execution.
    # list type
    my_list = [1, 2, 3, "apple", "banana"]
    print(type(my_list))  # Output: <class 'list'>
  5. Tuples: Tuples are similar to lists, but they are immutable, which means that once they are created, they cannot be modified.
    # tuple type
    my_tuple = (1, 2, 3, "apple", "banana")
    print(type(my_tuple))  # Output: <class 'tuple'>
  6. Sets: Sets are unordered collections of unique items. They are created using curly braces {} or the set() function.
    # set type
    my_set = {1, 2, 3, "apple", "banana"}
    print(type(my_set))  # Output: <class 'set'>
  7. Dictionaries: Dictionaries are collections of key-value pairs. Each value is associated with a unique key, which can be used to access the value.
    # dictionary type
    my_dict = {"name": "John", "age": 30, "city": "New York"}
    print(type(my_dict))  # Output: <class 'dict'>
  8. None Type: The None type represents the absence of a value. It is often used as a placeholder for variables that have not been assigned a value yet.
    # none type
    x = None
    print(type(x))  # Output: <class 'NoneType'>

Python also has some built-in functions that can be used to convert data types. For example, the int() function can be used to convert a string to an integer, and the float() function can be used to convert a string to a floating-point number.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial