Python 入门简明教程
1. 1. 与 Python 的第一次亲密接触-写给初学者 2. 2. Python 语法关键字 3. 3. Python 的注释和语法 4. 4. Python 基本数据类型 5. 5. Python 输入输出 6. 6. Python 运算符 7. 7. Python 变量 8. 8. Python 命名空间和变量生存范围 9. 9. Python 逻辑判断 10. 10. Python For 循环语法 11. 11. Python While 循环语法 12. 12. Python 循环语法中 break 与 continue的使用 13. 14. Python 三目运算符 14. 15. Python 循环语法中 pass 语句的使用 15. 16. Python 循环语法示例 16. 17. Python 函数 17. 18. Python main 函数 18. 19. Python print 函数 19. 20. Python print format 输出格式化 20. 21. Python print 输出到文件 21. 22. Python 输入 22. 23. Python import 导入模块 23. 25. Python 中递归的使用 24. 26. Python 匿名函数 25. 27. Python lambda 26. 28. Python PIP 包管理机制 27. 29. Python 命令行参数 28. 30. Python Numbers 包 29. 31. Python 随机数 30. 32. Python String 转 int 31. 34. Python 自定义异常 Exception 32. 35. Python 继承 33. 36. Python 父类 34. 38. Python 操作符重载 35. 39. Python __str__ and __repr__

3. Python 的注释和语法

因此,在本教程中,我们将学习Python注释和语句。之前我们了解到Python中的关键字和标识符.

Python注释

Python注释是不属于程序一部分的语句。因此,在执行程序时会跳过注释语句。通常我们使用注释对一段代码做简短的注释。另外,注释也很重要,这样其他人在阅读你的程序时可以很容易地理解。另一方面,注释对程序员自己也很有用。人们可以简单地从程序的注释中理解一个很久以前做过的程序。

这里是一个评论的例子-


#this is a comment. I can write whatever I want here

#print("I will not be executed")

print("I will be executed")

如果您运行这个,那么您将看到如下图片的输出- 因此可以看到以#开头的行没有执行。这些是评论。

Python注释的类型

在Python中有两种类型的注释-单行注释和多行注释。单行注释通常用于简短而快速的注释(或调试程序,我们稍后再看)。另一方面,我们使用多行注释来记录更详细的内容,或者阻塞整个代码块。

单行注释

在Python中,对于单行注释,使用#sign将该行后面的所有内容注释掉。


#this is a comment

myVar = "hello comments" # a variable containing something

print(myVar) #print statement to print contents of a variable

多行注释

多行注释略有不同。只需在你想评论的部分前后使用3个单引号。


"""

print("I am in Multiple line comment line 1")

print ("I am in Multiple line comment line 2")

"""

print("I am out of Multiple line comment")

Python语句

语句是我们在代码中编写的逻辑行。语句可以如下所示。

  1. 转让声明:
    
    myVariable1="hello world"
    
    myVariable2=100
    
    myVariable3=12.23
    
  2. 附加声明:
    
    myVariable4=myVariable2 + myVariable3
    
  3. 减法语句:
    
    myVariable4=myVariable2 - myVariable3
    
  4. 乘法语句:
    
    myVariable4=myVariable2 * myVariable3
    
  5. 部门声明:
    
    myVariable4=myVariable2 / myVariable3
    

还有更多。这些只是一些例子。

好了,我们已经完成了Python注释和语句。试着在你自己的机器上运行这里给出的每一段代码片段。正如那句老话所说,如果你想学游泳,那就跳入水中;。#happy_编码*