logo

Python If-else teiginiai

Sprendimų priėmimas yra svarbiausias beveik visų programavimo kalbų aspektas. Kaip rodo pavadinimas, sprendimų priėmimas leidžia paleisti tam tikrą kodo bloką tam tikram sprendimui. Čia sprendimai priimami dėl konkrečių sąlygų galiojimo. Būklės tikrinimas yra sprendimų priėmimo pagrindas.

Linux redaguoti failą

Python sistemoje sprendimai priimami šiais teiginiais.

pareiškimas apibūdinimas
Jei pareiškimas Jei sakinys naudojamas konkrečiai sąlygai patikrinti. Jei sąlyga teisinga, bus vykdomas kodo blokas (jei-blokas).
Jei – kitaip Pareiškimas Jei-else teiginys yra panašus į if teiginį, išskyrus tai, kad jame taip pat pateikiamas kodo blokas, skirtas patikrinti klaidingą sąlygos atvejį. Jei if sakinyje pateikta sąlyga yra klaidinga, bus vykdomas teiginys else.
Įdėta, jei pareiškimas Įdėtas if teiginiai leidžia mums naudoti if ? else teiginys išorinio if teiginio viduje.

Įtrauka Python

Kad būtų lengviau programuoti ir pasiekti paprastumo, python neleidžia naudoti skliaustų bloko lygio kodui. Python programoje įtrauka naudojama blokui deklaruoti. Jei du teiginiai yra tame pačiame įtraukos lygyje, tada jie yra to paties bloko dalis.

Paprastai teiginiams, kurie yra tipiškas python įtraukos dydis, yra skiriami keturi tarpai.

Įtrauka yra dažniausiai naudojama python kalbos dalis, nes ji deklaruoja kodo bloką. Visi vieno bloko teiginiai yra skirti to paties lygio įtraukoje. Pamatysime, kaip faktinė įtrauka vyksta priimant sprendimus ir kitus dalykus python.

Jei teiginys

Jei sakinys naudojamas tam tikrai sąlygai patikrinti, o jei sąlyga teisinga, jis vykdo kodo bloką, vadinamą if-bloku. Jei sakinio sąlyga gali būti bet kokia tinkama loginė išraiška, kuri gali būti įvertinta kaip teisinga arba klaidinga.

Python If-else teiginiai

If-teiginio sintaksė pateikta žemiau.

 if expression: statement 

1 pavyzdys

 # 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') 

Išvestis:

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

2 pavyzdys: Programa spausdinti didžiausią iš trijų skaičių.

 # 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'); 

Išvestis:

 Enter a: 100 Enter b: 120 Enter c: 130 From the above three numbers given c is largest 

Jei-kitas teiginys

Jei-else sakinys pateikia else bloką kartu su if sakiniu, kuris vykdomas klaidingu sąlygos atveju.

Jei sąlyga yra teisinga, įvykdomas if-blokas. Kitu atveju bus vykdomas blokas else.

objektų klasė java
Python If-else teiginiai

If-else teiginio sintaksė pateikta žemiau.

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

1 pavyzdys: programa, skirta patikrinti, ar asmuo turi teisę balsuoti, ar ne.

 # 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 !!'); 

Išvestis:

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

2 pavyzdys: Programa, skirta patikrinti, ar skaičius yra lyginis, ar ne.

 # 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') 

Išvestis:

objekto konvertavimas į eilutę
 enter the number: 10 The Given number is even number 

Elif pareiškimas

Elif sakinys leidžia mums patikrinti kelias sąlygas ir vykdyti konkretų teiginių bloką, priklausomai nuo tikrosios sąlygos tarp jų. Priklausomai nuo mūsų poreikio, savo programoje galime turėti bet kokį elif teiginių skaičių. Tačiau elif naudojimas yra neprivalomas.

Elif teiginys veikia kaip if-else-if kopėčių sakinys C kalboje. Jį turi pakeisti if teiginys.

Elif teiginio sintaksė pateikta žemiau.

 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 teiginiai

1 pavyzdys

 # 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'); 

Išvestis:

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

2 pavyzdys

 # 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></=>