Python knowledge points

It has been procrastinating, and I have been sorting out about Python.

        1. Naming

            Variable name: letter + underscore + number Global variable global_a

            Package name: lowercase letters

            Module name: lowercase letters

    Class name: first letter uppercase, other lowercase private objects of the class, private methods are prefixed with two underscores

            object name: lowercase

            Function name: lowercase letters

            Variables and constants:

        2. Indent

        3. Import the module

              import .... the entire module

              from ... import ... part of the module and creates a reference to the imported object

              import ... as ... modules create aliases

        4. Notes

              Comment out a single line #

        Comment multiple lines '''...''' ; "''..."''

              Shortcut Spyder: Ctrl + 1 Comment\Uncomment a single line

         5. Data Types 

number

string

 tuple 

list 

List operations include the following functions:
1. cmp(list1, list2): compares the elements of two lists
2, len(list): the number of list elements
3, max(list): returns the maximum value of the list elements
4, min(list) : Returns the minimum value
of list elements 5. list(seq): Converts tuples to lists
List
operations include the following methods: 1. list.append(obj): add a new object at the end of the list
2. list.count(obj): Count the number of times an element appears in a list
3. list.extend(seq): append multiple values ​​from another sequence at the end of the list at one time (extend the original list with a new list)
4. list.index(obj) : find the index position of the first occurrence of a value from a list
5, list.insert(index, obj): insert an object into the list
6, list.pop(obj=list[-1]): remove the list An element in (the last element by default), and returns the value of the element
7, list.remove(obj): remove the first occurrence of a value in the list
8, list.reverse(): reverse the list Element
9, list.sort([func]): sort the original list

dictionary

     6. Operators

  +   -   *   /   %  **   //

<  >   <=   >=  ==  !=   <>

                and  or    not   

All objects in Python are objects. Objects are instances of classes with attributes and methods. Objects use the . operator to call their own methods.

  • import model_name as fn 
  • from model_name import fuction_name as fn #Define an alias for the method
  • input(): When inputting a number, output the calculation result raw_input(): Generally, the default input is a string (not present in 3)
  • pop( )    deletes the value corresponding to the given key key in the dictionary
  • open( )
  • items()
  • sys.stdout.write sys.stdout is the standard output file, and write is to write data to this file.

Case conversion u=str.upper() l=str.lower()

Capitalize the first letter str.capitalize() string.capword(str)

Split and join strings split splitlines join

  • __init__()
  • time.time(): returns a timestamp
  • time.asctime([t]): Convert the tuple or struct_time returned by gmtime() and localtime() to a string.
  • time.clock(): On the first call, returns the time the program was running. Returns the interval from the previous after the second.
  • time.ctime([secs]): Convert the timestamp to a time string, or return the current time string if not provided, same as asctime(localtime()).
  • time.gmtime([secs]): Convert timestamp to struct_time in UTC time zone.
  • time.localtime([secs]): Like gmtime() but converts it to the local time zone.
  • time.mktime(t): struct_time converted to timestamp.
  • time.sleep(secs): The thread is delayed for the specified time, in seconds.
  • time.strftime(format[,t]): Converts a sturc_time or tuple to a string according to the argument.
  • time.strptime(string[, format]): Contrary to strftime, returns a struct_time.

  Decorators seem to be a bit tricky to me, let's bury a hole first.

The prophet will have a pair of concepts: absolute path: the path relative to the entire hard disk, which needs to be written from the root directory. Relative paths can only use "/" to separate characters

                           Relative path: a path relative to a base directory; absolute paths can use "/" and "\".

reduce /map/filter map function: map map(function,sequence)

                                                 map(lambda x:x**2,[1,2,3,4,5])  return:[1,4,9,16,25]

                                                 map(lambda x,y:x+y,[1,2,3],[5,6,7])   return:[6,8,10]

                                                  map(lambda x,y:x+y,[1,2,3],[5,6])     return:[6,8]

                                                  map(None,[1,2,3],[2,4,5])             return: [(1,2),(2,4),(3,5)]

                                reduce function: merge reduce(function,sequence)

                                                    reduce(lambda x,y:x+y,[2,3,4,5])  return: 14

         filter function: filter filter(lambda N:len(filter(lambda M:N%M==0,range(2,int(N**0.5)+1)))==0,range(100,201))

                                      filter(lambda x:x%2,range(9))

   A decorator that takes a function as a parameter and returns an alternative function closure

        enumerate() function

        zip() function

                


       

Guess you like

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