Thứ Tư, 28 tháng 9, 2016

Python 3 Tutorial : IF ELIF ELSE Conditionals Statements in Python 3


Python 3 Tutorial - In python 3 languages, else statement can be combined with an if/elif statement. Else statement has mean the block of code that will executes if the conditional expression resolves to 0 or a FALSE value in the if statement or an function.

At the previews lessons, we have learn about python 3, please read :
  1. Basics Syntax in Python 3
  2. Create First Program with Python 3
  3. How to Install Python Tools for Visual Studio 2015
Next, we will learn how to use IF ELIF ELSE Conditionals Statements in Python 3, The else statement is an optional statement and there could be at most only one else statement following if syntax :

IF ELSE Statement

#!/usr/bin/python3
if expression:
   our statement(s)
else:
   our statement(s)

for more example using the if else statement in python 3

#!/usr/bin/python3
data = int(input("Enter any integer : "))
if data < 6000:
    price = data * 1.14
    print ("the price is ",price)
else:
    price = data * 2
    print ("the price is ",price)
print ("All Price is :",data - price)

try to debugging your application, then tell me what're you getting.

IF ELIF ELSE Conditionals Statements in Python 3

IF ELIF ELSE Statements

In python 3 language, elif statement will allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE.

To try using if elif else statement, following this python syntax code :

if expression-1:
   our statement(s)
elif expression-2:
   our statement(s)
elif expression-3:
   our statement(s)
else:
   our statement(s)

for example

#!/usr/bin/python3
data = int(input("Enter any integer : "))
if data < 6000:
    price = data * 1.14
    print ("the price is ",price)
elif data < 3000:
    discount= data * 1
    print ("the price is ",price)
else:
    price = data * 2
    print ("the price is ",price)
print ("All Price is :",data - price)

if you still confused, or you use python 2, go to this page "Python 2 Tutorial".

see you next lessons ......

Không có nhận xét nào:

Đăng nhận xét