From 8a359457455b1224b7165c1328409a5648914607 Mon Sep 17 00:00:00 2001 From: bgroveben <bgroveben@gmail.com> Date: Wed, 5 Jul 2017 01:17:30 -0700 Subject: [PATCH 1/3] Add lessons and challenges for Functions chapter --- challenges/8.1.Built_In_Functions/lesson.md | 23 +++++++++++ .../lesson_settings.json | 7 ++++ .../8.1.Built_In_Functions/lesson_tests.py | 10 +++++ challenges/8.1.Built_In_Functions/main.py | 8 ++++ .../8.2.User_Defined_Functions/lesson.md | 25 ++++++++++++ .../lesson_settings.json | 7 ++++ .../lesson_tests.py | 28 +++++++++++++ challenges/8.2.User_Defined_Functions/main.py | 6 +++ .../lesson.md | 31 ++++++++++++++ .../lesson_settings.json | 7 ++++ .../lesson_tests.py | 8 ++++ .../main.py | 9 +++++ .../8.4.Arguments_Vs_Parameters/lesson.md | 25 ++++++++++++ .../lesson_settings.json | 7 ++++ .../lesson_tests.py | 17 ++++++++ .../8.4.Arguments_Vs_Parameters/main.py | 9 +++++ challenges/8.5.Default_Arguments/lesson.md | 22 ++++++++++ .../lesson_settings.json | 7 ++++ .../8.5.Default_Arguments/lesson_tests.py | 9 +++++ challenges/8.5.Default_Arguments/main.py | 11 +++++ challenges/8.6.Calling_Functions/lesson.md | 25 ++++++++++++ .../lesson_settings.json | 7 ++++ .../8.6.Calling_Functions/lesson_tests.py | 8 ++++ challenges/8.6.Calling_Functions/main.py | 9 +++++ challenges/8.7.Keyword_Arguments/lesson.md | 40 +++++++++++++++++++ .../lesson_settings.json | 7 ++++ .../8.7.Keyword_Arguments/lesson_tests.py | 17 ++++++++ challenges/8.7.Keyword_Arguments/main.py | 10 +++++ challenges/8.8.Positional_Arguments/lesson.md | 40 +++++++++++++++++++ .../lesson_settings.json | 7 ++++ .../8.8.Positional_Arguments/lesson_tests.py | 15 +++++++ challenges/8.8.Positional_Arguments/main.py | 10 +++++ 32 files changed, 471 insertions(+) create mode 100644 challenges/8.1.Built_In_Functions/lesson.md create mode 100644 challenges/8.1.Built_In_Functions/lesson_settings.json create mode 100644 challenges/8.1.Built_In_Functions/lesson_tests.py create mode 100644 challenges/8.1.Built_In_Functions/main.py create mode 100644 challenges/8.2.User_Defined_Functions/lesson.md create mode 100644 challenges/8.2.User_Defined_Functions/lesson_settings.json create mode 100644 challenges/8.2.User_Defined_Functions/lesson_tests.py create mode 100644 challenges/8.2.User_Defined_Functions/main.py create mode 100644 challenges/8.3.Function_Documentation_Strings/lesson.md create mode 100644 challenges/8.3.Function_Documentation_Strings/lesson_settings.json create mode 100644 challenges/8.3.Function_Documentation_Strings/lesson_tests.py create mode 100644 challenges/8.3.Function_Documentation_Strings/main.py create mode 100644 challenges/8.4.Arguments_Vs_Parameters/lesson.md create mode 100644 challenges/8.4.Arguments_Vs_Parameters/lesson_settings.json create mode 100644 challenges/8.4.Arguments_Vs_Parameters/lesson_tests.py create mode 100644 challenges/8.4.Arguments_Vs_Parameters/main.py create mode 100644 challenges/8.5.Default_Arguments/lesson.md create mode 100644 challenges/8.5.Default_Arguments/lesson_settings.json create mode 100644 challenges/8.5.Default_Arguments/lesson_tests.py create mode 100644 challenges/8.5.Default_Arguments/main.py create mode 100644 challenges/8.6.Calling_Functions/lesson.md create mode 100644 challenges/8.6.Calling_Functions/lesson_settings.json create mode 100644 challenges/8.6.Calling_Functions/lesson_tests.py create mode 100644 challenges/8.6.Calling_Functions/main.py create mode 100644 challenges/8.7.Keyword_Arguments/lesson.md create mode 100644 challenges/8.7.Keyword_Arguments/lesson_settings.json create mode 100644 challenges/8.7.Keyword_Arguments/lesson_tests.py create mode 100644 challenges/8.7.Keyword_Arguments/main.py create mode 100644 challenges/8.8.Positional_Arguments/lesson.md create mode 100644 challenges/8.8.Positional_Arguments/lesson_settings.json create mode 100644 challenges/8.8.Positional_Arguments/lesson_tests.py create mode 100644 challenges/8.8.Positional_Arguments/main.py diff --git a/challenges/8.1.Built_In_Functions/lesson.md b/challenges/8.1.Built_In_Functions/lesson.md new file mode 100644 index 0000000..7c9ac63 --- /dev/null +++ b/challenges/8.1.Built_In_Functions/lesson.md @@ -0,0 +1,23 @@ +## Built-in Functions + +The Python interpreter has a number of functions and types built into it that are always available. +They are listed [here](https://docs.python.org/3/library/functions.html) in alphabetical order. +Python 3's builtins module provides direct access to all of the built-in functions. +You don't need to import any modules when using a built-in function, just call the function with the appropriate arguments. +- https://docs.python.org/3/library/functions.html#built-in-functions +- https://docs.python.org/3/library/builtins.html#module-builtins +- https://github.com/python/cpython/blob/3.6/Python/bltinmodule.c +``` +>>> print("print() is a built-in function in Python 3") +print() is a built-in function in Python 3 +>>> type([1,2,3,4,5]) +<class 'list'> +>>> sum([1,2,3,4,5]) +15 +>>> id(sum([1,2,3,4,5])) +4412662784 +``` +**_Instructions:_** +**In the console you will find a list of numbers.** +**There is a built-in function that will return a new sorted list from the items in numbers.** +**Find this function, call it using the variable named numbers as the argument, and assign this function call to the variable named result.** diff --git a/challenges/8.1.Built_In_Functions/lesson_settings.json b/challenges/8.1.Built_In_Functions/lesson_settings.json new file mode 100644 index 0000000..3f80741 --- /dev/null +++ b/challenges/8.1.Built_In_Functions/lesson_settings.json @@ -0,0 +1,7 @@ +{ + "lesson_title": "Built In Functions", + "chapter_number": 8, + "lesson_number": 1, + "id": "", + "repl": "" +} diff --git a/challenges/8.1.Built_In_Functions/lesson_tests.py b/challenges/8.1.Built_In_Functions/lesson_tests.py new file mode 100644 index 0000000..a05aa90 --- /dev/null +++ b/challenges/8.1.Built_In_Functions/lesson_tests.py @@ -0,0 +1,10 @@ +import unittest +from main import * + +class BuiltInFunctionsTests(unittest.TestCase): + def test_main(self): + self.assertIsInstance(result, list) + self.assertNotIsInstance(result, str) + self.assertEqual(result, [1, 2, 3, 4, 5, 6, 7, 8]) + self.assertNotEqual(result, [6, 3, 1, 8, 5, 2, 4, 7]) + self.assertNotEqual(result, numbers) diff --git a/challenges/8.1.Built_In_Functions/main.py b/challenges/8.1.Built_In_Functions/main.py new file mode 100644 index 0000000..4d5e312 --- /dev/null +++ b/challenges/8.1.Built_In_Functions/main.py @@ -0,0 +1,8 @@ +numbers = [6, 3, 1, 8, 5, 2, 4, 7] + +### Assign the correct built-in function to the variable named result below: ### + +result = "Replace this string with the correct answer" + +### Write your code above this line +print(result) diff --git a/challenges/8.2.User_Defined_Functions/lesson.md b/challenges/8.2.User_Defined_Functions/lesson.md new file mode 100644 index 0000000..4c4ec2c --- /dev/null +++ b/challenges/8.2.User_Defined_Functions/lesson.md @@ -0,0 +1,25 @@ +## User Defined Functions + +The keyword `def` introduces a function definition. +It must be followed by the function name and a parenthesized list of formal parameters (if any). +The statements that form the body of the function start at the next line, and must be indented. +After a function has been defined, it can be called by typing the function name immediately followed by parentheses that contain the function's arguments (if any). +- https://docs.python.org/3/glossary.html#term-function +- https://docs.python.org/3/glossary.html#term-parameter +- https://docs.python.org/3/glossary.html#term-argument +- https://docs.python.org/3/tutorial/controlflow.html#defining-functions +- https://docs.python.org/3/reference/compound_stmts.html#def +- https://docs.python.org/3/reference/expressions.html#calls +- https://www.python.org/dev/peps/pep-0008/#function-names +``` +>>> def the_truth(): +... return True +... +>>> the_truth() +True +``` + +**_Instructions:_** +**Define a function named say_hello() that has no parameters and prints the string "Hello World!" to the console.** +**Your function should not contain a return statement, only a print statement.** +**After you have defined the function, call it.** diff --git a/challenges/8.2.User_Defined_Functions/lesson_settings.json b/challenges/8.2.User_Defined_Functions/lesson_settings.json new file mode 100644 index 0000000..0922618 --- /dev/null +++ b/challenges/8.2.User_Defined_Functions/lesson_settings.json @@ -0,0 +1,7 @@ +{ + "lesson_title": "User Defined Functions", + "chapter_number": 8, + "lesson_number": 2, + "id": "", + "repl": "" +} diff --git a/challenges/8.2.User_Defined_Functions/lesson_tests.py b/challenges/8.2.User_Defined_Functions/lesson_tests.py new file mode 100644 index 0000000..9439b0c --- /dev/null +++ b/challenges/8.2.User_Defined_Functions/lesson_tests.py @@ -0,0 +1,28 @@ +import unittest +import sys +from io import StringIO +from main import * + +class UserDefinedFunctionsTests(unittest.TestCase): + def test_main(self): + # Make sure there is no return statement. + self.assertIsNone(say_hello()) + + def test_function_name(self): + # Make sure that the function has the correct name. + f = open('main.py') + lines = str(f.readlines()) + f.close() + self.assertRegex(lines, 'say_hello()', msg="The name of the function is incorrect.") + + def test_output(self): + # Make sure that calling say_hello() outputs the correct string. + saved_stdout = sys.stdout + try: + out = StringIO() + sys.stdout = out + say_hello() + output = out.getvalue().strip() + assert output == "Hello World!" + finally: + sys.stdout = saved_stdout diff --git a/challenges/8.2.User_Defined_Functions/main.py b/challenges/8.2.User_Defined_Functions/main.py new file mode 100644 index 0000000..11145b9 --- /dev/null +++ b/challenges/8.2.User_Defined_Functions/main.py @@ -0,0 +1,6 @@ +### Define the say_hello() function and fill in the function body below this line: ### + + + +### Call the say_hello() function below this line: ### +say_hello() diff --git a/challenges/8.3.Function_Documentation_Strings/lesson.md b/challenges/8.3.Function_Documentation_Strings/lesson.md new file mode 100644 index 0000000..e317d76 --- /dev/null +++ b/challenges/8.3.Function_Documentation_Strings/lesson.md @@ -0,0 +1,31 @@ +## Function Documentation Strings + +Function documentation strings (docstrings) offer a way to provide additional information about a function and its properties. +A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. +Such a docstring becomes the \_\_doc\_\_ special attribute of that object. +The docstring for a function or method should summarize its behavior and document its arguments, return value(s), side effects, exceptions raised, and restrictions on when it can be called (all if applicable). +Optional arguments should be indicated. +There are two forms of docstrings: one-liners and multi-line docstrings. +For consistency, always use """triple double quotes""" around docstrings. + +- https://docs.python.org/3/tutorial/controlflow.html#documentation-strings +- https://www.python.org/dev/peps/pep-0257/ +``` +>>> def some_function(): +... """ +... This function does nothing, but it's documented. +... +... Multiline docstrings are for more detailed descriptions. +... """ +... pass +... +>>> print(some_function.__doc__) + + This function does nothing, but it's documented. + + Multiline docstrings are for more detailed descriptions. +``` + +**_Instructions:_** +**In the console you will find a function named docstring\_function().** +**In the space between the function definition and the `pass` keyword, write any string you like using the docstring triple-double quotes convention.** diff --git a/challenges/8.3.Function_Documentation_Strings/lesson_settings.json b/challenges/8.3.Function_Documentation_Strings/lesson_settings.json new file mode 100644 index 0000000..1206832 --- /dev/null +++ b/challenges/8.3.Function_Documentation_Strings/lesson_settings.json @@ -0,0 +1,7 @@ +{ + "lesson_title": "Function Documentation Strings", + "chapter_number": 8, + "lesson_number": 3, + "id": "", + "repl": "" +} diff --git a/challenges/8.3.Function_Documentation_Strings/lesson_tests.py b/challenges/8.3.Function_Documentation_Strings/lesson_tests.py new file mode 100644 index 0000000..c3cbe78 --- /dev/null +++ b/challenges/8.3.Function_Documentation_Strings/lesson_tests.py @@ -0,0 +1,8 @@ +import unittest +from main import * + +class FunctionDocumentationStringsTests(unittest.TestCase): + def test_main(self): + self.assertIsNone(docstring_function()) + self.assertIsNotNone(docstring_function.__doc__) + self.assertIsInstance(docstring_function.__doc__, str) diff --git a/challenges/8.3.Function_Documentation_Strings/main.py b/challenges/8.3.Function_Documentation_Strings/main.py new file mode 100644 index 0000000..2ea9f61 --- /dev/null +++ b/challenges/8.3.Function_Documentation_Strings/main.py @@ -0,0 +1,9 @@ +def docstring_function(): + ### Write your docstring below this line. ### + + + + ### Write your docstring above this line. ### + pass + +print(docstring_function.__doc__) diff --git a/challenges/8.4.Arguments_Vs_Parameters/lesson.md b/challenges/8.4.Arguments_Vs_Parameters/lesson.md new file mode 100644 index 0000000..870086b --- /dev/null +++ b/challenges/8.4.Arguments_Vs_Parameters/lesson.md @@ -0,0 +1,25 @@ +## Arguments vs Parameters + +When dealing with functions, the terms argument and parameter are sometimes used interchangeably, but they do NOT mean the same thing. +The difference between arguments and parameters can cause some confusion, especially for novice programmers. +Parameters are defined by the names that appear in a function definition and define what types of arguments a function can accept. +Arguments are the values actually passed to a function when calling it. +When you DEFINE a function after the `def` keyword, the parameters are named inside the parentheses that follow. +When you CALL a function, the arguments are assigned inside the parentheses that follow. +- https://docs.python.org/3/faq/programming.html#what-is-the-difference-between-arguments-and-parameters +- https://docs.python.org/3/glossary.html#term-argument +- https://docs.python.org/3/glossary.html#term-parameter +``` +# The parameters are inside the parentheses in the function definition. +>>> def some_function(foo, bar=None): +... return foo, bar +... +# The arguments are inside the parentheses in the function call. +>>> some_function(42, bar='baz') +(42, 'baz') +``` +**_Instructions:_** +**In the console, above the variable named result, define a function named my_function that has a single parameter named parameter.** +**In the function body, tell the function to return parameter.** +**After the equal sign for the variable named result, call my_function() and set the argument as the string 'argument'.** +**When finished, you will have assigned the result of the my_function() call to the variable named result.** diff --git a/challenges/8.4.Arguments_Vs_Parameters/lesson_settings.json b/challenges/8.4.Arguments_Vs_Parameters/lesson_settings.json new file mode 100644 index 0000000..6ffe41e --- /dev/null +++ b/challenges/8.4.Arguments_Vs_Parameters/lesson_settings.json @@ -0,0 +1,7 @@ +{ + "lesson_title": "Arguments vs Parameters", + "chapter_number": 8, + "lesson_number": 4, + "id": "", + "repl": "" +} diff --git a/challenges/8.4.Arguments_Vs_Parameters/lesson_tests.py b/challenges/8.4.Arguments_Vs_Parameters/lesson_tests.py new file mode 100644 index 0000000..6987366 --- /dev/null +++ b/challenges/8.4.Arguments_Vs_Parameters/lesson_tests.py @@ -0,0 +1,17 @@ +import unittest +from main import * + +class ArgumentsVsParametersTests(unittest.TestCase): + def test_output(self): + self.assertIsNotNone(result) + self.assertIsInstance(result, str) + self.assertEqual(result, 'argument') + + def test_names_for_function_parameter_and_argument(self): + f = open('main.py') + lines = str(f.readlines()) + f.close() + # The following tests only ensure that the named strings exist somewhere in the file, not that they are where they should be or have the correct values assigned. + self.assertRegex(lines, 'my_function()', msg="The name of the function is incorrect.") + self.assertRegex(lines, 'parameter', msg="The parameter must have the correct name.") + self.assertRegex(lines, 'argument', msg="The argument must have the correct name.") diff --git a/challenges/8.4.Arguments_Vs_Parameters/main.py b/challenges/8.4.Arguments_Vs_Parameters/main.py new file mode 100644 index 0000000..ad033a0 --- /dev/null +++ b/challenges/8.4.Arguments_Vs_Parameters/main.py @@ -0,0 +1,9 @@ +### Define your function with the appropriate parameter below this line: ### + + + +### Assign the function call with the appropriate argument to the variable named result below this line: ### + +result = "Replace this string with the correct answer" + +print(result) diff --git a/challenges/8.5.Default_Arguments/lesson.md b/challenges/8.5.Default_Arguments/lesson.md new file mode 100644 index 0000000..d0e7383 --- /dev/null +++ b/challenges/8.5.Default_Arguments/lesson.md @@ -0,0 +1,22 @@ +## Default Arguments + +When a function is defined in Python, you can specify formal parameters inside of the trailing parentheses. +If any of those parameters are declared using the "argument=value" format, then you don't necessarily have to specify a value for those arguments when the function is called. +If a value is not specified, then that parameter uses the default argument value when the function executes. +A default value can be specified for any or all of the arguments, which creates a function that can be called with fewer arguments than it is defined to allow. +- https://docs.python.org/3.6/tutorial/controlflow.html#default-argument-values +``` +>>> def greeting(number=1, message='Hello'): +... print(number, message) +... +>>> greeting() +1 Hello +>>> greeting(2, message='Ahoy There!') +2 Ahoy There! +>>> greeting(number='Three') +Three Hello +``` +**_Instructions:_** +**In the console there is a function named favorite_python() with a single parameter named member that has a default argument of None.** +**Call the function and change the default argument to a string containing the name of your favorite Monty Python member.** +**Assign that function call to the variable named my_choice.** diff --git a/challenges/8.5.Default_Arguments/lesson_settings.json b/challenges/8.5.Default_Arguments/lesson_settings.json new file mode 100644 index 0000000..00bb427 --- /dev/null +++ b/challenges/8.5.Default_Arguments/lesson_settings.json @@ -0,0 +1,7 @@ +{ + "lesson_title": "Default Arguments", + "chapter_number": 8, + "lesson_number": 5, + "id": "", + "repl": "" +} diff --git a/challenges/8.5.Default_Arguments/lesson_tests.py b/challenges/8.5.Default_Arguments/lesson_tests.py new file mode 100644 index 0000000..75ff21e --- /dev/null +++ b/challenges/8.5.Default_Arguments/lesson_tests.py @@ -0,0 +1,9 @@ +import unittest +from main import * + +class DefaultArgumentsTests(unittest.TestCase): + def test_main(self): + self.assertIsNotNone(my_choice) + self.assertTrue(my_choice) + self.assertIsInstance(my_choice, str) + self.assertNotEqual(my_choice, "Replace this string with the correct function call.") diff --git a/challenges/8.5.Default_Arguments/main.py b/challenges/8.5.Default_Arguments/main.py new file mode 100644 index 0000000..9bc5c40 --- /dev/null +++ b/challenges/8.5.Default_Arguments/main.py @@ -0,0 +1,11 @@ +def favorite_python(member=None): + print(member) + return member + +### Call the favorite_python() function with the default argument changed to +### the name of your favorite Monty Python member, and assign that function +### call to the variable named my_choice. + +my_choice = "Replace this string with the correct function call." + +### Call the function above this line. diff --git a/challenges/8.6.Calling_Functions/lesson.md b/challenges/8.6.Calling_Functions/lesson.md new file mode 100644 index 0000000..fd2ea0d --- /dev/null +++ b/challenges/8.6.Calling_Functions/lesson.md @@ -0,0 +1,25 @@ +## Calling Functions + +Once a function has been defined, it must be called in order to be executed. +A function is a callable object that can execute when its `__call__()` method is invoked. +A function can be called by appending the arguments in parentheses to the function name. +If a function has no parameters, or if the parameters have default arguments that you do not want to change, then you can invoke the call method by appending empty parentheses to the function name. +- https://docs.python.org/3/reference/datamodel.html#object.\_\_call\_\_ +- https://docs.python.org/3.6/tutorial/controlflow.html#defining-functions +- https://docs.python.org/3/library/functions.html#callable +``` +# Define the function: +>>> def call_something(something=None): +... return something +... +# Call the function with the parameter's default argument of None: +>>> call_something() +# Nothing is returned. +# Now call the function with an argument that changes the default: +>>> call_something(something="Here's something") +"Here's something" +``` +**_Instructions:_** +**In the console, there is a function named say_hello() that returns a greeting.** +**Call the function and put any string you want as the greeting argument inside of the parentheses.** +**Assign the function call to the variable named result in the console.** diff --git a/challenges/8.6.Calling_Functions/lesson_settings.json b/challenges/8.6.Calling_Functions/lesson_settings.json new file mode 100644 index 0000000..7037706 --- /dev/null +++ b/challenges/8.6.Calling_Functions/lesson_settings.json @@ -0,0 +1,7 @@ +{ + "lesson_title": "Calling Functions", + "chapter_number": 8, + "lesson_number": 6, + "id": "", + "repl": "" +} diff --git a/challenges/8.6.Calling_Functions/lesson_tests.py b/challenges/8.6.Calling_Functions/lesson_tests.py new file mode 100644 index 0000000..952c4bb --- /dev/null +++ b/challenges/8.6.Calling_Functions/lesson_tests.py @@ -0,0 +1,8 @@ +import unittest +from main import * + +class CallingFunctionsTests(unittest.TestCase): + def test_output(self): + self.assertIsNotNone(result) + self.assertIsInstance(result, str) + self.assertNotEqual(result, "Replace this string with the correct function call.") diff --git a/challenges/8.6.Calling_Functions/main.py b/challenges/8.6.Calling_Functions/main.py new file mode 100644 index 0000000..99f669b --- /dev/null +++ b/challenges/8.6.Calling_Functions/main.py @@ -0,0 +1,9 @@ +def say_hello(greeting): + return greeting + +### Call the say_hello function below this line: ### + +result = "Replace this string with the correct function call." + +### Call the say_hello function above this line: ### +print(result) diff --git a/challenges/8.7.Keyword_Arguments/lesson.md b/challenges/8.7.Keyword_Arguments/lesson.md new file mode 100644 index 0000000..9f6fb0f --- /dev/null +++ b/challenges/8.7.Keyword_Arguments/lesson.md @@ -0,0 +1,40 @@ +## Keyword and Variable Keyword Arguments + +A keyword argument can be an argument preceded by an identifier in a function call using the form `kwarg=value`. +Variable keyword arguments can be passed as values in a dictionary preceded by `**`, like this: +`def example(default_arguments, *args, **kwargs):` +In a function call, keyword arguments must follow positional arguments, and all keyword arguments passed must match one of the arguments accepted by the function. +[Here is a detailed explanation](https://docs.python.org/3/reference/expressions.html#calls) of how keyword arguments are dealt with when the function is called. +- https://docs.python.org/3.6/tutorial/controlflow.html#keyword-arguments +- https://docs.python.org/3.6/glossary.html#term-argument +- https://docs.python.org/3/reference/expressions.html#grammar-token-keywords_arguments +- https://docs.python.org/3/glossary.html#term-parameter +- https://www.python.org/dev/peps/pep-0362/ +- https://www.python.org/dev/peps/pep-0448/ +- https://docs.python.org/3/reference/expressions.html#calls +- https://docs.python.org/3/reference/compound_stmts.html#function-definitions +``` +>>> # 3 and 5 are both keyword arguments in the following calls to complex() +... +>>> complex(real=3, imag=5) +(3+5j) +>>> complex(**{'real': 3, 'imag': 5}) +(3+5j) +``` +``` +>>> # arg1 and arg2 are keyword arguments, **kwargs is for variable keyword arguments +... +>>> >>> def some_dr_seuss(arg1='arg1', arg2='arg2', **kwargs): +... return arg1, arg2, kwargs +... +>>> print(some_dr_seuss(**{'arg3': 'arg_red', 'arg4': 'arg_blue'})) +('arg1', 'arg2', {'arg3': 'arg_red', 'arg4': 'arg_blue'}) +``` +**_Instructions:_** +**In the console there is a function named keyword_argument_example() that takes a default argument an an unspecified number of keyword arguments.** +**When you call the function:** +- **Enter an integer, your age, as the value for the default argument named your_age.** +- **Create two keyword arguments named first and last.** +- **Assign a string with your first name to first, and your last name to last.** + +**Assign your function call to the variable named about_me.** diff --git a/challenges/8.7.Keyword_Arguments/lesson_settings.json b/challenges/8.7.Keyword_Arguments/lesson_settings.json new file mode 100644 index 0000000..0b7a35c --- /dev/null +++ b/challenges/8.7.Keyword_Arguments/lesson_settings.json @@ -0,0 +1,7 @@ +{ + "lesson_title": "Keyword Arguments", + "chapter_number": 8, + "lesson_number": 7, + "id": "", + "repl": "" +} diff --git a/challenges/8.7.Keyword_Arguments/lesson_tests.py b/challenges/8.7.Keyword_Arguments/lesson_tests.py new file mode 100644 index 0000000..d920f5e --- /dev/null +++ b/challenges/8.7.Keyword_Arguments/lesson_tests.py @@ -0,0 +1,17 @@ +import unittest +from main import * + +class KeywordArgumentsTests(unittest.TestCase): + def test_main(self): + self.assertIsNotNone(about_me) + self.assertTrue(about_me) + self.assertNotEqual(about_me, "Replace this string with the correct function call.") + + def test_about_me_variable_has_correct_types(self): + self.assertIsInstance(about_me, tuple) + self.assertIsInstance(about_me[0], int) + self.assertIsInstance(about_me[1], dict) + + def test_correct_keyword_arguments(self): + self.assertIn("first", about_me[1].keys()) + self.assertIn("last", about_me[1].keys()) diff --git a/challenges/8.7.Keyword_Arguments/main.py b/challenges/8.7.Keyword_Arguments/main.py new file mode 100644 index 0000000..d09005b --- /dev/null +++ b/challenges/8.7.Keyword_Arguments/main.py @@ -0,0 +1,10 @@ +def keyword_argument_example(your_age, **kwargs): + return your_age, kwargs + +### Write your code below this line ### + +about_me = "Replace this string with the correct function call." + +### Write your code above this line ### + +print(about_me) diff --git a/challenges/8.8.Positional_Arguments/lesson.md b/challenges/8.8.Positional_Arguments/lesson.md new file mode 100644 index 0000000..0e61434 --- /dev/null +++ b/challenges/8.8.Positional_Arguments/lesson.md @@ -0,0 +1,40 @@ +## Positional and Variable Positional Arguments + +A positional argument is, simply put, any argument that is not a keyword argument. +Positional refers to the fact that the relative position, or order, of the arguments is significant when they are stored in their corresponding tuple. +Positional arguments can be used to form [arbitrary argument lists](https://docs.python.org/3.6/tutorial/controlflow.html#arbitrary-argument-lists). +Functions can accept a variable number of (optional) positional arguments by using the `*args` syntax in the def statement, like this: +`def example(default_arguments, *args, **kwargs):` +The `*` operator instructs Python to pass argument inputs to a tuple that receives any excess positional parameters, defaulting to the empty tuple. +When a function is defined and the parameters are declared, variable positional arguments must follow normal/default arguments (if any) and precede any variable keyword arguments (if any). +- https://docs.python.org/3.6/tutorial/controlflow.html#arbitrary-argument-lists +- https://docs.python.org/3/glossary.html#term-argument +- https://docs.python.org/3/reference/expressions.html#grammar-token-positional_arguments +- https://www.python.org/dev/peps/pep-0362/ +- https://www.python.org/dev/peps/pep-0448/ +- https://docs.python.org/3/reference/expressions.html#calls +- https://docs.python.org/3/reference/compound_stmts.html#function-definitions +``` +>>> # 3 and 5 are both positional arguments in the following calls to complex() +... +>>> complex(3, 5) +(3+5j) +>>> complex(*(3, 5)) +(3+5j) +``` +``` +>>> # arg1 and arg2 are positional arguments, *args is for variable positional arguments +... +>>> def some_dr_seuss(arg1, arg2, *args): +... return arg1, arg2, args +... +>>> print(some_dr_seuss('arg1', 'arg2', 'arg_red', 'arg_blue')) +('arg1', 'arg2', ('arg_red', 'arg_blue')) +``` +**_Instructions:_** +**In the console there is a function named positional_argument_example() that takes a default argument and an unspecified number of positional arguments.** +**When you call the function:** +- **Enter an integer, your age, as the value for the default argument named your_age.** +- **Assign two strings, the first with your first name, the second with your last name, as the variable positional arguments**. + +**Assign your function call to the variable named about_me.* diff --git a/challenges/8.8.Positional_Arguments/lesson_settings.json b/challenges/8.8.Positional_Arguments/lesson_settings.json new file mode 100644 index 0000000..c3d9931 --- /dev/null +++ b/challenges/8.8.Positional_Arguments/lesson_settings.json @@ -0,0 +1,7 @@ +{ + "lesson_title": "Positional Arguments", + "chapter_number": 8, + "lesson_number": 8, + "id": "", + "repl": "" +} diff --git a/challenges/8.8.Positional_Arguments/lesson_tests.py b/challenges/8.8.Positional_Arguments/lesson_tests.py new file mode 100644 index 0000000..aa8f9f0 --- /dev/null +++ b/challenges/8.8.Positional_Arguments/lesson_tests.py @@ -0,0 +1,15 @@ +import unittest +from main import * + +class PositionalArgumentsTests(unittest.TestCase): + def test_main(self): + self.assertIsNotNone(about_me) + self.assertTrue(about_me) + self.assertNotEqual(about_me, "Replace this string with the correct function call.") + + def test_correct_types(self): + self.assertIsInstance(about_me, tuple) + self.assertIsInstance(about_me[0], int) + self.assertIsInstance(about_me[1], tuple) + self.assertIsInstance(about_me[1][0], str) + self.assertIsInstance(about_me[1][1], str) diff --git a/challenges/8.8.Positional_Arguments/main.py b/challenges/8.8.Positional_Arguments/main.py new file mode 100644 index 0000000..b0bf907 --- /dev/null +++ b/challenges/8.8.Positional_Arguments/main.py @@ -0,0 +1,10 @@ +def positional_argument_example(your_age, *args): + return your_age, args + +### Write your code below this line ### + +about_me = "Replace this string with the correct function call." + +### Write your code above this line ### + +print(about_me) From 800e65416b739839f2a3d8bfcaaa2b44ec2e0900 Mon Sep 17 00:00:00 2001 From: bgroveben <bgroveben@gmail.com> Date: Wed, 5 Jul 2017 01:25:14 -0700 Subject: [PATCH 2/3] Add mongo ObjectId's to lesson_settings files in Functions chapter --- challenges/8.1.Built_In_Functions/lesson_settings.json | 2 +- challenges/8.2.User_Defined_Functions/lesson_settings.json | 2 +- .../8.3.Function_Documentation_Strings/lesson_settings.json | 2 +- challenges/8.4.Arguments_Vs_Parameters/lesson_settings.json | 2 +- challenges/8.5.Default_Arguments/lesson_settings.json | 2 +- challenges/8.6.Calling_Functions/lesson_settings.json | 2 +- challenges/8.7.Keyword_Arguments/lesson_settings.json | 2 +- challenges/8.8.Positional_Arguments/lesson_settings.json | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/challenges/8.1.Built_In_Functions/lesson_settings.json b/challenges/8.1.Built_In_Functions/lesson_settings.json index 3f80741..9ba54d6 100644 --- a/challenges/8.1.Built_In_Functions/lesson_settings.json +++ b/challenges/8.1.Built_In_Functions/lesson_settings.json @@ -2,6 +2,6 @@ "lesson_title": "Built In Functions", "chapter_number": 8, "lesson_number": 1, - "id": "", + "id": "595ca15cddca05761bc04efa", "repl": "" } diff --git a/challenges/8.2.User_Defined_Functions/lesson_settings.json b/challenges/8.2.User_Defined_Functions/lesson_settings.json index 0922618..52f7d1c 100644 --- a/challenges/8.2.User_Defined_Functions/lesson_settings.json +++ b/challenges/8.2.User_Defined_Functions/lesson_settings.json @@ -2,6 +2,6 @@ "lesson_title": "User Defined Functions", "chapter_number": 8, "lesson_number": 2, - "id": "", + "id": "595ca17addca05761bc04efb", "repl": "" } diff --git a/challenges/8.3.Function_Documentation_Strings/lesson_settings.json b/challenges/8.3.Function_Documentation_Strings/lesson_settings.json index 1206832..ab3b5c7 100644 --- a/challenges/8.3.Function_Documentation_Strings/lesson_settings.json +++ b/challenges/8.3.Function_Documentation_Strings/lesson_settings.json @@ -2,6 +2,6 @@ "lesson_title": "Function Documentation Strings", "chapter_number": 8, "lesson_number": 3, - "id": "", + "id": "595ca194ddca05761bc04efc", "repl": "" } diff --git a/challenges/8.4.Arguments_Vs_Parameters/lesson_settings.json b/challenges/8.4.Arguments_Vs_Parameters/lesson_settings.json index 6ffe41e..7e54454 100644 --- a/challenges/8.4.Arguments_Vs_Parameters/lesson_settings.json +++ b/challenges/8.4.Arguments_Vs_Parameters/lesson_settings.json @@ -2,6 +2,6 @@ "lesson_title": "Arguments vs Parameters", "chapter_number": 8, "lesson_number": 4, - "id": "", + "id": "595ca1aaddca05761bc04efd", "repl": "" } diff --git a/challenges/8.5.Default_Arguments/lesson_settings.json b/challenges/8.5.Default_Arguments/lesson_settings.json index 00bb427..a45d760 100644 --- a/challenges/8.5.Default_Arguments/lesson_settings.json +++ b/challenges/8.5.Default_Arguments/lesson_settings.json @@ -2,6 +2,6 @@ "lesson_title": "Default Arguments", "chapter_number": 8, "lesson_number": 5, - "id": "", + "id": "595ca1c5ddca05761bc04efe", "repl": "" } diff --git a/challenges/8.6.Calling_Functions/lesson_settings.json b/challenges/8.6.Calling_Functions/lesson_settings.json index 7037706..695334b 100644 --- a/challenges/8.6.Calling_Functions/lesson_settings.json +++ b/challenges/8.6.Calling_Functions/lesson_settings.json @@ -2,6 +2,6 @@ "lesson_title": "Calling Functions", "chapter_number": 8, "lesson_number": 6, - "id": "", + "id": "595ca1d9ddca05761bc04eff", "repl": "" } diff --git a/challenges/8.7.Keyword_Arguments/lesson_settings.json b/challenges/8.7.Keyword_Arguments/lesson_settings.json index 0b7a35c..3870a31 100644 --- a/challenges/8.7.Keyword_Arguments/lesson_settings.json +++ b/challenges/8.7.Keyword_Arguments/lesson_settings.json @@ -2,6 +2,6 @@ "lesson_title": "Keyword Arguments", "chapter_number": 8, "lesson_number": 7, - "id": "", + "id": "595ca1f2ddca05761bc04f00", "repl": "" } diff --git a/challenges/8.8.Positional_Arguments/lesson_settings.json b/challenges/8.8.Positional_Arguments/lesson_settings.json index c3d9931..f478855 100644 --- a/challenges/8.8.Positional_Arguments/lesson_settings.json +++ b/challenges/8.8.Positional_Arguments/lesson_settings.json @@ -2,6 +2,6 @@ "lesson_title": "Positional Arguments", "chapter_number": 8, "lesson_number": 8, - "id": "", + "id": "595ca20addca05761bc04f01", "repl": "" } From 789601be705e684e9c362b2d04090a028149cef6 Mon Sep 17 00:00:00 2001 From: jamaurz <mazurhalinapost@gmail.com> Date: Sun, 30 Jul 2017 22:37:39 +0300 Subject: [PATCH 3/3] upadte and add new challenges --- challenges/1.1.Print/lesson.md | 34 +++++++++++++++++ challenges/1.1.Print/lesson_tests.py | 10 +++++ challenges/1.1.Print/main.py | 1 + challenges/1.2.Escape_Characters/lesson.md | 20 ++++++++++ .../1.2.Escape_Characters/lesson_tests.py | 7 ++++ challenges/1.2.Escape_Characters/main.py | 4 ++ challenges/6.1.Conditionals/lesson.md | 30 +++++++++++++++ .../6.1.Conditionals/lesson_settings.py | 7 ++++ challenges/6.1.Conditionals/lesson_tests.py | 7 ++++ challenges/6.1.Conditionals/main.py | 8 ++++ challenges/7.1.Loops/lesson.md | 38 +++++++++++++++++++ challenges/7.1.Loops/lesson_settings.py | 7 ++++ challenges/7.1.Loops/lesson_tests.py | 7 ++++ challenges/7.1.Loops/main.py | 12 ++++++ 14 files changed, 192 insertions(+) create mode 100644 challenges/6.1.Conditionals/lesson.md create mode 100644 challenges/6.1.Conditionals/lesson_settings.py create mode 100644 challenges/6.1.Conditionals/lesson_tests.py create mode 100644 challenges/6.1.Conditionals/main.py create mode 100644 challenges/7.1.Loops/lesson.md create mode 100644 challenges/7.1.Loops/lesson_settings.py create mode 100644 challenges/7.1.Loops/lesson_tests.py create mode 100644 challenges/7.1.Loops/main.py diff --git a/challenges/1.1.Print/lesson.md b/challenges/1.1.Print/lesson.md index e69de29..08f62bf 100644 --- a/challenges/1.1.Print/lesson.md +++ b/challenges/1.1.Print/lesson.md @@ -0,0 +1,34 @@ +## Print +- http://www.python-course.eu/python3_print.php +The print() function is used to present the output of a program. +``` +>>> print(“Hello World”) +Hello World +``` +It can have several parameters. +``` +>>> print(‘Hello’, 12, ‘98’) +Hello 12 98 +``` +The arguments of the print function: +sep=’’ +Sep is a separator, it is used to divide parameters. +``` +>>> print(‘Hello’, “World’, sep=’-‘) +Hello-World +``` +We can assign an string to the keyword parameter "end". This string will be used for ending the output of the values of a print call. +end=’’ +``` +>>> print(‘Hello’, ‘World’, end=’!’) +Hello World? +``` +By redefining the keyword parameter "file" we can send the output into a different stream file. +file=’’ +``` +>>> fh = open("data.txt","w") +>> print("no", file=fh) +>>> fh.close() +``` +**_Instructions:_** +**Put your name and sname to appropriate places, change '*' to parameters of separator '_' and in the end of the line '!'.** diff --git a/challenges/1.1.Print/lesson_tests.py b/challenges/1.1.Print/lesson_tests.py index e69de29..37bbad0 100644 --- a/challenges/1.1.Print/lesson_tests.py +++ b/challenges/1.1.Print/lesson_tests.py @@ -0,0 +1,10 @@ +import unittest + + +class PrintTests(unittest.TestCase): + def test_main(self): + f = open('main.py') + lines = str(f.readlines()) + f.close() + self.assertRegex(lines, "sep='_'", msg="_ mising") + self.assertRegex(lines, "end='!'", msg="! mising") \ No newline at end of file diff --git a/challenges/1.1.Print/main.py b/challenges/1.1.Print/main.py index e69de29..a30ae45 100644 --- a/challenges/1.1.Print/main.py +++ b/challenges/1.1.Print/main.py @@ -0,0 +1 @@ +print('firstname', 'sname', sep='*', end='*') #change this line diff --git a/challenges/1.2.Escape_Characters/lesson.md b/challenges/1.2.Escape_Characters/lesson.md index e69de29..cb51b23 100644 --- a/challenges/1.2.Escape_Characters/lesson.md +++ b/challenges/1.2.Escape_Characters/lesson.md @@ -0,0 +1,20 @@ +## Escape Character +The escape character is "\\" and this character is invoked an alternative interpretation in a character sequence. +- https://docs.python.org/2.0/ref/strings.html +``` +>>> print('\\') +\ +>>> print('\'') +' +>>> print('Helllo\nWorld') +Hello +World +``` +We can use escape character to print hex value Unicode character. +``` +>>> print('\uxxxx') +Л +``` + +**_Instructions:_** +**Use escape character "new line" in the string_escape** \ No newline at end of file diff --git a/challenges/1.2.Escape_Characters/lesson_tests.py b/challenges/1.2.Escape_Characters/lesson_tests.py index e69de29..7ab0e97 100644 --- a/challenges/1.2.Escape_Characters/lesson_tests.py +++ b/challenges/1.2.Escape_Characters/lesson_tests.py @@ -0,0 +1,7 @@ +import unittest +from main import * + + +class EscapeTests(unittest.TestCase): + def test_main(self): + self.assertRegex(repr(string_escape), r'\\n', msg='the string must contain "new line"') \ No newline at end of file diff --git a/challenges/1.2.Escape_Characters/main.py b/challenges/1.2.Escape_Characters/main.py index e69de29..e953a27 100644 --- a/challenges/1.2.Escape_Characters/main.py +++ b/challenges/1.2.Escape_Characters/main.py @@ -0,0 +1,4 @@ +string_escape = 'Hello, this is escape string' # change this line + +# change the string_escape, the string must contain "new line" +print(string_escape) \ No newline at end of file diff --git a/challenges/6.1.Conditionals/lesson.md b/challenges/6.1.Conditionals/lesson.md new file mode 100644 index 0000000..198a310 --- /dev/null +++ b/challenges/6.1.Conditionals/lesson.md @@ -0,0 +1,30 @@ +## if elif else + +- https://docs.python.org/3/tutorial/controlflow.html + +Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise. In Python zero and null is assumed as FALSE value. +``` +if value == 'some': + print('yes') +``` +An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value. +``` +if value == 'some': + print('yes') +else: + print('no') +``` + +The elif statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE. + +``` +if value == 'some': + print('yes') +elif value = 'value': + print('maybe') +else: + print('no') +``` + +**_Instructions_** +**Modify the value, so that program must print 'yes'.** diff --git a/challenges/6.1.Conditionals/lesson_settings.py b/challenges/6.1.Conditionals/lesson_settings.py new file mode 100644 index 0000000..a901770 --- /dev/null +++ b/challenges/6.1.Conditionals/lesson_settings.py @@ -0,0 +1,7 @@ +{ + "lesson_title": "Conditionals", + "chapter_number": 6, + "lesson_number": 1, + "id":"5965e5fd60d67217f0ce232f", + "repl": "" +} diff --git a/challenges/6.1.Conditionals/lesson_tests.py b/challenges/6.1.Conditionals/lesson_tests.py new file mode 100644 index 0000000..6d274d0 --- /dev/null +++ b/challenges/6.1.Conditionals/lesson_tests.py @@ -0,0 +1,7 @@ +import unittest +from main import * + +class ConditionalsTests(unittest.TestCase): + def test_main(self): + self.assertIsInstance(value, str) + self.assertIs(value, 'y', "program must print 'yes'") \ No newline at end of file diff --git a/challenges/6.1.Conditionals/main.py b/challenges/6.1.Conditionals/main.py new file mode 100644 index 0000000..faf0d94 --- /dev/null +++ b/challenges/6.1.Conditionals/main.py @@ -0,0 +1,8 @@ +value = 'some' #modify this line + +if value == 'Y' or value == 'y': + print('yes') +elif value == 'N' or value == 'n': + print('no') +else: + print('error') diff --git a/challenges/7.1.Loops/lesson.md b/challenges/7.1.Loops/lesson.md new file mode 100644 index 0000000..d80d890 --- /dev/null +++ b/challenges/7.1.Loops/lesson.md @@ -0,0 +1,38 @@ +## Loops (While) + +- https://docs.python.org/3/tutorial/controlflow.html + +A loop statement allows to execute a block of code multiple times. Python supplies two type of loops: +* while +* for + +The syntax of a while loop is: + +``` +while condition: + statement +``` + +While loops repeat a target statement as long as a given condition is true. + +``` +count = 0 +while count < 9: + print('Loop iteration: ', count) + count = count + 1 +``` + +Python supports to have an else statement associated with a loop statement. The else statement is executed when the condition becomes false. + +``` +count = 0 +while count < 9: + print('Loop iteration ', count) + count = count + 1 +else: + print('loop end') +``` + +**_Instructions_** +**Modify the count, so that condition and switch_loop must become true.** +**Use else statement, to mark the end of a program and switch switch_end to value true.** diff --git a/challenges/7.1.Loops/lesson_settings.py b/challenges/7.1.Loops/lesson_settings.py new file mode 100644 index 0000000..f6d8ee3 --- /dev/null +++ b/challenges/7.1.Loops/lesson_settings.py @@ -0,0 +1,7 @@ +{ + "lesson_title": "Loops", + "chapter_number": 7, + "lesson_number": 1, + "id":"597e1a1a634a36abfebcda38", + "repl": "" +} diff --git a/challenges/7.1.Loops/lesson_tests.py b/challenges/7.1.Loops/lesson_tests.py new file mode 100644 index 0000000..0a206ab --- /dev/null +++ b/challenges/7.1.Loops/lesson_tests.py @@ -0,0 +1,7 @@ +import unittest +from main import * + +class LoopsTests(unittest.TestCase): + def test_main(self): + self.assertTrue(switch_end) + self.assertTrue(switch_loop) diff --git a/challenges/7.1.Loops/main.py b/challenges/7.1.Loops/main.py new file mode 100644 index 0000000..f0aac5c --- /dev/null +++ b/challenges/7.1.Loops/main.py @@ -0,0 +1,12 @@ +count = 10 # Change this count +switch_loop = False +switch_end = False + +while count < 9: + print('step ', count) + count = count + 1 + switch_loop = True +# use else statements + print('loop end') + switch_end = True; +