.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/basics/conditional_statements.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_basics_conditional_statements.py: ============================ 1.8 conditional statements ============================ .. important:: This lesson is still under development. .. GENERATED FROM PYTHON SOURCE LINES 11-19 The basic syntax of if statement in python is: .. code-block:: python if (condition): do something .. GENERATED FROM PYTHON SOURCE LINES 21-27 .. code-block:: default name = "zardari" if name == 'zardari': print('chor') .. rst-class:: sphx-glr-script-out .. code-block:: none chor .. GENERATED FROM PYTHON SOURCE LINES 28-33 .. code-block:: default name = 'Zardari' if name == 'zardari' or name == 'Zardari': print('chor') .. rst-class:: sphx-glr-script-out .. code-block:: none chor .. GENERATED FROM PYTHON SOURCE LINES 34-42 .. code-block:: default name = 'nawaz' if name == 'zardari' or name == 'Zardari': print('chor') if name == 'nawaz' or name == 'Nawaz': print('chor') .. rst-class:: sphx-glr-script-out .. code-block:: none chor .. GENERATED FROM PYTHON SOURCE LINES 43-54 Similarly the syntax for `if` and `elif` statement is .. code-block:: python if (condition): do something elif (condition): do something else: do something .. GENERATED FROM PYTHON SOURCE LINES 56-65 .. code-block:: default name = 'edhi' if name == 'zardari' or name == 'Zardari': print(name, ' is chor') elif name == 'nawaz' or name == 'Nawaz': print(name, ' is chor') else: print(name, ' is not chor') .. rst-class:: sphx-glr-script-out .. code-block:: none edhi is not chor .. GENERATED FROM PYTHON SOURCE LINES 66-71 ``elif`` vs multiple ``if`` ------------------------------- Multiple ifs means, the all the ifs will be checked, while with elif, the code will stop if one of the if is True. .. GENERATED FROM PYTHON SOURCE LINES 73-83 .. code-block:: default age = 14 if age < 16: print("You are a child") if age >= 16: #Greater than or equal to print("You are an adult") else: #Handle all cases where 'age' is negative print("The age must be a positive integer!") .. rst-class:: sphx-glr-script-out .. code-block:: none You are a child The age must be a positive integer! .. GENERATED FROM PYTHON SOURCE LINES 84-94 .. code-block:: default age = 14 if age < 16: print("You are a child") elif age > 16: print("You are an adult") else: #Handle all cases where 'age' is negative print("The age must be a positive integer!") .. rst-class:: sphx-glr-script-out .. code-block:: none You are a child .. GENERATED FROM PYTHON SOURCE LINES 95-98 ``in`` -------- We can use `in` statement to compare a variable against multiple variables. .. GENERATED FROM PYTHON SOURCE LINES 100-109 .. code-block:: default name = 'zardari' if name in ['zardari', 'nawaz', 'chaudhrys', 'mqm']: print(name, ' was democratic thug') elif name in ['zia', 'yahya', 'musharaf', 'ayub']: print(name, ' was non-democratic thug') else: print(name, ' is not chor') .. rst-class:: sphx-glr-script-out .. code-block:: none zardari was democratic thug .. GENERATED FROM PYTHON SOURCE LINES 110-119 .. code-block:: default name = 'Zia' if name.upper() in ['ZARDARI', 'NAWAZ', 'CHAUDHRYS', 'MQM']: print(name, ' was a democratic chor') elif name.upper() in ['ZIA', 'YAHYA', 'MUSHARAF', 'AYUB']: print(name, ' was a non-democratic is chor') else: print(name, ' is not chor') .. rst-class:: sphx-glr-script-out .. code-block:: none Zia was a non-democratic is chor .. GENERATED FROM PYTHON SOURCE LINES 120-122 comparing numbers ------------------- .. GENERATED FROM PYTHON SOURCE LINES 124-134 .. code-block:: default year = 2009 if 2007>year>2000: print('Non-democratic thug ruled Pakistan') elif 2020>year>=2007: print('democratic thug ruled Pakistan') else: print('not considering') .. rst-class:: sphx-glr-script-out .. code-block:: none democratic thug ruled Pakistan .. GENERATED FROM PYTHON SOURCE LINES 135-151 .. code-block:: default year = 2012 if 2007>year>2000: print('Non-democratic thug ruled Pakistan') elif 2020>year>=2007: if 2013>year>=2007: print('democratic thug zardari ruled Pakistan') elif 2018>year>=2013: print('democratic thug Nawaz ruled Pakistan') else: print('It seems the ruler is incapable') else: print('not considering') .. rst-class:: sphx-glr-script-out .. code-block:: none democratic thug zardari ruled Pakistan .. GENERATED FROM PYTHON SOURCE LINES 152-153 One liner .. GENERATED FROM PYTHON SOURCE LINES 155-160 .. code-block:: default day = "14 aug" if day == '14 aug': print('This is independence day not partition day.') .. rst-class:: sphx-glr-script-out .. code-block:: none This is independence day not partition day. .. GENERATED FROM PYTHON SOURCE LINES 161-167 .. code-block:: default oil = True us_presence = 1 if oil else 0 print(us_presence) .. rst-class:: sphx-glr-script-out .. code-block:: none 1 .. GENERATED FROM PYTHON SOURCE LINES 168-169 We can use such one liners to set default values to a variable. .. GENERATED FROM PYTHON SOURCE LINES 171-181 .. code-block:: default human = {"arms": 2, "legs": 2, "head": 1} default_age = 14 age = human["age"] if "age" in human else default_age print(age) .. rst-class:: sphx-glr-script-out .. code-block:: none 14 .. GENERATED FROM PYTHON SOURCE LINES 182-190 .. code-block:: default provinces = 4 capital = "Kathmandu" pm = "unknown" if provinces == 4 and capital == "Islamabad" or pm == "Imran Khan": print("This is Pakistan") .. GENERATED FROM PYTHON SOURCE LINES 191-199 .. code-block:: default provinces = 4 capital = "unknown" pm = "Imran Khan" if provinces == 4 and capital == "Islamabad" or pm == "Imran Khan": print("This is Pakistan") .. rst-class:: sphx-glr-script-out .. code-block:: none This is Pakistan .. GENERATED FROM PYTHON SOURCE LINES 200-203 ``if not`` vs ``!=`` --------------------- Inner working is different but the output is same however ``not`` is preferred. .. GENERATED FROM PYTHON SOURCE LINES 205-210 .. code-block:: default day = "Thursday" if day != 'Friday': print('no jumma prayer') .. rst-class:: sphx-glr-script-out .. code-block:: none no jumma prayer .. GENERATED FROM PYTHON SOURCE LINES 211-216 .. code-block:: default day = "Thursday" if not day == 'Friday': print('no jumma prayer') .. rst-class:: sphx-glr-script-out .. code-block:: none no jumma prayer .. GENERATED FROM PYTHON SOURCE LINES 217-223 .. code-block:: default if day == 'Friday': pass else: print("no jumma prayer") .. rst-class:: sphx-glr-script-out .. code-block:: none no jumma prayer .. GENERATED FROM PYTHON SOURCE LINES 224-226 ``any`` vs ``all`` ------------------- .. GENERATED FROM PYTHON SOURCE LINES 228-233 .. code-block:: default a = [False, 2>4, 2!=1] if any(a): print('go ahead') .. rst-class:: sphx-glr-script-out .. code-block:: none go ahead .. GENERATED FROM PYTHON SOURCE LINES 234-239 .. code-block:: default a = [False, 2>4, 2!=1] if all(a): print('go ahead') .. GENERATED FROM PYTHON SOURCE LINES 240-248 **Question** What will be the output of following code? .. code-block:: python a = [True, 2>4, 2!=1] if any(a): print('go ahead') .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.007 seconds) .. _sphx_glr_download_auto_examples_basics_conditional_statements.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/AtrCheema/python-seekho/master?urlpath=lab/tree/notebooks/auto_examples/basics/conditional_statements.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: conditional_statements.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: conditional_statements.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_