Python |集2(变量、表达式、条件和函数)

Python简介已在中介绍 本文 .现在,让我们从学习python开始。

null

用Python运行第一个代码 Python程序不是编译的,而是解释的。现在,让我们开始编写python代码并运行它。请确保您正在使用的系统上安装了python。如果未安装,请从下载 在这里 .我们将使用python 2.7。

制作Python文件: Python文件以扩展名“.py”存储。打开一个文本编辑器,保存一个名为“hello.py”的文件。打开它并编写以下代码:

Python3

print ( "Hello World" )
# Notice that NO semi-colon is to be used


读取文件内容: Linux系统–使用“cd”命令,从存储所创建文件(hello.py)的终端移动到目录,然后在终端中键入以下内容:

python hello.py

Windows系统–打开命令提示符,使用“cd”命令移动到存储文件的目录,然后通过将文件名写入命令来运行文件。

Python中的变量 在python中不需要首先声明变量。它们可以直接使用。python中的变量和大多数其他编程语言一样区分大小写。

例子:

Python3

a = 3
A = 4
print (a)
print (A)


输出为:

34

Python中的表达式 python中的算术运算可以通过使用算术运算符和一些内置函数来执行。

Python3

a = 2
b = 3
c = a + b
print (c)
d = a * b
print (d)


输出为:

56

Python中的条件 python中的条件输出可以通过使用if-else和elif(else-if)语句获得。

Python3

a = 3
b = 9
if b % a = = 0 :
print ( "b is divisible by a" )
elif b + 1 = = 10 :
print ( "Increment in b produces 10" )
else :
print ( "You are in else statement" )


输出为:

b is divisible by a

Python中的函数 python中的函数在函数名前用关键字“def”声明。函数的返回类型不需要在python中显式指定。可以通过在括号中写入函数名和参数列表来调用函数。

Python3

# Function for checking the divisibility
# Notice the indentation after function declaration
# and if and else statements
def checkDivisibility(a, b):
if a % b = = 0 :
print ( "a is divisible by b" )
else :
print ( "a is not divisible by b" )
#Driver program to test the above function
checkDivisibility( 4 , 2 )


输出为:

a is divisible by b

因此,python是一种非常简单且不那么麻烦的语言。python的这种易用性本身促进了它的广泛使用。

https://www.youtube.com/watch?v=gzDPuWKjmGQ

本文由 尼希尔·库马尔·辛格。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享