logo

Python If-else 문

의사결정은 거의 모든 프로그래밍 언어에서 가장 중요한 측면입니다. 이름에서 알 수 있듯이 의사결정을 통해 특정 결정을 위해 특정 코드 블록을 실행할 수 있습니다. 여기서는 특정 조건의 타당성에 대한 결정이 내려집니다. 상태 확인은 의사 결정의 핵심입니다.

슬라이스 자바

Python에서는 다음 명령문을 통해 의사결정을 수행합니다.

성명 설명
If 문 if 문은 특정 조건을 테스트하는 데 사용됩니다. 조건이 true이면 코드 블록(if-block)이 실행됩니다.
If - else 문 if-else 문은 확인할 조건의 잘못된 경우에 대한 코드 블록도 제공한다는 점을 제외하면 if 문과 유사합니다. if 문에 제공된 조건이 false이면 else 문이 실행됩니다.
중첩된 if 문 중첩된 if 문을 사용하면 if ? 외부 if 문 내부의 else 문입니다.

Python의 들여쓰기

프로그래밍의 용이성과 단순성을 달성하기 위해 Python에서는 블록 수준 코드에 괄호 사용을 허용하지 않습니다. Python에서는 들여쓰기를 사용하여 블록을 선언합니다. 두 명령문이 동일한 들여쓰기 수준에 있으면 동일한 블록의 일부입니다.

일반적으로 파이썬에서 일반적인 들여쓰기 양인 문장을 들여쓰기 위해 4개의 공백이 제공됩니다.

들여쓰기는 코드 블록을 선언하기 때문에 파이썬 언어에서 가장 많이 사용되는 부분입니다. 한 블록의 모든 명령문은 동일한 수준의 들여쓰기를 사용합니다. 우리는 Python의 의사 결정 및 기타 작업에서 실제 들여쓰기가 어떻게 이루어지는지 살펴보겠습니다.

if 문

if 문은 특정 조건을 테스트하는 데 사용되며 조건이 true이면 if-block이라는 코드 블록을 실행합니다. if 문의 조건은 true 또는 false로 평가될 수 있는 유효한 논리 표현식일 수 있습니다.

Python If-else 문

if문의 구문은 아래와 같습니다.

 if expression: statement 

실시예 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') 

산출:

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

예제 2 : 세 숫자 중 가장 큰 숫자를 인쇄하는 프로그램입니다.

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

산출:

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

if-else 문

if-else 문은 조건이 거짓인 경우 실행되는 if 문과 결합된 else 블록을 제공합니다.

조건이 true이면 if 블록이 실행됩니다. 그렇지 않으면 else 블록이 실행됩니다.

C의 문자열
Python If-else 문

if-else 문의 구문은 다음과 같습니다.

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

예제 1 : 투표할 자격이 있는지 여부를 확인하는 프로그램입니다.

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

산출:

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

예제 2: 숫자가 짝수인지 아닌지 확인하는 프로그램.

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

산출:

문자열 분할 C++
 enter the number: 10 The Given number is even number 

elif 문

elif 문을 사용하면 여러 조건을 확인하고 그 중 실제 조건에 따라 특정 문 블록을 실행할 수 있습니다. 필요에 따라 프로그램에 elif 문을 얼마든지 가질 수 있습니다. 그러나 elif 사용은 선택 사항입니다.

elif 문은 C의 if-else-if 래더 문처럼 작동합니다. 뒤에는 if 문이 있어야 합니다.

elif 문의 구문은 다음과 같습니다.

 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 문

실시예 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'); 

산출:

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

실시예 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></=>