Variable Types in Python

Python : Variables and Types

What is Variables in python?

In Python, a variable is a name that refers to a value. It is a placeholder for storing data or values in memory, which can be accessed and modified during program execution. In other words, it's a way to give a name to a value so that we can use it later in the program. To create a variable in Python, we simply assign a value to a name using the equal sign =

What is Type in python?

In Python, every value has a type, which determines the kind of data that the value represents. The most common types in Python include integers, floating-point numbers, strings, booleans, and lists etc.

Here is the example


# variable declaration
a = 20
b = .23
c = "xyz"

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

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

# String type
name = 'John'
print(type(name))  # Output: <class 'str'>

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

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

Different types of variables in python

Python has different data types for variables, including:

  1. Numeric types: int (integer), float (floating-point number), complex (complex number)
  2. String type: str
  3. Boolean type: bool
  4. Sequence types: list, tuple, range
  5. Mapping type: dict
  6. Set types: set, frozenset
  7. Binary types: bytes, bytearray, memoryview
# Numeric types
x = 10        # int
y = 3.14      # float
z = 2 + 3j    # complex

# String type
name = "John Doe"

# Boolean type
is_admin = True

# Sequence types
my_list = [1, 2, 3]
my_tuple = (4, 5, 6)
my_range = range(5)

# Mapping type
my_dict = {'name': 'John', 'age': 25}

# Set types
my_set = {1, 2, 3}
my_frozenset = frozenset({4, 5, 6})

# Binary types
my_bytes = b"hello"
my_bytearray = bytearray(5)
my_memoryview = memoryview(bytes(5))

Conclusion

Variables are used to store values in memory and types are used to determine the kind of data that the value represents. By using variables and types correctly in Python, we can create more powerful and flexible programs.

Basic Operators in Python