Write a Python program that adds the numbers passed as arguments.
Output
19.5
Approach
Convert each argument with float().
Solution
Python
importsubprocessimportsyswithopen("add.py","w")asf:f.write("import sys\nprint(sum(float(a) for a in sys.argv[1:]))\n")out=subprocess.run([sys.executable,"add.py","10","2.5","7"],capture_output=True,text=True)print(out.stdout.strip())
Output
19.5
Learn the concept
New to this topic? Read the lesson, then come back.
20 programs for the collections module: Counter for counting, defaultdict for grouping, deque for queues and sliding windows, namedtuple for records, OrderedDict for an LRU cache and ChainMap for layered settings.
Before you start
Read the Collections lesson first if the topic is new to you.