Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Data Types in Python

Data Types in Python

? Data Types in Python

In Python, data types define the type of a value or variable. Python has several built-in data types that allow you to store different kinds of values, such as numbers, text, sequences, and more. These data types are classified into mutable (changeable) and immutable (unchangeable) types.


? Built-in Data Types in Python

1?? Numeric Types

  • int (Integer): Represents whole numbers, both positive and negative, without decimals.

    x = 5y = -10
  • float (Floating-point number): Represents numbers with a decimal point.

    a = 3.14b = -7.5
  • complex (Complex number): Represents complex numbers in the form a + bj, where a is the real part and b is the imaginary part.

    c = 2 + 3j

2?? Sequence Types

  • list: An ordered, mutable collection of items. Lists can store multiple data types and can be modified (elements can be added, removed, or changed).

    fruits = ["apple", "banana", "cherry"]numbers = [1, 2, 3, 4, 5]
  • tuple: An ordered, immutable collection of items. Once created, you cannot modify the elements of a tuple.

    colors = ("red", "green", "blue")
  • range: Represents a sequence of numbers, typically used in for loops. It is often used for iterating over a sequence of numbers in a specific range.

    r = range(0, 10)

3?? Text Type

  • str (String): Represents a sequence of characters. Strings are immutable in Python.

    message = "Hello, world!"

4?? Mapping Type

  • dict (Dictionary): An unordered collection of key-value pairs. Keys are unique and immutable, while values can be any data type. Dictionaries are mutable.

    person = {"name": "John", "age": 30, "city": "New York"}

5?? Set Types

  • set: An unordered collection of unique items. Sets are mutable and do not allow duplicate values.

    fruits = {"apple", "banana", "cherry"}
  • frozenset: An immutable version of a set. Once created, you cannot add or remove elements from a frozenset.

    frozen_fruits = frozenset({"apple", "banana", "cherry"})

6?? Boolean Type

  • bool: Represents two values, True and False, used for logical operations.

    is_valid = Trueis_ready = False

7?? Binary Types

  • bytes: Immutable sequence of bytes, often used for binary data.

    byte_data = b"hello"
  • bytearray: Mutable sequence of bytes. Unlike bytes, the contents of a bytearray can be modified.

    byte_array = bytearray([65, 66, 67])
  • memoryview: Provides memory access to binary data without copying it.

    mv = memoryview(b"hello")

? Type Conversion in Python

You can convert one data type to another using built-in functions like int(), float(), str(), etc.

Example: Type Conversion

# Convert a float to an integerx = 3.14int_x = int(x)  # Result: 3# Convert an integer to a stringy = 10str_y = str(y)  # Result: "10"# Convert a string to an integers = "20"int_s = int(s)  # Result: 20

? Checking the Data Type

You can check the data type of a variable using the type() function.

Example:

x = 5print(type(x))  # Output: <class 'int'>y = "hello"print(type(y))  # Output: <class 'str'>

? Summary of Data Types

  • Numeric: int, float, complex

  • Sequence: list, tuple, range

  • Text: str

  • Mapping: dict

  • Set: set, frozenset

  • Boolean: bool

  • Binary: bytes, bytearray, memoryview


Let me know if you'd like more details on any specific data type or any other questions! ?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql