{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyMiVtMQ21BQ+AdT4sgnWMy1",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"
"
]
},
{
"cell_type": "markdown",
"source": [
"# **Best Practices for Conditional Statements**\n",
"Practice\tWhy It Matters\tExample\n",
"Order conditions properly\tEnsures correct logic flow\tMost restrictive first\n",
"\n",
"Use descriptive conditions\tImproves code readability\tif is_valid_score:\n",
"\n",
"Validate input\tPrevents runtime errors\tif 0 <= score <= 100:\n",
"\n",
"\n"
],
"metadata": {
"id": "xtIJiHITRPjT"
}
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Fkk0WNpnRHQ2",
"outputId": "18f000e9-0ab5-460e-a1d5-c35ec46e3051"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Enter temperature in Celsius: 20\n",
"20.0°C = 68.0°F\n",
"Weather: Cool - Light jacket recommended\n",
"For List 1:\n",
"list contains an even number\n",
" \n",
"For List 2:\n",
"list does not contain an even number\n",
"Or here? Or over here?\n",
"1\n",
"Yes\n",
"Enter an integer: 1\n",
"Enter an integer: 1\n",
"W\n",
"Y\n",
"Alexa, do not add Apples to the cart.\n",
"The number is odd\n",
"The number is less than 3\n",
"The number is neither divisible by 5 nor 10\n",
"The character is A\n",
"The number is divisible by 3 but not 5\n",
"a,b, and c are equal\n",
"n is equal to 1\n",
"The value of n is 1\n",
"You have the right to vote!\n",
"c is the highest\n",
"It is a leap year\n",
"Isosceles Triangle\n",
"x is greater than 5\n",
"You are eligible to vote.\n",
"You are eligible to vote and also in the youth demographic.\n",
"You are eligible to vote in the USA.\n",
"Eligible to vote.\n",
"Two\n",
"You are eligible to vote.\n",
"You are eligible to vote.\n"
]
}
],
"source": [
"celsius = float(input(\"Enter temperature in Celsius: \"))\n",
"fahrenheit = (celsius * 9/5) + 32\n",
"\n",
"print(f\"{celsius}°C = {fahrenheit}°F\")\n",
"\n",
"# Weather description based on Celsius\n",
"if celsius <= 0:\n",
" description = \"Freezing - Water freezes\"\n",
"elif celsius <= 10:\n",
" description = \"Very Cold - Wear heavy clothing\"\n",
"elif celsius <= 20:\n",
" description = \"Cool - Light jacket recommended\"\n",
"elif celsius <= 30:\n",
" description = \"Comfortable - Perfect weather\"\n",
"elif celsius <= 35:\n",
" description = \"Warm - Stay hydrated\"\n",
"else:\n",
" description = \"Very Hot - Seek shade and drink plenty of water\"\n",
"\n",
"print(f\"Weather: {description}\")\n",
"\n",
"# Python 3.x program to check if an array consists\n",
"# of even number\n",
"def contains_even_number(l):\n",
" for ele in l:\n",
" if ele % 2 == 0:\n",
" print (\"list contains an even number\")\n",
" break\n",
"\n",
" # This else executes only if break is NEVER\n",
" # reached and loop terminated after all iterations.\n",
" else:\n",
" print (\"list does not contain an even number\")\n",
"\n",
"# Driver code\n",
"print (\"For List 1:\")\n",
"contains_even_number([1, 9, 8])\n",
"print (\" \\nFor List 2:\")\n",
"contains_even_number([1, 3, 5])\n",
"\n",
"\n",
"x = 3\n",
"if x == 0:\n",
" print (\"Am I here?\", end = ' ')\n",
"elif x == 3:\n",
" print(\"Or here?\", end = ' ')\n",
"else :\n",
" pass\n",
"print (\"Or over here?\")\n",
"\n",
"x = float(input())\n",
"if(x==1):\n",
" print(\"Yes\")\n",
"elif (x >= 2):\n",
" print(\"Maybe\")\n",
"else:\n",
" print (\"No\")\n",
"\n",
"\n",
"a = int(input(\"Enter an integer: \"))\n",
"b = int(input(\"Enter an integer: \"))\n",
"if a <= 0:\n",
" b = b +1\n",
"else:\n",
" a = a + 1\n",
"if a > 0 and b > 0:\n",
" print (\"W\")\n",
"elif a > 0:\n",
" print(\"X\")\n",
"if b > 0:\n",
" print(\"Y\")\n",
"else:\n",
" print(\"Z\")\n",
"\n",
"applePrice = 210\n",
"budget = 200\n",
"if (applePrice <= budget):\n",
" print(\"Alexa, add 1kg Apples to the cart.\")\n",
"else:\n",
" print(\"Alexa, do not add Apples to the cart.\")\n",
"\n",
"\n",
"n=1\n",
"if n%2==0:\n",
" print(\"The number is even\")\n",
"else:\n",
" print(\"The number is odd\")\n",
"\n",
"n=3\n",
"if(n>3):\n",
" print(\"The number is greater than 3\")\n",
"else:\n",
" print(\"The number is less than 3\")\n",
"\n",
"\n",
"\n",
"n=4\n",
"if(n%5==0):\n",
" print(\"The number is divisible by 5\")\n",
"elif (n%10==0):\n",
" print(\"The number is divisible by 10\")\n",
"else:\n",
" print(\"The number is neither divisible by 5 nor 10\")\n",
"\n",
"n=3\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"ch='A'\n",
"if(ch=='a'):\n",
" print(\"The character is a\")\n",
"elif(ch=='A'):\n",
" print(\"The character is A\")\n",
"\n",
"n=9\n",
"if(n%5==0):\n",
" if(n%3==0):\n",
" print(\"The number is divisible by both 5 and 3\")\n",
" else:\n",
" print(\"The number is divisible by both 5 and 3\")\n",
"else:\n",
" if(n%3==0):\n",
" print(\"The number is divisible by 3 but not 5\")\n",
" else:\n",
" print(\"The number is not divisible by 5 and not divisible by 3\")\n",
"\n",
"a=1\n",
"b=c=1\n",
"if(a==b):\n",
" if(a==c):\n",
" print(\"a,b, and c are equal\")\n",
"\n",
"\n",
"n=1\n",
"if(n==1): print('n is equal to 1'); print(\"The value of n is \",n)\n",
"\n",
"age=18\n",
"if(age>=18):\n",
" print(\"You have the right to vote!\")\n",
"else:\n",
" print(\"You are not eligible to vote\")\n",
"\n",
"a=3\n",
"b=-2\n",
"c=9\n",
"\n",
"if(a>b):# a is the maximum of the first two\n",
" if(a>c):\n",
" print(\"a is the highest\")\n",
" else:\n",
" print(\"c is the highest\")\n",
"else: # b is the maximum of the first two\n",
" if(b>c):\n",
" print(\"b is the highest\")\n",
" else:\n",
" print(\"c is the highest\")\n",
"\n",
"year=2000\n",
"\n",
"if(year%100==0):\n",
" if(year%400==0):\n",
" print(\"It is a leap year\")\n",
" else:\n",
" print(\"It is not a leap year\")\n",
"else:\n",
" if(year%4==0):\n",
" print(\"It is a leap year\")\n",
" else :\n",
" print(\"It is not a leap year\")\n",
"\n",
"a=3\n",
"b=4\n",
"c=3\n",
"\n",
"if(a==b and b==c):\n",
" print(\"Equilateral Triangle\")\n",
"elif(a==b or b==c or c==a):\n",
" print(\"Isosceles Triangle\")\n",
"else:\n",
" print(\"Scalene Triangle\")\n",
"\n",
"x = 10\n",
"if x > 5:\n",
" print('x is greater than 5')\n",
"else:\n",
" print('x is not greater than 5')\n",
"\n",
"# Output:\n",
"# 'x is greater than 5'\n",
"\n",
"\n",
"age = 18\n",
"if age >= 18:\n",
" print('You are eligible to vote.')\n",
"else:\n",
" print('You are not eligible to vote.')\n",
"\n",
"# Output:\n",
"# 'You are eligible to vote.'\n",
"\n",
"\n",
"age = 20\n",
"if age >= 18:\n",
" if age <= 35:\n",
" print('You are eligible to vote and also in the youth demographic.')\n",
" else:\n",
" print('You are eligible to vote but not in the youth demographic.')\n",
"else:\n",
" print('You are not eligible to vote.')\n",
"\n",
"# Output:\n",
"# 'You are eligible to vote and also in the youth demographic.'\n",
"\n",
"\n",
"age = 20\n",
"citizenship = 'USA'\n",
"if age >= 18 and citizenship == 'USA':\n",
" print('You are eligible to vote in the USA.')\n",
"else:\n",
" print('You are not eligible to vote in the USA.')\n",
"\n",
"# Output:\n",
"# 'You are eligible to vote in the USA.'\n",
"\n",
"\n",
"age = 20\n",
"message = 'Eligible to vote.' if age >= 18 else 'Not eligible to vote.'\n",
"print(message)\n",
"\n",
"# Output:\n",
"# 'Eligible to vote.'\n",
"\n",
"\n",
"def switch_case(value):\n",
" return {\n",
" 1: 'One',\n",
" 2: 'Two',\n",
" 3: 'Three'\n",
" }.get(value, 'Unknown')\n",
"\n",
"print(switch_case(2))\n",
"\n",
"# Output:\n",
"# 'Two'\n",
"\n",
"\n",
"age = 20\n",
"if age >= 18:\n",
" print('You are eligible to vote.')\n",
"\n",
"# Output:\n",
"# IndentationError: expected an indented block\n",
"\n",
"\n",
"age = 20\n",
"if age >= 18:\n",
" print('You are eligible to vote.')\n",
"\n",
"# Output:\n",
"# 'You are eligible to vote.'\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"source": [
"# **Summary**\n",
"\n",
"Basic if-elif-else structure for decision-making in Python\n",
"Comparison operators (>, <, >=, <=, ==, !=) for building conditions\n",
"Order importance in elif statements for correct logic flow\n",
"Practical applications including grading systems and classification programs\n",
"\n",
"Best practices for writing clean, maintainable conditional code\n",
"Common pitfalls and how to avoid them\n",
"\n",
"**Key Takeaways:**\n",
"Conditional statements enable programs to make intelligent decisions\n",
"Order matters in elif chains - most restrictive conditions first\n",
"Input validation prevents runtime errors and improves user experience\n",
"Descriptive conditions make code more readable and maintainable\n",
"Proper indentation and syntax are crucial for Python conditional statements"
],
"metadata": {
"id": "qYTycBbYRRCa"
}
},
{
"cell_type": "code",
"source": [
"age = 17\n",
"if age > 18:\n",
" print('You are eligible to vote.')\n",
"else:\n",
" print('You are not eligible to vote.')\n",
"\n",
"# Output:\n",
"# 'You are not eligible to vote.'\n",
"\n",
"\n",
"x = 5\n",
"if x > 0:\n",
" print('x is positive')\n",
"else:\n",
" print('x is not positive')\n",
"\n",
"# Output:\n",
"# 'x is positive'\n",
"\n",
"\n",
"x = 5\n",
"if x > 0:\n",
" print('x is positive')\n",
" print('This is also part of the if block')\n",
"else:\n",
" print('x is not positive')\n",
"print('This is outside the if-else block')\n",
"\n",
"# Output:\n",
"# 'x is positive'\n",
"# 'This is also part of the if block'\n",
"# 'This is outside the if-else block'\n",
"\n",
"\n",
"def check_eligibility(age):\n",
" if age >= 18:\n",
" return 'Eligible to vote.'\n",
" else:\n",
" return 'Not eligible to vote.'\n",
"\n",
"ages = [16, 20, 22, 15, 30]\n",
"for age in ages:\n",
" message = check_eligibility(age)\n",
" print(f'Age {age}: {message}')\n",
"\n",
"# Output:\n",
"# 'Age 16: Not eligible to vote.'\n",
"# 'Age 20: Eligible to vote.'\n",
"# 'Age 22: Eligible to vote.'\n",
"# 'Age 15: Not eligible to vote.'\n",
"# 'Age 30: Eligible to vote.'\n",
"\n",
"\n",
"#Example 1\n",
"x = 3\n",
"if x >= 2:\n",
" print(\"x is bigger than or equal to 2.\")\n",
"\n",
"#Example 2\n",
"x = -5\n",
"y = 5\n",
"if x > 0 or y > 0:\n",
" print(\"Either x or y is bigger than 0.\")\n",
"\n",
"age = int (input(\"Enter your age : \"))\n",
"if age >= 18:\n",
" print(\"You are eligible to vote !!\")\n",
"else:\n",
" print(\"Sorry! you have to wait !!\")\n",
"\n",
"\n",
"marks = int(input(\"Enter the marks :\"))\n",
"if marks > 85 and marks <= 100:\n",
" print(\"Congrats! you scored grade A..\")\n",
"elif marks > 60 and marks <= 85:\n",
" print(\"You scored grade B + ..\")\n",
"elif marks > 40 and marks <= 60:\n",
" print(\"You scored grade B ..\")\n",
"elif (marks > 30 and marks <= 40):\n",
" print(\"You scored grade C ..\")\n",
"else:\n",
" print(\"Sorry you are fail ?\")\n",
"\n",
"\n",
"a=int(input(\"Enter a value : \"))\n",
"b=int(input(\"Enter b value : \"))\n",
"c=int(input(\"Enter c value : \"))\n",
"if (a>b) and (a>c):\n",
"\tprint(\"Maximum value is :\",a)\n",
"elif (b>c):\n",
"\tprint(\"Maximum value is :\",b)\n",
"else:\n",
"\tprint(\"Maximum value is :\",c)\n",
"\n",
"a = 1\n",
"b = 3\n",
"c = 4\n",
"\n",
"if ac:\n",
" print(\"B is largest\")\n",
"else:\n",
" print(\"B is not the largest\")\n",
"\n",
"x = 4\n",
"if x == 4:\n",
" print('x is equal to 4')\n",
"elif x == 3:\n",
" print('x is equal to 3')\n",
"elif x == 2:\n",
" print('x is equal to 2')\n",
"elif x == 1:\n",
" print('x is equal to 1')\n",
"else:\n",
" print('x is not 1, 2, 3, nor 4')\n",
"\n",
"\n",
"\n",
"x = 5\n",
"\n",
"if x == 4:\n",
" print('x is equal to 4')\n",
"elif x == 3:\n",
" print('x is equal to 3')\n",
"elif x == 2:\n",
" print('x is equal to 2')\n",
"elif x == 1:\n",
" print('x is equal to 1')\n",
"else:\n",
" print('x is not 1, 2, 3, nor 4')\n",
"\n",
"# list of numbers\n",
"list_of_numbers = [2,4,6,9,5]\n",
"\n",
"# for loop to iterate through the list and check each elements of the list\n",
"for i in list_of_numbers:\n",
" # check if no. is odd\n",
" if i % 2 != 0:\n",
" # if condition is True print \"odd\"\n",
" print (i, \"is an odd number\")\n",
" # check if no. is even\n",
" if i % 2 == 0:\n",
" # if condition is false print \"even\"\n",
" print (i, \"is an even number\")\n",
"\n",
"# list of numbers\n",
"list_of_numbers = [2,4,6,9,5]\n",
"\n",
"# for loop to iterate through the list and check each element of the list\n",
"for i in list_of_numbers:\n",
"\n",
" # check if no. is odd\n",
" if i%2 != 0:\n",
"\n",
" # if condition is True print \"odd\"\n",
" print(i, \"is an odd number\")\n",
"\n",
" # if the no. is not odd then the no. is even therfore print \"even\"\n",
" else:\n",
"\n",
" # if condition is True print \"even\"\n",
" print(i, \"is an even number\")\n",
"\n",
"# take a sample string\n",
"string =\"ShikshaOnline\"\n",
"\n",
"# check if value in the string variable\n",
"# matches \"Shik\"\n",
"if string == \"Shik\":\n",
" print (\"The first condition is true\")\n",
"\n",
"# check if value in the string variable\n",
"# matches \"Shiksha\"\n",
"elif string == \"Shiksha\":\n",
" print(\"The second condition is true\")\n",
"\n",
"# check if value in the string variable\n",
"# matches \"Online\"\n",
"elif string == \"Online\":\n",
" print(\"The third condition is true\")\n",
"\n",
"# check if value in the string variable\n",
"# matches \"ShikshaOnline\"\n",
"elif string==\"ShikshaOnline\":\n",
" print(\"The fourth condition is true\")\n",
"\n",
"# if none of the above\n",
"# conditions evaluate to true\n",
"# execute the code inside the else block\n",
"else:\n",
" print (\"All the above conditions are false\")\n",
"\n",
"# list of numbers\n",
"list_of_numbers = [4,5,9,17,21]\n",
"\n",
"# for loop to iterate through the list and check each element of the list\n",
"for i in list_of_numbers:\n",
"\n",
" # condition1: check if no. is odd\n",
" # if yes execute the code block inside the first if statement\n",
" if i%2!=0:\n",
"\n",
" # condition2: check if no. is also divisible by 3.\n",
" if i%3==0:\n",
"\n",
" # if condition2 is true\n",
" # execute the below code block\n",
" print (i,\"is an odd number & divisible by 3\")\n",
"\n",
" # if codition2 if False\n",
" # execute the below code block\n",
"\n",
" else:\n",
" print (i, \"is an odd number but not divisible by 3\")\n",
"\n",
" # if condition1 is False\n",
" # execute the below code block\n",
" else:\n",
" print(i,\"is an even number\")\n",
"\n",
"# list of numbers\n",
"list_of_numbers = [4,5,9,17,21]\n",
"\n",
"# for loop to iterate through the list and check each element of the list\n",
"for i in list_of_numbers:\n",
"\n",
" # condition1: check if no. is odd\n",
" # if yes execute the code block inside the first if statement\n",
" if i%2!=0:\n",
"\n",
" # condition2: check if no. is also divisible by 3.\n",
" if i%3==0:\n",
"\n",
" # if condition2 is true\n",
" # execute the below code block\n",
" print (i,\"is an odd number & divisible by 3\")\n",
"\n",
" # if codition2 if False\n",
" # execute the below code block\n",
"\n",
" else:\n",
" print (i, \"is an odd number but not divisible by 3\")\n",
"\n",
" # if condition1 is False\n",
" # execute the below code block\n",
" else:\n",
" print(i,\"is an even number\")\n",
"\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "wxpjm59sRRJy",
"outputId": "31244696-3de8-494d-cd4f-4d632759079b"
},
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"You are not eligible to vote.\n",
"x is positive\n",
"x is positive\n",
"This is also part of the if block\n",
"This is outside the if-else block\n",
"Age 16: Not eligible to vote.\n",
"Age 20: Eligible to vote.\n",
"Age 22: Eligible to vote.\n",
"Age 15: Not eligible to vote.\n",
"Age 30: Eligible to vote.\n",
"x is bigger than or equal to 2.\n",
"Either x or y is bigger than 0.\n",
"Enter your age : 25\n",
"You are eligible to vote !!\n",
"Enter the marks :52\n",
"You scored grade B ..\n",
"Enter a value : 3\n",
"Enter b value : 2\n",
"Enter c value : 2\n",
"Maximum value is : 3\n",
"B is not the largest\n",
"x is equal to 4\n",
"x is not 1, 2, 3, nor 4\n",
"2 is an even number\n",
"4 is an even number\n",
"6 is an even number\n",
"9 is an odd number\n",
"5 is an odd number\n",
"2 is an even number\n",
"4 is an even number\n",
"6 is an even number\n",
"9 is an odd number\n",
"5 is an odd number\n",
"The fourth condition is true\n",
"4 is an even number\n",
"5 is an odd number but not divisible by 3\n",
"9 is an odd number & divisible by 3\n",
"17 is an odd number but not divisible by 3\n",
"21 is an odd number & divisible by 3\n",
"4 is an even number\n",
"5 is an odd number but not divisible by 3\n",
"9 is an odd number & divisible by 3\n",
"17 is an odd number but not divisible by 3\n",
"21 is an odd number & divisible by 3\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# **Conditional Statements in Python performs different computations or actions depending on conditions.**\n",
"\n",
"In python, the following are conditional statements\n",
"\n",
"if\n",
"if – else\n",
"if – elif –else\n",
"Indentation in Python:\n",
"For the ease of programming and to achieve simplicity, python doesn't allow the use of parentheses for the block level code. In Python, indentation is used to declare a block. If two statements are at the same indentation level, then they are the part of the same block.\n",
"\n",
"Generally, four spaces are given to indent the statements which are a typical amount of indentation in python.\n",
"Indentation is the most used part of the python language since it declares the block of code. All the statements of one block are intended at the same level indentation.\n",
"\n",
"If statement in Python\n",
"The if statement is used to test a specific condition. If the condition is true, a block of code (if-block) will be executed."
],
"metadata": {
"id": "rAroUjUGRRR6"
}
},
{
"cell_type": "code",
"source": [
"a = 2\n",
"if a<3:\n",
" print (a);\n",
"if a>3:\n",
" print('Hi')\n",
"\n",
"\n",
"x = 5\n",
"if x == 2:\n",
" print('x is equal to 2')\n",
"elif x < 2:\n",
" print('x is less than 2')\n",
"else:\n",
" print('x is greater than 2')\n",
"\n",
"applePrice = 210\n",
"budget = 200\n",
"if (applePrice <= budget):\n",
" print(\"Alexa, add 1kg Apples to the cart.\")\n",
"else:\n",
" print(\"Alexa, do not add Apples to the cart.\")\n",
"\n",
"if 10 > 5:\n",
" print(\"10 greater than 5\")\n",
"print(\"Program ended\")\n",
"\n",
"x = 3\n",
"if x == 4:\n",
" print(\"Yes\")\n",
"else:\n",
" print(\"No\")\n",
"\n",
"\n",
"letter = \"A\"\n",
"if letter == \"B\":\n",
" print(\"letter is B\")\n",
"else:\n",
" if letter == \"C\":\n",
" print(\"letter is C\")\n",
" else:\n",
" if letter == \"A\":\n",
" print(\"letter is A\")\n",
" else:\n",
" print(\"letter isn't A, B and C\")\n",
"\n",
"a = 10\n",
"if a > 5:\n",
" print(\"Bigger than 5\")\n",
" if a <= 15:\n",
" print(\"Between 5 and 15\")\n",
"\n",
"letter = \"A\"\n",
"if letter == \"B\":\n",
" print(\"letter is B\")\n",
"elif letter == \"C\":\n",
" print(\"letter is C\")\n",
"elif letter == \"A\":\n",
" print(\"letter is A\")\n",
"else:\n",
" print(\"letter isn't A, B or C\")\n",
"\n",
"x = 10\n",
"y = 5\n",
"if x > 5:\n",
" if y > 5:\n",
" print(\"x is greater than 5\")\n",
" elif y==5:\n",
" print(\"x is greater than 5 and y is 5\")\n",
" else:\n",
" print(\"x is greater than 5 and y is less than 5\")\n",
"\n",
"x = 3\n",
"if x > 1:\n",
" y = 2\n",
"elif x > 2:\n",
" y = 4\n",
"else:\n",
" y = 0\n",
"print(y)\n",
"\n",
"\n",
"x = 3\n",
"if x > 1 and x < 2:\n",
" y = 2\n",
"elif x > 2 and x < 4:\n",
" y = 4\n",
"else:\n",
" y = 0\n",
"print(y)\n",
"\n",
"x = 3\n",
"if 1 < x < 2:\n",
" y = 2\n",
"elif 2 < x < 4:\n",
" y = 4\n",
"else:\n",
" y = 0\n",
"print(y)\n",
"\n",
"\n",
"def my_nested_branching(x,y):\n",
" \"\"\"\n",
" Nested Branching Statement Example\n",
" author\n",
" date\n",
" :type x: Int\n",
" :type y: Int\n",
" :rtype: Int\n",
" \"\"\"\n",
" if x > 2:\n",
" if y < 2:\n",
" out = x + y\n",
" else:\n",
" out = x - y\n",
" else:\n",
" if y > 2:\n",
" out = x*y\n",
" else:\n",
" out = 0\n",
" return out\n",
"\n",
" a = 33\n",
"b = 33\n",
"if b > a:\n",
" print(\"b is greater than a\")\n",
"elif a == b:\n",
" print(\"a and b are equal\")\n",
"\n",
"\n",
"i = input('if exercise:\\n')\n",
"if i == 'A' or 'B' or 'C':\n",
" print('code1')\n",
"elif i == 'D' or 'E' or 'F':\n",
" print('code2')\n",
"elif i == 'G' or 'H' or 'I':\n",
" print('code3')\n",
"else:\n",
" print('code4')\n",
"\n",
"\n",
"y = 3\n",
"\n",
"if y > 4:\n",
" print(\"I won't print!\") #this statement does not execute\n",
"else:\n",
" print(\"The condition wasn't true!\") #this statement executes\n",
"\n",
"z = 7\n",
"\n",
"if z > 8:\n",
" print(\"I won't print!\") #this statement does not execute\n",
"elif z > 5:\n",
" print(\"I will!\") #this statement will execute\n",
"elif z > 6:\n",
" print(\"I also won't print!\") #this statement does not execute\n",
"else:\n",
" print(\"Neither will I!\") #this statement does not execute\n",
"\n",
"x = 34\n",
"if x % 2 == 0: # this is how you create a comment and now, checking for even.\n",
" if x > 10:\n",
" print(\"This number is even and is greater than 10\")\n",
" else:\n",
" print(\"This number is even, but not greater 10\")\n",
"else:\n",
" print (\"The number is not even. So point checking further.\")\n",
"\n",
"x = 89\n",
"is_greater = True if x >= 50 else False\n",
"\n",
"print(is_greater)\n",
"\n",
"\n",
"player_age = 12\n",
"\n",
"if player_age >= 18:\n",
" print(\"You could be in college.\")\n",
"elif player_age >= 13:\n",
" print(\"You can also attend iD Academies!\")\n",
"elif player_age >= 7:\n",
" print(\"You can attend iD Tech Camps!\")\n",
"else:\n",
" print(\"You're young.\")\n",
"\n",
"import math\n",
"def check_sqrt(num):\n",
" if math.sqrt(num)%2 == 0:\n",
" return True\n",
" elif (math.sqrt(num)+1) % 2 == 0:\n",
" return True\n",
" else:\n",
" return False\n",
"\n",
"print(check_sqrt(23))\n",
"\n",
"x = 10\n",
"if x > 5:\n",
" print(\"x is greater than 5\")\n",
"else:\n",
" print(\"x is not greater than 5\")\n",
"\n",
"\n",
"age = 18\n",
"if age >= 18:\n",
" print(\"You are an adult.\")\n",
"else:\n",
" print(\"You are not an adult.\")\n",
"\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "0Qq2TVC9RRZq",
"outputId": "fb68879c-fb1b-4e34-b00c-ae838e455fb2"
},
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"2\n",
"x is greater than 2\n",
"Alexa, do not add Apples to the cart.\n",
"10 greater than 5\n",
"Program ended\n",
"No\n",
"letter is A\n",
"Bigger than 5\n",
"Between 5 and 15\n",
"letter is A\n",
"x is greater than 5 and y is 5\n",
"2\n",
"4\n",
"4\n",
"b is greater than a\n",
"if exercise:\n",
"25\n",
"code1\n",
"The condition wasn't true!\n",
"I will!\n",
"This number is even and is greater than 10\n",
"True\n",
"You can attend iD Tech Camps!\n",
"False\n",
"x is greater than 5\n",
"You are an adult.\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# **IF statements** are used to set logical conditions for operations or outputs. When using IF statements, you must indent the lines that follow the IF statement. If statements can also include ELSE statements to account for those outputs/values that fall outside of the parameters set by the IF statement.\n",
"\n",
"In this section, you will learn how to:\n",
"\n",
"Employ IF...ELSE statements (IF Statements)\n",
"Identify and use comparison operators (IF Statements)\n",
"Appropriately use the IN keyword in an IF...ELSE statement (IF Statements)\n",
"Describe the appropriate use of indentation (IF Statements)\n",
"Appropriately use the AND keyword in an IF...ELSE statement to set multiple parameters (IF Statements)\n",
"Appropriately use the OR keyword in an IF...ELSE statement to set multiple parameters (IF Statements)\n",
"Appropriately use the ELIF keyword in an IF...ELSE statement to set multiple outputs (IF Statements)\n",
"Use input to collect user information (IF Statements)"
],
"metadata": {
"id": "eJ2PuZVVRRiy"
}
}
]
}