Skip to content

Commit 10e25ae

Browse files
Dictionaries and Sets
1 parent 2291c11 commit 10e25ae

File tree

1 file changed

+328
-2
lines changed

1 file changed

+328
-2
lines changed

PythonFromScratch.ipynb

Lines changed: 328 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,14 +1749,340 @@
17491749
"source": [
17501750
"tp1.count(1)"
17511751
]
1752+
},
1753+
{
1754+
"cell_type": "markdown",
1755+
"metadata": {},
1756+
"source": [
1757+
"# Dictionaries and Sets"
1758+
]
1759+
},
1760+
{
1761+
"cell_type": "code",
1762+
"execution_count": 10,
1763+
"metadata": {},
1764+
"outputs": [],
1765+
"source": [
1766+
"d1 = dict()\n",
1767+
"d2 = {}\n",
1768+
"d3 = {'nom':'Azdad','prenom':'Anas','age':22, 4:5}\n",
1769+
"d4 = {'a':1, 'b':2, 'c':3}"
1770+
]
1771+
},
1772+
{
1773+
"cell_type": "code",
1774+
"execution_count": 7,
1775+
"metadata": {},
1776+
"outputs": [
1777+
{
1778+
"data": {
1779+
"text/plain": [
1780+
"5"
1781+
]
1782+
},
1783+
"execution_count": 7,
1784+
"metadata": {},
1785+
"output_type": "execute_result"
1786+
}
1787+
],
1788+
"source": [
1789+
"d3[4]"
1790+
]
1791+
},
1792+
{
1793+
"cell_type": "code",
1794+
"execution_count": 8,
1795+
"metadata": {},
1796+
"outputs": [
1797+
{
1798+
"data": {
1799+
"text/plain": [
1800+
"4"
1801+
]
1802+
},
1803+
"execution_count": 8,
1804+
"metadata": {},
1805+
"output_type": "execute_result"
1806+
}
1807+
],
1808+
"source": [
1809+
"len(d3)"
1810+
]
1811+
},
1812+
{
1813+
"cell_type": "code",
1814+
"execution_count": 11,
1815+
"metadata": {},
1816+
"outputs": [
1817+
{
1818+
"data": {
1819+
"text/plain": [
1820+
"'a'"
1821+
]
1822+
},
1823+
"execution_count": 11,
1824+
"metadata": {},
1825+
"output_type": "execute_result"
1826+
}
1827+
],
1828+
"source": [
1829+
"min(d4)"
1830+
]
1831+
},
1832+
{
1833+
"cell_type": "code",
1834+
"execution_count": 12,
1835+
"metadata": {},
1836+
"outputs": [],
1837+
"source": [
1838+
"del(d4['a'])"
1839+
]
1840+
},
1841+
{
1842+
"cell_type": "code",
1843+
"execution_count": 13,
1844+
"metadata": {},
1845+
"outputs": [
1846+
{
1847+
"data": {
1848+
"text/plain": [
1849+
"{'b': 2, 'c': 3}"
1850+
]
1851+
},
1852+
"execution_count": 13,
1853+
"metadata": {},
1854+
"output_type": "execute_result"
1855+
}
1856+
],
1857+
"source": [
1858+
"d4"
1859+
]
1860+
},
1861+
{
1862+
"cell_type": "code",
1863+
"execution_count": 14,
1864+
"metadata": {},
1865+
"outputs": [
1866+
{
1867+
"data": {
1868+
"text/plain": [
1869+
"{}"
1870+
]
1871+
},
1872+
"execution_count": 14,
1873+
"metadata": {},
1874+
"output_type": "execute_result"
1875+
}
1876+
],
1877+
"source": [
1878+
"d4.clear()\n",
1879+
"d4"
1880+
]
1881+
},
1882+
{
1883+
"cell_type": "code",
1884+
"execution_count": 16,
1885+
"metadata": {},
1886+
"outputs": [
1887+
{
1888+
"name": "stdout",
1889+
"output_type": "stream",
1890+
"text": [
1891+
"nom Azdad\n",
1892+
"prenom Anas\n",
1893+
"age 22\n",
1894+
"4 5\n"
1895+
]
1896+
}
1897+
],
1898+
"source": [
1899+
"for indices, values in d3.items():\n",
1900+
" print(indices, ' ', values)"
1901+
]
1902+
},
1903+
{
1904+
"cell_type": "code",
1905+
"execution_count": 17,
1906+
"metadata": {},
1907+
"outputs": [
1908+
{
1909+
"name": "stdout",
1910+
"output_type": "stream",
1911+
"text": [
1912+
"nom\n",
1913+
"prenom\n",
1914+
"age\n",
1915+
"4\n"
1916+
]
1917+
}
1918+
],
1919+
"source": [
1920+
"for indices in d3.keys():\n",
1921+
" print(indices)"
1922+
]
1923+
},
1924+
{
1925+
"cell_type": "code",
1926+
"execution_count": 18,
1927+
"metadata": {},
1928+
"outputs": [
1929+
{
1930+
"name": "stdout",
1931+
"output_type": "stream",
1932+
"text": [
1933+
"Azdad\n",
1934+
"Anas\n",
1935+
"22\n",
1936+
"5\n"
1937+
]
1938+
}
1939+
],
1940+
"source": [
1941+
"for values in d3.values():\n",
1942+
" print(values)"
1943+
]
1944+
},
1945+
{
1946+
"cell_type": "code",
1947+
"execution_count": 20,
1948+
"metadata": {},
1949+
"outputs": [
1950+
{
1951+
"data": {
1952+
"text/plain": [
1953+
"5"
1954+
]
1955+
},
1956+
"execution_count": 20,
1957+
"metadata": {},
1958+
"output_type": "execute_result"
1959+
}
1960+
],
1961+
"source": [
1962+
"d3.pop(4)"
1963+
]
1964+
},
1965+
{
1966+
"cell_type": "code",
1967+
"execution_count": 27,
1968+
"metadata": {},
1969+
"outputs": [
1970+
{
1971+
"data": {
1972+
"text/plain": [
1973+
"{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f'}"
1974+
]
1975+
},
1976+
"execution_count": 27,
1977+
"metadata": {},
1978+
"output_type": "execute_result"
1979+
}
1980+
],
1981+
"source": [
1982+
"lt = ['a', 'b', 'c', 'd', 'e', 'f']\n",
1983+
"lt_dt = {i:lt[i] for i in range(len(lt))}\n",
1984+
"lt_dt"
1985+
]
1986+
},
1987+
{
1988+
"cell_type": "code",
1989+
"execution_count": 22,
1990+
"metadata": {},
1991+
"outputs": [
1992+
{
1993+
"data": {
1994+
"text/plain": [
1995+
"{1, 2, 3, 4, 5, 7, 'anas'}"
1996+
]
1997+
},
1998+
"execution_count": 22,
1999+
"metadata": {},
2000+
"output_type": "execute_result"
2001+
}
2002+
],
2003+
"source": [
2004+
"lt = ['anas', 5, 7, 2, 3, 4, 5, 'anas', 1, 1]\n",
2005+
"ens = set(lt)\n",
2006+
"ens"
2007+
]
2008+
},
2009+
{
2010+
"cell_type": "code",
2011+
"execution_count": 23,
2012+
"metadata": {},
2013+
"outputs": [
2014+
{
2015+
"ename": "TypeError",
2016+
"evalue": "'set' object is not subscriptable",
2017+
"output_type": "error",
2018+
"traceback": [
2019+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
2020+
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
2021+
"\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_30060/3208747743.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mens\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
2022+
"\u001b[1;31mTypeError\u001b[0m: 'set' object is not subscriptable"
2023+
]
2024+
}
2025+
],
2026+
"source": [
2027+
"ens[0]"
2028+
]
2029+
},
2030+
{
2031+
"cell_type": "code",
2032+
"execution_count": 25,
2033+
"metadata": {},
2034+
"outputs": [
2035+
{
2036+
"data": {
2037+
"text/plain": [
2038+
"set"
2039+
]
2040+
},
2041+
"execution_count": 25,
2042+
"metadata": {},
2043+
"output_type": "execute_result"
2044+
}
2045+
],
2046+
"source": [
2047+
"ens1 = {'anas',1, 'anas'}\n",
2048+
"type(ens1)"
2049+
]
2050+
},
2051+
{
2052+
"cell_type": "code",
2053+
"execution_count": 26,
2054+
"metadata": {},
2055+
"outputs": [
2056+
{
2057+
"data": {
2058+
"text/plain": [
2059+
"{1, 'anas'}"
2060+
]
2061+
},
2062+
"execution_count": 26,
2063+
"metadata": {},
2064+
"output_type": "execute_result"
2065+
}
2066+
],
2067+
"source": [
2068+
"ens1"
2069+
]
2070+
},
2071+
{
2072+
"cell_type": "code",
2073+
"execution_count": null,
2074+
"metadata": {},
2075+
"outputs": [],
2076+
"source": []
17522077
}
17532078
],
17542079
"metadata": {
17552080
"interpreter": {
1756-
"hash": "0bd0263b2349850895389e1032d04cbc5bd2ce95c132b2480249abf6cf466d9f"
2081+
"hash": "ce2665c828cadf88826a7c4b72219d2463d2835c9122540076c4da858c580ad1"
17572082
},
17582083
"kernelspec": {
1759-
"display_name": "Python 3.8.8 64-bit ('base': conda)",
2084+
"display_name": "Python 3.9.7 ('base')",
2085+
"language": "python",
17602086
"name": "python3"
17612087
},
17622088
"language_info": {

0 commit comments

Comments
 (0)