Chapter 2 - Python Basics

Front

Variables: letters, numbers, underscores, etc., do not use keywords

python is case sensitive

Comments are handled with #

list

You can use positive and negative indexes to access elements in the list (the absolute value of the difference between the positive and negative indexes is the list size)

slice access

list[start:end]: Returns a list of left-closed and right-open intervals from index start to index end [start, end)

list[start:end:step]: Returns a list of left-open and right-closed intervals from index start to index end with a step size of step

  1. start can be omitted if the slice starts at 0
  2. If the slice ends at the last element, end can be omitted
  3. If the slice step is 1, you can omit: step

list add list.append(XXX)

list delete del list[index]

Supports the addition of multiple lists to generate a new large list, similar to list accumulation

Support list multiplication by integer to form a new list (multiplier<1, return an empty list, multiplier==1, return a new list with the same elements of the original list, multiplier greater than 1, it is analogous to the accumulation of multiple lists)

len(list): returns the number of elements in the list

Tuple

It is similar to a list in use, enclosed in parentheses, the biggest difference is that the ancestor element cannot be modified

Yuanzu uses [] instead of () when taking values ​​and slicing functions

Dictionary (Dict)

kv storage method, {aa:AA,bb:BB}, the value is obtained through the key key, and the method of obtaining the value dir[key], yes, also through [], returns the value if it exists, and returns None if it does not exist

Addition and modification: dir[key]=new_value If there is one, change it and if not, add it

Delete: del dir[key]

Traverse keys: dirc.keys()

Traversing values: dirc.values()

Circumstances kv: for k,v in dirc.items()

other

range(): range, which returns a collection

range(5)==range(0,5) left closed right open interval

range(1,10,2) within the range of [1,10) with a step size of 2 for the range of values

Python commonly used standard modules

random: Randomly selected function

os: interaction with the operating system

sys: system-related operations

time: interaction with time

math: Interaction with mathematics

re: tools that provide regular expressions for advanced string processing

 

Guess you like

Origin blog.csdn.net/sunguanyong/article/details/129149449
Recommended