Blog

# Condition Objects - Python

• ###### | 1596 Ratings

If you have knowledge of other programming languages, then you would know the importance of conditional statements. Conditional statements are required for taking decisions. Whenever we operate the software, our choice of action decides the next step. A simple example is booking an air ticket. If you want to travel in economy class, you will be presented a different package, whereas if you opt for business class you will be given a different package. So, let’s take a look at the conditional statements available in python.

Learn how to use Python, from beginner basics to advanced techniques, with online video tutorials taught by industry experts. Enroll for Free Python Training Demo!

-An else statement can be combined with an if statement. An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value.

Format:

```if (expression): code1

else:

code2```

EXAMPLE:

```a= 7
b=8
if(a>b):
print ‘I am greater than b’ else:
print ‘I am greater than a’```

Output :I am greater than a

-To check multiple conditions and run the block of code for the condition that returns ‘true’

Format:

```if expression1: code1

elif expression2: code2

elif expression3: code3

else:

code4```

EXAMPLE:

```a=7
b=7
if(a>b):
print ‘I am greater than b’ elif (b>a):
print ‘I am greater than a’ elif (a==b):
print ‘Hey!! we both are equal’```

Output: Hey!! we both are equal

- A simple if statement can be used without any else or else if. Usage of simple if statement means that the lines of code should be executed only if the condition is true.

Format:

`if condition : execute the code`

EXAMPLE:

```a=7
b=7
if(a>b):
print ‘I am greater than b’ print ‘Good Bye’```

Output: Good Bye
{"ft_lang_label":"__label__en","ft_lang_prob":0.79812354,"math_prob":0.9014229,"size":1652,"snap":"2019-51-2020-05","text_gpt3_token_len":400,"char_repetition_ratio":0.1425971,"word_repetition_ratio":0.06985294,"special_character_ratio":0.23607749,"punctuation_ratio":0.13450292,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97037095,"pos_list":[0,1,2,3,4,5,6,7,8],"im_url_duplicate_count":[null,9,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-27T12:25:15Z\",\"WARC-Record-ID\":\"<urn:uuid:4c2875b8-4c4f-4a17-a593-30c939c34636>\",\"Content-Length\":\"135604\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7165dac3-63c9-4805-870c-19573226e17a>\",\"WARC-Concurrent-To\":\"<urn:uuid:45a26f7c-6159-4ed3-a40d-9ca0fa5c797d>\",\"WARC-IP-Address\":\"109.199.103.126\",\"WARC-Target-URI\":\"https://mindmajix.com/python/conditions\",\"WARC-Payload-Digest\":\"sha1:A4DEK6CPT2FDMA25TXUX4DRDUFUERFGQ\",\"WARC-Block-Digest\":\"sha1:SJXQSEUVAHGUK6HG7GGFSDTGEXHKUAYD\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579251700675.78_warc_CC-MAIN-20200127112805-20200127142805-00034.warc.gz\"}"}