Python-basic grammar 2

1. Number types: integer, boolean, floating point, complex number.

    int: There is only one integer type int, which means long integer, no long.

    bool:True,Flase.

    float:12.22、3e-2.

    complex:2+3j.

2. String

(1) The use of python single quotes and double quotes is exactly the same. Strings can also be placed in single quotes. And use triple quotation marks ('''') to specify a multi-line string.

w='字符串'

s="句子。"

p='''段落,
  三引号'''

p="""段落
     三引号组成"""

(2) The backslash is used to escape, and the use of r can make the backslash not escape

    

(3) There are two indexing methods for strings in Python, starting with 0 from left to right, and starting with -1 from right to left.

        print(str[0:-1]) # output all characters from the first to the penultimate

         print(str[2:5]) # Print the characters starting from the third to the fifth, including subscript 2 but not subscript 5.

         print(str[2:]) # output all characters starting from the third

         print(str * 2) # output string twice

         

         

         

3. Python can write multiple sentences on the same line, and use them in the middle; split.

4. A group of statements with the same indentation constitutes a code block, which we call a code group.

5. Print will output newline by default, if it is required not to wrap, add end=' 'at the end.

  

x='a'
y='b'
print(x)
print(y)
print(x,end='')
print(y,end=' ')
print('cde')

  result:

   

Strings in python cannot be changed.

6.import和from import

Use import or from...import in python to import the corresponding module.

Import the sys module in the format: import sys

Import a function from a module, the format is: from somemodule import somefunction

Import multiple functions from a module, the format is: from somemodule import firstfunc, secondfunc, thirdfunc

Import all functions in a module, the format is: from somemodule import *

 

Guess you like

Origin blog.csdn.net/zhangxue1232/article/details/108986028