python grocery store basis

List python link

1, getpass module

 

The module has two main methods:

  • getuser This function returns the login user name, no parameters
  • getpass displays a prompt string, close-screen keyboard echo, then read the password
getpass Import 

the User = getpass.getuser () 
passwd = getpass.getpass ( "Please enter your password:") 
Print (the User) 
Print (passwd)

 

 

2, string formatting

Percent formula:

%[(name)][flags][width].[precision]typecode

  • (Name) Alternatively, for selecting a specified key
  • flags optional, alternative values ​​are:
    • + Right aligned; added just before the positive, negative numbers preceded by a minus sign;
    • - Left; unsigned positive front, a minus sign before the negative;
    • Right-aligned spaces; spaces, a minus sign before the negative before positive number;
    • Align Right 0; unsigned before positive and negative numbers preceded by a minus sign; the space filled with 0
  • width optional, possession of width
  • .precision optional decimal places reserved
  • typecode Required
    • s, __str__ method of obtaining the return value of the incoming object and is formatted to the specified position
    • r, __repr__ method of obtaining the return value of the incoming object and is formatted to the specified position
    • C, integers: a number into its corresponding unicode values, decimal range 0 <= i <= 1114111 (py27 only supports 0-255); character: adding a character to the specified position
    • O, the octal notation is converted into an integer, and formats it into the designated location
    • x, an integer converted into hexadecimal notation, and formats it into the designated location
    • d, convert integer, floating point decimal representation and formats it to a designated position
    • E, integer, floating-point numbers into scientific notation, and formats it into the designated location (lowercase e)
    • E, converting the integer, floating-point number in scientific notation, and formats it to the specified position (uppercase E)
    • F, converting integers, floating point numbers to floating point representation and formats it to the specified position (after the default 6 decimal places)
    • F, ibid.
    • g, automatic adjustment convert integer, float to float or scientific notation (more figures in scientific notation), and formats it to a specified location (if it is a scientific notation E;)
    • G, automatic adjustment convert integer, float to float or scientific notation (more figures in scientific notation), and formats it to a specified location (if it is a scientific notation E;)
    • %, When the flag is present formatted string, a percent sign indicates required by %%

Note: Python the percent format is automatically converted into a binary representation of an integer as not exist

 

Common Formatting:

tpl = "i am %s" % "alex"
 
tpl = "i am %s age %d" % ("alex", 18)
 
tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}
 
tpl = "percent %.2f" % 99.97623
 
tpl = "i am %(pp).2f" % {"pp": 123.425556, }
 
tpl = "i am %.2f %%" % {"pp": 123.425556, } 

 format format

[[fill]align][sign][#][0][width][,][.precision][type]

[optional] fill the space filled characters 
align] [optional alignment (in conjunction with the use of width)
     < contents Left
     > contents in the right alignment (default) 
    = contents in the right alignment, the symbol on the left pad characters side, and only valid digital type. Even: Symbol + filler + digital
     ^ , center the contents 
sign signed or unsigned numbers [optional]
     + , n number plus or minus sign plus minus;
      - , the same positive sign, plus minus minus; 
    spaces, number spaces n , plus minus negative; 
# [optional] for binary, octal, hexadecimal, if coupled with # displayed 0B / 0o / 0x, otherwise it is not displayed 
, [optional] add a separator to a number, such as: . 1 , 000 , 000 
width [optional] format bit width occupied 
.precision [optional] reserved decimal precision 
type [optional] format type 
    passed "character string type" parameter  
        s, the type of format strings data
        blank type is not specified, The default is None, with s 
    the incoming "integral type" parameter 
        B, will automatically convert a decimal to a binary integer format represented and 
        C, will automatically convert a decimal integer to its corresponding the unicode characters 
        d, decimal integer 
        o, will be automatically converted to decimal integers and octal format; 
        X, is automatically converted into a decimal integer in hexadecimal format and (lowercase X) 
        X-, 10 binary integer automatically converted into a hexadecimal format and (uppercase X) 
    incoming "or decimal floating-point type" parameter 
        E, is converted to scientific notation (lowercase e) shows, then format; 
        E, is converted to scientific notation (uppercase E), and then format; 
        F, is converted to floating point (the default after the decimal point 6) shows, then format; 
        F., is converted to floating point (the default reserved six decimal) is , then format; 
        G, automatically switches e and f 
G, automatically switches E and F
 % , shows the percentage (default display 6 decimal place)
                        

 

Common Formatting:

tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')
  
tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])
  
tpl = "i am {0}, age {1}, really {0}".format("seven", 18)
  
tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])
  
tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
  
tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
  
tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
  
tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
  
tpl = "i am {:s}, age {:d}".format(*["seven", 18])
  
tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18)
  
tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
 
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
 
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
 
tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15)
 
tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)

 3, iterators and generators.

1, iterators

Iterator is a way to access the collection elements. Iterator object began a visit from the first element of the collection until all the elements are accessed session is over. Iterator can only move forward not backward, but that's OK, because people rarely go back in an iterative way. In addition, a major advantage of the iterator is not required to be prepared in advance throughout the iterative process all the elements. Iterator iterative calculation only when the element to an element, and before or after the element can not exist or be destroyed. This feature makes it particularly suitable for traversing some huge or infinite set, such as several files G

Features:

  1. Visitors do not need to be concerned about the internal structure of the iterator, only () methods continue to be removed by a content next
  2. Can not access a random set of values ​​can only be accessed sequentially from beginning to end
  3. Access to the back half can not retreat
  4. Cycle facilitates relatively large set of data, to save memory
>>> a = iter([1,2,3,4,5])
>>> a
<list_iterator object at 0x101402630>
>>> a.__next__()
1
>>> a.__next__()
2
>>> a.__next__()
3
>>> a.__next__()
4
>>> a.__next__()
5
>>> a.__next__()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

 2, generator

When a function call to return an iterator, then this function is called Generator (generator); if the yield is included in the function syntax, then this will become a function generator;

def func():
    yield 1
    yield 2
    yield 3
    yield 4

 The above code: func is a function known as a generator, when the execution of the function FUNC () will get an iterator.

>>> temp = func()
>>> temp.__next__()
1
>>> temp.__next__()
2
>>> temp.__next__()
3
>>> temp.__next__()
4
>>> temp.__next__()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

 3, examples

def nrange(num):
    temp = -1
    while True:
        temp = temp + 1
        if temp >= num:
            return
        else:
            yield temp

 

Guess you like

Origin www.cnblogs.com/sunshine-long/p/12521505.html