Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2f8e693

Browse files
committedMay 22, 2022
Mutable and Immutable types
1 parent 10e25ae commit 2f8e693

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed
 

‎PythonFromScratch.ipynb

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,6 +2068,175 @@
20682068
"ens1"
20692069
]
20702070
},
2071+
{
2072+
"cell_type": "markdown",
2073+
"metadata": {},
2074+
"source": [
2075+
"# Mutable and Immutable Objects"
2076+
]
2077+
},
2078+
{
2079+
"cell_type": "markdown",
2080+
"metadata": {},
2081+
"source": [
2082+
"**Mutable Types :** Lists, dictionaries, and sets.\n",
2083+
"\n",
2084+
"**Immutable Types :** Int, Float, String, Boolean, Lists."
2085+
]
2086+
},
2087+
{
2088+
"cell_type": "code",
2089+
"execution_count": 7,
2090+
"metadata": {},
2091+
"outputs": [
2092+
{
2093+
"name": "stdout",
2094+
"output_type": "stream",
2095+
"text": [
2096+
"2596621161152\n",
2097+
"2596510527088\n"
2098+
]
2099+
}
2100+
],
2101+
"source": [
2102+
"lt = ['a', 'b', 'c', 'd', 'e', 'f']\n",
2103+
"a = 43\n",
2104+
"print(id(lt))\n",
2105+
"print(id(a))"
2106+
]
2107+
},
2108+
{
2109+
"cell_type": "code",
2110+
"execution_count": 2,
2111+
"metadata": {},
2112+
"outputs": [
2113+
{
2114+
"name": "stdout",
2115+
"output_type": "stream",
2116+
"text": [
2117+
"2596591758016\n",
2118+
"2596510527312\n"
2119+
]
2120+
}
2121+
],
2122+
"source": [
2123+
"lt += 'g'\n",
2124+
"a += 7\n",
2125+
"print(id(lt))\n",
2126+
"print(id(a))"
2127+
]
2128+
},
2129+
{
2130+
"cell_type": "code",
2131+
"execution_count": 8,
2132+
"metadata": {},
2133+
"outputs": [
2134+
{
2135+
"name": "stdout",
2136+
"output_type": "stream",
2137+
"text": [
2138+
"2596621161152\n",
2139+
"2596510527088\n"
2140+
]
2141+
}
2142+
],
2143+
"source": [
2144+
"lt2 = lt\n",
2145+
"b = a\n",
2146+
"print(id(lt2))\n",
2147+
"print(id(b))"
2148+
]
2149+
},
2150+
{
2151+
"cell_type": "code",
2152+
"execution_count": 4,
2153+
"metadata": {},
2154+
"outputs": [
2155+
{
2156+
"name": "stdout",
2157+
"output_type": "stream",
2158+
"text": [
2159+
"True\n"
2160+
]
2161+
}
2162+
],
2163+
"source": [
2164+
"print(lt2 is lt)"
2165+
]
2166+
},
2167+
{
2168+
"cell_type": "code",
2169+
"execution_count": 9,
2170+
"metadata": {},
2171+
"outputs": [
2172+
{
2173+
"name": "stdout",
2174+
"output_type": "stream",
2175+
"text": [
2176+
"43\n"
2177+
]
2178+
}
2179+
],
2180+
"source": [
2181+
"b += 12\n",
2182+
"print(a)"
2183+
]
2184+
},
2185+
{
2186+
"cell_type": "code",
2187+
"execution_count": 11,
2188+
"metadata": {},
2189+
"outputs": [
2190+
{
2191+
"name": "stdout",
2192+
"output_type": "stream",
2193+
"text": [
2194+
"['a', 'b', 'c', 'd', 'e', 'f', 'h', 'h']\n",
2195+
"2596621161152\n"
2196+
]
2197+
}
2198+
],
2199+
"source": [
2200+
"lt2.append('h')\n",
2201+
"print(lt)\n",
2202+
"print(id(lt2))"
2203+
]
2204+
},
2205+
{
2206+
"cell_type": "code",
2207+
"execution_count": 12,
2208+
"metadata": {},
2209+
"outputs": [],
2210+
"source": [
2211+
"tp = ('hello', 12, lt)"
2212+
]
2213+
},
2214+
{
2215+
"cell_type": "code",
2216+
"execution_count": 13,
2217+
"metadata": {},
2218+
"outputs": [],
2219+
"source": [
2220+
"lt.append('i')"
2221+
]
2222+
},
2223+
{
2224+
"cell_type": "code",
2225+
"execution_count": 14,
2226+
"metadata": {},
2227+
"outputs": [
2228+
{
2229+
"name": "stdout",
2230+
"output_type": "stream",
2231+
"text": [
2232+
"('hello', 12, ['a', 'b', 'c', 'd', 'e', 'f', 'h', 'h', 'i'])\n"
2233+
]
2234+
}
2235+
],
2236+
"source": [
2237+
"print(tp)"
2238+
]
2239+
},
20712240
{
20722241
"cell_type": "code",
20732242
"execution_count": null,

0 commit comments

Comments
 (0)
Please sign in to comment.