logo

Declaracions If-else de Python

La presa de decisions és l'aspecte més important de gairebé tots els llenguatges de programació. Com el seu nom indica, la presa de decisions ens permet executar un bloc de codi concret per a una decisió concreta. Aquí, les decisions es prenen sobre la validesa de les condicions particulars. La comprovació de les condicions és la columna vertebral de la presa de decisions.

longitud de matriu java

A Python, la presa de decisions es realitza mitjançant les declaracions següents.

Declaració Descripció
Si Declaració La instrucció if s'utilitza per provar una condició específica. Si la condició és certa, s'executarà un bloc de codi (if-block).
If - else Declaració La sentència if-else és similar a la declaració if excepte el fet que, també proporciona el bloc del codi per al cas fals de la condició que cal comprovar. Si la condició proporcionada a la instrucció if és falsa, s'executarà la instrucció else.
Declaració if niada Les declaracions if niades ens permeten utilitzar if ? declaració else dins d'una declaració if externa.

Sagnat en Python

Per facilitar la programació i per aconseguir la simplicitat, Python no permet l'ús de parèntesis per al codi de nivell de bloc. A Python, el sagnat s'utilitza per declarar un bloc. Si dues sentències estan al mateix nivell de sagnat, formen part del mateix bloc.

En general, es donen quatre espais per sagnar les declaracions que són una quantitat típica de sagnat a Python.

El sagnat és la part més utilitzada del llenguatge Python ja que declara el bloc de codi. Totes les declaracions d'un bloc estan pensades al mateix nivell de sagnat. Veurem com es produeix el sagnat real en la presa de decisions i altres coses a Python.

La declaració if

La instrucció if s'utilitza per provar una condició particular i, si la condició és certa, executa un bloc de codi conegut com a bloc if. La condició de la instrucció if pot ser qualsevol expressió lògica vàlida que es pugui avaluar com a vertader o fals.

Declaracions If-else de Python

La sintaxi de la instrucció if es mostra a continuació.

 if expression: statement 

Exemple 1

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

Sortida:

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

Exemple 2: Programa per imprimir el més gran dels tres nombres.

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

Sortida:

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

La declaració si-else

La sentència if-else proporciona un bloc else combinat amb la sentència if que s'executa en el cas fals de la condició.

Si la condició és certa, s'executa el bloc if. En cas contrari, s'executa el bloc else.

excepció personalitzada a java
Declaracions If-else de Python

La sintaxi de la sentència if-else es mostra a continuació.

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

Exemple 1: programa per comprovar si una persona té dret a votar o no.

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

Sortida:

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

Exemple 2: Programa per comprovar si un nombre és parell o no.

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

Sortida:

text en negreta css
 enter the number: 10 The Given number is even number 

La declaració elif

La sentència elif ens permet comprovar diverses condicions i executar el bloc específic d'instruccions en funció de la condició real entre elles. Podem tenir qualsevol nombre de declaracions elif al nostre programa depenent de la nostra necessitat. Tanmateix, utilitzar elif és opcional.

La sentència elif funciona com una instrucció d'escala if-else-if a C. Ha de ser succeïda per una sentència if.

La sintaxi de la sentència elif es mostra a continuació.

 if expression 1: # block of statements elif expression 2: # block of statements elif expression 3: # block of statements else: # block of statements 
Declaracions If-else de Python

Exemple 1

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

Sortida:

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

Exemple 2

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