Python中的條件語句(if,if ... else,if ... elif ... else和嵌套的if)

Conditional statements decide the flow of program execution. In programming whenever we need to make execute any special blocks based on the decision then we use the conditional statements.

條件語句決定程序執行的流程。 在編程中,只要我們需要根據決策執行任何特殊的塊,就使用條件語句

No need to be confused about it because we are using it in our daily life when we need to make some decisions and based on these decisions, we decide what should we have to do next. Similar situations can occur while programming also where we need to make some decisions based on conditions given and based on these decisions we will execute the block of code.

無需對此感到困惑,因為當我們需要做出一些決定時,我們就在日常生活中使用它,并根據這些決定來決定下一步該做什么。 在編程時也會發生類似情況,在這種情況下,我們需要根據給定的條件做出一些決策,并根據這些決策執行代碼塊。

Conditional statements available in the Python are,

Python中可用的條件語句

  1. if statements

    如果陳述

  2. if...else statements

    如果...其他陳述

  3. if...elif...else statements

    if ... elif ... else語句

  4. Nested if statements

    嵌套if語句

1)Python if語句 (1) Python if statement)

It is one of the most common conditional statements in which some conditions are provided and if the condition is true then block under the if the condition will be executed.

它是最常見的條件語句之一,其中提供了一些條件,如果條件為true,則在條件將被執行時阻塞。

Syntax:

句法:

    if condition:
# what we want to execute here.

Example:

例:

# input age
age=int(input('what is your age: '))
# checking the condition
if age>=18:
print('You are Grown-up now !')

Output

輸出量

RUN 1:
what is your age: 21
You are Grown-up now !
RUN 2:
what is your age: 15

2)Python if ... else語句 (2) Python if...else statement )

In the above, we have seen that if the condition is true then block under if will execute then one thing is, comes to our mind that what happens when the condition will be false. So, to overcome this problem we are using if...else statements.

在上面的內容中,我們已經看到,如果條件為true,則在條件執行時阻塞,然后發生一件事,我們想到的是,條件為false時會發生什么。 因此,為了克服這個問題,我們使用了if ... else語句

Syntax:

句法:

    if condition:
# what we want to execute here.
else:
# what we want to execute here.

If the condition is true, then it will execute the block of if statements otherwise else statement.

如果條件為true ,則它將執行if語句的塊,否則執行else語句。

Example:

例:

# input age
age=int(input('what is your age: '))
# checking the condition
if age>=18:
print('You are Grown-up now !')
else:
print('You are Young!')

Output

輸出量

RUN 1:
what is your age: 21
You are Grown-up now !
RUN 2:
what is your age: 15
You are Young!

3)Python if ... elif ... else語句 (3) Python if...elif...else statement)

These conditional statements use where we have to check multiple conditions in the program. If these will not true that is false then the else blocks only execute.

這些條件語句用于我們必須檢查程序中的多個條件的地方。 如果這些都不 ,則返回true ,否則else塊僅執行。

Syntax:

句法:

    if condition:
# what we want to execute here.
elif conditions:
# what we want to execute here.
else:
# what we want to execute here.

Example:

例:

# input the age
n=int(input('Enter marks: '))
# checking the conditions
if n>=90:
print('Excellent')
elif n<90 and n>=75:
print('Passed')
else:
print('Fail')

Output

輸出量

RUN 1:
Enter marks: 95
Excellent
RUN 2:
Enter marks: 80
Passed
RUN 3:
Enter marks: 63
Fail

4)Python嵌套的if語句 (4) Python Nested if statement)

As we all have familiar with the word nested which means one statement inside another statement same in the programming nested if statements mean an if statement inside another if statement that is we can place one if statements inside another if statements.

眾所周知,嵌套一詞意味著在編程中相同的另一個語句中的一個語句,嵌套的if語句意味著另一個if語句內部的if語句,我們可以將一個if語句放在另一個if語句中。

Syntax:

句法:

    if condition:
# what we want to execute here.
if condition:
# what we want to execute here.
else:
# what we want to execute here.

Note: In Python, true and false are written as True and False. Since Python follow the indentation rule so the statements must write under the indentations of if statements or other.

注意:在Python中, truefalse分別寫為TrueFalse 。 由于Python遵循縮進規則,因此語句必須在if語句或其他語句的縮進下編寫。

Now, we will see an example based on the above conditional statements which will show us the grade of students.

現在,我們將基于上述條件陳述看到一個示例,該示例將向我們顯示學生的成績。

Example:

例:

# input the age
n=int(input('Enter marks: '))
# checking the conditions
if  n>=75:
if n >=95:
print('Excellent')
else:
print('Pass')
else:
print('Fail')

Output

輸出量

RUN 1:
Enter marks: 96
Excellent
RUN 2:
Enter marks: 89
Pass
RUN 3:
Enter marks: 69
Fail

翻譯自: https://www.includehelp.com/python/conditional-statements-if-if-else-if-elif-else-and-nested-if.aspx

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

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

相關文章

控制臺應用和空項目有什么區別_在公司做的項目和自己在學校做的有什么區別?...

前言 只有光頭才能變強。 文本已收錄至我的GitHub倉庫,歡迎Star:https://github.com/ZhongFuCheng3y/3y 不知道大家還是學生的時候有沒有這個問題:公司做的項目和自己在學校練手的項目有多大的區別。我以前在學校跟著視頻做一些項目練手,總感覺公司做的東西會要難很多,不知…

小樣本點云深度學習庫_合成魯棒的對抗樣本來欺騙深度學習分類器

本期一諾sec關注深度學習系統安全問題&#xff0c;推薦一篇來自ICML 2018會議論文Synthesizing Robust Adversarial Examples。論文鏈接http://proceedings.mlr.press/v80/athalye18b.html。深度模型對于對抗樣本具有高度的脆弱性&#xff0c;這已經是得到大家印證的事實。自從…

stl字符串去除空格_在列表中推送字符并在C ++ STL中將它們打印為空格

stl字符串去除空格In this example, we are declaring a character list and pushing the characters from A to Z using a for loop and push_back() function then printing the value of the vector separated by space. 在此示例中&#xff0c;我們聲明了一個字符列表&…

java數據類型_JAVA基礎篇(數據類型)

首先請大家想想這幾個問題&#xff1a;1.java數據類型是什么&#xff1f;2.Java數據類型有什么用&#xff1f;上一節&#xff08;JAVA基礎篇&#xff08;函數&#xff09;&#xff09;有個add函數&#xff0c;里面有兩個int類型&#xff0c;int類型就是整數的意思&#xff0c;這…

SharePoint CAML In Action——Part I

閱讀目錄 CAML In Action接下來在SharePoint中&#xff0c;我們經常要對List進行操作&#xff0c;比如要從List中取出相應的ListItem&#xff0c;利用CAML是個好辦法。在沒了解CAML之前&#xff0c;我是這樣取數據的&#xff1a; MyList.Items.Cast<SPListItem>().ToList…

地圖統計_博客 城市訪問量統計并且通過Echarts+百度地圖展示

本篇講解一下 如何在Vue 中使用 Echarts 百度地圖 統計 博客訪問量 并且通過QQWry 解析 ip 地址 利用Echarts 展示出來效果圖如下&#xff1a;1.純真Ip地址庫 QQWry這是我在github上找的 java版本的 解析 qqwry的1.1 maven 引入 qqwry<dependency> <grou…

修改console緩存大小_更改緩存的行大小將如何影響其他參數?

修改console緩存大小Prerequisites: Memory mapping and its types 先決條件&#xff1a; 內存映射及其類型 While designing a cache system of a PC, the size of cache lines is an important parameter. 在設計PC的緩存系統時&#xff0c;緩存行的大小是重要的參數。 In …

win10必須禁用的服務_Win10系統中這3個無用的設置,奉勸你還是早點關閉吧!

在PC端所有的操作系統中&#xff0c;占據市場份額最大的莫過于微軟發布的windows系統。其中最經典的莫過于XP和win7&#xff0c;無奈微軟已經停更了這兩個操作系統&#xff0c;所以為了電腦的安全著想&#xff0c;很多人都直接升級更新至最新版的win10系統&#xff0c;目前win1…

Android 布局練習

要求&#xff1a;使用多種布局完成以下練習。 1.要求效果 完成效果 代碼&#xff1a; <?xml version"1.0" encoding"utf-8"?> <…

有危害嗎_涂料漆對身體有害嗎?涂料漆危害怎么預防

目前很多人都會通過涂料漆來進行墻面裝飾&#xff0c;用它來對墻面進行裝飾是可以馬上的改善墻壁的狀態&#xff0c;但有些人也擔心它會對身體有害&#xff0c;涂料漆對身體有害嗎?由于擔心涂料漆會給健康帶來危害&#xff0c;很多人都想要預防&#xff0c;那涂料漆危害怎么預…

小寫大寫轉換_小寫到大寫轉換器JavaScript工具| 網絡應用項目

小寫大寫轉換Hi! At times, beginners always find it hard getting the application of the theory they learn in programming or a particular language. 嗨&#xff01; 有時&#xff0c;初學者總是很??難在編程或特定語言中應用他們學到的理論。 In this article, well…

inventor扳手制作視頻_弱電工程視頻監控系統施工方案,可作施工組織設計

1 工程概況 1.1 編制《工程總體實施方案》 主要包括&#xff1a;結合高清監控系統設計方案作配套的深化設計&#xff0c;編制高清監控系統實施計劃&#xff0c;并提出相關的配合要求。根據總體方案&#xff0c;對高清監控系統工程的技術設計作必要的補充。并提出相關的實施技術…

python print與input

python基礎語法1print()函數input()函數print()函數 不用引號&#xff0c;函數內為數字或數字運算 單引號&#xff0c;整條語句結構&#xff0c;’\n’ 雙引號&#xff0c;函數結構 三引號&#xff0c;對內容進行換行輸出 print("let is go")#函數結構 print(let i…

lol最克制諾手的英雄_LOL:究竟有沒有完美克制諾手的英雄?時光上單或可一戰?...

小伙伴們大家好&#xff0c;我是小數點。諾克薩斯之手德萊厄斯&#xff0c;他可以說是每一位上單玩家的噩夢了&#xff0c;因為喜歡玩諾手的人特別多&#xff0c;而會玩的諾手卻一般都在對面。要知道諾手這樣英雄拿到優勢兇起來&#xff0c;你就沒得打了&#xff0c;就算在塔下…

Oracle 創建表空間,用戶,賦值(簡裝)

一&#xff0c;1.Oracle 創建表空間&#xff0c;用戶&#xff0c;賦值&#xff08;簡裝&#xff09;C:\Documents and Settings\Administrator>sqlplus /nologSQL> conn /as sysdba2.刪除用戶drop user username cascade;3.創建自增表表空間SQL> create tablespace 表…

編程語言難度排名_編程語言TOP10!該如何選擇適合自己的?

本文轉載自公眾號“讀芯術”(ID&#xff1a;AI_Discovery)編程領域大約有700種代碼語言。理解編程語言的重要性以及其如何影響需要執行的具體任務至關重要。一篇文章窮盡700 種語言不現實&#xff0c;也沒有意義。因此&#xff0c;筆者挑選出了時下最熱門的原因&#xff0c;在本…

測試私有方法 重構_一個全棧工程師重構之路:中小公司 DevOps 落地實踐

為了這篇文章&#xff0c;我前后寫了將近十篇文章鋪墊&#xff0c;才將這篇整體重構思想引出。背景先說下背景&#xff0c;我們是一家小公司&#xff0c;雖然打著做產品的旗幟&#xff0c;但是每個客戶都有大量的個性化功能&#xff0c;這里指各個客戶的java端、Android端、ios…

python變量 數據類型 列表 元組 字典

python基礎語法2變量數據類型與類型轉換列表添加列表元素修改元素刪除列表元素組織列表創建數值列表操作列表元組元組轉列表字典創建字典列表取值字典刪除增加修改變量 變量命名要求&#xff1a; 1.只能是一個詞 2.只能包含字母、數字、下劃線 3.不能用數字開頭 變量定義位置不…

HDU 5777 domino

貪心一下。有k次機會&#xff0c;也就是那些數字中&#xff0c;最大的k-1可以不選擇。答案為&#xff1a;sum{a[i]}-sum{最大的k-1個a[i]}n。注意&#xff1a;k>n的時候直接輸出n。 #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio&…

puppeteer執行js_使用Node.js和Puppeteer與表單和網頁進行交互– 2

puppeteer執行jsHi guys! Today lets look at another powerful function of the puppeteer API using Node.js part 2. 嗨&#xff0c;大家好&#xff01; 今天&#xff0c;讓我們看看使用Node.js第2部分的puppeteer API的另一個強大功能。 In the first part of this sectio…