{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"toc_visible":true,"authorship_tag":"ABX9TyMUHkTJpMmYXSNognW7AwTm"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# **Python Basics**"],"metadata":{"id":"IAUt06OTx0_G"}},{"cell_type":"markdown","source":["Find more exrcises on https://www.w3resource.com/python-exercises/"],"metadata":{"id":"2-O4ppf1Y1OF"}},{"cell_type":"markdown","source":["## **Exercice 1**\n","Write a Python program that calculates the area of a circle based on the radius entered by the user.\n","\n","Sample Output :\n","\n","r = 1.1\n","\n","Area = 3.8013271108436504\n","\n","\n"],"metadata":{"id":"7AyiEDF6x715"}},{"cell_type":"code","source":["import math\n","r = float( input(\"Enter the radius of the cercle: \") )\n","area = ( r ** 2 ) * math.pi\n","print(\"The area of the circle = \" , area)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"fKXPYcg_2BkY","executionInfo":{"status":"ok","timestamp":1704916056615,"user_tz":-120,"elapsed":5548,"user":{"displayName":"Data Science","userId":"07742026815209387612"}},"outputId":"f24e36f7-1f2a-460e-9b9a-35ea80a5dfec"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Enter the radius of the cercle: 1.1\n","The area of the circle =  3.8013271108436504\n"]}]},{"cell_type":"markdown","source":["## **Exrcice 2**\n","Write a Python program that accepts a sequence of comma-separated numbers from the user and generates a list and a tuple of those numbers.\n","\n","Sample data : 3, 5, 7, 23\n","\n","Output :\n","\n","List : ['3', ' 5', ' 7', ' 23']"],"metadata":{"id":"-1ayWTjk64q7"}},{"cell_type":"code","source":["values = input(\"Enter a sequence of comma-separated numbers: \")\n","list = values.split(\",\")\n","print(\"List: \" , list)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"8JdaL_Og7XB9","executionInfo":{"status":"ok","timestamp":1704916548914,"user_tz":-120,"elapsed":17117,"user":{"displayName":"Data Science","userId":"07742026815209387612"}},"outputId":"8b9fb7e4-ccf5-49c8-996e-24f8fad67a29"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Enter a sequence of comma-separated numbers:  3, 5, 7, 23\n","List:  [' 3', ' 5', ' 7', ' 23']\n"]}]},{"cell_type":"markdown","source":["## **Exercice 3**\n","Write a Python program that accepts a filename from the user and prints the extension of the file.\n","\n","Sample filename : abc.java\n","\n","Output : java"],"metadata":{"id":"Ib7GgrCS8nXI"}},{"cell_type":"code","source":["name = input(\"Enter the name of the file: \")\n","ext = name.split(\".\")\n","print(\"The extension of the file is: \", ext[1])"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"nMdHyD8_8seo","executionInfo":{"status":"ok","timestamp":1704917095724,"user_tz":-120,"elapsed":3061,"user":{"displayName":"Data Science","userId":"07742026815209387612"}},"outputId":"84cb5351-8b84-45ef-edcb-b7a53a1c44c6"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Enter the name of the file: abc.java\n","The extension of the file is:  java\n"]}]},{"cell_type":"markdown","source":["## **Exercice 4**\n","Write a Python program to display the first and last colors from the following list.\n","\n","color_list = [\"Red\",\"Green\",\"White\" ,\"Black\"]"],"metadata":{"id":"MWhVrC5V-viS"}},{"cell_type":"code","source":["color_list = [\"Red\",\"Green\",\"White\" ,\"Black\"]\n","print(color_list[0] , \" \" , color_list[-1])"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"84B0uWEc-397","executionInfo":{"status":"ok","timestamp":1704917272732,"user_tz":-120,"elapsed":376,"user":{"displayName":"Data Science","userId":"07742026815209387612"}},"outputId":"828af081-5049-49fb-a679-ba07e83e6395"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Red   Black\n"]}]},{"cell_type":"markdown","source":["## **Exercice 5**\n","Write a Python program that accepts an integer (n) and computes the value of n+nn+nnn.\n","\n","Sample value of n is 5\n","\n","Expected Result : 615"],"metadata":{"id":"OYXkaZ8v_jWo"}},{"cell_type":"code","source":["n =  input(\"Enter an integer n: \")\n","n1 = int(str(n))\n","n2 = int(str(n) + str(n))\n","n3 = int(str(n) + str(n) + str(n))\n","\n","print(n1 + n2 + n3)\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"_T1JlwR6ArVY","executionInfo":{"status":"ok","timestamp":1704918375671,"user_tz":-120,"elapsed":2622,"user":{"displayName":"Data Science","userId":"07742026815209387612"}},"outputId":"e5696be4-1f00-4919-868d-d8467e05e2ef"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Enter an integer n: 5\n","615\n"]}]},{"cell_type":"markdown","source":["## **Exercice 6**\n","Write a Python program to calculate the difference between a given number and 17.\n","\n","If the number is greater than 17, return twice the absolute difference.\n"],"metadata":{"id":"8tKlZG76ENYo"}},{"cell_type":"code","source":["import math\n","n =  int( input(\"Enter an integer n: \") )\n","\n","if n > 17:\n","   result = (n - 17) * 2\n","else :\n","   result = abs(n-17)\n","\n","print(result)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"JT7yqqK2EeIp","executionInfo":{"status":"ok","timestamp":1704919490671,"user_tz":-120,"elapsed":22569,"user":{"displayName":"Data Science","userId":"07742026815209387612"}},"outputId":"1de35fbe-0622-4b79-f944-57907e7fa3fc"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Enter an integer n: 14\n","3\n"]}]},{"cell_type":"markdown","source":["## **Exercice 7**\n","Write a Python program to calculate the sum of three given numbers. If the values are equal, return three times their sum."],"metadata":{"id":"FufJqxHMIFII"}},{"cell_type":"code","source":["a= int( input(\"Enter the first number: \"))\n","b= int( input(\"Enter the second number: \"))\n","c= int( input(\"Enter the first number: \"))\n","\n","if a == b == c :\n","   result = (a + b + c) * 3\n","else:\n","   result = (a + b + c)\n","print(result)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"wN8p7-YdIK0g","executionInfo":{"status":"ok","timestamp":1704920074038,"user_tz":-120,"elapsed":4514,"user":{"displayName":"Data Science","userId":"07742026815209387612"}},"outputId":"e1bffa5b-e8dc-44dc-c6bc-319beead3ed3"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Enter the first number: 1\n","Enter the second number: 2\n","Enter the first number: 3\n","6\n"]}]},{"cell_type":"markdown","source":["## **Exercice 8**\n"," Write a Python program that determines whether a given number (accepted from the user) is even or odd, and prints an appropriate message to the user."],"metadata":{"id":"AyG4Tq6aVrOD"}},{"cell_type":"code","source":["n = int(input(\"Enter a number: \"))\n","if n % 2 == 0:\n","  print(\"The number \", n , \" is even\")\n","else :\n","  print(\"The number \", n , \" is odd\")"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"WdfC1jNIVwiO","executionInfo":{"status":"ok","timestamp":1704923335315,"user_tz":-120,"elapsed":2236,"user":{"displayName":"Data Science","userId":"07742026815209387612"}},"outputId":"ff900678-4c87-4f70-f405-c8bead648703"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Enter a number: 7\n","The number  7  is odd\n"]}]},{"cell_type":"markdown","source":["## **Exercice 9**\n","Write a Python program to count the number 4 in a given list."],"metadata":{"id":"TCVkPcZaWmIm"}},{"cell_type":"code","source":["def count_4 (numbers):\n","    c = 0\n","    for n in numbers:\n","      if n == 4:\n","       c = c + 1\n","    return c\n","\n","print (count_4([1,2,4,4,5,6,7,4]) )\n","\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"1ULhAx8MWteF","executionInfo":{"status":"ok","timestamp":1704923835275,"user_tz":-120,"elapsed":346,"user":{"displayName":"Data Science","userId":"07742026815209387612"}},"outputId":"2d21a34e-4ab3-4728-9c00-23803e5c9c7c"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["3\n"]}]},{"cell_type":"markdown","source":["## **Exercice 10**\n","Write a Python program that checks whether a specified value is contained within a group of values.\n","\n","Test Data :\n","\n","3 -> [1, 5, 8, 3] : True\n","\n","-1 -> [1, 5, 8, 3] : False"],"metadata":{"id":"zdSjynPQYfnP"}},{"cell_type":"code","source":["def check_for_value(nums , n) :\n","   for num in nums:\n","     if n == num:\n","       return True\n","\n","   return False\n","\n","print (check_for_value([1,2,3,4,5,6] , 9) )\n","\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"IoSupvAiYsz-","executionInfo":{"status":"ok","timestamp":1704924302997,"user_tz":-120,"elapsed":316,"user":{"displayName":"Data Science","userId":"07742026815209387612"}},"outputId":"8b854ff9-7e05-44f6-97f1-335c8a5f9437"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["False\n"]}]},{"cell_type":"markdown","source":["## **Exercice 11**\n","Write a Python program to print all even numbers from a given list of numbers in the same order and stop printing any after 237 in the sequence.\n","Sample numbers list :\n","\n","numbers = [386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345,  399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217, 815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717, 958,743, 527]"],"metadata":{"id":"2chYQhztawh9"}},{"cell_type":"code","source":["nums = [386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615,\n","        953, 345, 399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950,\n","        626, 949, 687, 217, 815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81,\n","        379, 843, 831, 445, 742, 717, 958,743, 527]\n","\n","def print_even(nums):\n","  for num in nums:\n","      if num % 2 == 0:\n","        print(num , \" \")\n","\n","      if num == 237:\n","        break\n","\n","print_even(nums)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"vCZ-oc9ta5YD","executionInfo":{"status":"ok","timestamp":1704925226702,"user_tz":-120,"elapsed":297,"user":{"displayName":"Data Science","userId":"07742026815209387612"}},"outputId":"cd177920-1240-4251-884e-96ef09f8f9a5"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["386  \n","462  \n","418  \n","344  \n","236  \n","566  \n","978  \n","328  \n","162  \n","758  \n","918  \n"]}]},{"cell_type":"markdown","source":["## **Exercice 12**\n","Write a Python program that returns true if the two given integer values are equal or their sum or difference is 5."],"metadata":{"id":"OxERD7Kcdwl4"}},{"cell_type":"code","source":["a = int(input(\"Enter a number: \"))\n","b = int(input(\"Enter a number: \"))\n","\n","if a == b or a + b == 5 or a - b == 5 :\n","   print(\"True\")\n","else:\n","  print(\"False\")\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"e5zLKhxTeCgo","executionInfo":{"status":"ok","timestamp":1704926244112,"user_tz":-120,"elapsed":19074,"user":{"displayName":"Data Science","userId":"07742026815209387612"}},"outputId":"5c2485d5-bed1-4620-9c6c-2d32eb1953b5"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Enter a number: 6\n","Enter a number: 1\n","True\n"]}]},{"cell_type":"markdown","source":["## **Exercice 12**\n","Write a Python program to calculate sum of digits of a number.\n","\n","\n","\n","\n"],"metadata":{"id":"hnx8qEB4iBI9"}},{"cell_type":"code","source":["num = int(input(\"Input a four-digit number: \"))\n","\n","x = num // 1000\n","x1 = (num - x * 1000) // 100\n","x2 = ( num -  x * 1000 - x1 * 100) // 10\n","x3 = num -  x * 1000 - x1 * 100 - x2 * 10\n","\n","print(\" The sum of digits of \" , num ,\": \" , x + x1 + x2 + x3)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"n6GXaD4ziNn1","executionInfo":{"status":"ok","timestamp":1704927070127,"user_tz":-120,"elapsed":4551,"user":{"displayName":"Data Science","userId":"07742026815209387612"}},"outputId":"6c3874a1-db82-45f5-896d-ec3c4fdc3ef4"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Input a four-digit number: 1234\n"," The sum of digits of  1234 :  10\n"]}]},{"cell_type":"markdown","source":["## **Exercice 13**\n","Write a Python program to calculate the sum of all items of a list"],"metadata":{"id":"WD23v9wSk3aJ"}},{"cell_type":"code","source":["print ( sum([10, 20, 30]) )"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Vc4F6GfCk9Nq","executionInfo":{"status":"ok","timestamp":1704927238854,"user_tz":-120,"elapsed":276,"user":{"displayName":"Data Science","userId":"07742026815209387612"}},"outputId":"6a74cbea-d539-4c41-91c1-f88be6d335aa"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["60\n"]}]}]}