If statements

If statements are all about choices.

A block of code is executed based on one or more conditions. The block of code will only be executed if the condition is true.

Related Course:

If statements

Computer programs do not only execute instructions. Occasionally, a choice needs to be made. Such as a choice is based on a condition.

Python has several conditional operators:

> greater than
< smaller than
== equals
!= is not

Conditions are always combined with variables. A program can make a choice using the if keyword. For example:

The condition is shown on line 3.

When you execute this program it will always print ‘End of program’, but the text ‘You guessed correctly!’ will only be printed if the variable x equals to four (see table above).

Note: Code is executed based on the variable x. Try different numbers: 2,3,4

If else

Python can also execute a block of code if x does not equal to 4. The else keyword is used for that.

If x is set to 2, the second code block is executed. If x equals (==) four, the first code block is executed.

Take a look at the execution below:

if statement

 

Previous Post
Next Post

16 Comments

  • jeykiran says:

    Where do i type in this command
    x = int(input(“Tell X”))

    if x == 4:
    print(‘You guessed correctly!’)
    else:
    print(‘Wrong guess’)

    print(‘End of program.’)
    i tried to type this command in python 3.5.2 Shell, its showing an error. i need help in this.

    Thanks

  • emeka says:

    after writing print( ‘you guessed correctly!’) i get this error message indentation error ; expected an intend block please help , when i go back and click enter it works normal please assist

  • Anthony Newton says:

    Hi, I need help with this. For example, the:

    x = int(input(“Tell X”))

    if x == 4:
    print(‘You guessed correctly!’)
    else:
    print(‘Wrong guess’)

    print(‘End of program.’)

    One that it gives as an example, I want to know how to run it all at once in Python. When I type it out, after I type the:

    if x == 4:
    print(‘You guessed correctly!’)
    else:
    print(‘Wrong guess’)

    Bit out it already prints either “You guessed correctly” or “Wrong guess” but I want them to print after I have typed all the code. And when I copy and paste it all in at once, it doesn’t recognise another line/bit of it as meant to be on a new line and it doesn’t put the >>> before each bit therefore not running it properly.

    Am I missing something?

  • abdulmajeed says:

    after typing x = int(input(“tell x”)) in the second line it says tell x , at the same line i write if x == 4 and then it says Traceback (most recent call last):
    File “”, line 1, in
    File “”, line 1
    if x == 4:
    ^
    SyntaxError: invalid syntax
    and if i press enter its the same , how should i go to the third line without this appearing

  • abdulmajeed says:

    when i type x = int(input(“Tell X”)) in the second line it says tell x, the what should i do to go to the next line without is saying syntaxerorr: invalid syntax

  • user says:

    I have also not obtained, and spaces set, the combination of money changers, execution begins after the Enter.

  • Alexander Horton says:

    What program exactly should I be using for this? I am using the Python 3.5.1 Shell on Mac and when I attempt to do the beginning of the program (x = int(input(“tell x”))) and hit enter a blue “Tell X” appears and if I attempt to go down another line to continue the program it treats it as if I have ended that program. This is probably an easy fix but any help is appreciated 🙂

  • Sagar Dasgupta says:

    After writing the word else and typing the colon it is saying invalid syntax. I need help with this.

Leave a Reply