1、代码格式规范(可以通过Pycharm检查)
- 操作符(operator)两边需要有空格(whitespace)
- :(代码块指示符)左边不能有空格(whitespace)
- 算数运算等不需要用圆括号(parentheses)
- 函数或类末尾需要有两空行(blank lines)
- 函数参数直接要有空格
2、案例
- # This is a sample Python script.
-
- # Press Shift+F10 to execute it or replace it with your code.
- # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
-
-
- def print_hi(name):
- # Use a breakpoint in the code line below to debug your script.
- print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
- a = 100
- if a < 1:
- print("a > 1")
- elif a > 10:
- print("a > 10")
- else:
- print("a is not a valid value")
-
-
- def test_sum(a: int, b: int) -> int:
- print("a+b=", a+b)
- return a+b
-
-
- def print_error(name):
- print(f'Warning, {name}')
-
-
- # Press the green button in the gutter to run the script.
- if __name__ == '__main__':
- print(__name__)
- print_hi('PyCharm')
- print_error('Error')
- test_sum(2, 10)
-
-
- # See PyCharm help at https://www.jetbrains.com/help/pycharm/