diff --git a/6-TuplesDictionaries.ipynb b/6-TuplesDictionaries.ipynb index 0011bcc..52c55dd 100644 --- a/6-TuplesDictionaries.ipynb +++ b/6-TuplesDictionaries.ipynb @@ -32,9 +32,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "-2\n", + "4\n" + ] + } + ], "source": [ "# index based iteration\n", "coord = (3, -2, 4)\n", @@ -44,9 +54,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "-2\n", + "4\n" + ] + } + ], "source": [ "# element based iteration\n", "for x in coord:\n", @@ -91,12 +111,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ - "coord = (3, -2, 4)\n", - "coord[1] = 0" + "coord = (3, -2, 4)\n" ] }, { @@ -137,18 +156,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# A dictionary with integer keys\n", - "squares = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}\n", - "print(squares[4])" + "squares = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -158,13 +176,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# A dictorionary with list of values\n", - "models = {'Audi': ['A4', 'A5', 'A6', 'A7', 'A8'], 'BMW': ['X1', 'X3', 'X5'], 'Mercedes': ['C-Class', 'E-Class']}\n", - "print(models['BMW'][1])" + "models = {'Audi': ['A4', 'A5', 'A6', 'A7', 'A8'], 'BMW': ['X1', 'X3', 'X5'], 'Mercedes': ['C-Class', 'E-Class']}" ] }, { @@ -181,9 +198,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6.94\n" + ] + } + ], "source": [ "atoms = {'H': 1.01, 'He': 4.00, 'Li': 6.94, 'Na': 22.99, 'O': 16.00}\n", "print(atoms['Li'])" @@ -204,9 +229,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "18.02\n" + ] + } + ], "source": [ "w = 2 * atoms['H'] + atoms['O']\n", "print(w)" @@ -225,9 +258,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'H': 1.01, 'He': 4.0, 'Li': 6.94, 'Na': 22.99, 'O': 16.0, 'C': 12.01}\n" + ] + } + ], "source": [ "# Add a new element\n", "atoms['C'] = 12.01\n", @@ -246,9 +287,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'H': 1.01, 'He': 4.0, 'Li': 6.94, 'Na': 23.0, 'O': 16.0, 'C': 12.01}\n" + ] + } + ], "source": [ "# Change the value of an existing key\n", "atoms['Na'] = 23.00\n", @@ -264,9 +313,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['A4', 'A5', 'A6', 'A7', 'A8', 'A9']\n" + ] + } + ], "source": [ "# Add a new element to an existing list\n", "models = {'Audi': ['A4', 'A5', 'A6', 'A7', 'A8'], 'BMW': ['X1', 'X3', 'X5'], 'Mercedes': ['C-Class', 'E-Class']}\n", @@ -287,18 +344,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "print(atoms)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'H': 1.01, 'Li': 6.94, 'Na': 23.0, 'O': 16.0, 'C': 12.01}\n" + ] + } + ], "source": [ "# Delete an element\n", "del atoms['He']\n", @@ -314,12 +370,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6.94\n" + ] + } + ], "source": [ - "del atoms['He']\n", - "print(atoms)" + "\n", + "print(atoms['Li'])" ] }, { @@ -341,7 +405,18 @@ "cell_type": "code", "execution_count": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_keys(['H', 'He', 'Li', 'Na', 'O'])\n", + "True\n", + "True\n", + "False\n" + ] + } + ], "source": [ "atoms = {'H': 1.01, 'He': 4.00, 'Li': 6.94, 'Na': 22.99, 'O': 16.00}\n", "print(atoms.keys())\n", @@ -360,9 +435,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['H', 'He', 'Li', 'Na', 'O']\n" + ] + } + ], "source": [ "keys_list = list(atoms.keys())\n", "print(keys_list)" @@ -410,9 +493,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ "models = {'Audi': ['A4', 'A5', 'A6', 'A7', 'A8'], 'BMW': ['X1', 'X3', 'X5'], 'Mercedes': ['C-Class', 'E-Class']}\n", "found = False\n", @@ -432,9 +523,18 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ] + } + ], "source": [ "models = {'Audi': ['A4', 'A5', 'A6', 'A7', 'A8'], 'BMW': ['X1', 'X3', 'X5'], 'Mercedes': ['C-Class', 'E-Class']}\n", "print('X1' in models.values())\n", @@ -458,15 +558,26 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'tom': '76r76438', 'tobby': '3567638'}\n" + ] + } + ], "source": [ "phone_dict = {}\n", - "data = \"-\"\n", - "while data != \"\":\n", - " data = input(\"Give a name and a phone number separated by a comma: \")\n", + "data = input(\"Give a name and a phone number separated by a comma: \")\n", "\n", + "while data != \"\":\n", " ... # add the name and phone number to the dictionary\n", "\n", + " name, phone = data.split(',')\n", + " \n", + " phone_dict[name] = phone\n", + " data = input(\"Give a name and a phone number separated by a comma: \")\n", "print(phone_dict)" ] }, @@ -500,9 +611,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['H-Hydrogen-Nonmetal|1,0,1.00794,2.20\\n', 'He-Helium-NobleGas|2,2,4.002602,0.00\\n', 'Li-Lithium-AlkaliMetal|3,4,6.941,0.98\\n', 'Be-Beryllium-AlkalineEarthMetal|4,5,9.012182,1.57\\n', 'B-Boron-Metalloid|5,6,10.811,2.04\\n', 'C-Carbon-Nonmetal|6,6,12.0107,2.55\\n', 'N-Nitrogen-Nonmetal|7,7,14.0067,3.04\\n', 'O-Oxygen-Chalcogen|8,8,15.9994,3.44\\n', 'F-Fluorine-Halogen|9,10,18.9984032,3.98\\n', 'Ne-Neon-NobleGas|10,10,20.1791,0.00\\n', 'Na-Sodium-AlkaliMetal|11,12,22.98976928,0.93\\n', 'Mg-Magnesium-AlkalineEarthMetal|12,12,24.305,1.31\\n', 'Al-Aluminum-PoorMetal|13,14,26.9815386,1.61\\n', 'Si-Silicon-Metalloid|14,14,28.0855,1.90\\n', 'P-Phosphorus-Nonmetal|15,16,30.973762,2.19\\n', 'S-Sulfur-Chalcogen|16,16,32.065,2.58\\n', 'Cl-Chlorine-Halogen|17,18,35.453,3.16\\n', 'Ar-Argon-NobleGas|18,22,39.948,0.00\\n', 'K-Potassium-AlkaliMetal|19,20,39.0983,0.82\\n', 'Ca-Calcium-AlkalineEarthMetal|20,20,40.078,1.00\\n', 'Sc-Scandium-TransitionMetal|21,24,44.955912,1.36\\n', 'Ti-Titanium-TransitionMetal|22,26,47.867,1.54\\n', 'V-Vanadium-TransitionMetal|23,28,50.9415,1.63\\n', 'Cr-Chromium-TransitionMetal|24,28,51.9961,1.66\\n', 'Mn-Manganese-TransitionMetal|25,30,54.938045,1.55\\n', 'Fe-Iron-TransitionMetal|26,30,55.845,1.83\\n', 'Co-Cobalt-TransitionMetal|27,32,58.933195,1.88\\n', 'Ni-Nickel-TransitionMetal|28,31,58.6934,1.91\\n', 'Cu-Copper-TransitionMetal|29,35,63.546,1.90\\n', 'Zn-Zinc-TransitionMetal|30,35,65.38,1.65\\n', 'Ga-Gallium-PoorMetal|31,39,69.723,1.81\\n', 'Ge-Germanium-Metalloid|32,41,72.64,2.01\\n', 'As-Arsenic-Metalloid|33,42,74.9216,2.18\\n', 'Se-Selenium-Chalcogen|34,45,78.96,2.55\\n', 'Br-Bromine-Halogen|35,45,79.904,2.96\\n', 'Kr-Krypton-NobleGas|36,48,83.798,3.00\\n', 'Rb-Rubidium-AlkaliMetal|37,48,85.4678,0.82\\n', 'Sr-Strontium-AlkalineEarthMetal|38,50,87.62,0.95\\n', 'Y-Yttrium-TransitionMetal|39,50,88.90585,1.22\\n', 'Zr-Zirconium-TransitionMetal|40,51,91.224,1.33\\n', 'Nb-Niobium-TransitionMetal|41,52,92.90638,1.60\\n', 'Mo-Molybdenum-TransitionMetal|42,54,95.96,2.16\\n', 'Tc-Technetium-TransitionMetal|43,55,98,1.90\\n', 'Ru-Ruthenium-TransitionMetal|44,57,101.07,2.20\\n', 'Rh-Rhodium-TransitionMetal|45,58,102.9055,2.28\\n', 'Pd-Palladium-TransitionMetal|46,60,106.42,2.20\\n', 'Ag-Silver-TransitionMetal|47,61,107.8682,1.93\\n', 'Cd-Cadmium-TransitionMetal|48,64,112.411,1.69\\n', 'In-Indium-PoorMetal|49,66,114.818,1.78\\n', 'Sn-Tin-PoorMetal|50,69,118.71,1.96\\n', 'Sb-Antimony-Metalloid|51,71,121.76,2.05\\n', 'Te-Tellurium-Chalcogen|52,76,127.6,2.10\\n', 'I-Iodine-Halogen|53,74,126.90447,2.66\\n', 'Xe-Xenon-NobleGas|54,77,131.293,2.60\\n', 'Cs-Cesium-AlkaliMetal|55,78,132.9054519,0.79\\n', 'Ba-Barium-AlkalineEarthMetal|56,81,137.327,0.89\\n', 'La-Lanthanum-Lanthanide|57,82,138.90547,1.10\\n', 'Ce-Cerium-Lanthanide|58,82,140.116,1.12\\n', 'Pr-Praseodymium-Lanthanide|59,82,140.90765,1.13\\n', 'Nd-Neodymium-Lanthanide|60,84,144.242,1.14\\n', 'Pm-Promethium-Lanthanide|61,84,145,0.00\\n', 'Sm-Samarium-Lanthanide|62,88,150.36,1.17\\n', 'Eu-Europium-Lanthanide|63,89,151.964,0.00\\n', 'Gd-Gadolinium-Lanthanide|64,93,157.25,1.20\\n', 'Tb-Terbium-Lanthanide|65,94,158.92535,0.00\\n', 'Dy-Dysprosium-Lanthanide|66,96,162.5,1.22\\n', 'Ho-Holmium-Lanthanide|67,98,164.93032,1.23\\n', 'Er-Erbium-Lanthanide|68,99,167.259,1.24\\n', 'Tm-Thulium-Lanthanide|69,100,168.93421,1.25\\n', 'Yb-Ytterbium-Lanthanide|70,103,173.054,0.00\\n', 'Lu-Lutetium-TransitionMetal|71,104,174.9668,1.27\\n', 'Hf-Hafnium-TransitionMetal|72,106,178.49,1.30\\n', 'Ta-Tantalum-TransitionMetal|73,108,180.94788,1.50\\n', 'W-Tungsten-TransitionMetal|74,110,183.84,2.36\\n', 'Re-Rhenium-TransitionMetal|75,111,186.207,1.90\\n', 'Os-Osmium-TransitionMetal|76,114,190.23,2.20\\n', 'Ir-Iridium-TransitionMetal|77,115,192.217,2.20\\n', 'Pt-Platinum-TransitionMetal|78,117,195.084,2.28\\n', 'Au-Gold-TransitionMetal|79,118,196.966569,2.54\\n', 'Hg-Mercury-TransitionMetal|80,121,200.59,2.00\\n', 'Tl-Thallium-PoorMetal|81,123,204.3833,1.62\\n', 'Pb-Lead-PoorMetal|82,125,207.2,2.33\\n', 'Bi-Bismuth-PoorMetal|83,126,208.9804,2.02\\n', 'Po-Polonium-Chalcogen|84,125,209,2.00\\n', 'At-Astatine-Halogen|85,125,210,2.20\\n', 'Rn-Radon-NobleGas|86,136,222,0.00\\n', 'Fr-Francium-AlkaliMetal|87,136,223,0.70\\n', 'Ra-Radium-AlkalineEarthMetal|88,138,226,0.90\\n', 'Ac-Actinium-Actinide|89,138,227,1.10\\n', 'Th-Thorium-Actinide|90,142,232.03806,1.30\\n', 'Pa-Protactinium-Actinide|91,140,231.03586,1.50\\n', 'U-Uranium-Actinide|92,146,238.02891,1.38\\n', 'Np-Neptunium-Actinide|93,144,237,1.36\\n', 'Pu-Plutonium-Actinide|94,150,244,1.28\\n', 'Am-Americium-Actinide|95,148,243,1.30\\n', 'Cm-Curium-Actinide|96,151,247,1.30\\n', 'Bk-Berkelium-Actinide|97,150,247,1.30\\n', 'Cf-Californium-Actinide|98,153,251,1.30\\n', 'Es-Einsteinium-Actinide|99,153,252,1.30\\n', 'Fm-Fermium-Actinide|100,157,257,1.30\\n', 'Md-Mendelevium-Actinide|101,157,258,1.30\\n', 'No-Nobelium-Actinide|102,157,259,1.30\\n', 'Lr-Lawrencium-TransitionMetal|103,159,262,0.00\\n', 'Rf-Rutherfordium-TransitionMetal|104,161,265,0.00\\n', 'Db-Dubnium-TransitionMetal|105,163,268,0.00\\n', 'Sg-Seaborgium-TransitionMetal|106,165,271,0.00\\n', 'Bh-Bohrium-TransitionMetal|107,165,272,0.00\\n', 'Hs-Hassium-TransitionMetal|108,162,270,0.00\\n', 'Mt-Meitnerium-TransitionMetal|109,167,276,0.00\\n', 'Ds-Darmstadtium-TransitionMetal|110,171,281,0.00\\n', 'Rg-Roentgenium-TransitionMetal|111,169,280,0.00\\n', 'Cn-Copernicium-TransitionMetal|112,173,285,0.00\\n', 'Uut-Ununtrium-PoorMetal|113,171,284,0.00\\n', 'Uuq-Ununquadium-PoorMetal|114,175,289,0.00\\n', 'Uup-Ununpentium-PoorMetal|115,173,288,0.00\\n', 'Uuh-Ununhexium-PoorMetal|116,177,293,0.00\\n', 'Uus-Ununseptium-Halogen|117,177,294,0.00\\n', 'Uuo-Ununoctium-NobleGas|118,176,294,0.00\\n']\n" + ] + } + ], "source": [ "f = open(\"files/mendelejev.txt\")\n", "content = f.readlines()\n", @@ -518,7 +637,7 @@ "\n", "Write a function `make_dict()` that accepts a list of strings as input and returns a dictionary `element_dict` where the keys are the symbols of the elements and the values are tuples containing the name, group name, atomic number, atomic weight, neutron number and electronegativity of the elements. The function should return the dictionary `element_dict`. The first key value pair of the dictionary should look like this:\n", "\n", - "` 'H': ('Hydrogen', 'Nonmetal', 1, 1.00794, 1, 2.20)`.\n", + "` 'H': ['Hydrogen', 'Nonmetal', 1, 1.00794, 1, 2.20]`.\n", "\n", "**Hints**\n", "* Each string can be split into two parts using the `split('|')` method. The first part contains the textual information and the second part contains the numerical information. \n", @@ -528,21 +647,59 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['H-Hydrogen-Nonmetal|1,0,1.00794,2.20\\n', 'He-Helium-NobleGas|2,2,4.002602,0.00\\n', 'Li-Lithium-AlkaliMetal|3,4,6.941,0.98\\n', 'Be-Beryllium-AlkalineEarthMetal|4,5,9.012182,1.57\\n', 'B-Boron-Metalloid|5,6,10.811,2.04\\n', 'C-Carbon-Nonmetal|6,6,12.0107,2.55\\n', 'N-Nitrogen-Nonmetal|7,7,14.0067,3.04\\n', 'O-Oxygen-Chalcogen|8,8,15.9994,3.44\\n', 'F-Fluorine-Halogen|9,10,18.9984032,3.98\\n', 'Ne-Neon-NobleGas|10,10,20.1791,0.00\\n', 'Na-Sodium-AlkaliMetal|11,12,22.98976928,0.93\\n', 'Mg-Magnesium-AlkalineEarthMetal|12,12,24.305,1.31\\n', 'Al-Aluminum-PoorMetal|13,14,26.9815386,1.61\\n', 'Si-Silicon-Metalloid|14,14,28.0855,1.90\\n', 'P-Phosphorus-Nonmetal|15,16,30.973762,2.19\\n', 'S-Sulfur-Chalcogen|16,16,32.065,2.58\\n', 'Cl-Chlorine-Halogen|17,18,35.453,3.16\\n', 'Ar-Argon-NobleGas|18,22,39.948,0.00\\n', 'K-Potassium-AlkaliMetal|19,20,39.0983,0.82\\n', 'Ca-Calcium-AlkalineEarthMetal|20,20,40.078,1.00\\n', 'Sc-Scandium-TransitionMetal|21,24,44.955912,1.36\\n', 'Ti-Titanium-TransitionMetal|22,26,47.867,1.54\\n', 'V-Vanadium-TransitionMetal|23,28,50.9415,1.63\\n', 'Cr-Chromium-TransitionMetal|24,28,51.9961,1.66\\n', 'Mn-Manganese-TransitionMetal|25,30,54.938045,1.55\\n', 'Fe-Iron-TransitionMetal|26,30,55.845,1.83\\n', 'Co-Cobalt-TransitionMetal|27,32,58.933195,1.88\\n', 'Ni-Nickel-TransitionMetal|28,31,58.6934,1.91\\n', 'Cu-Copper-TransitionMetal|29,35,63.546,1.90\\n', 'Zn-Zinc-TransitionMetal|30,35,65.38,1.65\\n', 'Ga-Gallium-PoorMetal|31,39,69.723,1.81\\n', 'Ge-Germanium-Metalloid|32,41,72.64,2.01\\n', 'As-Arsenic-Metalloid|33,42,74.9216,2.18\\n', 'Se-Selenium-Chalcogen|34,45,78.96,2.55\\n', 'Br-Bromine-Halogen|35,45,79.904,2.96\\n', 'Kr-Krypton-NobleGas|36,48,83.798,3.00\\n', 'Rb-Rubidium-AlkaliMetal|37,48,85.4678,0.82\\n', 'Sr-Strontium-AlkalineEarthMetal|38,50,87.62,0.95\\n', 'Y-Yttrium-TransitionMetal|39,50,88.90585,1.22\\n', 'Zr-Zirconium-TransitionMetal|40,51,91.224,1.33\\n', 'Nb-Niobium-TransitionMetal|41,52,92.90638,1.60\\n', 'Mo-Molybdenum-TransitionMetal|42,54,95.96,2.16\\n', 'Tc-Technetium-TransitionMetal|43,55,98,1.90\\n', 'Ru-Ruthenium-TransitionMetal|44,57,101.07,2.20\\n', 'Rh-Rhodium-TransitionMetal|45,58,102.9055,2.28\\n', 'Pd-Palladium-TransitionMetal|46,60,106.42,2.20\\n', 'Ag-Silver-TransitionMetal|47,61,107.8682,1.93\\n', 'Cd-Cadmium-TransitionMetal|48,64,112.411,1.69\\n', 'In-Indium-PoorMetal|49,66,114.818,1.78\\n', 'Sn-Tin-PoorMetal|50,69,118.71,1.96\\n', 'Sb-Antimony-Metalloid|51,71,121.76,2.05\\n', 'Te-Tellurium-Chalcogen|52,76,127.6,2.10\\n', 'I-Iodine-Halogen|53,74,126.90447,2.66\\n', 'Xe-Xenon-NobleGas|54,77,131.293,2.60\\n', 'Cs-Cesium-AlkaliMetal|55,78,132.9054519,0.79\\n', 'Ba-Barium-AlkalineEarthMetal|56,81,137.327,0.89\\n', 'La-Lanthanum-Lanthanide|57,82,138.90547,1.10\\n', 'Ce-Cerium-Lanthanide|58,82,140.116,1.12\\n', 'Pr-Praseodymium-Lanthanide|59,82,140.90765,1.13\\n', 'Nd-Neodymium-Lanthanide|60,84,144.242,1.14\\n', 'Pm-Promethium-Lanthanide|61,84,145,0.00\\n', 'Sm-Samarium-Lanthanide|62,88,150.36,1.17\\n', 'Eu-Europium-Lanthanide|63,89,151.964,0.00\\n', 'Gd-Gadolinium-Lanthanide|64,93,157.25,1.20\\n', 'Tb-Terbium-Lanthanide|65,94,158.92535,0.00\\n', 'Dy-Dysprosium-Lanthanide|66,96,162.5,1.22\\n', 'Ho-Holmium-Lanthanide|67,98,164.93032,1.23\\n', 'Er-Erbium-Lanthanide|68,99,167.259,1.24\\n', 'Tm-Thulium-Lanthanide|69,100,168.93421,1.25\\n', 'Yb-Ytterbium-Lanthanide|70,103,173.054,0.00\\n', 'Lu-Lutetium-TransitionMetal|71,104,174.9668,1.27\\n', 'Hf-Hafnium-TransitionMetal|72,106,178.49,1.30\\n', 'Ta-Tantalum-TransitionMetal|73,108,180.94788,1.50\\n', 'W-Tungsten-TransitionMetal|74,110,183.84,2.36\\n', 'Re-Rhenium-TransitionMetal|75,111,186.207,1.90\\n', 'Os-Osmium-TransitionMetal|76,114,190.23,2.20\\n', 'Ir-Iridium-TransitionMetal|77,115,192.217,2.20\\n', 'Pt-Platinum-TransitionMetal|78,117,195.084,2.28\\n', 'Au-Gold-TransitionMetal|79,118,196.966569,2.54\\n', 'Hg-Mercury-TransitionMetal|80,121,200.59,2.00\\n', 'Tl-Thallium-PoorMetal|81,123,204.3833,1.62\\n', 'Pb-Lead-PoorMetal|82,125,207.2,2.33\\n', 'Bi-Bismuth-PoorMetal|83,126,208.9804,2.02\\n', 'Po-Polonium-Chalcogen|84,125,209,2.00\\n', 'At-Astatine-Halogen|85,125,210,2.20\\n', 'Rn-Radon-NobleGas|86,136,222,0.00\\n', 'Fr-Francium-AlkaliMetal|87,136,223,0.70\\n', 'Ra-Radium-AlkalineEarthMetal|88,138,226,0.90\\n', 'Ac-Actinium-Actinide|89,138,227,1.10\\n', 'Th-Thorium-Actinide|90,142,232.03806,1.30\\n', 'Pa-Protactinium-Actinide|91,140,231.03586,1.50\\n', 'U-Uranium-Actinide|92,146,238.02891,1.38\\n', 'Np-Neptunium-Actinide|93,144,237,1.36\\n', 'Pu-Plutonium-Actinide|94,150,244,1.28\\n', 'Am-Americium-Actinide|95,148,243,1.30\\n', 'Cm-Curium-Actinide|96,151,247,1.30\\n', 'Bk-Berkelium-Actinide|97,150,247,1.30\\n', 'Cf-Californium-Actinide|98,153,251,1.30\\n', 'Es-Einsteinium-Actinide|99,153,252,1.30\\n', 'Fm-Fermium-Actinide|100,157,257,1.30\\n', 'Md-Mendelevium-Actinide|101,157,258,1.30\\n', 'No-Nobelium-Actinide|102,157,259,1.30\\n', 'Lr-Lawrencium-TransitionMetal|103,159,262,0.00\\n', 'Rf-Rutherfordium-TransitionMetal|104,161,265,0.00\\n', 'Db-Dubnium-TransitionMetal|105,163,268,0.00\\n', 'Sg-Seaborgium-TransitionMetal|106,165,271,0.00\\n', 'Bh-Bohrium-TransitionMetal|107,165,272,0.00\\n', 'Hs-Hassium-TransitionMetal|108,162,270,0.00\\n', 'Mt-Meitnerium-TransitionMetal|109,167,276,0.00\\n', 'Ds-Darmstadtium-TransitionMetal|110,171,281,0.00\\n', 'Rg-Roentgenium-TransitionMetal|111,169,280,0.00\\n', 'Cn-Copernicium-TransitionMetal|112,173,285,0.00\\n', 'Uut-Ununtrium-PoorMetal|113,171,284,0.00\\n', 'Uuq-Ununquadium-PoorMetal|114,175,289,0.00\\n', 'Uup-Ununpentium-PoorMetal|115,173,288,0.00\\n', 'Uuh-Ununhexium-PoorMetal|116,177,293,0.00\\n', 'Uus-Ununseptium-Halogen|117,177,294,0.00\\n', 'Uuo-Ununoctium-NobleGas|118,176,294,0.00\\n']\n" + ] + } + ], + "source": [ + "print(content)" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'H': ['Hydrogen', 'Nonmetal', 1, 0.0, 1.00794, 2.2], 'He': ['Helium', 'NobleGas', 2, 2.0, 4.002602, 0.0], 'Li': ['Lithium', 'AlkaliMetal', 3, 4.0, 6.941, 0.9], 'Be': ['Beryllium', 'AlkalineEarthMetal', 4, 5.0, 9.012182, 1.5], 'B': ['Boron', 'Metalloid', 5, 6.0, 10.811, 2.0], 'C': ['Carbon', 'Nonmetal', 6, 6.0, 12.0107, 2.5], 'N': ['Nitrogen', 'Nonmetal', 7, 7.0, 14.0067, 3.0], 'O': ['Oxygen', 'Chalcogen', 8, 8.0, 15.9994, 3.4], 'F': ['Fluorine', 'Halogen', 9, 10.0, 18.9984032, 3.9], 'Ne': ['Neon', 'NobleGas', 10, 10.0, 20.1791, 0.0], 'Na': ['Sodium', 'AlkaliMetal', 11, 12.0, 22.98976928, 0.9], 'Mg': ['Magnesium', 'AlkalineEarthMetal', 12, 12.0, 24.305, 1.3], 'Al': ['Aluminum', 'PoorMetal', 13, 14.0, 26.9815386, 1.6], 'Si': ['Silicon', 'Metalloid', 14, 14.0, 28.0855, 1.9], 'P': ['Phosphorus', 'Nonmetal', 15, 16.0, 30.973762, 2.1], 'S': ['Sulfur', 'Chalcogen', 16, 16.0, 32.065, 2.5], 'Cl': ['Chlorine', 'Halogen', 17, 18.0, 35.453, 3.1], 'Ar': ['Argon', 'NobleGas', 18, 22.0, 39.948, 0.0], 'K': ['Potassium', 'AlkaliMetal', 19, 20.0, 39.0983, 0.8], 'Ca': ['Calcium', 'AlkalineEarthMetal', 20, 20.0, 40.078, 1.0], 'Sc': ['Scandium', 'TransitionMetal', 21, 24.0, 44.955912, 1.3], 'Ti': ['Titanium', 'TransitionMetal', 22, 26.0, 47.867, 1.5], 'V': ['Vanadium', 'TransitionMetal', 23, 28.0, 50.9415, 1.6], 'Cr': ['Chromium', 'TransitionMetal', 24, 28.0, 51.9961, 1.6], 'Mn': ['Manganese', 'TransitionMetal', 25, 30.0, 54.938045, 1.5], 'Fe': ['Iron', 'TransitionMetal', 26, 30.0, 55.845, 1.8], 'Co': ['Cobalt', 'TransitionMetal', 27, 32.0, 58.933195, 1.8], 'Ni': ['Nickel', 'TransitionMetal', 28, 31.0, 58.6934, 1.9], 'Cu': ['Copper', 'TransitionMetal', 29, 35.0, 63.546, 1.9], 'Zn': ['Zinc', 'TransitionMetal', 30, 35.0, 65.38, 1.6], 'Ga': ['Gallium', 'PoorMetal', 31, 39.0, 69.723, 1.8], 'Ge': ['Germanium', 'Metalloid', 32, 41.0, 72.64, 2.0], 'As': ['Arsenic', 'Metalloid', 33, 42.0, 74.9216, 2.1], 'Se': ['Selenium', 'Chalcogen', 34, 45.0, 78.96, 2.5], 'Br': ['Bromine', 'Halogen', 35, 45.0, 79.904, 2.9], 'Kr': ['Krypton', 'NobleGas', 36, 48.0, 83.798, 3.0], 'Rb': ['Rubidium', 'AlkaliMetal', 37, 48.0, 85.4678, 0.8], 'Sr': ['Strontium', 'AlkalineEarthMetal', 38, 50.0, 87.62, 0.9], 'Y': ['Yttrium', 'TransitionMetal', 39, 50.0, 88.90585, 1.2], 'Zr': ['Zirconium', 'TransitionMetal', 40, 51.0, 91.224, 1.3], 'Nb': ['Niobium', 'TransitionMetal', 41, 52.0, 92.90638, 1.6], 'Mo': ['Molybdenum', 'TransitionMetal', 42, 54.0, 95.96, 2.1], 'Tc': ['Technetium', 'TransitionMetal', 43, 55.0, 98.0, 1.9], 'Ru': ['Ruthenium', 'TransitionMetal', 44, 57.0, 101.07, 2.2], 'Rh': ['Rhodium', 'TransitionMetal', 45, 58.0, 102.9055, 2.2], 'Pd': ['Palladium', 'TransitionMetal', 46, 60.0, 106.42, 2.2], 'Ag': ['Silver', 'TransitionMetal', 47, 61.0, 107.8682, 1.9], 'Cd': ['Cadmium', 'TransitionMetal', 48, 64.0, 112.411, 1.6], 'In': ['Indium', 'PoorMetal', 49, 66.0, 114.818, 1.7], 'Sn': ['Tin', 'PoorMetal', 50, 69.0, 118.71, 1.9], 'Sb': ['Antimony', 'Metalloid', 51, 71.0, 121.76, 2.0], 'Te': ['Tellurium', 'Chalcogen', 52, 76.0, 127.6, 2.1], 'I': ['Iodine', 'Halogen', 53, 74.0, 126.90447, 2.6], 'Xe': ['Xenon', 'NobleGas', 54, 77.0, 131.293, 2.6], 'Cs': ['Cesium', 'AlkaliMetal', 55, 78.0, 132.9054519, 0.7], 'Ba': ['Barium', 'AlkalineEarthMetal', 56, 81.0, 137.327, 0.8], 'La': ['Lanthanum', 'Lanthanide', 57, 82.0, 138.90547, 1.1], 'Ce': ['Cerium', 'Lanthanide', 58, 82.0, 140.116, 1.1], 'Pr': ['Praseodymium', 'Lanthanide', 59, 82.0, 140.90765, 1.1], 'Nd': ['Neodymium', 'Lanthanide', 60, 84.0, 144.242, 1.1], 'Pm': ['Promethium', 'Lanthanide', 61, 84.0, 145.0, 0.0], 'Sm': ['Samarium', 'Lanthanide', 62, 88.0, 150.36, 1.1], 'Eu': ['Europium', 'Lanthanide', 63, 89.0, 151.964, 0.0], 'Gd': ['Gadolinium', 'Lanthanide', 64, 93.0, 157.25, 1.2], 'Tb': ['Terbium', 'Lanthanide', 65, 94.0, 158.92535, 0.0], 'Dy': ['Dysprosium', 'Lanthanide', 66, 96.0, 162.5, 1.2], 'Ho': ['Holmium', 'Lanthanide', 67, 98.0, 164.93032, 1.2], 'Er': ['Erbium', 'Lanthanide', 68, 99.0, 167.259, 1.2], 'Tm': ['Thulium', 'Lanthanide', 69, 100.0, 168.93421, 1.2], 'Yb': ['Ytterbium', 'Lanthanide', 70, 103.0, 173.054, 0.0], 'Lu': ['Lutetium', 'TransitionMetal', 71, 104.0, 174.9668, 1.2], 'Hf': ['Hafnium', 'TransitionMetal', 72, 106.0, 178.49, 1.3], 'Ta': ['Tantalum', 'TransitionMetal', 73, 108.0, 180.94788, 1.5], 'W': ['Tungsten', 'TransitionMetal', 74, 110.0, 183.84, 2.3], 'Re': ['Rhenium', 'TransitionMetal', 75, 111.0, 186.207, 1.9], 'Os': ['Osmium', 'TransitionMetal', 76, 114.0, 190.23, 2.2], 'Ir': ['Iridium', 'TransitionMetal', 77, 115.0, 192.217, 2.2], 'Pt': ['Platinum', 'TransitionMetal', 78, 117.0, 195.084, 2.2], 'Au': ['Gold', 'TransitionMetal', 79, 118.0, 196.966569, 2.5], 'Hg': ['Mercury', 'TransitionMetal', 80, 121.0, 200.59, 2.0], 'Tl': ['Thallium', 'PoorMetal', 81, 123.0, 204.3833, 1.6], 'Pb': ['Lead', 'PoorMetal', 82, 125.0, 207.2, 2.3], 'Bi': ['Bismuth', 'PoorMetal', 83, 126.0, 208.9804, 2.0], 'Po': ['Polonium', 'Chalcogen', 84, 125.0, 209.0, 2.0], 'At': ['Astatine', 'Halogen', 85, 125.0, 210.0, 2.2], 'Rn': ['Radon', 'NobleGas', 86, 136.0, 222.0, 0.0], 'Fr': ['Francium', 'AlkaliMetal', 87, 136.0, 223.0, 0.7], 'Ra': ['Radium', 'AlkalineEarthMetal', 88, 138.0, 226.0, 0.9], 'Ac': ['Actinium', 'Actinide', 89, 138.0, 227.0, 1.1], 'Th': ['Thorium', 'Actinide', 90, 142.0, 232.03806, 1.3], 'Pa': ['Protactinium', 'Actinide', 91, 140.0, 231.03586, 1.5], 'U': ['Uranium', 'Actinide', 92, 146.0, 238.02891, 1.3], 'Np': ['Neptunium', 'Actinide', 93, 144.0, 237.0, 1.3], 'Pu': ['Plutonium', 'Actinide', 94, 150.0, 244.0, 1.2], 'Am': ['Americium', 'Actinide', 95, 148.0, 243.0, 1.3], 'Cm': ['Curium', 'Actinide', 96, 151.0, 247.0, 1.3], 'Bk': ['Berkelium', 'Actinide', 97, 150.0, 247.0, 1.3], 'Cf': ['Californium', 'Actinide', 98, 153.0, 251.0, 1.3], 'Es': ['Einsteinium', 'Actinide', 99, 153.0, 252.0, 1.3], 'Fm': ['Fermium', 'Actinide', 100, 157.0, 257.0, 1.3], 'Md': ['Mendelevium', 'Actinide', 101, 157.0, 258.0, 1.3], 'No': ['Nobelium', 'Actinide', 102, 157.0, 259.0, 1.3], 'Lr': ['Lawrencium', 'TransitionMetal', 103, 159.0, 262.0, 0.0], 'Rf': ['Rutherfordium', 'TransitionMetal', 104, 161.0, 265.0, 0.0], 'Db': ['Dubnium', 'TransitionMetal', 105, 163.0, 268.0, 0.0], 'Sg': ['Seaborgium', 'TransitionMetal', 106, 165.0, 271.0, 0.0], 'Bh': ['Bohrium', 'TransitionMetal', 107, 165.0, 272.0, 0.0], 'Hs': ['Hassium', 'TransitionMetal', 108, 162.0, 270.0, 0.0], 'Mt': ['Meitnerium', 'TransitionMetal', 109, 167.0, 276.0, 0.0], 'Ds': ['Darmstadtium', 'TransitionMetal', 110, 171.0, 281.0, 0.0], 'Rg': ['Roentgenium', 'TransitionMetal', 111, 169.0, 280.0, 0.0], 'Cn': ['Copernicium', 'TransitionMetal', 112, 173.0, 285.0, 0.0], 'Uut': ['Ununtrium', 'PoorMetal', 113, 171.0, 284.0, 0.0], 'Uuq': ['Ununquadium', 'PoorMetal', 114, 175.0, 289.0, 0.0], 'Uup': ['Ununpentium', 'PoorMetal', 115, 173.0, 288.0, 0.0], 'Uuh': ['Ununhexium', 'PoorMetal', 116, 177.0, 293.0, 0.0], 'Uus': ['Ununseptium', 'Halogen', 117, 177.0, 294.0, 0.0], 'Uuo': ['Ununoctium', 'NobleGas', 118, 176.0, 294.0, 0.0]}\n", + "['Hydrogen', 'Nonmetal', 1, 0.0, 1.00794, 2.2]\n" + ] + } + ], "source": [ "def make_dict(data):\n", " element_dict = {}\n", " for line in data:\n", - " textual, numerical = line.split(\"|\")\n", - " abbreviation, name, group = textual.split(\"-\")\n", - " atomic_nr, neutrons,mass,electroneg = numerical.split(\",\")\n", - " element_dict[abr] = (name, group, int(atomic_nr), float(mass), int(neutrons), float(electroneg))\n", + " keys_list, values_list = line.split('|')\n", + " symbol, name, type = keys_list.split('-')\n", + " number, weight, neutron, electronegativity = values_list.split(',')\n", + " electronegativity = electronegativity[0:-2:1]\n", + " my_list = []\n", + " my_list.append(name)\n", + " my_list.append(type)\n", + " my_list.append(int(number))\n", + " my_list.append(float(weight))\n", + " my_list.append(float(neutron))\n", + " my_list.append(float(electronegativity))\n", + " \n", + " \n", + " element_dict[symbol] = my_list\n", + " \n", " return element_dict\n", "\n", "element_dict = make_dict(content)\n", - "element_dict[\"H\"] # ['Hydrogen', 'Nonmetal', 1, 1.00794, 0, 2.20]" + "print(element_dict)\n", + "print(element_dict[\"H\"]) # ['Hydrogen', 'Nonmetal', 1, 1.00794, 1, 2.20]" ] }, { @@ -562,13 +719,17 @@ "metadata": {}, "outputs": [], "source": [ - "def make_dict(data):\n", - " element_dict = {}\n", - " ... # code \n", - " return element_dict\n", + "def make_group_dict(element_dict):\n", + " group_dict = {}\n", + " key_list = []\n", + " for j in element_dict.key():\n", + " key_list.append(j)\n", + " for i in element_dict.value():\n", + " group_dict[i[0]] = key_list\n", + " return group_dict\n", "\n", - "element_dict = make_dict(content)\n", - "element_dict[\"H\"] # ['Hydrogen', 'Nonmetal', 1, 1.00794, 1, 2.20]" + "group_dict = make_group_dict(element_dict)\n", + "group_dict[\"AlkaliMetal\"] # ['Li', 'Na', 'K', 'Rb', 'Cs', 'Fr']" ] }, { @@ -586,7 +747,7 @@ ], "metadata": { "kernelspec": { - "display_name": "base", + "display_name": ".venv", "language": "python", "name": "python3" }, @@ -600,7 +761,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.13.9" + "version": "3.12.3" } }, "nbformat": 4, diff --git a/8-NumPy.ipynb b/8-NumPy.ipynb index 3231394..25ecdd2 100644 --- a/8-NumPy.ipynb +++ b/8-NumPy.ipynb @@ -27,7 +27,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -71,9 +71,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 3 4 5]\n" + ] + } + ], "source": [ "import numpy as np\n", "\n", @@ -84,9 +92,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 2 3]\n", + " [4 5 6]\n", + " [7 8 9]]\n" + ] + } + ], "source": [ "# Create an array from a LIST of LISTS (see also Section 5.5.1)\n", "M = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n", @@ -102,9 +120,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ True False True False True]\n" + ] + } + ], "source": [ "# Array of bools\n", "b = np.array([True, False, True, False, True])\n", @@ -147,9 +173,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 1 2 3 4 5 6 7 8 9 10]\n" + ] + } + ], "source": [ "import numpy as np\n", "# increasing array from 1 to 10\n", @@ -159,9 +193,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10 9 8 7 6 5 4 3 2 1]\n" + ] + } + ], "source": [ "# decreasing array from 10 to 1\n", "y = np.arange(10, 0, -1)\n", @@ -170,9 +212,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 2 4 6 8 10]\n" + ] + } + ], "source": [ "# even numbers from 1 to 10\n", "z = np.arange(2, 11, 2)\n", @@ -181,9 +231,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1. 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2. ]\n" + ] + } + ], "source": [ "# from 1 to 2 in steps of 0.1\n", "w = np.arange(1, 2.1, 0.1)\n", @@ -212,9 +270,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0. 0.25 0.5 0.75 1. ]\n" + ] + } + ], "source": [ "import numpy as np\n", "# divide the interval from 0 to 1 into 5 equal parts\n", @@ -224,9 +290,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0. 0.20512821 0.41025641 0.61538462 0.82051282 1.02564103\n", + " 1.23076923 1.43589744 1.64102564 1.84615385 2.05128205 2.25641026\n", + " 2.46153846 2.66666667 2.87179487 3.07692308 3.28205128 3.48717949\n", + " 3.69230769 3.8974359 4.1025641 4.30769231 4.51282051 4.71794872\n", + " 4.92307692 5.12820513 5.33333333 5.53846154 5.74358974 5.94871795\n", + " 6.15384615 6.35897436 6.56410256 6.76923077 6.97435897 7.17948718\n", + " 7.38461538 7.58974359 7.79487179 8. ]\n" + ] + } + ], "source": [ "# create an array with 40 linearly spaced elements in the interval from 4 to 8\n", "y = np.linspace(0, 8, 40)\n", @@ -235,9 +315,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.20512820512820512\n" + ] + } + ], "source": [ "# return the step size\n", "print(y[1] - y[0])\n" @@ -276,9 +364,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0.]]\n" + ] + } + ], "source": [ "import numpy as np\n", "\n", @@ -289,9 +387,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1. 1. 1. 1. 1.]\n", + " [1. 1. 1. 1. 1.]\n", + " [1. 1. 1. 1. 1.]]\n" + ] + } + ], "source": [ "# create a 3x5 array of ones\n", "y = np.ones((3, 5)) # default dtype is float64\n", @@ -300,9 +408,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ True True True True True]\n", + " [ True True True True True]\n", + " [ True True True True True]]\n" + ] + } + ], "source": [ "# create a 3x5 array with True\n", "z = np.ones((3, 5), dtype = bool)\n", @@ -327,9 +445,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 1 2 3 4 5 6 7 8 9 10]\n" + ] + } + ], "source": [ "import numpy as np\n", "\n", @@ -473,11 +599,77 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18\n", + " 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36\n", + " 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54\n", + " 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72\n", + " 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90\n", + " 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108\n", + " 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126\n", + " 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144\n", + " 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162\n", + " 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180\n", + " 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198\n", + " 199]\n" + ] + } + ], "source": [ - "import numpy as np" + "import numpy as np\n", + "\n", + "x = np.arange(1, 200, 1)\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33\n", + " 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15\n", + " 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3\n", + " -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20 -21\n", + " -22 -23 -24 -25 -26 -27 -28 -29 -30 -31 -32 -33 -34 -35 -36 -37 -38 -39\n", + " -40 -41 -42 -43 -44 -45 -46 -47 -48 -49 -50]\n" + ] + } + ], + "source": [ + "y = np.arange(50,-51,-1)\n", + "print(y)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-10. -9.5 -9. -8.5 -8. -7.5 -7. -6.5 -6. -5.5 -5. -4.5\n", + " -4. -3.5 -3. -2.5 -2. -1.5 -1. -0.5 0. 0.5 1. 1.5\n", + " 2. 2.5 3. 3.5 4. 4.5 5. 5.5 6. 6.5 7. 7.5\n", + " 8. 8.5 9. 9.5 10. 10.5]\n" + ] + } + ], + "source": [ + "z = np.arange(-10.0, 11, 0.5)\n", + "print(z)" ] }, { @@ -495,12 +687,81 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 57, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 1 2 3 4 5]\n", + " [ 6 7 8 9 10]]\n" + ] + } + ], "source": [ "import numpy as np\n", - "... " + "y = np.arange(1,11).reshape(2, 5)\n", + "print(y)" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 0 1 0 1]\n", + " [0 1 0 1 0]\n", + " [1 0 1 0 1]\n", + " [0 1 0 1 0]]\n" + ] + } + ], + "source": [ + "s = np.array([1, 0]*10).reshape(4, 5)\n", + "print(s)" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1.]\n", + " [ 50. 49. 48. 47. 46. 45. 44. 43. 42. 41. 40. 39. 38. 37.\n", + " 36. 35. 34. 33. 32. 31. 30. 29. 28. 27. 26. 25. 24. 23.\n", + " 22. 21. 20. 19. 18. 17. 16. 15. 14. 13. 12. 11. 10. 9.\n", + " 8. 7. 6. 5. 4. 3. 2. 1. 0. -1. -2. -3. -4. -5.\n", + " -6. -7. -8. -9. -10. -11. -12. -13. -14. -15. -16. -17. -18. -19.\n", + " -20. -21. -22. -23. -24. -25. -26. -27. -28. -29. -30. -31. -32. -33.\n", + " -34. -35. -36. -37. -38. -39. -40. -41. -42. -43. -44. -45. -46. -47.\n", + " -48. -49. -50.]]\n" + ] + } + ], + "source": [ + "\n", + "v = np.array([1]*100)\n", + "k = np.arange(50, -51, -1)\n", + "\n", + "arr = np.ones((2,101))\n", + "arr[1,:] = k\n", + "print(arr)" ] }, { @@ -545,9 +806,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-3 -2 -1 0 1 2 3 4 5 6 7]\n" + ] + } + ], "source": [ "import numpy as np\n", "# create a 1D array with values from -3 to 7\n", @@ -557,9 +826,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-1\n", + "7\n", + "[-2 -1 0 1]\n", + "[-2 0 2 4 6]\n" + ] + } + ], "source": [ "# get the third element of x\n", "print(x[2])\n", @@ -601,24 +881,99 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 1 2 3 4 5]\n", + " [ 6 7 8 9 10]\n", + " [11 12 13 14 15]]\n" + ] + } + ], "source": [ "import numpy as np\n", "\n", "# create a 2D array with 3 rows and 5 columns with the numbers from 1 to 15\n", "M = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]])\n", - "print(M)\n", + "print(M)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9\n" + ] + } + ], + "source": [ "\n", "# get the element in the second row and the fourth column\n", - "print(M[1, 3])\n", + "print(M[1, 3])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 3 4 5]\n" + ] + } + ], + "source": [ "\n", "# get the first row of M\n", - "print(M[0])\n", + "print(M[0])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 3 8 13]\n" + ] + } + ], + "source": [ "\n", "# get the third column of M\n", - "print(M[:, 2])\n", + "print(M[:, 2])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 3 4 5]\n", + " [ 8 9 10]]\n" + ] + } + ], + "source": [ "\n", "# get the subarray of M consisting of the first two rows and the last three columns\n", "print(M[:2, 2:])" @@ -654,16 +1009,98 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10]\n", + "10\n" + ] + } + ], "source": [ "import numpy as np\n", "x = np.arange(-4, 11)\n", "print(x)\n", "\n", - "x[...] # last element\n", - "..." + "print(x[-1])# last element\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "[-4 -3 -2 -1 0 1 2 3 4 5]\n" + ] + } + ], + "source": [ + "\n", + "print(x[9])\n", + "print(x[0:10])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "[10 9 8 7 6 5 4 3 2 1]\n" + ] + } + ], + "source": [ + "print(x[-1])\n", + "y = x[::-1]\n", + "print(y[:10])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 3 4 5 6 7]\n" + ] + } + ], + "source": [ + "print(x[5:12])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-4 -2 0 2 4 6 8 10]\n" + ] + } + ], + "source": [ + "print(x[::2])" ] }, { @@ -701,14 +1138,35 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 1 2 3 4 5 6 7]\n", + " [ 8 9 10 11 12 13 14]\n", + " [15 16 17 18 19 20 21]\n", + " [22 23 24 25 26 27 28]\n", + " [29 30 31 32 33 34 35]]\n", + "[[1]]\n" + ] + } + ], "source": [ "import numpy as np\n", "M = np.arange(1, 36).reshape(5, 7)\n", "print(M)\n", "\n", - "M[..., ...] # element in the first row and first column\n", - "..." + "print(M[0,0]) # element in the first row and first column\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(M[-1:, :-1])" ] }, { @@ -1106,7 +1564,7 @@ "```python\n", " np.sum(array, axis = 0) # compute the sum value along the columns\n", " np.max(array, axis = 1) # compute the maximum value along the rows\n", - " np.mean(array, axis = 1) # compute the average along the rows\n", + " np.mean(array, axis = 1) # compute the average along the columns\n", "```\n", "\n", "Some examples of using the `axis` parameter are:" @@ -2516,7 +2974,7 @@ ], "metadata": { "kernelspec": { - "display_name": "base", + "display_name": ".venv", "language": "python", "name": "python3" }, @@ -2530,7 +2988,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.12.3" } }, "nbformat": 4,