In the Python programming language, variables are containers for storing data values. They are fundamental to programming as they allow us to store and manipulate data. This article will take you deep into the concepts of variables in Python, their types, and how to use them.
Variable Naming
Correctly using variables is crucial for writing clear and maintainable code. By appropriately naming and organizing your variables in code, you can enhance the readability and efficiency of your code.When using variables in Python, you need to follow naming rules:
-
The name must start with a letter or an underscore.
-
The name can only contain letters, numbers, and underscores.
-
You cannot use Python keywords as names.
Naming Principles: Meaningful Names
-
Underscore: english_score
-
Lower Camel Case: englishScore
-
Upper Camel Case: EnglishScore
Defining Variables
Python is a dynamically typed language, which means you do not need to declare the type of a variable beforehand. As shown in the code below, you do not need to specify its data type; it is determined by the first assignment.
score = 100 # Defines an integer variable named score
sum = 15.7 # Defines a float variable
name = "amos" # Defines a string variable
numbers = [1, 2, 3] # Creates a list
number_dict = {"one": 1, "two": 2} # Creates a dictionary
Variable Types
In programming, data types are an important concept. Variables can store different types of data, and different types can perform different operations. Among these categories, Python has the following built-in data types by default:
Text Type | <span>str</span> |
Numeric Types | <span>int, </span><code><span>float, </span> <span>complex</span> |
Sequence Types | <span>list, </span><code><span>tuple, </span> <span>range</span> |
Mapping Type | <span>dict</span> |
Set Types | <span>set, </span><code><span>frozenset</span> |
Boolean Type | <span>bool</span> |
Binary Types | <span>bytes, </span><code><span>bytearray, </span> <span>memoryview</span> |
1
Immutable Data Types (Object content cannot be modified)
1. Numeric Types
<span>int</span>
: Integers, such as
a = 10
<span>float</span>
: Floating-point numbers, such as
b = 3.14
<span>complex</span>
: Complex numbers, such as
c = 2 + 3j
2. Strings (<span>str</span>
)
A sequence of characters,immutable, supports slicing, concatenation, and other operations. For example:
s = "hello"
3. Tuples (<span>tuple</span>
)
An ordered immutable sequence,can contain elements of different types; if the elements are mutable objects (like lists), their contents can be modified. For example:
t = (1, "a", True)
4. Booleans (<span>bool</span>
)
Values are <span>True</span>
or <span>False</span>
, which is a subclass of <span>int</span>
(<span>True</span>
is 1, <span>False</span>
is 0).
5. Bytes (<span>bytes</span>
)
An immutable sequence of bytes, such as
b = b"bytes"
6. Frozensets (<span>frozenset</span>
)
An immutable set, such as
fs = frozenset([1, 2, 3])
2
Mutable Data Types (Object content can be modified)
1. Lists (<span>list</span>
)
An ordered mutable sequence,supports add, delete, modify, and query operations (such as <span>append()</span>
, <span>pop()</span>
). For example:
li = [1, 2, 3]
2. Dictionaries (<span>dict</span>
)
A collection of key-value pairs,keys must be of immutable types (such as <span>str</span>
, <span>int</span>
), values can be of any type. For example:
d = {"name": "Alice", "age": 25}
3. Sets (<span>set</span>
)
Unordered and unique elements,elements must be of immutable types (cannot contain lists or dictionaries). For example:
s = {1, 2, 3}
4. Byte Arrays (<span>bytearray</span>
)
A mutable sequence of bytes, such as
ba = bytearray(b"hello")
3
Special Types
<span>NoneType</span>
: The only value is <span>None</span>
, representing a null or missing value. For example:
x = None
Type Conversion Functions
# Convert data to corresponding types
int()
float()
str()
list()
tuple()
set()