banner
Zoney

Zoney

一个励志成为Web安全工程师的女青年!

python study 1

Introduction#

Python was developed by the famous "Uncle Turtle" Guido van Rossum in 1989.

Python is cross-platform, meaning it can run on Windows, Mac, and various Linux/Unix systems. A Python program written on Windows can also run on Linux.

Advantages
1 Python provides us with a very comprehensive standard library that covers a vast amount of content, including networking, file handling, GUI, databases, and text processing, which is vividly referred to as "batteries included." When developing with Python, many functionalities do not need to be written from scratch; they can be directly utilized from existing libraries.

2 In addition to the built-in libraries, Python has a large number of third-party libraries, which are developed by others and available for direct use.

Disadvantages
1 The execution speed is slow, especially compared to C programs, because Python is an interpreted language. Your code is translated line by line into machine code that the CPU can understand during execution, which is a time-consuming process. In contrast, C programs are compiled into machine code that the CPU can execute before running, making them much faster.
2 Code cannot be encrypted. If you want to publish your Python program, you are essentially publishing the source code, which is different from C language, where you do not need to publish the source code; you only need to distribute the compiled machine code (the xxx.exe file commonly seen on Windows). It is impossible to reverse-engineer machine code back into C code, so compiled languages do not have this issue, while interpreted languages must publish the source code.

Python Interpreter
When we write Python code, we get a text file with a .py extension containing the Python code. To run the code, we need the Python interpreter to execute the .py file.
Python interpreters:
CPython: After downloading and installing Python 3.x from the official Python website, we directly obtain an official version of the interpreter: CPython. This interpreter is developed in C, hence the name CPython. Running python in the command line starts the CPython interpreter.

IPython: An interactive interpreter based on CPython, meaning IPython enhances the interactive experience but has the same functionality for executing Python code as CPython.

PyPy: Another Python interpreter focused on execution speed. PyPy uses JIT technology to dynamically compile Python code (note that it is not interpreted), significantly improving the execution speed of Python code. Most Python code can run under PyPy, but there are some differences between PyPy and CPython, which may lead to different results when executing the same Python code in both interpreters. If your code is to be executed under PyPy, you need to understand the differences between PyPy and CPython.

Jython: A Python interpreter that runs on the Java platform, allowing Python code to be compiled directly into Java bytecode for execution.

IronPython: Similar to Jython, but IronPython runs on the Microsoft .Net platform and can compile Python code directly into .Net bytecode.

There are many Python interpreters, but the most widely used is CPython. If you need to interact with Java or .Net platforms, the best approach is not to use Jython or IronPython, but to interact via network calls to ensure the independence of each program.

Command Line Mode and Python Interactive Mode
In command line mode, you can execute python to enter the Python interactive environment, or you can execute python hello.py to run a .py file.
A .py file can only be executed in command line mode. Type the command python hello.py.

The Python interactive environment automatically prints the result of each line of Python code, but directly running Python code does not.

In Python interactive mode, the code is input one line at a time and executed one line at a time, while in command line mode, running a .py file executes all the code in that file at once. Thus, Python interactive mode is primarily used for debugging Python code and is convenient for beginners to learn; it is not a formal environment for running Python code!

Text Editor
Visual Studio Code: Developed by Microsoft, cross-platform.
Note: Do not use Word or the built-in Notepad in Windows. Word does not save plain text files, and Notepad may add a few special characters (UTF-8 BOM) at the beginning of the file, which can lead to inexplicable errors during program execution.
File names can only be a combination of letters, numbers, and underscores.

Directly running a .py file
Can a .py file be run directly like an .exe file? It cannot be done on Windows, but it can be done on Mac and Linux by adding a special comment at the beginning of the .py file:

#!/usr/bin/env python3

print('hello, world')

Then, give hello.py execution permissions with the command:
$ chmod a+x hello.py

You can then run hello.py directly.

When developing programs in Python, you can write code in a text editor while opening an interactive command window, allowing you to paste parts of the code into the command line for verification, making the process more efficient!

Input and Output
Output:
Using print() with a string in parentheses will output the specified text to the screen.
The print() function can also accept multiple strings separated by commas, which will be concatenated in the output:

print('The quick brown fox', 'jumps over', 'the lazy dog')

print() will print each string in turn, and when it encounters a comma, it will output a space, so the output string is concatenated as follows:

image

Input:
input() allows the user to input a string and store it in a variable. For example, to input the user's name:

>>>name = input()
Michael

When you input name = input() and press enter, the Python interactive command line will wait for your input. At this point, you can enter any characters and press enter to complete the input.

After input is complete, there will be no prompt, and the Python interactive command line will return to the >>> state. So where did the content we just entered go? The answer is that it is stored in the name variable. You can directly input name to check the variable's content:

>>> name
'Michael'

In computer programs, variables can hold not only integers or floats but also strings, so name as a variable is a string.

To print the content of the name variable, besides directly writing name and pressing enter, you can also use the print() function:

>>> print(name)
Michael

input() can also display a string to prompt the user:

name = input('please enter your name: ')
print('hello,', name)

Input is Input, and output is Output, so we collectively refer to input and output as Input/Output, or abbreviated as IO.

Python Basics#

Every programming language has its own syntax, and the compiler or interpreter is responsible for converting syntactically correct program code into machine code that the CPU can execute.

Python's syntax is relatively simple, using indentation:

# print absolute value of an integer:
a = 100
if a >= 0:
    print(a)
else:
    print(-a)

Statements starting with # are comments, which are for human readability and can contain any content; the interpreter will ignore comments. Every other line is a statement, and when a statement ends with a colon :, the indented statements are considered a code block.

Indentation has its pros and cons. The advantage is that it forces you to write formatted code, but there is no specification on whether the indentation should be a certain number of spaces or tabs. According to convention, you should always use 4 spaces for indentation.

The downside of indentation is that the "copy-paste" function becomes ineffective, which can be inconvenient. When refactoring code, pasted code must be checked for correct indentation. Additionally, IDEs find it difficult to format Python code like they do for Java.

Note that Python programs are case-sensitive; if you write the wrong case, the program will throw an error.

Data Types and Variables#

Computers can handle more than just numerical values; they can also process text, graphics, audio, video, web pages, and various types of data. Different data types need to be defined for different data. In Python, the following data types can be directly handled:

2.1 Integers
Python can handle integers of any size, including negative integers, represented in the same way as in mathematics, for example: 1, 100, -8080, 0, etc.

Since computers use binary, sometimes it is more convenient to represent integers in hexadecimal, which uses a 0x prefix and digits 0-9, a-f, for example: 0xff00, 0xa5b4c3d2, etc.

For very large numbers, such as 10000000000, it can be hard to count the number of zeros. Python allows underscores _ to be used as separators in numbers, so writing 10_000_000_000 is equivalent to 10000000000. Hexadecimal numbers can also be written as 0xa1b2_c3d4.

2.2 Floating-point Numbers
Floating-point numbers are decimals; they are called floating-point numbers because the position of the decimal point can vary when expressed in scientific notation. For example, 1.23x10^9 and 12.3x10^8 are completely equal. Floating-point numbers can be written in mathematical notation, such as 1.23, 3.14, -9.01, etc. However, for very large or very small floating-point numbers, scientific notation must be used, replacing 10 with e, so 1.23x10^9 becomes 1.23e9, or 12.3e8, and 0.000012 can be written as 1.2e-5, etc.

Integers and floating-point numbers are stored differently in the computer; integer operations are always exact (is division also exact? Yes!), while floating-point operations may have rounding errors.

2.3 Strings
Strings are any text enclosed in single quotes ' or double quotes ", such as 'abc', "xyz", etc. Note that '' or "" is merely a representation and not part of the string itself; thus, the string 'abc' consists only of the characters a, b, and c. If ' itself is a character, it can be enclosed in "", for example, "I'm OK" contains the characters I, ', m, space, O, K, totaling 6 characters.

What if the string contains both ' and "? You can use the escape character \ to indicate, for example:

'I\'m \"OK\"!'

The string content represented is:
I'm "OK"!

The escape character \ can escape many characters, such as \n for newline, \t for tab, and the character \ itself must also be escaped, so \\ represents the character .

If there are many characters in the string that need to be escaped, it requires adding many . To simplify, Python also allows using r'' to indicate that the string inside '' is not escaped by default.

>>> print('\\\t\\')
\       \
>>> print(r'\\\t\\')
\\\t\\

If a string contains many newlines, writing them in one line with \n is not easy to read. To simplify, Python allows using the '''...''' format to represent multi-line content.

>>> print('''line1
... line2
... line3''')
line1
line2
line3

The above is input in the interactive command line. Note that when inputting multi-line content, the prompt changes from >>> to ..., indicating that you can continue inputting from the previous line. Note that ... is a prompt, not part of the code. When you finish inputting and close the quotes and parentheses, the statement is executed and the result is printed.

If written as a program and saved as a .py file, it would be:

print('''line1
line2
line3''')

Multi-line strings '''...''' can also be prefixed with r:

print(r'''hello,\n
world''')
hello,\n
world

2.4 Boolean Values
Boolean values correspond directly to Boolean algebra, with only True and False as possible values; it can either be True or False. In Python, you can directly use True and False to represent Boolean values (note the case), or they can be calculated through Boolean operations:

>>> True
True
>>> False
False
>>> 3 > 2
True
>>> 3 > 5
False

Boolean values can be operated with and, or, and not.

The and operation is a conjunction; the result is True only if all operands are True.

The or operation is a disjunction; the result is True if at least one operand is True.

The not operation is a negation; it is a unary operator that turns True into False and vice versa.

2.5 None
None is a special value in Python, represented by None. None should not be understood as 0, because 0 has meaning, while None is a special empty value.

Additionally, Python provides various data types such as lists and dictionaries, and allows for the creation of custom data types.

Variables
In a program, a variable is represented by a variable name, which must be a combination of uppercase and lowercase letters, numbers, and underscores, and cannot start with a number.

In Python, the equals sign = is an assignment statement, allowing any data type to be assigned to a variable. The same variable can be reassigned multiple times, and it can be of different types.

Languages where the type of a variable is not fixed are called dynamic languages, in contrast to static languages. Static languages require specifying the variable type when defining a variable; if the type does not match during assignment, an error will occur. For example, Java is a static language, and the assignment statement is as follows (// indicates a comment):

int a = 123; // a is an integer variable
a = "ABC"; // Error: cannot assign a string to an integer variable

Understanding how variables are represented in computer memory is also very important.

a = 'ABC'

The Python interpreter does two things:

It creates a string 'ABC' in memory;

It creates a variable named a in memory and points it to 'ABC'.

a = 'ABC'
b = a
a = 'XYZ'
print(b)

Executing a = 'ABC' creates the string 'ABC' and the variable a, pointing a to 'ABC':

image

Executing b = a creates the variable b and points b to the string 'ABC' that a points to:

image

Executing a = 'XYZ' creates the string 'XYZ' and changes a's reference to 'XYZ', but b remains unchanged:

image

Thus, the final printed result of variable b is naturally 'ABC'.

Constants
A constant is a variable that cannot change, such as the commonly used mathematical constant π. In Python, constants are usually represented by variable names in uppercase:

PI = 3.14159265359

However, in reality, PI is still a variable; Python has no mechanism to ensure that PI will not be changed, so using uppercase variable names to represent constants is merely a convention. If you must change the value of the variable PI, no one can stop you.

To explain why integer division is also exact: In Python, there are two types of division, one is /:

>>> 10 / 3
3.3333333333333335

The / division yields a floating-point number, even if two integers divide evenly, the result is still a floating-point number:

>>> 9 / 3
3.0

The other type of division is //, known as floor division, where the division of two integers remains an integer:

>>> 10 // 3
3

You are not mistaken; the floor division // of integers is always an integer, even if it does not divide evenly. To perform exact division, use /.

Since // division only takes the integer part of the result, Python also provides a modulus operation %, which can yield the remainder of two integers divided:

>>> 10 % 3
1

Whether performing // division or modulus on integers, the result is always an integer, so integer operations are always exact.

Python supports various data types, and internally, any data can be viewed as an "object," while variables are used in programs to point to these data objects. Assigning a value to a variable x = y means that variable x points to the actual object that variable y points to. Subsequent assignments to variable y do not affect the reference of variable x.

Note: Python integers have no size limit, while integers in some languages have size limits based on their storage length; for example, Java limits 32-bit integers to the range of -2147483648 to 2147483647.

Python floating-point numbers also have no size limit, but exceeding a certain range will directly represent as inf (infinity).

List and Tuple#

List
Python has a built-in data type called list. A list is an ordered collection that allows you to add and remove elements at any time.

For example, to list all the names of classmates, you can use a list:

>>> classmates = ['Michael', 'Bob', 'Tracy']
>>> classmates
['Michael', 'Bob', 'Tracy']

The variable classmates is a list. You can use the len() function to get the number of elements in the list:

>>> len(classmates)
3

You can access each element in the list using indexing, remembering that indexing starts from 0:

>>> classmates[0]
'Michael'
>>> classmates[1]
'Bob'
>>> classmates[2]
'Tracy'
>>> classmates[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

When the index exceeds the range, Python will raise an IndexError, so ensure that the index does not go out of bounds; remember that the index of the last element is len(classmates) - 1.

To access the last element, you can use -1 as the index, directly retrieving the last element:

>>> classmates[-1]
'Tracy'

A list is a mutable ordered collection, so you can append elements to the end of the list:
list.append('')

>>> classmates.append('Adam')
>>> classmates
['Michael', 'Bob', 'Tracy', 'Adam']

list.insert(num,'')
You can also insert elements at a specified position, such as at index 1:

>>> classmates.insert(1, 'Jack')
>>> classmates
['Michael', 'Jack', 'Bob', 'Tracy', 'Adam']

list.pop()
To remove the last element from the list, use the pop() method:

>>> classmates.pop()
'Adam'
>>> classmates
['Michael', 'Jack', 'Bob', 'Tracy']

list.pop(i)
To remove an element at a specified position, use pop(i), where i is the index:

>>> classmates.pop(1)
'Jack'
>>> classmates
['Michael', 'Bob', 'Tracy']

To replace an element with another, you can directly assign a value to the corresponding index:

>>> classmates[1] = 'Sarah'
>>> classmates
['Michael', 'Sarah', 'Tracy']

The elements in a list can also have different data types, for example:

>>> L = ['Apple', 123, True]

List elements can also be another list, for example:

>>> s = ['python', 'java', ['asp', 'php'], 'scheme']
>>> len(s)
4

Note that s has only 4 elements, where s[2] is another list. If written separately, it would be easier to understand:

>>> p = ['asp', 'php']
>>> s = ['python', 'java', p, 'scheme']

To access 'php', you can write p[1] or s[2][1], so s can be seen as a two-dimensional array, and similarly, there can be three-dimensional, four-dimensional... arrays, though they are rarely used.

If a list has no elements, it is an empty list with a length of 0:

>>> L = []
>>> len(L)
0

Tuple
Another ordered list is called a tuple. Tuples are very similar to lists, but once a tuple is initialized, it cannot be modified. For example, to list classmates' names:

>>> classmates = ('Michael', 'Bob', 'Tracy')

Now, the classmates tuple cannot change; it does not have append() or insert() methods. Other methods for accessing elements are the same as for lists; you can normally use classmates[0], classmates[-1], but you cannot assign different elements.

What is the significance of an immutable tuple? Because tuples are immutable, the code is safer. If possible, use tuples instead of lists.

A pitfall of tuples: When defining a tuple, the elements of the tuple must be determined at the time of definition. For example:

>>> t = (1, 2)
>>> t
(1, 2)

To define an empty tuple, you can write it as ():

>>> t = ()
>>> t
()

However, to define a tuple with only one element, if you define it like this:

>>> t = (1)
>>> t
1

What is defined is not a tuple, but the number 1! This is because parentheses () can represent both tuples and mathematical expressions, leading to ambiguity. Therefore, Python specifies that in this case, it will be treated as a mathematical calculation, resulting in 1.

Thus, to define a tuple with only one element, you must add a comma to eliminate ambiguity:

>>> t = (1,)
>>> t
(1,)

When displaying a tuple with only one element, Python will also add a comma to prevent misunderstanding as mathematical parentheses.

Finally, let's look at a "mutable" tuple:

>>> t = ('a', 'b', ['A', 'B'])
>>> t[2][0] = 'X'
>>> t[2][1] = 'Y'
>>> t
('a', 'b', ['X', 'Y'])

This tuple was defined with 3 elements: 'a', 'b', and a list. It is not that a tuple cannot change after being defined; rather, the elements of the tuple are not changing, but the elements of the list are. The tuple initially points to the list, which has not changed to point to another list, so the tuple's so-called "immutability" means that the reference of each element in the tuple does not change. If it points to 'a', it cannot change to point to 'b'; if it points to a list, it cannot change to point to another object, but the list it points to can be mutable!

Understanding "the reference does not change" means that to create a tuple whose content also does not change, each element of the tuple must also be immutable.

Lists and tuples are Python's built-in ordered collections, one mutable and one immutable.

Conditional Statements#

According to Python's indentation rules, if the if statement evaluates to True, the two indented print statements are executed. If the if condition is False, the else block is executed.
Be careful not to forget the colon:

age = 3
if age >= 18:
    print('your age is', age)
    print('adult')
else:
    print('your age is', age)
    print('teenager')

elif is short for else if, and there can be multiple elif statements.
The execution characteristic of the if statement is that it checks from top to bottom; if a condition is True, it executes the corresponding statement and ignores the remaining elif and else blocks.

if <condition1>:
    <execute1>
elif <condition2>:
    <execute2>
elif <condition3>:
    <execute3>
else:
    <execute4>

The if condition can also be simplified, for example:

if x:
    print('True')

As long as x is a non-zero number, a non-empty string, a non-empty list, etc., it is considered True; otherwise, it is False.

Revisiting input
TypeError

birth = input('birth: ')
if birth < 2000:
    print('00前')
else:
    print('00后')

Inputting 1982 results in an error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() > int()

This is because input() returns a string data type, and strings cannot be directly compared to integers; you must first convert the string to an integer. Python provides the int() function to accomplish this.

The int() function will raise an error and exit the program if it finds that a string is not a valid number:

s = input('birth: ')
birth = int(s)
if birth < 2000:
    print('00前')
else:
    print('00后')

Running it again will yield the correct result. However, if you input abc, you will get an error message:
ValueError

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'abc'

Loops#

for...in loop
Python has two types of loops, one is the for...in loop, which iterates through each element in a list or tuple.

names = ['Michael', 'Bob', 'Tracy']
for name in names:
    print(name)

Executing this code will print each element in names:

Michael
Bob
Tracy

So the for x in ... loop assigns each element to the variable x and then executes the indented block of statements.

The range() function can generate a sequence of integers, which can be converted to a list using the list() function. For example, range(5) generates a sequence of integers starting from 0 and less than 5:

>>> list(range(5))
[0, 1, 2, 3, 4]

while loop
The second type of loop is the while loop, which continues looping as long as the condition is met; it exits when the condition is no longer met.

break
In a loop, the break statement can exit the loop early:

n = 1
while n <= 100:
    if n > 10: # When n = 11, the condition is met, and the break statement is executed
        break # The break statement will end the current loop
    print(n)
    n = n + 1
print('END')

Executing the above code will print numbers from 1 to 10, followed by printing END, and the program ends.

continue
During the loop, you can also use the continue statement to skip the current iteration and directly start the next iteration:

n = 0
while n < 10:
    n = n + 1
    if n % 2 == 0: # If n is even, execute the continue statement
        continue # The continue statement will directly continue to the next loop iteration, and the subsequent print() statement will not execute
    print(n)

The break statement can exit the loop directly, while the continue statement can end the current loop iteration early and start the next iteration. These two statements are usually used in conjunction with if statements.

Be particularly careful not to misuse break and continue statements.

An "infinite loop" is one that continues indefinitely. You can exit the program using Ctrl+C or forcibly terminate the Python process.

dict and set#

dict
Python has built-in support for dictionaries: dict, which stands for dictionary, also known as map in other languages. It uses key-value storage and has extremely fast lookup speeds.

Using dict, you only need a "name"-"score" correspondence table, allowing you to directly look up scores based on names, regardless of how large the table is, the lookup speed remains constant.

>>> d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}
>>> d['Michael']
95

Why is dict lookup speed so fast? Because the implementation principle of dict is similar to looking up a dictionary. Suppose the dictionary contains 10,000 Chinese characters, and we want to find a specific character. One method is to flip through the dictionary from the first page until we find the character we want, which is how we look for elements in a list; the larger the list, the slower the search.

The second method is to first check the index table of the dictionary (like the radical table) to find the page number corresponding to the character, and then directly flip to that page to find the character. Regardless of which character we are looking for, this lookup speed is very fast and does not slow down with the increase in the size of the dictionary.

Dict uses the second implementation method; given a name, such as 'Michael', dict can internally calculate the "page number" where Michael's score is stored, which is the memory address where the number 95 is stored, allowing for direct retrieval, hence the speed is very fast.

You can guess that this key-value storage method requires that when inserting data, the position of the value must be calculated based on the key; this way, when retrieving, it can directly access the value based on the key.

To add data to dict, besides specifying it during initialization, you can also add it using the key:

>>> d['Adam'] = 67
>>> d['Adam']
67

Since a key can only correspond to one value, assigning a value to a key multiple times will overwrite the previous value:

>>> d['Jack'] = 90
>>> d['Jack']
90
>>> d['Jack'] = 88
>>> d['Jack']
88

If a key does not exist, dict will raise an error: KeyError

>>> d['Thomas']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'Thomas'

To avoid the error of a non-existent key, there are two methods:
One is to check if the key exists using in:

>>> 'Thomas' in d
False

The other is to use the get() method provided by dict, which returns None if the key does not exist, or a specified value:

>>> d.get('Thomas')
>>> d.get('Thomas', -1)
-1

Note: When returning None, the Python interactive environment does not display the result.

To delete a key, use the pop(key) method, which will also remove the corresponding value from dict:

>>> d.pop('Bob')
75
>>> d
{'Michael': 95, 'Tracy': 85}

Please note that the order in which keys are stored internally in dict does not relate to the order in which they were added.

Compared to lists, dict has the following characteristics:
1 Fast lookup and insertion speeds that do not slow down with an increase in keys;
2 Requires a large amount of memory, leading to more memory waste.

In contrast, lists have:
1 Lookup and insertion times that increase with the number of elements;
2 Smaller memory usage, leading to less memory waste.
Thus, dict is a method of trading space for time.

Dict can be used in many places where fast lookups are needed, and it is essential to use dict correctly. The first rule to remember is that the key of a dict must be an immutable object.

This is because dict calculates the storage position of the value based on the key; if the result of calculating the same key varies each time, the internal structure of dict will become chaotic. This algorithm for calculating the position based on the key is called a hash algorithm.

To ensure the correctness of the hash, the object used as a key must not change. In Python, strings, integers, etc., are immutable, so they can safely be used as keys. However, lists are mutable and cannot be used as keys:

>>> key = [1, 2, 3]
>>> d[key] = 'a list'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

Set
A set is similar to dict; it is a collection of keys but does not store values. Since keys cannot be duplicated, there are no duplicate keys in a set.

To create a set, you need to provide a list as the input collection:

>>> s = set([1, 2, 3])
>>> s
{1, 2, 3}

Note that the input parameter [1, 2, 3] is a list, while the displayed {1, 2, 3} only indicates that this set contains the elements 1, 2, and 3; the displayed order does not indicate that the set is ordered.

Duplicate elements in a set are automatically filtered:

>>> s = set([1, 1, 2, 2, 3, 3])
>>> s
{1, 2, 3}

You can add elements to a set using the add(key) method; you can add the same element multiple times, but it will have no effect:

>>> s.add(4)
>>> s
{1, 2, 3, 4}
>>> s.add(4)
>>> s
{1, 2, 3, 4}

You can remove elements using the remove(key) method:

>>> s.remove(4)
>>> s
{1, 2, 3}

A set can be seen as a unordered and unique collection of elements, so two sets can perform mathematical operations like intersection and union:

>>> s1 = set([1, 2, 3])
>>> s2 = set([2, 3, 4])
>>> s1 & s2
{2, 3}
>>> s1 | s2
{1, 2, 3, 4}

The only difference between sets and dicts is that sets do not store corresponding values. However, the principles of sets are the same as those of dicts, so they also cannot contain mutable objects because it would be impossible to determine whether two mutable objects are equal, thus ensuring that there are "no duplicate elements" in the set.

Revisiting Immutable Objects

For mutable objects like lists, performing operations on a list will change its contents, for example:

>>> a = ['c', 'b', 'a']
>>> a.sort()
>>> a
['a', 'b', 'c']

But for immutable objects like strings, what happens when you perform operations on a string:

>>> a = 'abc'
>>> a.replace('a', 'A')
'Abc'
>>> a
'abc'

Although the string has a replace() method that indeed produces 'Abc', the variable a remains 'abc'. How should this be understood?

Let's modify the code like this:

>>> a = 'abc'
>>> b = a.replace('a', 'A')
>>> b
'Abc'
>>> a
'abc'

Always remember that a is a variable, while 'abc' is the string object! Sometimes we say that the content of object a is 'abc', but we actually mean that a is a variable pointing to the object whose content is 'abc':

a-------->'abc'

When we call a.replace('a', 'A'), we are actually calling the method replace on the string object 'abc'. Although the method is named replace, it does not change the content of the string 'abc'. Instead, the replace method creates a new string 'Abc' and returns it. If we use variable b to point to this new string, it becomes easier to understand: variable a still points to the original string 'abc', while variable b points to the new string 'Abc':

a-------->'abc'
b-------->'Abc'

Thus, for immutable objects, calling any method on the object itself will not change the content of that object. Instead, these methods will create new objects and return them, ensuring that the immutable object itself remains immutable.

Using the key-value storage structure of dict is very useful in Python, and it is crucial to choose immutable objects as keys. The most commonly used key is a string.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.