Assignment18 PythonBasic
Assignment18 PythonBasic
Assignment18 PythonBasic
"cells": [
{
"cell_type": "markdown",
"id": "8b475621",
"metadata": {},
"source": [
"# Assignment 18"
]
},
{
"cell_type": "markdown",
"id": "aafb842e",
"metadata": {},
"source": [
"### 1. Create a zoo.py file first. Define the hours() function, which prints
the string 'Open 9-5 daily'. Then, use the interactive interpreter to import the
zoo module and call its hours() function."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "2d169bb8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Open 9-5 daily\n"
]
}
],
"source": [
"import zoo\n",
"zoo.hours()"
]
},
{
"cell_type": "markdown",
"id": "f82104a6",
"metadata": {},
"source": [
"### 2. In the interactive interpreter, import the zoo module as menagerie and
call its hours() function."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "4c9d6032",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Open 9-5 daily\n"
]
}
],
"source": [
"import zoo as menagerie\n",
"menagerie.hours()"
]
},
{
"cell_type": "markdown",
"id": "b53f6af4",
"metadata": {},
"source": [
"### 3. Using the interpreter, explicitly import and call the hours() function
from zoo."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "a68beeb1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Open 9-5 daily\n"
]
}
],
"source": [
"from zoo import hours\n",
"hours()"
]
},
{
"cell_type": "markdown",
"id": "0ef59304",
"metadata": {},
"source": [
"### 4. Import the hours() function as info and call it."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "87e6787e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Open 9-5 daily\n"
]
}
],
"source": [
"from zoo import hours as info\n",
"info()"
]
},
{
"cell_type": "markdown",
"id": "aa636140",
"metadata": {},
"source": [
"### 5. Create a plain dictionary with the key-value pairs 'a': 1, 'b': 2, and
'c': 3, and print it out."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "634fc084",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'a': 1, 'b': 2, 'c': 3}\n"
]
}
],
"source": [
"plain_dict = {'a':1,'b':2,'c':3}\n",
"print(plain_dict)"
]
},
{
"cell_type": "markdown",
"id": "0c910446",
"metadata": {},
"source": [
"### 6.Make an OrderedDict called fancy from the same pairs listed in 5 and
print it. Did it print in the same order as plain?"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "a0fbc75e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"plain_dict -> {'a': 1, 'b': 2, 'c': 3}\n",
"fancy -> OrderedDict([('a', 1), ('b', 2), ('c', 3)])\n"
]
}
],
"source": [
"from collections import OrderedDict\n",
"fancy = OrderedDict(plain_dict)\n",
"print(f'plain_dict -> {plain_dict}')\n",
"print(f'fancy -> {fancy}')"
]
},
{
"cell_type": "markdown",
"id": "0e500b69",
"metadata": {},
"source": [
"### 7. Make a default dictionary called dict_of_lists and pass it the argument
list. Make the list dict_of_lists['a'] and append the value 'something for a' to it
in one assignment. Print dict_of_lists['a']."
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "fde4c0a8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['something for a']\n"
]
}
],
"source": [
"from collections import defaultdict\n",
"dict_of_lists = defaultdict(list)\n",
"dict_of_lists['a'].append('something for a')\n",
"print(dict_of_lists['a'])"
]
}
],
"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.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}