Python中的If,Elif和Else語句

如果Elif Else聲明 (If Elif Else Statements)

The if/elif/else structure is a common way to control the flow of a program, allowing you to execute specific blocks of code depending on the value of some data.

if / elif / else結構是控制程序流程的常用方法,它使您可以根據某些數據的值執行特定的代碼塊。

如果聲明 (if statement )

If the condition following the keyword if evaluates as true, the block of code will execute. Note that parentheses are not used before and after the condition check as in other languages.

如果關鍵字if之后的條件評估為true ,則代碼塊將執行。 請注意,在條件檢查之前和之后不會像其他語言一樣使用括號。

if True:print('If block will execute!')
x = 5if x > 4:print("The condition was true!") #this statement executes

其他陳述 (else statement)

You can optionally add an else response that will execute if the condition is false:

您可以選擇添加一個else響應,如果條件為false該響應將執行:

if not True:print('If statement will execute!')
else:print('Else statement will execute!')

Or you can also see this example:

或者您也可以看到以下示例:

y = 3if y > 4:print("I won't print!") #this statement does not execute
else:print("The condition wasn't true!") #this statement executes

Note that there is no condition following the else keyword - it catches all situations where the condition was false

請注意, else關鍵字后沒有條件-它捕獲條件為false所有情況

elif聲明 (elif statement)

Multiple conditions can be checked by including one or more elif checks after your initial if statement. Just keep in mind that only one condition will execute:

可以通過在初始if語句之后包含一個或多個elif檢查來檢查多個條件。 請記住,只有一種情況會執行:

z = 7if z > 8:print("I won't print!") #this statement does not execute
elif z > 5:print("I will!") #this statement will execute
elif z > 6:print("I also won't print!") #this statement does not execute
else:print("Neither will I!") #this statement does not execute

Note: only the first condition that evaluates as true will execute. Even though z > 6 is true, the if/elif/else block terminates after the first true condition. This means that an else will only execute if none of the conditions were true.

注意:只有第一個條件為true條件才會執行。 即使z > 6trueif/elif/elseif/elif/else在第一個true條件之后終止。 這意味著else僅在沒有條件true的情況下才會執行。

嵌套if語句 (Nested if statements)

We can also create nested if’s for decision making. Before preceding please refer to the href=’https://guide.freecodecamp.org/python/code-blocks-and-indentation’ target=’_blank’ rel=‘nofollow’>indentation guide once before preceding.

我們還可以創建嵌套的if決策。 在開始之前,請先參考href =“ https://guide.freecodecamp.org/python/code-blocks-and-indentation'target ='_ blank'rel ='nofollow'>縮進指南。

Let’s take an example of finding a number which is even and also greater than 10

讓我們以發現一個等于甚至大于10的數字為例

python 
x = 34
if x %  2 == 0:  # this is how you create a comment and now, checking for even.if x > 10:print("This number is even and is greater than 10")else:print("This number is even, but not greater 10")
else:print ("The number is not even. So point checking further.")

This was just a simple example for nested if’s. Please feel free to explore more online.

這只是嵌套if的一個簡單示例。 請隨時在線探索更多。

While the examples above are simple, you can create complex conditions using boolean comparisons and boolean operators.

盡管上面的示例很簡單,但是您可以使用布爾比較和布爾運算符創建復雜的條件。

內聯python if-else語句 (Inline python if-else statement)

We can also use if-else statements inline python functions. The following example should check if the number is greater or equal than 50, if yes return True:

我們還可以使用if-else語句內聯python函數。 以下示例應檢查該數字是否大于或等于50,如果是,則返回True:

python 
x = 89
is_greater = True if x >= 50 else Falseprint(is_greater)

Output

輸出量

>
True
>

有關if / elif / else語句的更多信息: (More info on if/elif/else statements:)

  • How to get out of if/else hell

    如何擺脫if / else地獄

  • If/else in JavaScript

    JavaScript中的if / else

翻譯自: https://www.freecodecamp.org/news/if-elif-else-statements/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390783.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390783.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390783.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Hadoop安裝及配置

Hadoop的三種運行模式 單機模式(Standalone,獨立或本地模式):安裝簡單,運行時只啟動單個進程,僅調試用途;偽分布模式(Pseudo-Distributed):在單節點上同時啟動namenode、datanode、secondarynamenode、resourcemanage…

漏洞發布平臺-安百科技

一個不錯的漏洞發布平臺:https://vul.anbai.com/ 轉載于:https://blog.51cto.com/antivirusjo/2093758

Android 微信分享圖片

private String APP_ID "00000000000000000"; //微信 APPID private IWXAPI iwxapi; private void regToWx() {iwxapi WXAPIFactory.createWXAPI(context, APP_ID, true);//這里context記得初始化iwxapi.registerApp(APP_ID); } IMServer.getDiskBitmap(IMServer.u…

蒙蒂霍爾問題_常見的邏輯難題–騎士和刀,蒙蒂·霍爾和就餐哲學家的問題解釋...

蒙蒂霍爾問題While not strictly related to programming, logic puzzles are a good warm up to your next coding session. You may encounter a logic puzzle in your next technical interview as a way to judge your problem solving skills, so its worth being prepare…

西格爾零點猜想_我從埃里克·西格爾學到的東西

西格爾零點猜想I finished reading Eric Siegel’s Predictive Analytics. And I have to say it was an awesome read. How do I define an awesome or great book? A book that changes your attitude permanently. You must not be the same person that you were before y…

C/C++實現刪除字符串的首尾空格

StdStringTrimTest.cpp #include <iostream> int main() {std::string str(" 字符串 String ");std::cout << str << std::endl;std::cout << str.size() << std::endl;str.erase(str.find_first_of( ), str.find_first_not_of…

assign復制對象_JavaScript標準對象:assign,values,hasOwnProperty和getOwnPropertyNames方法介紹...

assign復制對象In JavaScript, the Object data type is used to store key value pairs, and like the Array data type, contain many useful methods. These are some useful methods youll use while working with objects.在JavaScript中&#xff0c; Object數據類型用于存…

HDFS 技術

HDFS定義 Hadoop Distributed File System&#xff0c;是一個使用 Java 實現的、分布式的、可橫向擴展的文件系 統&#xff0c;是 HADOOP 的核心組件 HDFS特點 處理超大文件流式地訪問數據運行于廉價的商用機器集群上&#xff1b; HDFS 不適合以下場合&#xff1a;低延遲數據…

深度學習算法和機器學習算法_啊哈! 4種流行的機器學習算法的片刻

深度學習算法和機器學習算法Most people are either in two camps:大多數人都在兩個營地中&#xff1a; I don’t understand these machine learning algorithms. 我不了解這些機器學習算法。 I understand how the algorithms work, but not why they work. 我理解的算法是如…

Python第一次周考(0402)

2019獨角獸企業重金招聘Python工程師標準>>> 一、單選 1、Python3中下列語句錯誤的有哪些&#xff1f; A s input() B s raw_input() C print(hello world.) D print(hello world.) 2、下面哪個是 Pycharm 在 Windows 下 默認 用于“批量注釋”的快捷鍵 A Ctrl d…

express 路由中間件_Express通過示例進行解釋-安裝,路由,中間件等

express 路由中間件表達 (Express) When it comes to build web applications using Node.js, creating a server can take a lot of time. Over the years Node.js has matured enough due to the support from community. Using Node.js as a backend for web applications a…

ASP.NET 頁面之間傳值的幾種方式

對于任何一個初學者來說&#xff0c;頁面之間傳值可謂是必經之路&#xff0c;卻又是他們的難點。其實&#xff0c;對大部分高手來說&#xff0c;未必不是難點。 回想2016年面試的將近300人中&#xff0c;有實習生&#xff0c;有應屆畢業生&#xff0c;有1-3年經驗的&#xff0c…

Mapreduce原理和YARN

MapReduce定義 MapReduce是一種分布式計算框架&#xff0c;由Google公司2004年首次提出&#xff0c;并貢獻給Apache基金會。 MR版本 MapReduce 1.0&#xff0c;Hadoop早期版本(只支持MR模型)MapReduce 2.0&#xff0c;Hadoop 2.X版本&#xff08;引入了YARN資源調度框架后&a…

數據可視化圖表類型_數據可視化中12種最常見的圖表類型

數據可視化圖表類型In the current era of large amounts of information in the form of numbers available everywhere, it is a difficult task to understand and get insights from these dense piles of data.在當今時代&#xff0c;到處都是數字形式的大量信息&#xff…

三大紀律七項注意(Access數據庫)

三大紀律&#xff08;規則或范式&#xff09; 要有主鍵其他字段依賴主鍵其他字段之間不能依賴七項注意 一表一主鍵(訂單表&#xff1a;訂單號&#xff1b;訂單明細表&#xff1a;訂單號產品編號)經常查&#xff0c;建索引&#xff0c;小數據&#xff08;日期&#xff0c;數字類…

CentOS下安裝JDK的三種方法

來源&#xff1a;Linux社區 作者&#xff1a;spiders http://www.linuxidc.com/Linux/2016-09/134941.htm 由于各Linux開發廠商的不同,因此不同開發廠商的Linux版本操作細節也不一樣,今天就來說一下CentOS下JDK的安裝: 方法一&#xff1a;手動解壓JDK的壓縮包&#xff0c;然后…

MapReduce編程

自定義Mapper類 class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> &#xff5b; … }自定義mapper類都必須實現Mapper類&#xff0c;有4個類型參數&#xff0c;分別是&#xff1a; Object&#xff1a;Input Key Type-------------K1Text: Input…

統計信息在數據庫中的作用_統計在行業中的作用

統計信息在數據庫中的作用數據科學與機器學習 (DATA SCIENCE AND MACHINE LEARNING) Statistics are everywhere, and most industries rely on statistics and statistical thinking to support their business. The interest to grasp on statistics also required to become…

IOS手機關于音樂自動播放問題的解決辦法

2019獨角獸企業重金招聘Python工程師標準>>> 評估手機自帶瀏覽器不能識別 aduio標簽重的autoplay屬性 也不能自動執行play()方法 一個有效的解決方案是在微信jssdk中調用play方法 document.addEventListener("WeixinJSBridgeReady", function () { docum…

svg標簽和svg文件區別_什么是SVG文件? SVG圖片和標簽說明

svg標簽和svg文件區別SVG (SVG) SVG or Scalable Vector Graphics is a web standard for defining vector-based graphics in web pages. Based on XML the SVG standard provides markup to describe paths, shapes, and text within a viewport. The markup can be embedded…