logo

Python If-else utasítások

A döntéshozatal szinte az összes programozási nyelv legfontosabb szempontja. Ahogy a név is sugallja, a döntéshozatal lehetővé teszi számunkra, hogy egy adott döntéshez egy adott kódblokkot futtassunk. Itt az adott feltételek érvényessége alapján születnek döntések. Az állapotellenőrzés a döntéshozatal gerince.

halomba rendezni

A pythonban a döntéshozatalt a következő utasítások hajtják végre.

Nyilatkozat Leírás
Ha Nyilatkozat Az if utasítás egy adott feltétel tesztelésére szolgál. Ha a feltétel igaz, akkor egy kódblokk (if-blokk) végrehajtásra kerül.
Ha - különben Nyilatkozat Az if-else utasítás hasonló az if utasításhoz, kivéve azt a tényt, hogy a kód blokkját is megadja az ellenőrizendő feltétel hamis esetéhez. Ha az if utasításban megadott feltétel hamis, akkor az else utasítás végrehajtásra kerül.
Beágyazott if nyilatkozat Beágyazott if utasítások lehetővé teszik számunkra az if ? else utasítás egy külső if utasításon belül.

Behúzás Pythonban

A programozás megkönnyítése és az egyszerűség érdekében a python nem teszi lehetővé a zárójelek használatát a blokk szintű kódhoz. A Pythonban a behúzás a blokk deklarálására szolgál. Ha két utasítás azonos behúzási szinten van, akkor ugyanannak a blokknak a részei.

Általában négy szóköz van megadva az utasítások behúzására, amelyek tipikusan behúzások a pythonban.

A behúzás a Python nyelv leggyakrabban használt része, mivel deklarálja a kódblokkot. Egy blokk összes utasítása azonos szintű behúzásban van. Meglátjuk, hogyan történik a tényleges behúzás a döntéshozatalban és más dolgokban a pythonban.

Az if kijelentés

Az if utasítás egy adott feltétel tesztelésére szolgál, és ha a feltétel igaz, akkor egy if-blokkként ismert kódblokkot hajt végre. Az if utasítás feltétele bármilyen érvényes logikai kifejezés lehet, amely igazra vagy hamisra értékelhető.

Python If-else utasítások

Az if-utasítás szintaxisa alább látható.

 if expression: statement 

1. példa

 # Simple Python program to understand the if statement num = int(input('enter the number:')) # Here, we are taking an integer num and taking input dynamically if num%2 == 0: # Here, we are checking the condition. If the condition is true, we will enter the block print('The Given number is an even number') 

Kimenet:

 enter the number: 10 The Given number is an even number 

2. példa: Program a három szám közül a legnagyobb kinyomtatására.

 # Simple Python Program to print the largest of the three numbers. a = int (input('Enter a: ')); b = int (input('Enter b: ')); c = int (input('Enter c: ')); if a>b and a>c: # Here, we are checking the condition. If the condition is true, we will enter the block print ('From the above three numbers given a is largest'); if b>a and b>c: # Here, we are checking the condition. If the condition is true, we will enter the block print ('From the above three numbers given b is largest'); if c>a and c>b: # Here, we are checking the condition. If the condition is true, we will enter the block print ('From the above three numbers given c is largest'); 

Kimenet:

imessage játékok Androiddal
 Enter a: 100 Enter b: 120 Enter c: 130 From the above three numbers given c is largest 

Az if-else kijelentés

Az if-else utasítás egy else blokkot biztosít az if utasítással kombinálva, amely a feltétel hamis esetben hajtódik végre.

Ha a feltétel igaz, akkor az if-blokk végrehajtásra kerül. Ellenkező esetben az else-blokk végrehajtásra kerül.

Python If-else utasítások

Az if-else utasítás szintaxisa alább látható.

 if condition: #block of statements else: #another block of statements (else-block) 

1. példa: Program annak ellenőrzésére, hogy egy személy jogosult-e szavazni vagy sem.

 # Simple Python Program to check whether a person is eligible to vote or not. age = int (input('Enter your age: ')) # Here, we are taking an integer num and taking input dynamically if age>=18: # Here, we are checking the condition. If the condition is true, we will enter the block print('You are eligible to vote !!'); else: print('Sorry! you have to wait !!'); 

Kimenet:

 Enter your age: 90 You are eligible to vote !! 

2. példa: Program annak ellenőrzésére, hogy egy szám páros-e vagy sem.

 # Simple Python Program to check whether a number is even or not. num = int(input('enter the number:')) # Here, we are taking an integer num and taking input dynamically if num%2 == 0: # Here, we are checking the condition. If the condition is true, we will enter the block print('The Given number is an even number') else: print('The Given Number is an odd number') 

Kimenet:

ascii tábla java
 enter the number: 10 The Given number is even number 

Az elif kijelentés

Az elif utasítás lehetővé teszi számunkra, hogy több feltételt ellenőrizzünk, és végrehajtsuk az adott utasításblokkot attól függően, hogy melyik feltétel közöttük van. Igényünktől függően tetszőleges számú elif utasítás szerepelhet programunkban. Az elif használata azonban nem kötelező.

Az elif utasítás úgy működik, mint egy if-else-if létrautasítás C-ben. Ezt egy if utasításnak kell követnie.

Az elif utasítás szintaxisa alább látható.

 if expression 1: # block of statements elif expression 2: # block of statements elif expression 3: # block of statements else: # block of statements 
Python If-else utasítások

1. példa

 # Simple Python program to understand elif statement number = int(input('Enter the number?')) # Here, we are taking an integer number and taking input dynamically if number==10: # Here, we are checking the condition. If the condition is true, we will enter the block print('The given number is equals to 10') elif number==50: # Here, we are checking the condition. If the condition is true, we will enter the block print('The given number is equal to 50'); elif number==100: # Here, we are checking the condition. If the condition is true, we will enter the block print('The given number is equal to 100'); else: print('The given number is not equal to 10, 50 or 100'); 

Kimenet:

 Enter the number?15 The given number is not equal to 10, 50 or 100 

2. példa

 # Simple Python program to understand elif statement marks = int(input(&apos;Enter the marks? &apos;)) # Here, we are taking an integer marks and taking input dynamically if marks &gt; 85 and marks 60 and marks 40 and marks 30 and marks <= 40): # here, we are checking the condition. if condition is true, will enter block print('you scored grade c ...') else: print('sorry you fail ?') < pre> <p> <strong>Output:</strong> </p> <pre> Enter the marks? 89 Congrats ! you scored grade A ... </pre> <hr></=>