<jupyter_start><jupyter_text>**Python If...Else**  **1. If Statement**- An "if statement" is written by using the if keyword.<jupyter_code># Example - If statement:

a = 33
b = 200
if b > a:
  print("b is greater than a")<jupyter_output>b is greater than a<jupyter_text>- In this example we use two variables, a and b, which are used as part of the if statement to test whether b is greater than a. As a is 33, and b is 200, we know that 200 is greater than 33, and so we print to screen that "b is greater than a".  **2. Indentation**- Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose. - Example - If statement, without indentation (will raise an error):- a = 33- b = 200- if b > a:- print("b is greater than a")  you will get an error- File "demo_if_error.py", line 4    print("b is greater than a")        ^- IndentationError: expected an indented block<jupyter_code># Example - If statement with indentation block

a = 33
b = 200
if b > a:
  print("b is greater than a")<jupyter_output>b is greater than a<jupyter_text>**3. Elif**The elif keyword is pythons way of saying **"if the previous conditions were not true, then try this condition".**<jupyter_code># Example

a = 33
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")<jupyter_output>a and b are equal<jupyter_text>- In this example a is equal to b, so the first condition is not true, but the elif condition is true, so we print to screen that "a and b are equal".  **4. Else**- The else keyword catches anything which isn't caught by the preceding conditions.<jupyter_code>a = 200
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")<jupyter_output>a is greater than b<jupyter_text>- In this example a is greater than b, so the first condition is not true, also the elif condition is not true, so we go to the else condition and print to screen that "a is greater than b". - You can also have an else without the elif:<jupyter_code>#Example

a = 200
b = 33
if b > a:
  print("b is greater than a")
else:
  print("b is not greater than a")<jupyter_output>b is not greater than a<jupyter_text>**5. Short Hand If**- If you have only one statement to execute, you can put it on the same line as the if statement.<jupyter_code># Example - One line if statement:

if a > b : print("a is greater than b")<jupyter_output>a is greater than b<jupyter_text>**6. Short Hand If ... Else**- If you have only one statement to execute, one for if, and one for else, you can put it all on the same line.<jupyter_code># Example - One line if else statement:

a = 2
b = 330
print("A") if a > b else print("B")<jupyter_output>B<jupyter_text>- This technique is known as **Ternary Operators**, or **Conditional Expressions**. - You can also have multiple else statements on the same line:<jupyter_code># Example - One line if else statement, with 3 conditions:

a = 330
b = 330
print("A") if a > b else print("=") if a == b else print("B")<jupyter_output>=<jupyter_text>**7. And**- The and keyword is a logical operator, and is used to combine conditional statements.<jupyter_code># Example - Test if a is greater than b, AND if c is greater than a:

a = 200
b = 33
c = 500
if a > b and c > a:
  print("Both conditions are True")<jupyter_output>Both conditions are True<jupyter_text>**8. Or**- The or keyword is a logical operator, and is used to combine conditional statements.<jupyter_code># Example - Test if a is greater than b, OR if a is greater than c:

a = 200
b = 33
c = 500
if a > b or a > c:
  print("At least one of the conditions is True")<jupyter_output>At least one of the conditions is True<jupyter_text>**9. Nested If**- You can have if statements inside if statements, this is called nested if statements.<jupyter_code># Example 

x = 41

if x > 10:
  print("Above ten,")
  if x > 20:
    print("and also above 20!")
  else:
    print("but not above 20.")<jupyter_output>Above ten,
and also above 20!<jupyter_text>**10. 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.<jupyter_code># Example

a = 33
b = 200

if b > a:
  pass<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09615384615384616, 'fraction_numerical': 0.021749084249084248, 'mean_word_length': 4.004581901489118, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '630497', 'license': 'CC0-1.0', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1387, 'n_words': 686, 'path': 'Python_If_Else.ipynb', 'repo_name': 'pb111/Python', 'size': '4368'}