diff --git a/.DS_Store b/.DS_Store index 5008ddf..77dec8f 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/2_conditional_statements.ipynb b/2_conditional_statements.ipynb index 50314f3..06f99aa 100644 --- a/2_conditional_statements.ipynb +++ b/2_conditional_statements.ipynb @@ -60,7 +60,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -103,9 +103,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "b is greater\n" + ] + } + ], "source": [ "if a > b: # Here we are trying to find if a is greater than b\n", " print('a is greater')\n", @@ -167,9 +175,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "b is greater than a or both are equal\n" + ] + } + ], "source": [ "if a > b: # Here we are trying to find if which is greater, a or b\n", " print('a is greater') \n", @@ -179,9 +195,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "b is greater\n" + ] + } + ], "source": [ "if a > b: # Here we are trying to find if a is greater than b\n", " print('a is greater')\n", @@ -214,9 +238,18 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "b is greater\n", + "Done!\n" + ] + } + ], "source": [ "if a > b: # Here we are trying to find if a is greater than b\n", " print('a is greater')\n", @@ -269,9 +302,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a is greater\n", + "Value of a is 5\n", + "And value of b is 2\n" + ] + } + ], "source": [ "a = 5\n", "b = 2\n", @@ -302,9 +345,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "b is greater\n", + "Value of a is 10\n", + "And value of b is 50\n" + ] + } + ], "source": [ "a = 10\n", "b = 50\n", @@ -338,11 +391,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your answer here" + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "they both are equal\n" + ] + } + ], + "source": [ + "# The else would run = print('they both are equal')\n", + "# Your answer here\n", + "\n", + "a= 10\n", + "b= 10\n", + "\n", + "if a>b: \n", + " print('a is greater') \n", + " print('Value of a is',a) \n", + " print('And value of b is',b)\n", + "elif b>a:\n", + " print('b is greater')\n", + " print('Value of a is', a)\n", + " print('And value of b is',b)\n", + "else:\n", + " print('they both are equal')" ] }, { @@ -403,9 +479,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The number is negative.\n" + ] + } + ], "source": [ "number = int(input(\"Enter a number: \"))\n", "\n", @@ -433,9 +517,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Your grade is: F\n" + ] + } + ], "source": [ "percentage = float(input(\"Enter the percentage: \"))\n", "\n", @@ -493,9 +585,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The number is positive and even.\n" + ] + } + ], "source": [ "number = int(input(\"Enter a number: \"))\n", "\n", @@ -532,9 +632,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "c is the greatest\n" + ] + } + ], "source": [ "a = 5 \n", "b = 10\n", @@ -677,10 +785,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Both numbers are equal\n" + ] + } + ], + "source": [ + "# Get two numbers from the user\n", + "num1 = float(input(\"Enter the first number: \"))\n", + "num2 = float(input(\"Enter the second number: \"))\n", + "\n", + "# Compare the numbers\n", + "if num1 > num2:\n", + " print(\"The first number is larger\")\n", + "elif num2 > num1:\n", + " print(\"The second number is larger\")\n", + "else:\n", + " print(\"Both numbers are equal\")" + ] }, { "cell_type": "markdown", @@ -697,10 +825,35 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Your grade is: D\n" + ] + } + ], + "source": [ + "\n", + "\n", + "percentage = float(input(\"Enter the percentage: \"))\n", + "\n", + "if percentage >= 90:\n", + " grade = \"A\"\n", + "elif 90 > percentage >= 80:\n", + " grade = \"B\"\n", + "elif 80 > percentage >= 70:\n", + " grade = \"C\"\n", + "elif 70 > percentage >= 60:\n", + " grade = \"D\"\n", + "else:\n", + " grade = \"F\"\n", + "\n", + "print(\"Your grade is:\", grade)" + ] }, { "cell_type": "markdown", @@ -749,10 +902,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is a vowel!\n" + ] + } + ], + "source": [ + "# Get a letter from the user and convert to lowercase\n", + "letter = input(\"Enter a single letter: \").lower()\n", + "\n", + "# Check if it's a vowel\n", + "if letter in ['a', 'e', 'i', 'o', 'u']:\n", + " print(\"This is a vowel!\")\n", + "else:\n", + " print(\"This is a consonant!\")" + ] }, { "cell_type": "markdown", @@ -775,17 +945,102 @@ "Hint: search for the method *isdigit()*." ] }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a positive integer.\n" + ] + } + ], + "source": [ + "user_input = input(\"Enter a positive integer: \").strip()\n", + "\n", + "if user_input.isdigit():\n", + " number = int(user_input)\n", + " if number == 0:\n", + " print(\"The number is zero.\")\n", + " elif number % 2 == 0:\n", + " print(\"The number is even.\")\n", + " else:\n", + " print(\"The number is odd.\")\n", + "else:\n", + " print(\"Invalid input. Please enter a positive integer.\")" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "user_input = input(\"Enter an integer: \").strip() # Remove extra spaces\n", + "\n", + "# Check for valid integer format\n", + "valid = False\n", + "\n", + "# Check for numbers with signs\n", + "if user_input.startswith(('-', '+')):\n", + " if len(user_input) > 1 and user_input[1:].isdigit():\n", + " valid = True\n", + "elif user_input.isdigit():\n", + " valid = True\n", + "\n", + "if valid:\n", + " number = int(user_input)\n", + " if number == 0:\n", + " print(\"The number is zero.\")\n", + " elif number % 2 == 0:\n", + " print(\"The number is even.\")\n", + " else:\n", + " print(\"The number is odd.\")\n", + "else:\n", + " print(\"Invalid input. Please enter a valid integer.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The number is odd.\n" + ] + } + ], + "source": [ + "# Get user input\n", + "user_input = input(\"Enter an integer: \")\n", + "\n", + "try:\n", + " # Try to convert to integer\n", + " number = int(user_input)\n", + " \n", + " # Check number properties\n", + " if number == 0:\n", + " print(\"The number is zero.\")\n", + " elif number % 2 == 0:\n", + " print(\"The number is even.\")\n", + " else:\n", + " print(\"The number is odd.\")\n", + " \n", + "except ValueError:\n", + " # Handle invalid input\n", + " print(\"Invalid input. Please enter a valid integer.\")" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -799,7 +1054,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,