<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ko">
	<id>https://devcafe.co.kr/w/index.php?action=history&amp;feed=atom&amp;title=Python_Cheat_Sheet</id>
	<title>Python Cheat Sheet - 편집 역사</title>
	<link rel="self" type="application/atom+xml" href="https://devcafe.co.kr/w/index.php?action=history&amp;feed=atom&amp;title=Python_Cheat_Sheet"/>
	<link rel="alternate" type="text/html" href="https://devcafe.co.kr/w/index.php?title=Python_Cheat_Sheet&amp;action=history"/>
	<updated>2026-05-17T10:24:22Z</updated>
	<subtitle>이 문서의 편집 역사</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://devcafe.co.kr/w/index.php?title=Python_Cheat_Sheet&amp;diff=26&amp;oldid=prev</id>
		<title>Devcafe: 새 문서: = Python Cheat Sheet =  Support/Big Data/Document/My Docs/Python/Python_CheatSheet.MD  == Installation/Config ==  === PIP ===  &lt;source lang=bash&gt;  -- list all installed packages pip list  -- list only local installed packages in a virtual env pip list --local  -- search a package pip list|grep &lt;packagename&gt;  -- show the package location pip show &lt;packagename&gt; &lt;/source&gt;  === location of the globally installed packages === &lt;source lang=bash&gt; python -m site &lt;/source&gt;  &lt;source lang=...</title>
		<link rel="alternate" type="text/html" href="https://devcafe.co.kr/w/index.php?title=Python_Cheat_Sheet&amp;diff=26&amp;oldid=prev"/>
		<updated>2024-10-04T15:08:12Z</updated>

		<summary type="html">&lt;p&gt;새 문서: = Python Cheat Sheet =  Support/Big Data/Document/My Docs/Python/Python_CheatSheet.MD  == Installation/Config ==  === PIP ===  &amp;lt;source lang=bash&amp;gt;  -- list all installed packages pip list  -- list only local installed packages in a virtual env pip list --local  -- search a package pip list|grep &amp;lt;packagename&amp;gt;  -- show the package location pip show &amp;lt;packagename&amp;gt; &amp;lt;/source&amp;gt;  === location of the globally installed packages === &amp;lt;source lang=bash&amp;gt; python -m site &amp;lt;/source&amp;gt;  &amp;lt;source lang=...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;새 문서&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Python Cheat Sheet =&lt;br /&gt;
&lt;br /&gt;
Support/Big Data/Document/My Docs/Python/Python_CheatSheet.MD&lt;br /&gt;
&lt;br /&gt;
== Installation/Config ==&lt;br /&gt;
&lt;br /&gt;
=== PIP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=bash&amp;gt;&lt;br /&gt;
&lt;br /&gt;
-- list all installed packages&lt;br /&gt;
pip list&lt;br /&gt;
&lt;br /&gt;
-- list only local installed packages in a virtual env&lt;br /&gt;
pip list --local&lt;br /&gt;
&lt;br /&gt;
-- search a package&lt;br /&gt;
pip list|grep &amp;lt;packagename&amp;gt;&lt;br /&gt;
&lt;br /&gt;
-- show the package location&lt;br /&gt;
pip show &amp;lt;packagename&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== location of the globally installed packages ===&lt;br /&gt;
&amp;lt;source lang=bash&amp;gt;&lt;br /&gt;
python -m site&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import site&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; print(site.getsitepackages())&amp;#039;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import sys&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; sys.path&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== location of the locally installed packages ===&lt;br /&gt;
&amp;lt;source lang=sql&amp;gt;&lt;br /&gt;
pythom -m site --user-site&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Virtual Env (Python 3.3+) ===&lt;br /&gt;
&lt;br /&gt;
* venv is a package shipped with Python 3, which you can run using python3 -m venv. &lt;br /&gt;
* It serves the same purpose as virtualenv, but only has a subset of its features. &lt;br /&gt;
* virtualenv continues to be more popular than venv, especially since the former supports both Python 2 and 3.&lt;br /&gt;
&lt;br /&gt;
[Difference between virtualenv and venv](https://virtualenv.pypa.io/en/latest/)&lt;br /&gt;
&lt;br /&gt;
==== Install virtualenv ====&lt;br /&gt;
&amp;lt;source lang=bash&amp;gt;&lt;br /&gt;
pip install virtualenv&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== upgrade virtualenv ====&lt;br /&gt;
&amp;lt;source lang=bash&amp;gt;&lt;br /&gt;
pip install --upgrade virtualenv&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== using virtualenv to create a virtual env for a particular python version ====&lt;br /&gt;
&amp;lt;source lang=sql&amp;gt;&lt;br /&gt;
virtualenv -p /usr/local/bin/python3.5 kevin-venv&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Create a virtual env ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=sql&amp;gt;&lt;br /&gt;
python -m venv &amp;lt;venv-name&amp;gt;&lt;br /&gt;
&lt;br /&gt;
python -m venv kevin-venv&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== include global python packages ====&lt;br /&gt;
&amp;lt;source lang=sql&amp;gt;&lt;br /&gt;
python -m venv kevin-venv --system-site-packages&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== using --local to list only local installed packages ===&lt;br /&gt;
&amp;lt;source lang=sql&amp;gt;&lt;br /&gt;
pip list --local&lt;br /&gt;
&lt;br /&gt;
-- typically, you create a folder for your project first;&lt;br /&gt;
-- then create a virtual env within/insider of that project folder&lt;br /&gt;
&amp;gt;1. mkdir kevin_python_proj1&lt;br /&gt;
-- 2a and 2b is same; pick one you like&lt;br /&gt;
&amp;gt;2a. python -m venv kevin_python_proj1/.venv&lt;br /&gt;
&amp;gt;2b. cd mkdir kevin_python_proj1 &amp;amp;&amp;amp; python -m venv .venv&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Activate/Deactivate the vtirual env ===&lt;br /&gt;
==== activate ====&lt;br /&gt;
&amp;lt;source lang=sql&amp;gt;&lt;br /&gt;
source &amp;lt;venv-name&amp;gt;/bin/activate&lt;br /&gt;
source kevin-venv/bin/activate&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== deactivate ====&lt;br /&gt;
&amp;lt;source lang=sql&amp;gt;&lt;br /&gt;
deactivate&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Delete a virtualenv ===&lt;br /&gt;
&amp;lt;source lang=bash&amp;gt;&lt;br /&gt;
rm -rf &amp;lt;venv-name&amp;gt;&lt;br /&gt;
rm -rf kevin-venv&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Store copy of python package versions ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=bash&amp;gt;&lt;br /&gt;
-- all packages&lt;br /&gt;
pip freeze &amp;gt; requirements.txt&lt;br /&gt;
&lt;br /&gt;
-- only local installed packages&lt;br /&gt;
pip freeze --local &amp;gt; requirements.txt&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Install all packages from requirements.txt ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=sql&amp;gt;&lt;br /&gt;
pip install -r requriments.txt&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Tips and Tricks ==&lt;br /&gt;
&lt;br /&gt;
=== Date, Time, DateTime, Timezone ===&lt;br /&gt;
&lt;br /&gt;
[See this](https://docs.python.org/3/library/datetime.html#datetime.datetime.now)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
&lt;br /&gt;
from datetime import datetime, date, time, timezone, timedelta&lt;br /&gt;
&lt;br /&gt;
current_time = datetime.now(timezone.utc)&lt;br /&gt;
print(&amp;quot;The current time with UTC is: &amp;quot;, current_time.strftime(&amp;#039;%Y-%m-%d %H:%M:%S %Z&amp;#039;))&lt;br /&gt;
#The current time with UTC is:  2022-02-15 21:50:52 UT ==&amp;gt; when I run this from laptop, it is 3:50:52 PM,&lt;br /&gt;
# this mean Chicago is UTC-6&lt;br /&gt;
&lt;br /&gt;
yesterday = date.today() - timedelta(days=1)&lt;br /&gt;
dt = yesterday.strftime(&amp;quot;%Y-%m-%d&amp;quot;)&lt;br /&gt;
print(&amp;quot;Yesterday is: &amp;quot;, dt)&lt;br /&gt;
# Yesterday is:  2022-02-14&lt;br /&gt;
&lt;br /&gt;
# local time&lt;br /&gt;
print(&amp;quot;The local time is: &amp;quot;, time.strftime(&amp;quot;%Y-%m-%d %H:%M:%S&amp;quot;, time.localtime()));&lt;br /&gt;
# The local time is:  2022-02-15 15:50:03&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== switch value ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a = 10&lt;br /&gt;
b = 30&lt;br /&gt;
&lt;br /&gt;
# switch value, now a = 30, b = 10&lt;br /&gt;
a,b = b,a&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Merging Dictionaries (Python 3.5+) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=sql&amp;gt;&lt;br /&gt;
&lt;br /&gt;
x = {&amp;#039;a&amp;#039;: 1, &amp;#039;b&amp;#039;: 2}&lt;br /&gt;
y = {&amp;#039;c&amp;#039;, 3, &amp;#039;d&amp;#039;: 4}&lt;br /&gt;
&lt;br /&gt;
# merged = {&amp;#039;a&amp;#039;: 1, &amp;#039;b&amp;#039;: 2, &amp;#039;c&amp;#039;: 3, &amp;#039;d&amp;#039;: 4}&lt;br /&gt;
merged = {**x, **y}&lt;br /&gt;
&lt;br /&gt;
# if have same key, then right one win&lt;br /&gt;
x = {&amp;#039;a&amp;#039;: 1, &amp;#039;b&amp;#039;: 2}&lt;br /&gt;
y = {&amp;#039;a&amp;#039;, 3, &amp;#039;c&amp;#039;: 4}&lt;br /&gt;
&lt;br /&gt;
# merged = {&amp;#039;a&amp;#039;: 3, &amp;#039;b&amp;#039;: 2, &amp;#039;c&amp;#039;: 4}&lt;br /&gt;
merged = {**x, **y}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Check if a module contain a class/method ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Using a list comprehension&lt;br /&gt;
import os&lt;br /&gt;
[name for name in dir(os) if &amp;#039;uname&amp;#039; in name.lower()] # return [&amp;#039;uname&amp;#039;, &amp;#039;uname_result&amp;#039;]&lt;br /&gt;
&lt;br /&gt;
# After you find the class, you can use built-in help() to see the detail&lt;br /&gt;
help(os.uname)&lt;br /&gt;
&lt;br /&gt;
# another example&lt;br /&gt;
import collections&lt;br /&gt;
[name for name in dir(collections) if &amp;#039;tuple&amp;#039; in name.lower()] # return [&amp;#039;_tuplegetter&amp;#039;, &amp;#039;namedtuple&amp;#039;]&lt;br /&gt;
&lt;br /&gt;
help(collections.namedtuple)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
## Comprehensions&lt;br /&gt;
&lt;br /&gt;
=== List Comprehensions ===&lt;br /&gt;
&lt;br /&gt;
[expression for item in iterable]&lt;br /&gt;
[expression for item in iterable if condition]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
squares = [x * x for x in range(10)]&lt;br /&gt;
&lt;br /&gt;
even_squares = [x * x for x in range(10) if x % 2 == 0]&lt;br /&gt;
&lt;br /&gt;
# multiple loops&lt;br /&gt;
x_dimension = [&amp;#039;a&amp;#039;, &amp;#039;b&amp;#039;]&lt;br /&gt;
y_dimension = [1, 2, 3]&lt;br /&gt;
matrix = [(x,y) for x in x_dimension for y in y_dimension]&lt;br /&gt;
#output: [(&amp;#039;a&amp;#039;, 1), (&amp;#039;a&amp;#039;, 2), (&amp;#039;a&amp;#039;, 3), (&amp;#039;b&amp;#039;, 1), (&amp;#039;b&amp;#039;, 2), (&amp;#039;b&amp;#039;, 3)]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Tuple Comprehensions ===&lt;br /&gt;
&lt;br /&gt;
there is no such thing. there is no tuple comprehension&lt;br /&gt;
&lt;br /&gt;
=== Dictionary Comprehensions ===&lt;br /&gt;
&lt;br /&gt;
{key_expression: value_expression for expression in iterable}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
word = &amp;quot;dictionary&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Method A:&lt;br /&gt;
letter_count1 = {letter: word.count(letter) for letter in word}&lt;br /&gt;
&lt;br /&gt;
# Method B: better performance using set() to remove duplicated letter from word first&lt;br /&gt;
letter_count2 = {letter: word.count(letter) for letter in set(word)}&lt;br /&gt;
&lt;br /&gt;
# with condition&lt;br /&gt;
old_salary = {&amp;#039;Kevin&amp;#039;: 2000, &amp;#039;Brandon&amp;#039;: 400, &amp;#039;Elena&amp;#039;: 300}&lt;br /&gt;
new_salary = {k: v*1.5 if v &amp;lt; 500 else v for (k, v) in old_salary.items()}&lt;br /&gt;
#output: new_salary = {&amp;#039;Kevin&amp;#039;: 2000, &amp;#039;Brandon&amp;#039;: 600.0, &amp;#039;Elena&amp;#039;: 450.0}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Set Comprehensions ===&lt;br /&gt;
&lt;br /&gt;
{expression for item in iterable}&lt;br /&gt;
{expression for item in iterable if condition}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
random_nums = [3, 6, 9, 3, 9, 18, 20, 18]&lt;br /&gt;
&lt;br /&gt;
great_tens = {num for num in random_nums if num &amp;gt; 10}&lt;br /&gt;
&lt;br /&gt;
#output: {18, 20}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generator Comprehensions ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
from pprint import pprint&lt;br /&gt;
some_nums = (num for num in range(5))&lt;br /&gt;
pprint(some_nums)&lt;br /&gt;
#output: &amp;lt;generator object &amp;lt;genexpr&amp;gt; at 0x00000264B87BCF90&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
## Functional Programming&lt;br /&gt;
&lt;br /&gt;
=== Named Tuple ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
import collections&lt;br /&gt;
from pprint import pprint&lt;br /&gt;
&lt;br /&gt;
Cruise = collections.namedtuple(&lt;br /&gt;
    &amp;quot;Cruise&amp;quot;,&lt;br /&gt;
    [&lt;br /&gt;
        &amp;quot;year&amp;quot;,&lt;br /&gt;
        &amp;quot;cruise_line&amp;quot;,&lt;br /&gt;
        &amp;quot;ship&amp;quot;,&lt;br /&gt;
        &amp;quot;days&amp;quot;,&lt;br /&gt;
    ],&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
my_cruise = (&lt;br /&gt;
    Cruise(year=2016, cruise_line=&amp;quot;RCL&amp;quot;, ship=&amp;quot;Independency of Seas&amp;quot;, days=5),&lt;br /&gt;
    Cruise(year=2017, cruise_line=&amp;quot;NCL&amp;quot;, ship=&amp;quot;Escape&amp;quot;, days=7),&lt;br /&gt;
    Cruise(year=2018, cruise_line=&amp;quot;NCL&amp;quot;, ship=&amp;quot;Bliss&amp;quot;, days=7),&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
pprint(my_cruise)&lt;br /&gt;
&lt;br /&gt;
# output&lt;br /&gt;
((Cruise(year=2016, cruise_line=&amp;#039;RCL&amp;#039;, ship=&amp;#039;Independency of Seas&amp;#039;, days=5),&lt;br /&gt;
 Cruise(year=2017, cruise_line=&amp;#039;NCL&amp;#039;, ship=&amp;#039;Escape&amp;#039;, days=7),&lt;br /&gt;
 Cruise(year=2018, cruise_line=&amp;#039;NCL&amp;#039;, ship=&amp;#039;Bliss&amp;#039;, days=7))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Filter() ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
# Method A: print only RCL cruise: using filter&lt;br /&gt;
rcl_cruise = tuple(&lt;br /&gt;
    filter(lambda cruise: cruise.cruise_line.upper() == &amp;quot;RCL&amp;quot;, my_cruise)&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
pprint(rcl_cruise)&lt;br /&gt;
#output&lt;br /&gt;
(Cruise(year=2016, cruise_line=&amp;#039;RCL&amp;#039;, ship=&amp;#039;Independency of Seas&amp;#039;, days=5),)&lt;br /&gt;
&lt;br /&gt;
# Method B: print only RCL cruise: using iterator&lt;br /&gt;
rcl_cruise2 = tuple(&lt;br /&gt;
    cruise for cruise in my_cruise if cruise.cruise_line.upper() == &amp;quot;RCL&amp;quot;&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
pprint(rcl_cruise2)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Map Function ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
&lt;br /&gt;
## Method A: using map function&lt;br /&gt;
cruise_history = tuple(&lt;br /&gt;
    map(&lt;br /&gt;
        lambda cruise: {&lt;br /&gt;
            &amp;quot;cruise_line&amp;quot;: cruise.cruise_line,&lt;br /&gt;
            &amp;quot;name&amp;quot;: cruise.ship,&lt;br /&gt;
            &amp;quot;past_year&amp;quot;: 2022 - cruise.year,&lt;br /&gt;
        },&lt;br /&gt;
        my_cruise,&lt;br /&gt;
    )&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
pprint(cruise_history)&lt;br /&gt;
&lt;br /&gt;
#({&amp;#039;cruise_line&amp;#039;: &amp;#039;RCL&amp;#039;, &amp;#039;name&amp;#039;: &amp;#039;Independency of Seas&amp;#039;, &amp;#039;past_year&amp;#039;: 6},&lt;br /&gt;
# {&amp;#039;cruise_line&amp;#039;: &amp;#039;NCL&amp;#039;, &amp;#039;name&amp;#039;: &amp;#039;Escape&amp;#039;, &amp;#039;past_year&amp;#039;: 5},&lt;br /&gt;
# {&amp;#039;cruise_line&amp;#039;: &amp;#039;NCL&amp;#039;, &amp;#039;name&amp;#039;: &amp;#039;Bliss&amp;#039;, &amp;#039;past_year&amp;#039;: 4})&lt;br /&gt;
&lt;br /&gt;
# Method B: using generator comprehension and convert it to tuple&lt;br /&gt;
cruise_history2 = tuple(&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;cruise_line&amp;quot;: cruise.cruise_line,&lt;br /&gt;
        &amp;quot;name&amp;quot;: cruise.ship,&lt;br /&gt;
        &amp;quot;past_year&amp;quot;: 2022 - cruise.year,&lt;br /&gt;
    }&lt;br /&gt;
    for cruise in my_cruise&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
# Another great example: track for each cruise line, which year I took it&lt;br /&gt;
&lt;br /&gt;
# Method A: hard-coded the cruise line dictionary object as initial value for reduce() function&lt;br /&gt;
&lt;br /&gt;
def reducer(acc, val):&lt;br /&gt;
    acc[val.cruise_line].append(val.year)&lt;br /&gt;
    return acc&lt;br /&gt;
&lt;br /&gt;
cruise_line_year_history = reduce(reducer, my_cruise, {&amp;quot;RCL&amp;quot;: [], &amp;quot;NCL&amp;quot;: [], &amp;quot;DISNEY&amp;quot;: []})&lt;br /&gt;
&lt;br /&gt;
pprint(cruise_line_year_history)&lt;br /&gt;
# {&amp;#039;DISNEY&amp;#039;: [], &amp;#039;NCL&amp;#039;: [2017, 2018], &amp;#039;RCL&amp;#039;: [2016]}&lt;br /&gt;
&lt;br /&gt;
# Method B: improve on top of Method A using collections.defaultdict&lt;br /&gt;
import collections&lt;br /&gt;
&lt;br /&gt;
cruise_line_year_history2 = reduce(reducer, my_cruise, collections.defaultdict(list))&lt;br /&gt;
&lt;br /&gt;
pprint(cruise_line_year_history2)&lt;br /&gt;
# defaultdict(&amp;lt;class &amp;#039;list&amp;#039;&amp;gt;, {&amp;#039;RCL&amp;#039;: [2016], &amp;#039;NCL&amp;#039;: [2017, 2018]})&lt;br /&gt;
# please notice result doesn&amp;#039;t have &amp;#039;DISNEY&amp;#039; in it&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Reduce Function ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
# Method A: using reduce() function&lt;br /&gt;
from functools import reduce&lt;br /&gt;
total_cruise_days = reduce(lambda acc, cruise: acc + cruise.days, my_cruise, 0)&lt;br /&gt;
&lt;br /&gt;
pprint(total_cruise_days)&lt;br /&gt;
#output: 19&lt;br /&gt;
&lt;br /&gt;
# Method B: using sum() function instead of reduce()&lt;br /&gt;
total_cruise_days2 = sum(cruise.days for cruise in my_cruise)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Parallel Processing ===&lt;br /&gt;
&lt;br /&gt;
Please watch below youtube by Dan Bader&lt;br /&gt;
&lt;br /&gt;
- [multiprocessing](https://www.youtube.com/watch?v=aysceqdGFw8&amp;amp;list=PLP8GkvaIxJP1z5bu4NX_bFrEInBkAgTMr)&lt;br /&gt;
&lt;br /&gt;
- [concurrent.futures](https://www.youtube.com/watch?v=0NNV8FDuck8&amp;amp;list=PLP8GkvaIxJP1z5bu4NX_bFrEInBkAgTMr)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Method A&lt;br /&gt;
import multiprocessing&lt;br /&gt;
&lt;br /&gt;
# Method B&lt;br /&gt;
import concurrent.futures&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
## Class &amp;amp; Object&lt;br /&gt;
&lt;br /&gt;
=== Dynamic property set and get: setattr()/getattr() ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
class Cruise():&lt;br /&gt;
    pass&lt;br /&gt;
&lt;br /&gt;
cruise_2019 = Cruise()&lt;br /&gt;
&lt;br /&gt;
first_key = &amp;#039;cruise_line&amp;#039;&lt;br /&gt;
first_val = &amp;#039;RCL Symphony&amp;#039;&lt;br /&gt;
&lt;br /&gt;
second_key = &amp;#039;cruise_days&amp;#039;&lt;br /&gt;
second_val = 7&lt;br /&gt;
&lt;br /&gt;
setattr(cruise_2019, first_key, first_val)&lt;br /&gt;
setattr(cruise_2019, second_key, second_val)&lt;br /&gt;
&lt;br /&gt;
print(cruise_2019.cruise_line, cruise_2019.cruise_days)&lt;br /&gt;
# RCL Symphony 7&lt;br /&gt;
&lt;br /&gt;
cruise_2019_days = getattr(cruise_2019, second_key)&lt;br /&gt;
print(cruise_2019_days)&lt;br /&gt;
# 7&lt;br /&gt;
&lt;br /&gt;
# convert dictionary to class object&lt;br /&gt;
cruise_2018_dict = {&amp;#039;cruise_line&amp;#039;: &amp;#039;NCL Bliss&amp;#039;, &amp;#039;cruise_days&amp;#039;: 7}&lt;br /&gt;
cruise_2018 = Cruise()&lt;br /&gt;
&lt;br /&gt;
for k, v in cruise_2018_dict.items():&lt;br /&gt;
    setattr(cruise_2018, k, v)&lt;br /&gt;
&lt;br /&gt;
for k in cruise_2018_dict.keys():&lt;br /&gt;
    print(getattr(cruise_2018, k))&lt;br /&gt;
&lt;br /&gt;
# NCL Bliss&lt;br /&gt;
# 7&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
## List&lt;br /&gt;
&lt;br /&gt;
=== Looping List with both index and item: enumerate ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
cruise_hist = [&amp;#039;NCL Bliss&amp;#039;, &amp;#039;NCL Escape&amp;#039;, &amp;#039;Disney Dreamer&amp;#039;]&lt;br /&gt;
&lt;br /&gt;
for idx, cruise in enumerate(cruise_hist):&lt;br /&gt;
    print(f&amp;quot;{idx}: {cruise}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Test&lt;br /&gt;
0: NCL Bliss&lt;br /&gt;
1: NCL Escape&lt;br /&gt;
2: Disney Dreamer&lt;br /&gt;
&lt;br /&gt;
# using start=1&lt;br /&gt;
cruise_hist = [&amp;#039;NCL Bliss&amp;#039;, &amp;#039;NCL Escape&amp;#039;, &amp;#039;Disney Dreamer&amp;#039;]&lt;br /&gt;
&lt;br /&gt;
for idx, cruise in enumerate(cruise_hist, start=1):&lt;br /&gt;
    print(f&amp;quot;{idx}: {cruise}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Test&lt;br /&gt;
1: NCL Bliss&lt;br /&gt;
2: NCL Escape&lt;br /&gt;
3: Disney Dreamer&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Loop multiple lists using zip() ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
cruise_lines = [&amp;#039;NCL Bliss&amp;#039;, &amp;#039;RCL Independency&amp;#039;, &amp;#039;Disney Dreamer&amp;#039;]&lt;br /&gt;
cruise_days = [7, 5,  4]&lt;br /&gt;
cruise_years = [2018, 2016, 2015]&lt;br /&gt;
&lt;br /&gt;
for cruise_line, cruise_day, cruise_year in zip(cruise_lines, cruise_days, cruise_years):&lt;br /&gt;
    print(&lt;br /&gt;
        f&amp;quot;I cruised with {cruise_line} in year {cruise_year} for {cruise_day} days&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Test&lt;br /&gt;
I cruised with NCL Bliss in year 2018 for 7 days&lt;br /&gt;
I cruised with RCL Independency in year 2016 for 5 days&lt;br /&gt;
I cruised with Disney Dreamer in year 2015 for 4 days&lt;br /&gt;
&lt;br /&gt;
# cruise_tuple is a tuple in below&lt;br /&gt;
for cruise_tuple in zip(cruise_lines, cruise_days, cruise_years):&lt;br /&gt;
    print(f&amp;quot;I cruised with {cruise_tuple} &amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Test&lt;br /&gt;
I cruised with (&amp;#039;NCL Bliss&amp;#039;, 7, 2018)&lt;br /&gt;
I cruised with (&amp;#039;RCL Independency&amp;#039;, 5, 2016)&lt;br /&gt;
I cruised with (&amp;#039;Disney Dreamer&amp;#039;, 4, 2015)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
## File Operation&lt;br /&gt;
&lt;br /&gt;
=== Using Content Manager ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
with open(&amp;#039;test.txt&amp;#039;, &amp;#039;rd&amp;#039;) as f:&lt;br /&gt;
    file_contents = f.read()&lt;br /&gt;
&lt;br /&gt;
words = file_contents.split(&amp;#039; &amp;#039;)&lt;br /&gt;
word_count = len(words)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
## Misc&lt;br /&gt;
&lt;br /&gt;
=== Get Help ===&lt;br /&gt;
&lt;br /&gt;
- In Python Interpreter, type &amp;#039;help(dir)&amp;#039;&lt;br /&gt;
- In Python Interpreter, type build-in &amp;#039;dir(xyz)&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Make sure you import the module(xyz) you want to investigate before you&lt;br /&gt;
run help(xyz) or dir(xyz)&lt;br /&gt;
&lt;br /&gt;
=== Input secret information (e.g. password) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
from getpass import getpass&lt;br /&gt;
&lt;br /&gt;
username = input(&amp;#039;Username: &amp;#039;)&lt;br /&gt;
password = getpass(&amp;#039;Password: &amp;#039;)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Unpacking ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
cruise_2018 = (&amp;#039;NCL Bliss&amp;#039;, 7)&lt;br /&gt;
&lt;br /&gt;
# using _ as a variable is a convention in Python&lt;br /&gt;
# to indicate you are not use that variable&lt;br /&gt;
_, days_cruised_2018 = cruise_2018&lt;br /&gt;
&lt;br /&gt;
print(days_cruised_2018) # 7&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
cruise_years = [2008, 2015, 2016, 2017, 2018, 2019]&lt;br /&gt;
&lt;br /&gt;
first_cruise_yr, second_cruise_yr, *rest_cruise_yrs = cruise_years&lt;br /&gt;
&lt;br /&gt;
print(f&amp;quot;my very 1st cruise is in year {first_cruise_yr}&amp;quot;)&lt;br /&gt;
print(f&amp;quot;my 2nd cruise is in year {second_cruise_yr}&amp;quot;)&lt;br /&gt;
print(f&amp;quot;my rest of cruise years are {rest_cruise_yrs}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Test&lt;br /&gt;
my very 1st cruise is in year 2008&lt;br /&gt;
my 2nd cruise is in year 2015&lt;br /&gt;
my rest of cruise years are [2016, 2017, 2018, 2019]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
cruise_years = [2008, 2015, 2016, 2017, 2018, 2019]&lt;br /&gt;
&lt;br /&gt;
first_cruise_yr, *rest_cruise_yrs, latest_cruise_yr = cruise_years&lt;br /&gt;
&lt;br /&gt;
print(f&amp;quot;my very 1st cruise is in year {first_cruise_yr}&amp;quot;)&lt;br /&gt;
print(f&amp;quot;my latest cruise year is ==&amp;gt;  {latest_cruise_yr}&amp;quot;)&lt;br /&gt;
print(f&amp;quot;my rest of cruise years are {rest_cruise_yrs}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Test&lt;br /&gt;
my very 1st cruise is in year 2008&lt;br /&gt;
my latest cruise year is ==&amp;gt;  2019&lt;br /&gt;
my rest of cruise years are [2015, 2016, 2017, 2018]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ternary Operator ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
&lt;br /&gt;
cruise_2018 = &amp;#039;NCL&amp;#039;&lt;br /&gt;
cruise_2019 = &amp;#039;RCL&amp;#039;&lt;br /&gt;
&lt;br /&gt;
favorite_flag = &amp;#039;Y&amp;#039; if cruise_2019 == my_favorite_cruise else &amp;#039;N&amp;#039;&lt;br /&gt;
print(f&amp;quot;my 2019 cruise is my favorite cruise? {favorite_flag}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
favorite_flag = &amp;#039;Y&amp;#039; if cruise_2018 == my_favorite_cruise else &amp;#039;N&amp;#039;&lt;br /&gt;
print(f&amp;quot;my 2018 cruise is my favorite cruise? {favorite_flag}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Test&lt;br /&gt;
my 2019 cruise is my favorite cruise? Y&lt;br /&gt;
my 2018 cruise is my favorite cruise? N&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Large Number format ===&lt;br /&gt;
&lt;br /&gt;
- Using underscore (\_) in declaration&lt;br /&gt;
- Using &amp;#039;:,&amp;quot; to set group seperator to &amp;#039;,&amp;#039; in f string&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=python&amp;gt;&lt;br /&gt;
china_population = 1_300_000_000_000&lt;br /&gt;
usa_population = 330_000_000_000&lt;br /&gt;
&lt;br /&gt;
print(f&amp;quot;China population = {china_population:,}&amp;quot;)&lt;br /&gt;
print(f&amp;quot;USA {usa_population:,}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Test&lt;br /&gt;
China population = 1,300,000,000,000&lt;br /&gt;
USA 330,000,000,000&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
[[category:python]]&lt;/div&gt;</summary>
		<author><name>Devcafe</name></author>
	</entry>
</feed>