Python study notes 1

This article is a study note, learning resource: Python tutorial on Liao Xuefeng's official website https://www.liaoxuefeng.com/
1.
Notes and statements: Statements
starting with "#" are comments, and comments are for people to see , can be anything, the interpreter ignores comments. Every other line is a statement, and when the statement ends with a colon ":", the indented statement is treated as a code block.
2. Python programs are case-sensitive, and if they are written incorrectly, the program will report an error.
2. Data types
1. Integer
2. Floating-point numbers
Integers and floating-point numbers are stored in different ways in the computer. Integer operations are always accurate (division is also accurate!), while floating-point operations may have rounding errors. .
3. String
What if the string contains both ' and "? You can use the escape character \ to identify the
eg 'I\'m \"OK\"!'
output result: I'm "OK"! The
escape character \ can escape many characters, such as \n for Newline, \t means tab character, and the character \ itself also needs to be escaped, so the character represented by \ is \
If there are many characters in the string that need to be escaped, you need to add a lot of \, in order to simplify, Python also allows r The string inside "means" is not escaped by default.
If , it is not easy to read with \n in one line. To simplify, Python allows the format of "'..."' to represent multi-line content
. 4. Boolean Value
5. Null value
Null value is a special value in Python, represented by None. None cannot be understood as 0, because 0 is meaningful, and None is a special null value.
6. Other data types
3. Variables and constants
1./ The result of division calculation is a floating-point number, even if two integers are exactly divided, the result is a floating -point number
2.//, called floor division, the division of two integers is still an integer:
3. Because / /Division only takes the integer part of the result, so Python also provides a remainder operation%
***Python supports a variety of data types. Inside the computer, 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 is to associate the data with the variable.
4.
1. Encoding
UTF-8 encoding encodes a Unicode character into 1-6 bytes according to different number sizes, commonly used English letters are encoded into 1 byte, Chinese characters are usually 3 bytes, only very rare characters will be encoded into 4-6 bytes. If the text you want to transmit contains a lot of English characters, you can save space with UTF-8 encoding
. 2. Formatting
Placeholders are the same as C.
Five, list and tuple
list
One of Python's built-in data types is a list: list. A list is an ordered collection from which elements can be added and removed at any time.
Use the index to access the element at each position in the list, remember that the index starts from 0.
If you want to get the last element, in addition to calculating the index position, you can also use -1 as the index to get the last element directly.
Insert
You can append elements to the list to the end

>>> classmates.append('ahs')
>>> classmates
['AA', 'BB', 'CC', 'ahs']

You can also insert an element at a specified position

>>> classmates.insert(1, 'jj')
>>> classmates
['AA', 'jj','BB', 'CC', 'ahs']

Delete
To delete an element at the end of the list, use the pop() method:

>>> classmates.pop()
'ahs'
>>> classmates
['AA', 'jj','BB', 'CC']

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

>>> classmates.pop(2)
'jj'
>>> classmates
['AA', 'BB', 'CC']

To replace an element with another element, you can directly assign it to the corresponding index position

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

The data type of the elements in the list can also be different. The
list element can also be another list
tuple
. Another ordered list is called a tuple: tuple. Tuple is very similar to list, but tuple cannot be modified once initialized;
there is no append() and insert();
if you want to define an empty tuple, you can write ();
define a tuple with only 1 element

>>>tt=('aaa',)
>>>tt
>>>'aaa'


6. Use dict and set 1.dict Python
has
a built-in dictionary: the support of dict, the full name of dict is dictionary, also known as map in other languages, using key-value (key-value) storage, with extremely fast search speed.
The method of putting data into dict :
specify when initializing:

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

Put in by key:

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

*A key can only correspond to one value, put a value into a key multiple times, and the later value will flush out the previous value.
Determine whether key exists :
1.

`>>> 'Thomas' in d
False`

2.

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

delete

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

The order in which the dict is stored has nothing to do with the order in which the keys are placed! ! ! !
In Python, strings, integers, etc. are immutable, so they can safely be used as keys. And list is mutable, so it cannot be used as key
2.set
set is similar to dict, it is also a set of keys, but does not store value. Since keys cannot be repeated, there are no repeated keys in the set.
create

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

Repeated elements are automatically filtered in the set, and the displayed results do not represent
order
. Adding elements can be added to the set through the add(key) method, and a number or character can be added repeatedly, but it will not have any effect.

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

delete

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

A set can be regarded as a collection of unordered and non-repetitive elements in a mathematical sense. Therefore, two sets can perform operations such as intersection and union in a mathematical sense.

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

Hit the point! ! ! !

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325730261&siteId=291194637