{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionaries" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'name': 'Morty', 'age': 26}\n" ] } ], "source": [ "#declaring a dictionary variable\n", "empty={} #empty dictionary\n", "person={\"name\":\"John Smith\"} #dictionary with one key:value pair\n", "customer={\n", " \"name\":\"Morty\",\n", " \"age\":26\n", "} #dictionary with two key:value pair\n", "print(customer)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Accessing dictionary information" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "John\n" ] } ], "source": [ "#accessing dictionary information through keys\n", "person={\"name\":\"John\"}\n", "print(person[\"name\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using the get method" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "John\n", "None\n", "Age is not available\n" ] } ], "source": [ "#using the get method to access dictionary information\n", "person={\"name\":\"John\"}\n", "print(person.get(\"name\")) #retrieve value of name key\n", "print(person.get(\"age\")) #key does not exist,return None\n", "print(person.get(\"age\",\"Age is not available\")) #get is a secure way to retrieve information" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionaries with Lists" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "football\n" ] } ], "source": [ "#storing a list within a dictionary and accessing it\n", "data={\"sports\":[\"baseball\",\"football\",\"hockey\",\"soccer\"]}\n", "print(data[\"sports\"][1]) #first access the key , then index" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#improperly storing a list within a dictionary\n", "sports=[\"baseball\",\"football\",\"hockey\",\"soccer\"]\n", "# sports_dict=dict(sports) #error, no key\n", "sports_dict=dict({\"sports\":sports})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists with Dictionaries" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'name': 'Kirsten'}\n", "Kirsten\n" ] } ], "source": [ "#storing a dictionary within a list and accessing it\n", "data = [\"John\",\"Dennies\",{\"name\":\"Kirsten\"}]\n", "print(data[2]) #dictionary is at index 2\n", "print(data[2][\"name\"]) #first access index, then key" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionaries with Dictionaries" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'2018': 108, '2017': 93}\n", "93\n" ] } ], "source": [ "#storing a dictionary within a dictionary and accessing it\n", "data ={\n", " \"team\":\"Boston Red Sox\",\n", " \"wins\":{\"2018\":108,\"2017\":93}\n", "}\n", "print(data[\"wins\"])\n", "print(data[\"wins\"][\"2017\"]) #first access the wins key, then next key" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Exercises\n", "n,a=input(\"Enter name\"),input(\"Enter age\")\n", "person={\"name\":n,\"age\":a}\n", "print(person)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cheese\n", "sausage\n", "peppers\n" ] } ], "source": [ "pizza={\n", " \"ingredients\":[\"cheese\",\"sausage\",\"peppers\"]\n", "}\n", "for ing in pizza[\"ingredients\"]:\n", " print(ing)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Working with Dictionaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Adding new information" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'year': 2018, 'color': 'red'}\n", "Year: 2018 \tColor: red\n" ] } ], "source": [ "#adding new key/value pairs to a dictionary\n", "car={\"year\":2018}\n", "car[\"color\"]=\"red\"\n", "print(car)\n", "print(\"Year: {} \\tColor: {}\".format(car[\"year\"],car[\"color\"]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Changing information" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Year: 2018 \tColor: blue\n" ] } ], "source": [ "#updating the value of key/value pair that already exists\n", "car={\"year\":2018,\"color\":\"red\"}\n", "car[\"color\"]=\"blue\"\n", "print(\"Year: {} \\tColor: {}\".format(car[\"year\"],car[\"color\"]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Deleting information" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{}\n" ] } ], "source": [ "#deleting a key/value pair from a dictionary\n", "car={\"year\":2018}\n", "try:\n", " del car[\"year\"]\n", " print(car)\n", "except:\n", " print(\"The car does not exits\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Looping a dictionary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Looping only keys" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#looping over a dictionary via keys\n", "person={\"name\":\"John\",\"keys\":26}\n", "for k in person.keys():\n", " print(k)\n", " print(person[k])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Looping only values" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#looping over a dictionary via values\n", "person={\"name\":\"John\",\"age\":26}\n", "for v in person.values():\n", " print(v)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Looping Key-Value pairs" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#looping over a dictionary via key/value pair\n", "person={\"name\":\"John\",\"age\":26}\n", "for k,v in person.items():\n", " print(\"{} : {}\".format(k,v))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tuples, Sets, Frozensets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Declaring a Tuple" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#declaring a tuple\n", "t1=(\"hello\",2,\"hello\") #with paren\n", "t2=True,1 #without paren\n", "print(t1)\n", "print(t2)\n", "print(type(t1),type(t2)) #both are tuples\n", "#t1[0]=1 #tuples are immutable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Declaring a Set" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#declaring a set\n", "s1=set([1,2,3,1]) #uses the set keyword,square brackets\n", "s2={4,4,5} #uses curly brackets like dictionary\n", "print(s1)\n", "print(s2)\n", "print(type(s1),type(s2))\n", "s1.add(5) #use add method to add new items to a set\n", "print(s1)\n", "s1.remove(1) #use remove method to get rid of a value\n", "print(s1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Declaring a Frozenset" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#declaring a frozenset\n", "f=frozenset([1,2,3,4,2,1])\n", "print(f)\n", "print(type(f))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Exercise\n", "nums=[3,4,3,7,10]\n", "s=set(nums)\n", "print(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reading & Writing files" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#opening/creating and writing to a text file\n", "f=open(\"test.txt\",\"w+\") #opening a file in writing and reading mode\n", "f.write(\"This is a test\")\n", "f.close() #anytime you open a file,you must close it\n", "f=open(\"test.txt\",\"r\") #reading a file\n", "data=f.read()\n", "f.close()\n", "print(data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Writing to csv files" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#opening/creating and writing to a csv file\n", "import csv\n", "with open(\"test.csv\",mode=\"w\",newline=\"\") as f:\n", " w=csv.writer(f,delimiter=\",\")\n", " w.writerow([\"Name\",\"City\"])\n", " w.writerow([\"Craig Lou\",\"Taiwan\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reading from csv files" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#reading from csv files\n", "import csv\n", "with open(\"test.csv\",mode=\"r\") as f:\n", " r=csv.reader(f,delimiter=\",\")\n", " for row in r:\n", " print(row)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Exercise\n", "i=input(\"Enter your favourite no:\")\n", "f=open(\"fav.txt\",\"w+\")\n", "f.write(i)\n", "f.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data={\n", " \"name\":[\"Dave\",\"Jennis\",\"Peter\",\"Jess\"],\n", " \"language\":[\"Python\",\"C\",\"Python\",\"Java\"]\n", "}\n", "import csv\n", "with open(\"lang.csv\",mode=\"w\",newline=\"\") as f:\n", " w=csv.writer(f,delimiter=\",\")\n", " w.writerow([\"name\",\"language\"])\n", " w.writerow([data[\"name\"][0],data[\"language\"][0]])\n", " w.writerow([data[\"name\"][1],data[\"language\"][1]])\n", " w.writerow([data[\"name\"][2],data[\"language\"][2]])\n", " w.writerow([data[\"name\"][3],data[\"language\"][3]])\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Project - Creating a User Database with csv files" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Thanks for using our software\n" ] } ], "source": [ "import csv\n", "from IPython.display import clear_output\n", "#handle user registration and writing to a csv file\n", "def registerUser():\n", " with open(\"users.csv\",mode=\"a\",newline=\"\") as f:\n", " w=csv.writer(f,delimiter=\",\")\n", " print(\"To Register, enter your info\")\n", " email=input(\"E-mail: \")\n", " password=input(\"Password: \")\n", " password2=input(\"Retype password: \")\n", " clear_output()\n", " if password == password2:\n", " w.writerow([email,password])\n", " print(\"You are now registered\")\n", " else:\n", " print(\"Something went wrong.Pl try again\")\n", "#ask for user info , return true for correct login and false for incorrect login\n", "def loginUser():\n", " print(\"To Login, enter your info\")\n", " email=input(\"E-mail: \")\n", " password=input(\"Password: \")\n", " clear_output()\n", " with open(\"users.csv\",mode=\"r\") as f:\n", " r=csv.reader(f,delimiter=\",\")\n", " for row in r:\n", " if row == [email,password]:\n", " print(\"You are logged in!\")\n", " return True\n", " print(\"Something went wrong.Pl try again\")\n", " return False\n", "#variables for main loop\n", "active = True\n", "logged_in = False\n", "#main loop\n", "while active:\n", " if logged_in:\n", " print(\"1. Logout\\n2. Quit\")\n", " else:\n", " print(\"1. Login\\n2. Register\\n3. Quit\")\n", " choice=input(\"What would you like to do: \").lower()\n", " clear_output()\n", " if choice == \"register\" and logged_in == False:\n", " registerUser()\n", " elif choice == \"login\" and logged_in == False:\n", " logged_in = loginUser()\n", " elif choice == \"quit\":\n", " active = False\n", " print(\"Thanks for using our software\")\n", " elif choice == \"logout\" and logged_in == True:\n", " logged_in = False\n", " print(\"Your are now logged out\")\n", " else:\n", " print(\"Sorry. Pl try again\")\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }