[Python] Python learning the basics first chapter notes


Python Basics

Program control structure

Cyclic structure - Extended Mode

That else for keywords and while the heel. When the cycle ends normally (no break.continue not affect), will execute the contents else statement. Instructions:

for <variable> in <struct>:
    <code1>
else:
    <code2>

while <condtion>:
    <code1>
else:
    <code2>

No operation

The pass statement, the code to play a sound effect.

String formatting

String object center (), ljust (), rjust () method

Centered, left justified, right justified, filled with character parameter settings. Instructions:

>>> 'python'.center(10)
'  python  '
>>> 'python'.center(10,'-')
'--python--'
>>> 'python'.ljust(10,'-')
'python----'
>>> 'python'.rjust(10,'-')
'----python'

The method of formatting a string (not recommended)

C Language Reference

String formatting Method 2 (recommended)

Use .format () function to control the format string.

>>> name="Bob"
>>> score=10
>>> "{0} scored {1} points.".format(name,score)
'Bob scored 10 points.'

Braces can control the content:

  1. ":" Pilot symbol.
  2. Character used to fill in
  3. Controlling the direction of alignment. "<" Left ">" right "^" Align
  4. Control output width
  5. "," Digital thousands separator
  6. Accuracy. The maximum length of the control output decimal floating-point number or a character string
  7. Types of

Example: Print multiplication table

for i in range(1,10):
    for j in range(1,i+1):
        print("{0}*{1}={2:<2}".format(i,j,i*j),end="  ")
    print()

or

for i in range(1,10):
    for j in range(1,i+1):
        print(f"{i}*{j}={i*j:<2}",end="  ")
    print()

String object split () method

Stresses string divided according to a parameter and returns a list. The default parameter spaces

Using the map () function

__name__ property Python script

Each script has created a __name__ property, independently run was __main__, as compared with the import module file name.

Guess you like

Origin www.cnblogs.com/charles1999/p/12512226.html