Skip to content

Commit 878c242

Browse files
BethanyGiHiDErikSchierboom
authored andcommitted
new concept exercise - basics
* Introduction to Python basics concept exercise. * Intructions to Python basics concept exercise. * Hints file for Python basics concept exercise. * After file for Python basics concept exercise. * Design and config files for Python basics exercise. * Example implementation of Python basics concept exercise. * Stub file and tests for Python basics concept exercise. * Ran prittier. AGAIN. * Update languages/exercises/concept/basics/.docs/after.md Co-authored-by: Jeremy Walker <[email protected]> * Update languages/exercises/concept/basics/.docs/hints.md Co-authored-by: Erik Schierboom <[email protected]> * Update languages/exercises/concept/basics/lasagna.py Co-authored-by: Erik Schierboom <[email protected]> * Update languages/exercises/concept/basics/.docs/introduction.md Co-authored-by: Erik Schierboom <[email protected]> * Update languages/exercises/concept/basics/.docs/introduction.md Co-authored-by: Erik Schierboom <[email protected]> * Update languages/exercises/concept/basics/.docs/after.md Co-authored-by: Erik Schierboom <[email protected]> * Language and link edits per PR suggestions by iHiD, ErikSchierboom, and cmccandless. * Added basics exercise slug and UUID. * Ran Prettier. Co-authored-by: Jeremy Walker <[email protected]> Co-authored-by: Erik Schierboom <[email protected]>
1 parent 963215e commit 878c242

File tree

9 files changed

+454
-0
lines changed

9 files changed

+454
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
[Python](https://docs.python.org/3/) is a [dynamic and strongly](https://stackoverflow.com/questions/11328920/is-python-strongly-typed) typed [object-oriented](https://en.wikipedia.org/wiki/Object-oriented_programming) programming language. As of version 3.5, it employs both [duck typing](https://en.wikipedia.org/wiki/Duck_typing) and [gradual typing](https://en.wikipedia.org/wiki/Gradual_typing), via [type hints](https://docs.python.org/3/library/typing.html). It supports both imperative (_object-oriented, procedural_) and declarative (_functional, concurrent_) programming paradigms. But do not be fooled: while programming in other paradigms is fully _supported_, [everything in Python is an object](https://docs.python.org/3/reference/datamodel.html).
2+
3+
Python was created by Guido van Rossum and first released in 1991. The [Python Software Foundation](https://www.python.org/psf/) manages and directs resources for Python and CPython development and receives proposals for changes to the language from [members](https://www.python.org/psf/membership/) of the community via [Python Enhancement Proposals or PEPs](https://www.python.org/dev/peps/).
4+
5+
Python puts a strong emphasis on code readability and (_similar to Haskell_) uses [significant indentation](https://docs.python.org/3/reference/lexical_analysis.html#indentation) to denote function, method, and class definitions. The [zen of Python (PEP 20)](https://www.python.org/dev/peps/pep-0020/) lays out additional inspiration and philosophy.
6+
7+
Objects are [assigned](https://docs.python.org/3/reference/simple_stmts.html#assignment-statements) to [names](https://docs.python.org/3/reference/executionmodel.html#naming-and-binding) in Python via the `=` or _assignment operator_. [Variables](https://realpython.com/python-variables/) are written in [`snake_case`](https://en.wikipedia.org/wiki/Snake_case), and _constants_ usually in `SCREAMING_SNAKE_CASE`. A name (_variable or constant_) is not itself _typed_, and can be attached or re-attached to different objects over its lifetime. For extended naming conventions and advice, see [PEP 8](https://www.python.org/dev/peps/pep-0008/).
8+
9+
```python
10+
>>> my_first_variable = 1
11+
>>> my_first_variable = "Last one, I promise"
12+
>>> print(my_first_variable)
13+
"Last one, I promise"
14+
```
15+
16+
Constants are usually defined on a [module](https://docs.python.org/3/tutorial/modules.html) or _global_ level, and although they _can_ be changed, they are _intended_ to be named only once. Their `SCREAMING_SNAKE_CASE` is a message to other developers that the assignment should not be altered:
17+
18+
```python
19+
# All caps signal that this is intended as a constant
20+
MY_FIRST_CONSTANT = 16
21+
22+
# Re-assignment will be allowed by the compiler & interpreter,
23+
# but is VERY strongly discouraged.
24+
# Please don't do: MY_FIRST_CONSTANT = "Some other value"
25+
```
26+
27+
In Python, units of functionality are encapsulated in [functions](https://docs.python.org/3/reference/compound_stmts.html#function), which are themselves [objects](https://docs.python.org/3/reference/datamodel.html#the-standard-type-hierarchy) (_Its [turtles all the way down](https://en.wikipedia.org/wiki/Turtles_all_the_way_down)_).
28+
29+
Functions can be executed by themselves, passed as arguments to other functions, nested, or bound to a class. When functions are bound to a [class](https://docs.python.org/3/reference/datamodel.html#classes) name, they're referred to as [methods](https://docs.python.org/3/c-api/method.html#method-objects). Related functions and classes (_with their methods_) can be grouped together in the same file or [module](https://docs.python.org/3/reference/datamodel.html#modules), and imported in part or in whole for use in other programs.
30+
31+
The keyword `def` begins a [function definition](https://docs.python.org/3/tutorial/controlflow.html#defining-functions). It must be followed by the function name and a parenthesized list of zero or more formal [parameters](https://docs.python.org/3/glossary.html#term-parameter), which can be of several different varieties, and even [vary](https://docs.python.org/3/tutorial/controlflow.html#more-on-defining-functions) in length.
32+
33+
The `def` line is terminated with a colon. Statements for the _body_ of the function begin on the next line, and must be _indented in a block_. There is no strict indentation amount (_either space **OR** [tab] characters are acceptable_), but [indentation](https://docs.python.org/3/reference/lexical_analysis.html#indentation) must be _consistent for all indented statements_. Functions explicitly return a value or object via the [`return`](https://docs.python.org/3/reference/simple_stmts.html#return) keyword. Functions that do not have an explicit `return` expression will return [`None`](https://docs.python.org/3/library/constants.html):
34+
35+
```python
36+
#function definition on first line.
37+
def add_two_numbers(number_one, number_two):
38+
return number_one + number_two #returns the sum of the numbers
39+
40+
>>> add_two_numbers(3, 4)
41+
7
42+
43+
#this function has no return keyword, and so will return None
44+
def multiply_two_numbers(number_one, number_two):
45+
result = number_one * number_two
46+
47+
>>> print(multiply_two_numbers(6, 8))
48+
None
49+
```
50+
51+
Functions are [called](https://docs.python.org/3/reference/expressions.html#calls) using their name followed by `()`. The number of arguments passed in the parentheses must match the number of parameters in the original function definition unless [default arguments](https://docs.python.org/3/tutorial/controlflow.html#default-argument-values) have been used:
52+
53+
```python
54+
def number_to_the_power_of(number_one, number_two):
55+
'''Returns float or int.
56+
57+
Takes number_one and raises it to the power of number_two, returning the result.
58+
'''
59+
60+
return number_one ** number_two
61+
62+
>>> number_to_the_power_of(3,3)
63+
27
64+
65+
>>> number_to_the_power_of(4,)
66+
Traceback (most recent call last):
67+
File "<stdin>", line 1, in <module>
68+
TypeError: number_to_the_power_of() missing 1 required positional argument: 'number_two'
69+
70+
...
71+
72+
def number_to_the_power_of_default(number_one, number_two=2):
73+
'''Returns float or int.
74+
75+
Takes number_one and raises it to the power of number_two, returning the result.
76+
'''
77+
78+
return number_one ** number_two
79+
80+
>>> number_to_the_power_of_default(4)
81+
16
82+
```
83+
84+
Methods bound to class names are invoked via _dot notation_ (`.`), as are functions, constants, or global names imported as part of a _module_.:
85+
86+
```python
87+
import string
88+
89+
#this is a constant provided by the *string* module
90+
>>> print(string.ascii_lowercase)
91+
"abcdefghijklmnopqrstuvwxyz"
92+
93+
#this is a method call of the str *class*
94+
>>> start_text = "my silly sentence for examples."
95+
>>> str.upper(start_text)
96+
"MY SILLY SENTENCE FOR EXAMPLES."
97+
98+
#this is a method call of an *instance* of the str *class*
99+
>>> start_text.upper()
100+
"MY SILLY SENTENCE FOR EXAMPLES."
101+
```
102+
103+
[Comments](https://realpython.com/python-comments-guide/#python-commenting-basics) in Python start with a `#` that is not part of a string, and end at line termination. Unlike many other programming languages, Python does not support multi-line comment marks. Each line of a comment block must start with the `#` character. Comments are ignored by the interpreter:
104+
105+
```python
106+
#this is a single line comment
107+
108+
x = "foo" #this is an in-line comment
109+
110+
#this is a multi-line
111+
#comment block over multiple lines
112+
#these should be used sparingly
113+
```
114+
115+
The first statement of a function body can optionally be a [docstring](https://docs.python.org/3/tutorial/controlflow.html#tut-docstrings), which concisely summarizes the function or object's purpose. These docstrings are read by automated documentation tools, and are returned by calling `__doc__` on the function, method, or class. They are recommended for programs of any size where documentation is needed:
116+
117+
```python
118+
#an example on a user-defined function
119+
def number_to_the_power_of(number_one, number_two):
120+
'''Returns float or int.
121+
122+
Takes number_one and raises it to the power of number_two, returning the result.
123+
'''
124+
125+
return number_one ** number_two
126+
127+
>>> print(number_to_the_power_of.__doc__)
128+
Returns float or int.
129+
130+
Takes number_one and raises it to the power of number_two, returning the result.
131+
132+
#an example for a built-in type: str
133+
>>> print(str.__doc__)
134+
str(object='') -> str
135+
str(bytes_or_buffer[, encoding[, errors]]) -> str
136+
137+
Create a new string object from the given object. If encoding or
138+
errors is specified, then the object must expose a data buffer
139+
that will be decoded using the given encoding and error handler.
140+
Otherwise, returns the result of object.__str__() (if defined)
141+
or repr(object).
142+
encoding defaults to sys.getdefaultencoding().
143+
errors defaults to 'strict'.
144+
```
145+
146+
Docstrings can also include [doctests](https://docs.python.org/3/library/doctest.html), which are interactive examples of how a method or function should work. Doctests can be read and run by PyTest, or by importing the `doctest` module.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## General
2+
3+
- [The Python Tutorial](https://docs.python.org/3/tutorial/introduction.html) can be a great introduction.
4+
- [Numbers](https://docs.python.org/3/tutorial/introduction.html#numbers) in Python can be integers, floats, or complex.
5+
6+
## 1. Define expected bake time in minutes
7+
8+
- You need to [name](https://realpython.com/python-variables/) a constant, and [assign](https://docs.python.org/3/reference/simple_stmts.html#grammar-token-assignment-stmt) it an integer value.
9+
10+
## 2. Calculate remaining bake time in minutes
11+
12+
- You need to define a [function](https://docs.python.org/3/tutorial/controlflow.html#defining-functions) with a single parameter representing the time elapsed so far.
13+
- Use the [mathematical operator for subtraction](https://docs.python.org/3/tutorial/introduction.html#numbers) to subtract values.
14+
- This function should [return a value](https://docs.python.org/3/reference/simple_stmts.html#return).
15+
16+
## 3. Calculate preparation time in minutes
17+
18+
- You need to define a [function](https://docs.python.org/3/tutorial/controlflow.html#defining-functions) with a single parameter representing the number of layers.
19+
- Use the [mathematical operator for multiplication](https://docs.python.org/3/tutorial/introduction.html#numbers) to multiply values.
20+
- You could define an extra _constant_ for the time in minutes per layer rather than using a "magic number" in your code.
21+
- This function should [return a value](https://docs.python.org/3/reference/simple_stmts.html#return).
22+
23+
## 4. Calculate total elapsed cooking time (prep + bake) in minutes
24+
25+
- You need to define a [function](https://docs.python.org/3/tutorial/controlflow.html#defining-functions) with two parameters.
26+
- Remember: you can always _call_ a function you've defined previously.
27+
- You can use the [mathematical operator for addition](https://docs.python.org/3/tutorial/introduction) to sum values.
28+
- This function should [return a value](https://docs.python.org/3/reference/simple_stmts.html#return).
29+
30+
## 5. Update the recipe with notes
31+
32+
- Clearly [commenting](https://realpython.com/python-comments-guide/) and [documenting](https://docs.python.org/3/tutorial/controlflow.html#tut-docstrings) your code is always recommended.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
You're going to write some code to help you cook a brilliant lasagna from your favorite cookbook.
2+
3+
You have five tasks, all related to cooking your recipe.
4+
5+
## 1. Define expected bake time in minutes
6+
7+
Define an `EXPECTED_BAKE_TIME` constant that returns how many minutes the lasagna should bake in the oven. According to your cookbook, the Lasagna should be in the oven for 40 minutes:
8+
9+
```python
10+
>>> lasagna.EXPECTED_BAKE_TIME
11+
40
12+
```
13+
14+
## 2. Calculate remaining bake time in minutes
15+
16+
Implement the `bake_time_remaining()` function that takes the actual minutes the lasagna has been in the oven as an argument and returns how many minutes the lasagna still needs to bake based on the `EXPECTED_BAKE_TIME`.
17+
18+
```python
19+
>>> bake_time_remaining(30)
20+
10
21+
```
22+
23+
## 3. Calculate preparation time in minutes
24+
25+
Implement the `preparation_time_in_minutes()` function that takes the number of layers you want to add to the lasagna as an argument and returns how many minutes you would spend making them. Assume each layer takes 2 minutes to prepare.
26+
27+
```python
28+
>>> preparation_time_in_minutes(2)
29+
4
30+
```
31+
32+
## 4. Calculate total elapsed cooking time (prep + bake) in minutes
33+
34+
Implement the `elapsed_time_in_minutes()` function that has two parameters: `number_of_layers` (_the number of layers added to the lasagna_) and `elapsed_bake_time` (_the number of minutes the lasagna has been baking in the oven_). This function should return the total number of minutes you've been cooking, or the sum of your preparation time and the time the lasagna has already spent baking in the oven.
35+
36+
```python
37+
>>> elapsed_time_in_minutes(3, 20)
38+
26
39+
```
40+
41+
## 5. Update the recipe with notes
42+
43+
Go back through the recipe, adding notes and documentation.
44+
45+
```python
46+
def total_time_in_minutes(number_of_layers, actual_bake_time):
47+
'''Return elapsed cooking time.
48+
49+
This function takes two numbers representing the number of layers & the time already spent baking and calculates the total elapsed minutes spent cooking the lasagna.
50+
'''
51+
```
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
Python is a dynamic and strongly typed object-oriented programming language. It supports both imperative (_object-oriented, procedural_) and declarative (_functional, concurrent_) programming paradigms. Python emphasizes code readability and (similar to Haskell) uses significant whitespace.
2+
3+
Objects are assigned to names in Python via the `=` or _assignment operator_. _Variables_ are written in [`snake_case`](https://en.wikipedia.org/wiki/Snake_case), and _constants_ (usually) in `SCREAMING_SNAKE_CASE`. A name (_variable or constant_) is not itself _typed_, and can be attached or re-attached to different objects over its lifetime:
4+
5+
```python
6+
>>> my_first_variable = 1
7+
>>> my_first_variable = "Last one, I promise"
8+
>>> print(my_first_variable)
9+
"Last one, I promise"
10+
```
11+
12+
Constants are usually defined at the top of a script or file, and although they _can_ be changed, they are _intended_ to be named only once. Their `SCREAMING_SNAKE_CASE` is a message to other developers that the assignment should not be altered:
13+
14+
```python
15+
# All caps signal that this is intended as a constant
16+
MY_FIRST_CONSTANT = 16
17+
18+
# Re-assignment will be allowed by the compiler & interpreter,
19+
# but is VERY strongly discouraged.
20+
# Please don't do: MY_FIRST_CONSTANT = "Some other value"
21+
```
22+
23+
In Python, units of functionality are encapsulated in _functions._
24+
25+
The keyword `def` begins a _function definition_, and must be followed by the _function name_ and a parenthesized list of zero or more formal _parameters_. The `def` line is terminated with a colon. Statements for the _body_ of the function begin on the next line, and must be _indented in a block_. There is no strict indentation amount (_either space **OR** [tab] characters are acceptable_), but indentation must be _consistent for all indented statements_. Functions explicitly return a value or object via the `return` keyword:
26+
27+
```python
28+
#function definition on first line.
29+
def add_two_numbers(number_one, number_two):
30+
return number_one + number_two #returns the sum of the numbers, and is indented by 2 spaces.
31+
32+
>>> add_two_numbers(3, 4)
33+
7
34+
35+
#the return statement line does not match the first line indent
36+
>>> def add_three_numbers_misformatted(number_one, number_two, number_three):
37+
... result = number_one + number_two + number_three #indented by 4 spaces
38+
... return result #this was only indented by 3 spaces
39+
File "<stdin>", line 3
40+
return result
41+
^
42+
IndentationError: unindent does not match any outer indentation level
43+
```
44+
45+
Functions are _called_ using their name followed by `()`. The number of arguments passed in the parentheses must match the number of parameters in the original function definition:
46+
47+
```python
48+
def number_to_the_power_of(number_one, number_two):
49+
return number_one ** number_two
50+
51+
>>> number_to_the_power_of(3,3)
52+
27
53+
54+
>>> number_to_the_power_of(4,)
55+
Traceback (most recent call last):
56+
File "<stdin>", line 1, in <module>
57+
TypeError: number_to_the_power_of() missing 1 required positional argument: 'number_two'
58+
```
59+
60+
Comments in Python start with a `#` that is not part of a string, and end at line termination. Unlike many other programming languages, Python does not support multi-line comment marks. Each line of a comment block must start with the `#` character. Comments are ignored by the interpreter:
61+
62+
```python
63+
#this is a single line comment
64+
65+
x = "foo" #this is an in-line comment
66+
67+
#this is a multi-line
68+
#comment block over multiple lines
69+
#these should be used sparingly
70+
```
71+
72+
The first statement of a function body can optionally be a _docstring_, which concisely summarizes the function or object's purpose. These docstrings are read by automated documentation tools and can be accessed from code. They are recommended for programs of any size where documentation is needed.
73+
74+
```python
75+
def number_to_the_power_of(number_one, number_two):
76+
'''Returns float or int.
77+
78+
Takes number_one and raises it to the power of number_two, returning the result.
79+
'''
80+
return number_one ** number_two
81+
82+
>>> print(number_to_the_power_of.__doc__)
83+
Returns float or int.
84+
85+
Takes number_one and raises it to the power of number_two, returning the result.
86+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"authors": [
3+
{
4+
"github_username": "BethanyG",
5+
"exercism_username": "BethanyG"
6+
}
7+
],
8+
"forked_from": ["csharp/basics", "ruby/basics"]
9+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## Goal
2+
3+
The goal of this exercise is to teach the student the basics of programming in Python.
4+
5+
## Learning objectives
6+
7+
- Know what a variable (_name_) is.
8+
- Know how to name a variable.
9+
- Know how to bind and re-bind a _name_ to a value (_assign or re-assign a value to a variable or assign a value to a constant_).
10+
- Know what a `function` is and how to define one via `def`.
11+
- Know how to add one or more `parameters` to a function definition.
12+
- Know what the `return` keyword is, and how to use it to return a value from a function.
13+
- Know how to call a function with zero or more arguments.
14+
- Know how to declare an integer and/or float.
15+
- Know how to use mathematical operators `+` and `*` on integers and/or floats.
16+
- Know how to define single- and multi-line comments.
17+
- Know how to define docstrings.
18+
19+
## Out of scope
20+
21+
- built-in types
22+
- built-in data structures
23+
- Classes
24+
- Lambdas
25+
- Methods and other function usages (_functions bound to classes, functions as arguments, nested functions_).
26+
- Memory and performance characteristics.
27+
- Default function parameters.
28+
- Variadic function parameters. (_\*args and \*\*kwargs_)
29+
30+
## Concepts
31+
32+
`basics`:
33+
34+
- function definition & `return` keyword
35+
- integers & floats,
36+
- mathematical operators `+` and `*`
37+
- single and muliti-line comments.
38+
- updating variable values
39+
- using functions
40+
- variables, variable assignment, & variable naming
41+
42+
## Prerequisites
43+
44+
There are no prerequisites for this exercise.
45+
46+
## Representer
47+
48+
This exercise does not require any specific representation logic to be added to the representer.
49+
50+
## Analyzer
51+
52+
This exercise does not require any specific representation logic to be added to the analyzer.

0 commit comments

Comments
 (0)