zishu's blog

zishu's blog

一个热爱生活的博主。https://zishu.me

My Python study notes

The reason for learning Python is originally to write some scripts or programs to crawl the text, images, and other materials that I want in the browser. There are actually many such programs on GitHub, but unfortunately I am completely unfamiliar with backend languages, and I don't know how to use them directly if I take them over.

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 didn't expect to be very powerful, but at least I hope to add some 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, and only variables have their own characteristics, so I will record them separately.

Variables#

Python is a dynamic language, and it can be changed continuously when assigning values, for example:

a = 123
print(a) # Output the value 123 to the terminal
a = 'abc'
print(a) # Output the string 'abc' to the terminal

This is allowed, but in Java, it is not allowed. Java is a static language, and it will report an error if assigned multiple times. Relatively speaking, dynamic languages are more flexible, but each has its own advantages and disadvantages.

And there is a logical order in assigning values. For example, defining a variable a = 'a'. In Python, it actually takes two steps, first creating a string a, and then assigning 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, first calculate x + 2, and then assign it to the left x. This is the logic of the computer.

x = 1
x = x + 2
print(x) # 3

In fact, 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 regarded as an "object", and variables are used in the program to point to these data objects. Assigning a value to a variable x = y means that the variable x points to the real object, which is pointed to by the variable y. Subsequent assignments to the variable y do not affect the pointing of the variable x.
Note: Python integers have no size limit, while integers in some languages ​​have size limits based on their storage length. For example, the range of 32-bit integers in Java is limited to -2147483648-2147483647.
Python's floating-point numbers also have no size limit, but if they exceed a certain range, they are directly represented as inf (infinity).---"Liao Xuefeng's Official Website"

Strings and Encoding#

Python provides two properties 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']

At this time, 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

At the same time, the list also has 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 when using indexes, you cannot exceed the range, otherwise the following error will be reported.

>>> 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.

  1. append adds an element to the end
>>> classmates.append('d')
>>> classmates
['a', 'b', 'c', 'd']
  1. insert adds an element at the specified position

Where 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']
  1. pop deletes the last element
>>> classmates.pop()
'c' # The deleted element when outputting

>>> classmates
['a', 'b']
  1. pop(i) deletes the element at the specified position
>>> classmates.pop(1)
'b' # The deleted element when outputting

>>> classmates
['a', 'c']
>>> 
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.