Python 101:基础知识
先决条件
为了充分利用本文,您应该熟悉以下概念:
- Python作为一种编程语言,支持python编程语言的编程技术及其编程语言类别。
2.变量,python编程语言中变量的定义和使用。
-
定义标识符文字时要遵循的注释、关键字、标识符、规则。
-
python 编程语言中的内置函数,如 print(...)
5.python编程语言中的运算符。
6.PE8规则和标准Python编程语言。
Python中的控制流和决策制定
条件语句也称为决策语句。如果给定条件为真或假,我们需要使用这些条件语句来执行特定的代码块。
在 Python 中,我们可以使用:
#1) -if else 语句
它检查条件是否为真。如果条件为真,则执行“if”块内的代码,否则不执行。如果第一个不是,Else 块提供要执行的替代代码。
If ( CONDITION == TRUE ):
Block to execute
else:
Block to execute otherwise
进入全屏模式 退出全屏模式
让我们看看并讨论一些 if 语句的例子:
print('Example: 1')
num = 5
if (num < 10):
print('Num is smaller than 10')
else:
print('Num is not smaller than 10')
print('Example: 2')
a = 7
b = 0
if(a):
print('true')
else:
print('false')
print('Example: 3')
if('python' in ['Java', 'Python', 'Dart']):
print('Python found in the List')
else:
print('Python not found')
进入全屏模式 退出全屏模式
在_Example 1_中,我们检查语句我们的变量是否小于 10。如果语句为真,则执行它后面的 print 方法。
记住在 if 语句的末尾使用 (:) 运算符,因为无论您在冒号运算符后面编写的代码都将成为“if 块”的一部分,并且缩进非常重要。
在_示例 2_ 中,我们不寻找任何条件。在任何编程语言中,正数都被认为是真值,因此我们的程序产生输出“真”。
在 Example 3 中,我们检查语言列表以检查 Python 是否在其中。
//Example 4
passing_Mark = 60
my_Score = 67
if(my_Score >= passing_Score):
print('Congratulations! You passed the exam')
print("You are passed in the exam")
else:
print('Sorry! You failed the exam, better luck next time')
进入全屏模式 退出全屏模式

#2) -elif 语句
仅当给定条件为假时,“elif”语句才用于检查多个条件。 elif 和 else 语句之间的区别在于 elif 检查条件,而 else 不检查条件。
if (condition):
#Set of statement to execute if condition is true
elif (condition):
#Set of statements to be executed when if condition is false and elif condition is true
else:
#Set of statement to be executed when both if and elif conditions are false
进入全屏模式 退出全屏模式
//Example of elif statement
num = int(input("Enter number: "))
if (num > 0):
print('Number is positive')
elif (num < 0):
print('Number is negative')
else:
print('Number is Zero')
进入全屏模式 退出全屏模式
在这个例子中,我们通过接受用户输入来尝试一些新的东西。默认情况下,输入是一个字符串,因此我们必须将其类型转换为整数才能执行操作。
程序将检查 num 是否大于 0(条件),
如果不是,我们检查 elif(num 小于 0) 中的条件,
如果两者都不为真,那么只有到达 else 语句。
注意: Python if 语句将布尔表达式评估为真或假,如果条件为真,则 if 块内的语句将被执行,如果条件为假,则 else 块内的语句将被执行只有当你写了 else 块,否则它什么也不做。
Python 循环 – For, While
在 Python 中,语句以顺序方式执行,即。一行接一行。但是,您可能希望重复某些行,直到满足特定条件。这就是循环为你而来的地方。
更多推荐

所有评论(0)