# Python Arguments Workshop # What are arguments? Arguments: Files and other data provided by the user needed to run a command example: `python3 main.py http://example.com` command = `python3` arguments = `main.py, http://example.com` Resource: https://www.geeksforgeeks.org/command-line-arguments-in-python/ # Why use arguments? We use arguments and user input to make the program more dynamic compared to hard coding values directly into the code. #### Why use arguments over raw input? sys.argv vs. input('enter stuff here: ') 1. Depends on what you are writing the code for. a. quick script to automate stuff on the side ---------OR----------- b. a user interactive program https://stackoverflow.com/questions/8246822/argv-vs-raw-input # How to interact with commandline arguments in Python There are several ways to interact with arguments. We will use the 'argv' function from Python's 'sys' module https://docs.python.org/3/library/sys.html import sys arguments = sys.argv argv is a list of argument values. sys.argv[0] = first_value_in_list # it is usually the python file being ran example: python3 main.py http://example.com sys.argv[0] = main.py sys.argv[1] = http://example.com #### A small review on python lists https://www.w3schools.com/python/python_lists.asp https://www.tutorialspoint.com/python/python_lists.htm Lists are defined by square brackets, []. Items inside the list have an index number. [-1] = last item in list [-2] = second to last item in list list = [1,2,3,4,5,6] list[1:5] = starting from index 1 to index 5, but not including index 5 output = [2,3,4,5] list[:2] output = [1,2] list[-2:] output = [6,5] Example list_of_stuff = ['apple', 'orange', 'banana'] list_of_stuff[0] = apple list_of_stuff[1] = orange # Practice Problems #### 1. Given an int count of a number of donuts, return a string of the form 'Number of donuts: [count]', where [count] is the number passed in. However, if the count is 10 or more, then use the word 'many' instead of the actual count. So donuts(5) returns 'Number of donuts: 5' and donuts(23) returns 'Number of donuts: many' SAMPLE INPUT: python3 your_app.py 5 SAMPLE OUTPUT: python3 your_app.py 5 Number of donuts: 5 #### Boilerplate code to get you started #--------import sys module---------# import sys #--------create a function---------# def name_of_function(): #--------insert code here---------# #--------call your function---------# name_of_function() --- #### 2. Given a string s, return a string made of the first 2 and the last 2 chars of the original string, so 'spring' yields 'spng'. However, if the string length is 2 or less, return instead the empty string. SAMPLE INPUT: python3 your_app.py spring SAMPLE OUTPUT: python3 your_app.py spring spng --- #### 3. Given a list of strings, return the count of the number of strings where the string length is 2 or more and the first and last chars of the string are the same. Note: python does not have a ++ operator, but += works. SAMPLE INPUT: python3 your_app.py aba xyz aa x bbb SAMPLE OUTPUT: python3 your_app.py aba xyz aa x bbb 3 ---- #### 4. Given a list of strings, return a list with the strings in sorted order, except group all the strings that begin with 'x' first. e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'] Hint: this can be done by making 2 lists and sorting each of them before combining them. SAMPLE INPUT: python3 your_app.py bbb ccc axx xzz xaa SAMPLE OUTPUT: python3 your_app.py bbb ccc axx xzz xaa ['xaa', 'xzz', 'axx', 'bbb', 'ccc']