[Python] If statement

 In this chapter we're going to learn if statement, this function is very useful.

I'm going to create Boolean variable(True or False) that's going to store whether or not the user is male

is_male = True

if is_male:
    print("You are a man")

Let's say is_male = True; means  male is True, The other is False 

so if it's the True, python is going to print "You are a man" and if it's not male, python prints nothing

If you want to print something even if it's not true, you can make like this;

is_male = True

if is_male:
    print("You are a man")
else:
    print("you are not a man")

then it will print "you are not a man" instead of nothing printed.

let me make it more complex

is_male = False
is_tall = True

if is_male or is_tall:
    print("You are a man or tall or both")
else:
    print("you neither man or tall")

You are a man or tall or both

now we made two Boolean variables, so if it's male or tall or both, it will print "You are a man or tall or both" . Otherwise, "You neither man or tall"


 "elif" it stands for "Else If", you can make other conditions inside.

is_male = True
is_tall = False

if is_male and is_tall:
    print("You are a man and tall")
elif is_male and not(is_tall):
    print("You a short man")
elif not(is_male) and is_tall:
    print("You are not a man but tall")
else:
    print("You neither man or tall")


I made more conditions. This time male is True, tall is False. So we're going to get

"You a short man" 


what if; 

is_male = False
is_tall = True


Yes. You are not a man but tall

 




Popular posts from this blog

[Python] Dictionary

[Visual Design 2/3]

[JavaScript] For loop , Function