Press "Enter" to skip to content

Python conditional statement

Zigya Acadmey 0

Under certain conditions, some decisions are sometimes in normal life inevitable, as we can see in our photo. It’s the same for every program, which has to solve some useful problem. There is hardly a way to program without having branches in the flow of code.

In programming and scripting languages, conditional statements or conditional constructs are used to perform different computations or actions depending on whether a condition evaluates to true or false. (Please note that true and false are always written as True and False in Python.)

The condition usually uses comparisons and arithmetic expressions with variables. These expressions are evaluated to the Boolean values True or False. The statements for the decision taking are called conditional statements, alternatively, they are also known as conditional expressions or conditional constructs.

The if-then construct (sometimes called if-then-else) is common across many programming languages, but the syntax varies from language to language.

The if Statement

In order to write useful programs, we almost always need the ability to check conditions and change the behavior of the program accordingly. Conditional statements give us this ability. The simplest form is the if statement, which has the general form:

if BOOLEAN EXPRESSION:
    STATEMENTS

A few important things to note about if statements:

  1. The colon (:) is significant and required. It separates the header of the compound statement from the body.
  2. The line after the colon must be indented. It is standard in Python to use four spaces for indenting.
  3. All lines indented the same amount after the colon will be executed whenever the BOOLEAN_EXPRESSION is true.

Here is an example:

food = 'spam'

if food == 'spam':
    print('Ummmm, my favorite!')
    print('I feel like saying it 100 times...')
    print(100 * (food + '! '))

The boolean expression after the if statement is called the condition. If it is true, then all the indented statements get executed. What happens if the condition is false, and food is not equal to 'spam'? In a simple if statement like this, nothing happens, and the program continues on to the next statement.

True or False

Unfortunately it is not as easy in real life as it is in Python to differentiate between true and false:
The following objects are evaluated by Python as False:

  • numerical zero values (0, 0L, 0.0, 0.0+0.0j),
  • the Boolean value False,
  • empty strings,
  • with empty lists and empty tuples,
  • empty dictionaries.
  • plus the special value None.

All other values are considered to be True.

Abbreviated IF statement

C programmers usually know the following abbreviated notation for the if construct:

max = (a > b) ? a : b; 

This is an abbreviation for the following C code:

if (a > b)
   max=a;
else
   max=b;  

C programmers have to get used to a different notation in Python:

max = a if (a > b) else b;

Nested If

You can have if statements inside if statements, this is called nested if statements.

x = 41

if x > 10:
  print("Above ten,")
  if x > 20:
    print("and also above 20!")
  else:
    print("but not above 20.")

The pass Statement

if statements cannot be empty, but if you for some reason have an if statement with no content put in the pass statement to avoid getting an error.

a = 33
b = 200

if b > a:
  pass

Conclusion

In this article, We studied what are conditional statements how to use them with examples.

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.

Do visit our page www.zigya.com/blog for more informative blogs on Data Science

Keep Reading! Cheers!

Zigya Academy
BEING RELEVANT

Leave a Reply

Your email address will not be published. Required fields are marked *