pub-3785248829033902 | Python: Elif, what's your gender? | - Big data with Python

| Python: Elif, what's your gender? |

The elif keyword is a composite of else and if.

Using only else and if, you might end having a script looking more or less cluttered. The elif clause is a combination of the else clause and a separate if statement.


The best way to learn new things is to take a practical approach of the things you want to learn. Here is a fragment of code that demonstrates the use of the elif statement in Python:

Æ’ python command

def get_gender(sex='Unknown'):
    if sex is 'm':
        sex = 'Male'
    elif sex is 'f':
        sex = 'Female'
    print(sex)
get_gender()
get_gender('m')
get_gender('f')

output

Unknown
Male
Female


Previous
Next Post »