Created
July 21, 2023 17:29
-
-
Save JenniferNorthrup/5b512eb7aa7048dd37c5dc4f7d64f8d8 to your computer and use it in GitHub Desktop.
Practice+Exercise+-+Conditional_Statements.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/JenniferNorthrup/5b512eb7aa7048dd37c5dc4f7d64f8d8/practice-exercise-conditional_statements.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "nje_y_zWakw7" | |
}, | |
"source": [ | |
"# **CONDITIONAL STATEMENTS EXERCISE**" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "7FvnDB5Zakw8" | |
}, | |
"source": [ | |
"### 1. Check if a is equal to 10, If yes, print \"Hello\", else print \"Good Bye\"\n", | |
"a = 56" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "t_252QC5akw9", | |
"outputId": "f53f9fd9-b67d-4d4a-f58f-12d38d5b1860" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Good Bye\n" | |
] | |
} | |
], | |
"source": [ | |
"a = 56\n", | |
"if a == 10:\n", | |
" print(\"Hello\")\n", | |
"else:\n", | |
" print(\"Good Bye\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "Lzktq5UZakw9" | |
}, | |
"source": [ | |
"### 2. Check whether a number is even or odd\n", | |
"a = 2020" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "uyBXPJJQakw-", | |
"outputId": "09fee8a3-136e-4200-dfcf-c6b05a8eab96" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Even\n" | |
] | |
} | |
], | |
"source": [ | |
"a = 2020\n", | |
"if a % 2 == 0:\n", | |
" print(\"Even\")\n", | |
"else:\n", | |
" print(\"Odd\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "qUTmSkGjakw-" | |
}, | |
"source": [ | |
"### 3. Check whether the area of the rectangle is greater than the perimeter of the rectangle\n", | |
"length = 5\n", | |
"breadth = 10\n", | |
"\n", | |
"Hint: Area of rectangle = length * breadth\n", | |
"\n", | |
"Hint: Perimeter of rectangle = 2 * (length + breadth)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "m8XzyV3zakw-", | |
"outputId": "ff0a12a2-d935-4002-ab70-034c41d372b4" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Area is greater\n" | |
] | |
} | |
], | |
"source": [ | |
"length = 5\n", | |
"breadth = 10\n", | |
"area = length * breadth\n", | |
"perimeter = 2 * (length + breadth)\n", | |
"\n", | |
"if area > perimeter:\n", | |
" print(\"Area is greater\")\n", | |
"elif perimeter > area:\n", | |
" print(\"Perimeter is greater\")\n", | |
"else:\n", | |
" print(\"Area = perimeter\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "NrFLnlv_akw-" | |
}, | |
"source": [ | |
"### 4. Calculate the average marks of a student in 5 subjects and give grades accordingly:\n", | |
"If average>90 then Grade = A\n", | |
"\n", | |
"If average>80 then Grade = B\n", | |
"\n", | |
"If average>70 then Grade = C\n", | |
"\n", | |
"If average>60 then Grade = D\n", | |
"\n", | |
"If average>50 then Grade = E\n", | |
"\n", | |
"else Fail\n", | |
"\n", | |
"Marks in subject 1= 72\n", | |
"\n", | |
"Marks in subject 2= 85\n", | |
"\n", | |
"Marks in subject 3= 96\n", | |
"\n", | |
"Marks in subject 4= 42\n", | |
"\n", | |
"Marks in subject 5= 95" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "1M6KxSqBakw-", | |
"outputId": "4f6c7a8e-6eff-45f7-f201-5c0abf34a9f6" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Subject 1 has a grade of C\n", | |
"Subject 2 has a grade of B\n", | |
"Subject 3 has a grade of A\n", | |
"Failing Subject 4!!!\n", | |
"Subject 5 has a grade of A\n" | |
] | |
} | |
], | |
"source": [ | |
"grades = (72, 85, 96, 42, 95)\n", | |
"\n", | |
"for i, grade in enumerate(grades):\n", | |
" if grade > 90:\n", | |
" print(f\"Subject {i+1} has a grade of A\")\n", | |
" elif grade > 80:\n", | |
" print(f\"Subject {i+1} has a grade of B\")\n", | |
" elif grade > 70:\n", | |
" print(f\"Subject {i+1} has a grade of C\")\n", | |
" elif grade > 60:\n", | |
" print(f\"Subject {i+1} has a grade of D\")\n", | |
" elif grade > 50:\n", | |
" print(f\"Subject {i+1} has a grade of E\")\n", | |
" else:\n", | |
" print(f\"Failing Subject {i+1}!!!\")\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "VK_Cjowtakw_" | |
}, | |
"source": [ | |
"### 5. Check if a triangle is equilateral, isosceles or scalene.\n", | |
"\n", | |
"Hint :\n", | |
"1. An equilateral triangle is a triangle in which all three sides are equal.\n", | |
"\n", | |
"2. A scalene triangle is a triangle that has three unequal sides.\n", | |
"\n", | |
"3. An isosceles triangle is a triangle with (at least) two equal sides.\n", | |
"\n", | |
"Dimensions of triangle:\n", | |
"\n", | |
"side 1 = 6\n", | |
"\n", | |
"side 2 = 8\n", | |
"\n", | |
"side 3 = 12" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "CrmS5HLgakw_", | |
"outputId": "a8c5529f-4361-4328-a28d-a013ba96f832" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Scalene\n" | |
] | |
} | |
], | |
"source": [ | |
"side_1 = 6\n", | |
"side_2 = 8\n", | |
"side_3 = 12\n", | |
"\n", | |
"if side_1 == side_2 and side_2 == side_3:\n", | |
" print(\"Equilateral\")\n", | |
"elif side_1 == side_2 or side_2 == side_3 or side_3 == side_1:\n", | |
" print(\"Isoceles\")\n", | |
"else:\n", | |
" print(\"Scalene\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "V4mysJW_akw_" | |
}, | |
"source": [ | |
"### 6. Check if the word \"Data\" is present in the sentence \"I am a Data Scientist\". If found print \"It is present\" else print FALSE" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "YJdknW4oakw_", | |
"outputId": "fd1ed0de-ef92-41aa-e601-51d48c87604e" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"It is present\n" | |
] | |
} | |
], | |
"source": [ | |
"if \"Data\" in \"I am a Data Scientist\":\n", | |
" print(\"It is present\")\n", | |
"else:\n", | |
" print(\"FALSE\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "9UZ1Nit7akw_" | |
}, | |
"source": [ | |
"### 7. Write Python code to check if a number is positive or negative." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "ewtceB9zakxA", | |
"outputId": "71fd2414-d60f-41e5-90bf-53275a4f3677" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"negative\n" | |
] | |
} | |
], | |
"source": [ | |
"number = -1\n", | |
"if number > 0:\n", | |
" print(\"positive\")\n", | |
"elif number < 0:\n", | |
" print(\"negative\")\n", | |
"else:\n", | |
" print(\"zero, neither positive nore negative\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "x_gTx9AYakxA" | |
}, | |
"source": [ | |
"### 8. x = 20 and y = 30. Write a Python code to check if x is less than y." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "QP_Q4iBuakxA", | |
"outputId": "b499aa93-3bd4-4ff6-ca35-fa97dad81ac7" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Is x less than y? True\n" | |
] | |
} | |
], | |
"source": [ | |
"x = 20\n", | |
"y = 30\n", | |
"answer = x < y\n", | |
"print(f\"Is x less than y? {answer}\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "X_0kueNGakxA" | |
}, | |
"source": [ | |
"### 9. Based on the users age, divide them into three groups\n", | |
" Group 1 : Age <18 , Minors who are not eligible to work\n", | |
" Group 2 : 18 < Age < 60 , Eligible to work\n", | |
" Group 3 : Age > 60, Too old to work as per govt. regulations.\n", | |
"\n", | |
"### Write a Python code for the same" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "4ZoVbR5cakxA", | |
"outputId": "96225edf-132f-4b2a-9671-4485551c3a80" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Minors who are not eligible to work: [12, 9, 4, 15]\n", | |
"Eligible to work: [34, 22, 45, 39]\n", | |
"Too old to work as per govt. regulations.: [95, 70]\n" | |
] | |
} | |
], | |
"source": [ | |
"user_ages = (12, 95, 34, 70, 22, 9, 4, 15, 45, 39)\n", | |
"group1 = []\n", | |
"group2 = []\n", | |
"group3 = []\n", | |
"\n", | |
"for age in user_ages:\n", | |
" if age < 18:\n", | |
" group1.append(age)\n", | |
" elif age > 60:\n", | |
" group3.append(age)\n", | |
" else:\n", | |
" group2.append(age)\n", | |
"\n", | |
"print(f\"Minors who are not eligible to work: {group1}\")\n", | |
"print(f\"Eligible to work: {group2}\")\n", | |
"print(f\"Too old to work as per govt. regulations.: {group3}\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "NqugFcOWakxA" | |
}, | |
"source": [ | |
"### 10. From the age of 3 people determine the oldest person among them.\n", | |
"Age of first person = 25\n", | |
"\n", | |
"Age of second person = 34\n", | |
"\n", | |
"Age of third person = 45" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "Fjzs3J4qakxA", | |
"outputId": "096e1d8e-31b3-4208-f07d-75b20fd9d7d9" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Oldest person is 45\n" | |
] | |
} | |
], | |
"source": [ | |
"ages = [25, 34, 45]\n", | |
"ages.sort()\n", | |
"print(f\"Oldest person is {ages.pop()}\")\n", | |
"#print(f\"Youngest person is {ages[0]}\")" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.9.7" | |
}, | |
"colab": { | |
"provenance": [], | |
"include_colab_link": true | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment