Python注释和声明

So, in this tutorial we are going to learn about Python Comment and Statement. Previously we learned about Keywords and Identifiers in Python.

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

Python注释 (Python Comment)

Python Comment are statements that are not part of your program. For this reason, comment statements are skipped while executing your program. Usually we use comments for making brief notes about a chunk of code. Also comments are important so that other can understand easily while reading your program. On the other hand, comments are also useful for the programmer himself. One can understand a program done a long time a ago simply from the comments of the program.

Python Comment是不属于您程序的语句。 因此,在执行程序时将跳过注释语句。 通常,我们使用注释来对一段代码做简短的注释。 注释也很重要,以便其他人在阅读程序时可以轻松理解。 另一方面,注释对于程序员自己也很有用。 仅仅从程序的注释中就可以了解一个很久以前完成的程序。

Here’s an example of comment-

这是一个评论示例-

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

#print("I will not be executed")

print("I will be executed")

If you run this, then you will see output will be like this below picture-

python statement, python comment example output

So you can see the lines starting with # are not executed. Those are the comments.

如果运行此命令,则将看到输出如下图所示:

因此,您可以看到以#开头的行未执行。 这些是评论。

Python注释的类型 (Types of Python Comments)

In Python there are two types of comments- Single line comments and Multiple lines comments. Single line commenting is commonly used for a brief and quick comment (or to debug a program, we will see it later). On the other hand we use the Multiple lines comments to note down something much more in details or to block out an entire chunk of code.

在Python中,注释有两种类型:单行注释和多行注释。 单行注释通常用于简短简短的注释(或调试程序,我们将在以后看到)。 另一方面,我们使用多行注释来记下更多细节或阻止整个代码块。

单行注释 (Single Line Comments)

In Python for single line comments use # sign to comment out everything following it on that line.

在Python中,单行注释使用#号注释掉该行之后的所有内容。

#this is a comment

myVar = "hello comments" # a variable containing something

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

多行注释 (Multiple Lines Comments)

Multiple lines comments are slightly different. Simply use 3 single quotes before and after the part you want to be commented.

多行注释略有不同。 只需在要评论的部分之前和之后使用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语句 (Python Statement)

Statements are logical lines we write in our code. Statements can be like below.

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

  1. Assignment Statement:
    myVariable1="hello world"
    
    myVariable2=100
    
    myVariable3=12.23

    转让声明:
  2. Addition Statement:
    myVariable4=myVariable2 + myVariable3

    补充声明:
  3. Subtraction Statement:
    myVariable4=myVariable2 - myVariable3

    减法声明:
  4. Multiplication Statement:
    myVariable4=myVariable2 * myVariable3

    乘法语句:
  5. Division Statement:
    myVariable4=myVariable2 / myVariable3

    部门声明:

And many more. These are only some examples.

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

Alright, we are done with Python comment and statement. Try to run every piece of code snippet given here in your own machine. As there’s an old saying – “if you want to learn how to swim, then jump into the water”.
#happy_coding 🙂

好的,我们已经完成了Python注释和声明。 尝试在自己的计算机上运行此处给出的每个代码段。 俗话说:“如果你想学习游泳,那就跳入水中”。
#happy_coding🙂

翻译自: https://www.journaldev.com/14012/python-comment-python-statement

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值