Skip to content

Commit 841a8f2

Browse files
Merge pull request freeCodeCamp#36 from bgroveben/master
Lessons and Challenges for Beginner/Functions Chapter
2 parents fe3e6c7 + 800e654 commit 841a8f2

File tree

32 files changed

+471
-0
lines changed

32 files changed

+471
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Built-in Functions
2+
3+
The Python interpreter has a number of functions and types built into it that are always available.
4+
They are listed [here](https://docs.python.org/3/library/functions.html) in alphabetical order.
5+
Python 3's builtins module provides direct access to all of the built-in functions.
6+
You don't need to import any modules when using a built-in function, just call the function with the appropriate arguments.
7+
- https://docs.python.org/3/library/functions.html#built-in-functions
8+
- https://docs.python.org/3/library/builtins.html#module-builtins
9+
- https://github.com/python/cpython/blob/3.6/Python/bltinmodule.c
10+
```
11+
>>> print("print() is a built-in function in Python 3")
12+
print() is a built-in function in Python 3
13+
>>> type([1,2,3,4,5])
14+
<class 'list'>
15+
>>> sum([1,2,3,4,5])
16+
15
17+
>>> id(sum([1,2,3,4,5]))
18+
4412662784
19+
```
20+
**_Instructions:_**
21+
**In the console you will find a list of numbers.**
22+
**There is a built-in function that will return a new sorted list from the items in numbers.**
23+
**Find this function, call it using the variable named numbers as the argument, and assign this function call to the variable named result.**
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"lesson_title": "Built In Functions",
3+
"chapter_number": 8,
4+
"lesson_number": 1,
5+
"id": "595ca15cddca05761bc04efa",
6+
"repl": ""
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import unittest
2+
from main import *
3+
4+
class BuiltInFunctionsTests(unittest.TestCase):
5+
def test_main(self):
6+
self.assertIsInstance(result, list)
7+
self.assertNotIsInstance(result, str)
8+
self.assertEqual(result, [1, 2, 3, 4, 5, 6, 7, 8])
9+
self.assertNotEqual(result, [6, 3, 1, 8, 5, 2, 4, 7])
10+
self.assertNotEqual(result, numbers)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
numbers = [6, 3, 1, 8, 5, 2, 4, 7]
2+
3+
### Assign the correct built-in function to the variable named result below: ###
4+
5+
result = "Replace this string with the correct answer"
6+
7+
### Write your code above this line
8+
print(result)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## User Defined Functions
2+
3+
The keyword `def` introduces a function definition.
4+
It must be followed by the function name and a parenthesized list of formal parameters (if any).
5+
The statements that form the body of the function start at the next line, and must be indented.
6+
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).
7+
- https://docs.python.org/3/glossary.html#term-function
8+
- https://docs.python.org/3/glossary.html#term-parameter
9+
- https://docs.python.org/3/glossary.html#term-argument
10+
- https://docs.python.org/3/tutorial/controlflow.html#defining-functions
11+
- https://docs.python.org/3/reference/compound_stmts.html#def
12+
- https://docs.python.org/3/reference/expressions.html#calls
13+
- https://www.python.org/dev/peps/pep-0008/#function-names
14+
```
15+
>>> def the_truth():
16+
... return True
17+
...
18+
>>> the_truth()
19+
True
20+
```
21+
22+
**_Instructions:_**
23+
**Define a function named say_hello() that has no parameters and prints the string "Hello World!" to the console.**
24+
**Your function should not contain a return statement, only a print statement.**
25+
**After you have defined the function, call it.**
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"lesson_title": "User Defined Functions",
3+
"chapter_number": 8,
4+
"lesson_number": 2,
5+
"id": "595ca17addca05761bc04efb",
6+
"repl": ""
7+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import unittest
2+
import sys
3+
from io import StringIO
4+
from main import *
5+
6+
class UserDefinedFunctionsTests(unittest.TestCase):
7+
def test_main(self):
8+
# Make sure there is no return statement.
9+
self.assertIsNone(say_hello())
10+
11+
def test_function_name(self):
12+
# Make sure that the function has the correct name.
13+
f = open('main.py')
14+
lines = str(f.readlines())
15+
f.close()
16+
self.assertRegex(lines, 'say_hello()', msg="The name of the function is incorrect.")
17+
18+
def test_output(self):
19+
# Make sure that calling say_hello() outputs the correct string.
20+
saved_stdout = sys.stdout
21+
try:
22+
out = StringIO()
23+
sys.stdout = out
24+
say_hello()
25+
output = out.getvalue().strip()
26+
assert output == "Hello World!"
27+
finally:
28+
sys.stdout = saved_stdout
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
### Define the say_hello() function and fill in the function body below this line: ###
2+
3+
4+
5+
### Call the say_hello() function below this line: ###
6+
say_hello()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## Function Documentation Strings
2+
3+
Function documentation strings (docstrings) offer a way to provide additional information about a function and its properties.
4+
A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition.
5+
Such a docstring becomes the \_\_doc\_\_ special attribute of that object.
6+
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).
7+
Optional arguments should be indicated.
8+
There are two forms of docstrings: one-liners and multi-line docstrings.
9+
For consistency, always use """triple double quotes""" around docstrings.
10+
11+
- https://docs.python.org/3/tutorial/controlflow.html#documentation-strings
12+
- https://www.python.org/dev/peps/pep-0257/
13+
```
14+
>>> def some_function():
15+
... """
16+
... This function does nothing, but it's documented.
17+
...
18+
... Multiline docstrings are for more detailed descriptions.
19+
... """
20+
... pass
21+
...
22+
>>> print(some_function.__doc__)
23+
24+
This function does nothing, but it's documented.
25+
26+
Multiline docstrings are for more detailed descriptions.
27+
```
28+
29+
**_Instructions:_**
30+
**In the console you will find a function named docstring\_function().**
31+
**In the space between the function definition and the `pass` keyword, write any string you like using the docstring triple-double quotes convention.**
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"lesson_title": "Function Documentation Strings",
3+
"chapter_number": 8,
4+
"lesson_number": 3,
5+
"id": "595ca194ddca05761bc04efc",
6+
"repl": ""
7+
}

0 commit comments

Comments
 (0)