The reason for learning Python is that I want to write some scripts or programs to scrape the text, images, and other materials I want from the browser. There are actually many programs like this on GitHub, but unfortunately, I am completely unfamiliar with backend languages, and I wouldn't know how to use them if I just took them and used them directly.
Therefore, I had the idea of learning the basics of Python first. I have always heard that Liao Xuefeng's Python tutorial is well-written, so I will take this opportunity to study it carefully. I don't expect to become extremely skilled, but at least I hope to add a little brilliance to my shallow technical stack. After all, I once dreamed of becoming a full-stack developer.
After downloading and installing the Python environment, let's start learning!
Running My First Python Program#
print('hello,world')
Data Types#
In Python, there are several types of data that can be directly processed, including integers, floating-point numbers, strings, Boolean values, null values, and variables.
This is not much different from other languages. The data types are basically similar. Only variables have their own characteristics, so I will record them separately.
Variables#
Python is a dynamic language, which means that it can be changed continuously during assignment. For example:
a = 123
print(a) # Output: 123
a = 'abc'
print(a) # Output: abc
This is allowed in Python, but not in Java. Java is a static language, and if you assign a value multiple times, it will throw an error. Relatively speaking, dynamic languages are more flexible, but each has its own advantages and disadvantages.
In addition, there is a logical order in assignment. For example, when defining a variable a = 'a'
, Python actually takes two steps: first, it creates a string a
, and then assigns this string to the variable a
.
In fact, this is somewhat illogical in mathematics. The calculation x = x + 2
is not feasible in mathematics, but in computers, it first calculates x + 2
, and then assigns it to the left side x
. This is the logic of computers.
x = 1
x = x + 2
print(x) # Output: 3
Many computer languages are like this, such as the well-known JavaScript, and so on.
If there are multiple variables that need to be assigned to each other, they are executed line by line.
a = 1
b = a
a = 2
print(a)
print(b)
The final output results are 2
and 1
, respectively.
Python supports multiple data types. Internally, any data can be seen as an "object", and variables are used in programs to point to these data objects. Assigning a value to a variable
x = y
means that the variablex
points to the real object, which is pointed to by the variabley
. Subsequent assignments to the variabley
do not affect the pointer of the variablex
.
Note: Python integers have no size limit, while integers in some languages have size limits based on their storage length. For example, Java limits the range of 32-bit integers to -2147483648-2147483647.
Python's floating-point numbers also have no size limit, but if they exceed a certain range, they will be directly represented as inf (infinity). --- "Liao Xuefeng's Official Website"
Strings and Encoding#
Python provides two attributes for handling encoding, ord
and chr
.
ord('舒')
# 33298
# Convert the string to an integer representation
chr(33298)
# '舒'
# Convert the integer representation to a string
List#
list
is similar to Array
in JavaScript, which is a list of multiple data. The syntax is as follows:
>>> classmates = ['a', 'b', 'c']
>>> classmates
['a', 'b', 'c']
Now we can say that the variable classmates
is a list.
There is a len()
function that can output the number of elements in the list.
>>> len(classmates)
>>> 3
Lists also have indexes, starting from 0, and the last index is len(classmates)-1
. If you want to directly output the last element, you can use classmates[-1]
.
>>> classmates[-1]
>>> 'c'
Since -1
can represent the last element, can -2
be used to represent the second to last element? The answer is yes.
>>> classmates[-2]
>>> 'b'
In addition, it should be noted that the index must not exceed the range when using indexes, otherwise an error will be thrown as shown below.
>>> classmates[4]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
list is a mutable ordered list, and elements can be added or deleted from it.
- append: add an element to the end
>>> classmates.append('d')
>>> classmates
['a', 'b', 'c', 'd']
- insert: add an element at a specified position
In this case, 1
is the index, which means inserting the element at index 1, and the following elements will be shifted accordingly.
>>> classmates.insert(1, 'd')
>>> classmates
['a', 'd', 'b', 'c']
- pop: delete the last element
>>> classmates.pop()
'c' # The deleted element when outputting
>>> classmates
['a', 'b']
- pop(i): delete the element at a specified position
>>> classmates.pop(1)
'b' # The deleted element when outputting
>>> classmates
['a', 'c']
>>>