Python
Python
In Python, data types define the type of value that a variable holds. These data
types are essential because they help Python interpret what kind of operations can
be performed on a particular variable and how it is stored in memory.
1. Numeric Types
These represent numbers and are the most basic types in Python.
x = 5 # integer
y = -10 # negative integer
float (Floating Point): Represents numbers with decimal points. For example:
x = 5.5 # float
y = -10.2 # negative float
z = 0.0 # float
x = 3 + 4j # complex number
y = 1 - 2j # complex number
2. Sequence Types
4. Mapping Type
5. Set Types
s = {1, 2, 3, 4}
fs = frozenset([1, 2, 3, 4])
6. Boolean Type
bool: Represents either True or False. It is used to store Boolean values and
is commonly used in conditional statements.
a = True
b = False
7. Binary Types
b = b'Hello'
bytearray: Mutable sequence of bytes, which is like a list of bytes.
mv = memoryview(b'Hello')
8. None Type
None: This represents the absence of a value or a null value. It is often used
to represent uninitialized variables or as a placeholder.
Python provides several functions to convert one data type into another. Here are
some common type conversion functions:
Example:
2. Dynamic Typing: Python is dynamically typed, which means that you don't
need to declare the type of a variable when you create it. The type is
determined automatically based on the value assigned to the variable.
Example:
x=5 # x is an integer
x = "Hello" # Now x is a string
3. Variable Naming Rules: When naming variables in Python, you must follow
a few rules:
o A variable name can only contain letters (a-z, A-Z), digits (0-9), and
underscores (_).
o A variable name cannot start with a digit.
o Python is case-sensitive, so myVar and myvar are two different
variables.
o Reserved words (keywords like if, for, while, etc.) cannot be used as
variable names.
my_var = 10
myVar = 20
_myVar = 30
var_2 = 40
Similarly, you can assign the same value to multiple variables at once:
python
Copy
a = b = c = 100 # All three variables will have the value 100
Example:
def example_function():
local_var = 10 # Local variable
print(local_var) # Prints the local variable
Example:
# Immutable
x = 10
x = x + 5 # This creates a new integer, rather than modifying the existing
one
# Mutable
lst = [1, 2, 3]
lst.append(4) # The list is modified in place
PI = 3.14159
MAX_USERS = 100
Even though these are considered "constants," the values could still
technically be changed in the code.
8. Variable Scope: The scope of a variable determines where in the code the
variable can be accessed. There are two main types of scope:
o Local Scope: A variable that is defined inside a function can only be
accessed inside that function.
o Global Scope: A variable that is defined outside of any function can
be accessed from anywhere in the code.
Example:
def my_function():
local_variable = 10 # Local variable
print(local_variable) # Accessing local variable inside function
my_function()
print(global_variable) # Accessing global variable outside function
# print(local_variable) # Error: local_variable is not accessible here
9. The del Keyword: The del keyword is used to delete a variable, meaning it
removes the variable from the memory.
Example:
x=5
del x # Deletes the variable x
# print(x) # Error: x is no longer defined
11.Type Checking: You can check the type of a variable using the type()
function to understand the type of value it holds.
a = 10
print(type(a)) # <class 'int'>
b = "hello"
print(type(b)) # <class 'str'>
Summary: