Write in front: I want to write a blog to record the knowledge on the way to learn python. I write an article for the first time. If my brothers see any shortcomings or mistakes, they can point out them. Please take care of them.
catalogue
1. Naming (associated with identifier)
III Multiline statement writing
I Naming and reserved words
1. Naming (associated with identifier)
Naming rules: upper and lower case letters, numbers, underscores, Chinese characters and combinations
Example: TempStr,Python_Great, this is a programming class
Note: case sensitive characters and numbers cannot be the same
Example: Python and python are different variables, and Python is illegal
2. Identifier
- The first character must be a letter or underscore in the alphabet_
- The rest of the identifier consists of letters, numbers, and underscores
- Identifiers are case sensitive
3. Reserved words
Reserved words are keywords and we cannot use them as any identifier name. Python's standard library provides a keyword module that can output all keywords of the current version:
>>> import keyword >>> keyword.kwlist ['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
II Notes and indents
1. Notes
- Single-Line Comments
In Python, a single line comment starts with # and the code is as follows:
print("Hello, Python!") #print("Hello, Python!")
The operation results are as follows:
print("Hello, Python!")
- multiline comment
Multi line comments can use multiple # numbers, as well as' 'and' ', and the code is as follows:
# First comment # Second comment ''' Third note Fourth note ''' """ Fifth note Sixth note """ print ("Hello, Python!")
Execute the above code and the output result is:
print("Hello, Python!")
2. Indent
The most distinctive feature of python is that it uses indentation to represent code blocks without curly braces {}.
The number of indented spaces is variable, but statements in the same code block must contain the same number of indented spaces. The code is as follows:
if True: print ("True") else: print ("False")
Inconsistent number of spaces in the indentation number of the last line of the following code will cause a running error:
if True: print ("Answer") print ("True") else: print ("Answer") print ("False") # Inconsistent indentation will lead to running errors
Due to the inconsistent indentation of the above procedures, the following errors will appear after execution:
File "test.py", line 6 print ("False") # Inconsistent indentation will lead to running errors ^ IndentationError: unindent does not match any outer indentation level
III Multiline statement writing
Python usually writes a statement on one line, but if the statement is very long, we can use backslash \ to implement multi line statements, for example:
total = item_one + \ item_two + \ item_three
For multiline statements in [], {}, or (), you do not need to use backslash \, for example:
total = ['item_one', 'item_two', 'item_three', 'item_four', 'item_five']
IV Number type
There are four types of numbers in python: integer, Boolean, floating-point number and complex number.
- Int (integer), such as 1. There is only one integer type int, expressed as a Long integer, without Long in Python 2
- bool (Boolean), such as True, False
- float (floating point number), such as 1.23, 3E-2
- Complex (complex), such as 1 + 2j, 1.1 + 2.2j
V String and blank line
1. String
- Single quotation marks and double quotation marks are used exactly the same in python.
- Use three quotation marks ('' 'or' ') to specify a multiline string.
- Escape character\
- Backslashes can be used to escape, and r can prevent backslashes from escaping.. If r"this is a line with \n", it will be displayed instead of a line break.
- Concatenate strings literally, such as "this" "is" "string" will be automatically converted to this is string.
- Strings can be concatenated with the + operator and repeated with the * operator.
- Strings in Python can be indexed in two ways, starting with 0 from left to right and - 1 from right to left.
- Strings in Python cannot be changed.
- Python does not have a separate character type. A character is a string with a length of 1.
- The syntax format of string interception is as follows: variable [header subscript: tail subscript: step size]
word = 'character string' sentence = "This is a sentence." paragraph = """This is a paragraph, Can consist of multiple lines"""
Example:
str='123456789' print(str) # Output string print(str[0:-1]) # Output all characters from the first to the penultimate print(str[0]) # First character of output string print(str[2:5]) # Output characters from the third to the fifth print(str[2:]) # Output all characters starting from the third print(str[1:5:2]) # Output every other character from the second to the fifth (in steps of 2) print(str * 2) # Output string twice print(str + 'Hello') # Connection string print('------------------------------') print('hello\nrunoob') # Escape special characters with backslash (\) + n print(r'hello\nrunoob') # Add an r before the string to represent the original string without escape
result:
123456789 12345678 1 345 3456789 24 123456789123456789 123456789 Hello ------------------------------ hello runoob hello\nrunoob
2. Blank line
Empty lines are used to separate functions or class methods, indicating the beginning of a new piece of code. The class and function entry are also separated by an empty line to highlight the beginning of the function entry.
Blank lines, unlike code indentation, are not part of Python syntax. When writing, do not insert blank lines, and the Python interpreter will run without error. However, the function of blank lines is to separate two sections of code with different functions or meanings, so as to facilitate the maintenance or reconstruction of the code in the future.
Remember: blank lines are also part of the program code.
Vi User input and output
1. Input
Input function input() uses format: variable = input (Prompt string)
Example:
nunber=input("please enter a number")
2. Output
The output function print() uses the format: Print (string or string variable to be output)
print. The default output is line feed. If you want to realize no line feed, you need to add "end =" "at the end of the variable
Example:
print("what do you mean?",end="") print("hello world")
result:
what do you mean?hello world
VII Use of import
In python, use , import , or , from Import , to import the corresponding module.
Import the whole module in the format of import somemodule
Import a function from a module in the format from some module import somefunction
Import multiple functions from a module. The format is: from some module import firstfunc, secondfunc, thirdffunc
Import all functions in a module in the format from some module import*