Skip to content

Commit 2f73fb3

Browse files
committed
Additional edits and adjustments to introduction and related files for clarity.
1 parent 27fa704 commit 2f73fb3

File tree

4 files changed

+165
-87
lines changed

4 files changed

+165
-87
lines changed

exercises/concept/guidos-gorgeous-lasagna/.docs/hints.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- [The Python Tutorial][the python tutorial] can be a great introduction.
66
- [Numbers][numbers] in Python can be integers, floats, or complex.
7+
- [PEP 8][PEP8] is the Python code style guide.
78

89
## 1. Define expected bake time in minutes
910

@@ -33,13 +34,13 @@
3334

3435
- Clearly [commenting][comments] and [documenting][docstrings] your code according to [PEP257][PEP257] is always recommended.
3536

36-
[the python tutorial]: https://docs.python.org/3/tutorial/introduction.html
37-
[numbers]: https://docs.python.org/3/tutorial/introduction.html#numbers
38-
[naming]: https://realpython.com/python-variables/
37+
[PEP257]: https://www.python.org/dev/peps/pep-0257/
3938
[assignment]: https://docs.python.org/3/reference/simple_stmts.html#grammar-token-assignment-stmt
40-
[defining functions]: https://docs.python.org/3/tutorial/controlflow.html#defining-functions
41-
[return]: https://docs.python.org/3/reference/simple_stmts.html#return
42-
[python as a calculator]: https://docs.python.org/3/tutorial/introduction.html#using-python-as-a-calculator
4339
[comments]: https://realpython.com/python-comments-guide/
40+
[defining functions]: https://docs.python.org/3/tutorial/controlflow.html#defining-functions
4441
[docstrings]: https://docs.python.org/3/tutorial/controlflow.html#tut-docstrings
45-
[PEP257]: https://www.python.org/dev/peps/pep-0257/
42+
[naming]: https://realpython.com/python-variables/
43+
[numbers]: https://docs.python.org/3/tutorial/introduction.html#numbers
44+
[python as a calculator]: https://docs.python.org/3/tutorial/introduction.html#using-python-as-a-calculator
45+
[return]: https://docs.python.org/3/reference/simple_stmts.html#return
46+
[the python tutorial]: https://docs.python.org/3/tutorial/introduction.html

exercises/concept/guidos-gorgeous-lasagna/.docs/instructions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ This function should return the total number of minutes you've been cooking, or
4949

5050
## 5. Update the recipe with notes
5151

52-
Go back through the recipe, adding notes and documentation.
52+
Go back through the recipe, adding notes in the form of function docstrings.
5353

5454
```python
5555
def elapsed_time_in_minutes(number_of_layers, elapsed_bake_time):

exercises/concept/guidos-gorgeous-lasagna/.docs/introduction.md

Lines changed: 119 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,103 @@
11
# Introduction
22

3-
[Python][python docs] is a [dynamic and strongly][dynamic typing in python] typed [object-oriented][object oriented programming] programming language.
3+
Python is a [dynamic and strongly][dynamic typing in python] typed [object-oriented][object oriented programming] programming language.
44
It employs both [duck typing][duck typing] and [gradual typing][gradual typing], via [type hints][type hints].
5-
Programming across paradigms is fully _supported_ -- but internally, [everything in Python is an object][everythings an object].
5+
Python supports Imperative, declarative (e.g. functional), and object oriented programming _styles_, but internally [everything in Python is an object][everythings an object].
66

7-
Python puts a strong emphasis on code readability and (_similar to Haskell_) uses [significant indentation][significant indentation] for function, method, and class definitions.
8-
[The Zen of Python (PEP 20)][the zen of python] and [_What is Pythonic?_][what is pythonic] lay out additional philosophies.
7+
This exercise introduces 4 major Python language features: Names (_variables and constants_), Functions (_and the return keyword_), Comments, and Docstrings.
98

10-
Objects are [assigned][assignment statements] to [names][naming and binding] via the _assignment operator_, `=`.
11-
[Variables][variables] are written in [`snake_case`][snake case], and _constants_ usually in `SCREAMING_SNAKE_CASE`.
129

13-
A `name` (_variable or constant_) is not itself typed, and can be attached or re-attached to different objects over its lifetime.
14-
For extended naming conventions and advice, see [PEP 8][pep8].
10+
~~~~exercism/note
11+
12+
In general, content, tests, and analyzer tooling for the Python track follow the style conventions outlined in [PEP 8](https://www.python.org/dev/peps/pep-0008/) and [PEP 257](https://www.python.org/dev/peps/pep-0257/) for Python code style, with the additional (strong) suggestion that there be no single letter variable names.
13+
14+
~~~~
15+
16+
17+
## Name Assignment and Re-assignment
18+
19+
20+
There are no keywords in Python to define variables or constants and there is no difference in the way Python treats them.
21+
Both are considered [_names_][facts-and-myths-about-python-names] that help programmers reference values (_objects_) in a program and are written differently only by convention.
22+
On Exercism, [variables][variables] are always written in [`snake_case`][snake case], and _constants_ in `SCREAMING_SNAKE_CASE`.
23+
24+
Names are assigned to values via `=`, or the [_assignment operator_][assignment statements]: `<name> = <value>`.
25+
A name (_variable or constant_) can be assigned or re-assigned over its lifetime to different values/different object types.
26+
For example, `my_first_variable` can be assigned and re-assigned many times using `=`, and can refer to different object types with each re-assignment:
1527

1628
```python
29+
# Assigning my_first_variable to a numeric value.
1730
>>> my_first_variable = 1
18-
>>> my_first_variable = "Last one, I promise"
31+
>>> print(type(my_first_variable))
32+
...
33+
<class 'int'>
34+
1935
>>> print(my_first_variable)
2036
...
21-
"Last one, I promise"
37+
1
38+
39+
# Reassigning my_first_variable to a new string value.
40+
>>> my_first_variable = "Now, I'm a string."
41+
>>> print(type(my_first_variable))
42+
...
43+
<class 'str'>
44+
45+
>>> print(my_first_variable)
46+
...
47+
"Now, I'm a string."
2248
```
2349

24-
Constants are typically defined on a [module][module] or _global_ level, and although they _can_ be changed, they are _intended_ to be named only once.
2550

26-
Their `SCREAMING_SNAKE_CASE` is a message to other developers that the assignment should not be altered:
51+
### Constants
52+
53+
Constants are typically defined at a [module][module] level, being values that are accessible outside function or class scope.
54+
Constant names **_can be reassigned to new values_**, but they are _intended_ to be named only once.
55+
Using `SCREAMING_SNAKE_CASE` warns other programmers that these names should not be mutated or reassigned.
56+
2757

2858
```python
2959
# All caps signal that this is intended as a constant.
3060
MY_FIRST_CONSTANT = 16
3161

3262
# Re-assignment will be allowed by the compiler & interpreter,
3363
# but this is VERY strongly discouraged.
34-
# Please don't do: MY_FIRST_CONSTANT = "Some other value"
64+
# Please don't: MY_FIRST_CONSTANT = "Some other value"
3565
```
3666

67+
68+
## Functions
69+
3770
The keyword `def` begins a [function definition][function definition].
3871
It must be followed by the function name and a parenthesized list of zero or more formal [parameters][parameters].
3972
Parameters can be of several different varieties, and can even [vary][more on functions] in length.
4073
The `def` line is terminated with a colon.
4174

75+
```python
76+
# function definition
77+
def my_function_name(parameter, second_parameter):
78+
<function body>
79+
80+
```
81+
4282
Statements for the _body_ of the function begin on the line following `def` and must be _indented in a block_.
4383
There is no strict indentation amount (_either space **OR** [tab] characters are acceptable_), but [indentation][indentation] must be _consistent for all indented statements_.
4484

85+
86+
```python
87+
# Function definition on first line.
88+
def add_two_numbers(number_one, number_two):
89+
print(number_one + number_two) # Prints the sum of the numbers, and is indented by 2 spaces.
90+
91+
>>> add_two_numbers(3, 4)
92+
7
93+
```
94+
4595
Functions explicitly return a value or object via the [`return`][return] keyword.
4696

4797
```python
4898
# Function definition on first line.
4999
def add_two_numbers(number_one, number_two):
50-
return number_one + number_two # Returns the sum of the numbers, and is indented by 2 spaces.
100+
return number_one + number_two # Returns the sum of the numbers.
51101

52102
>>> add_two_numbers(3, 4)
53103
7
@@ -64,41 +114,36 @@ def add_two_numbers(number_one, number_two):
64114
None
65115
```
66116

67-
Inconsistent indentation will raise an error:
117+
While you may choose any indentation depth, _inconsistent_ indentation in your code blocks will raise an error:
68118

69119
```python
70120
# The return statement line does not match the first line indent.
71121
>>> def add_three_numbers_misformatted(number_one, number_two, number_three):
72122
... result = number_one + number_two + number_three # Indented by 4 spaces.
73123
... return result #this was only indented by 3 spaces
124+
...
125+
...
74126
File "<stdin>", line 3
75127
return result
76128
^
77129
IndentationError: unindent does not match any outer indentation level
78130
```
79131

80-
Functions are [_called_][calls] using their name followed by `()`.
81-
The number of arguments passed in the parentheses must match the number of parameters in the original function definition unless [default arguments][default arguments] have been used:
132+
### Calling Functions
133+
134+
Functions are [_called_][calls] or invoked using their name followed by `()`.
135+
The number of arguments passed in the parentheses must match the number of parameters in the original function definition..
82136

83137
```python
84-
>>> def number_to_the_power_of(number_one, number_two):
85-
"""Raise a number to an arbitrary power.
86-
87-
:param number_one: int the base number.
88-
:param number_two: int the power to raise the base number to.
89-
:return: int - number raised to power of second number
90-
91-
Takes number_one and raises it to the power of number_two, returning the result.
92-
"""
93-
138+
>>> def number_to_the_power_of(number_one, number_two):
94139
return number_one ** number_two
95140
...
96141

97142
>>> number_to_the_power_of(3,3)
98143
27
99144
```
100145

101-
A mis-match between parameters and arguments will raise an error:
146+
A mis-match between the number of parameters and the number of arguments will raise an error:
102147

103148
```python
104149
>>> number_to_the_power_of(4,)
@@ -109,48 +154,34 @@ TypeError: number_to_the_power_of() missing 1 required positional argument: 'num
109154

110155
```
111156

112-
Adding a [default value][default arguments] for a parameter can defend against such errors:
157+
Calling functions defined inside a class (_class methods_) use `<class name>.<method name>(<parameters>)`, otherwise known as dot (.) notation:
113158

114159
```python
115-
>>> def number_to_the_power_of_default(number_one, number_two=2):
116-
"""Raise a number to an arbitrary power.
117-
118-
:param number_one: int the base number.
119-
:param number_two: int the power to raise the base number to.
120-
:return: int - number raised to power of second number
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
160+
# This is an example of a method call of the built in str class.
161+
# Define a variable and assign it to a string.
162+
>>> start_text = "my silly sentence for examples."
126163

127-
...
128-
>>> number_to_the_power_of_default(4)
129-
16
130-
```
164+
# Uppercase the string by calling the upper method from the str class.
165+
>>> str.upper(start_text)
166+
"MY SILLY SENTENCE FOR EXAMPLES."
131167

132-
Methods bound to class names are invoked via dot notation (.), as are functions, constants, or global names imported as part of a module.:
133168

134-
```python
169+
# Below is an example of a method call of the built in list class.
170+
# Define an empty list
171+
>>> my_list = []
135172

136-
import string
173+
# Add an element to the list by calling the append method from the list class.
174+
>>> my_list.append(start_text)
175+
>>> print(my_list)
176+
["my silly sentence for examples."]
177+
```
137178

138-
# This is a constant provided by the *string* module.
139-
>>> print(string.ascii_lowercase)
140-
"abcdefghijklmnopqrstuvwxyz"
141179

142-
# This is a method call of the str *class*.
143-
>>> start_text = "my silly sentence for examples."
144-
>>> str.upper(start_text)
145-
"MY SILLY SENTENCE FOR EXAMPLES."
180+
## Comments
146181

147-
# This is a method call of an *instance* of the str *class*.
148-
>>> start_text.upper()
149-
"MY SILLY SENTENCE FOR EXAMPLES."
150-
```
151182

152183
[Comments][comments] in Python start with a `#` that is not part of a string, and end at line termination.
153-
Unlike many other programming languages, Python does not support multi-line comment marks.
184+
Unlike many other programming languages, Python **does not support** multi-line comment marks.
154185
Each line of a comment block must start with the `#` character.
155186

156187
Comments are ignored by the interpreter:
@@ -165,10 +196,33 @@ x = "foo" # This is an in-line comment.
165196
# these should be used sparingly.
166197
```
167198

199+
## Docstrings
200+
168201
The first statement of a function body can optionally be a [_docstring_][docstring], which concisely summarizes the function or object's purpose.
169-
Docstrings are read by automated documentation tools and are returned by calling `.__doc__` on the function, method, or class name.
170-
They can also function as [lightweight unit tests][doctests], which will be covered in a later exercise.
171-
They are recommended for programs of any size where documentation is needed, and their conventions are laid out in [PEP257][PEP257]:
202+
Docstrings are declared using triple double quotes (""") indented at the same level as the code block:
203+
204+
205+
```python
206+
207+
# An example from PEP257 of a multi-line docstring.
208+
def complex(real=0.0, imag=0.0):
209+
"""Form a complex number.
210+
211+
Keyword arguments:
212+
real -- the real part (default 0.0)
213+
imag -- the imaginary part (default 0.0)
214+
"""
215+
216+
if imag == 0.0 and real == 0.0:
217+
return complex_zero
218+
...
219+
220+
```
221+
222+
Docstrings are read by automated documentation tools and are returned by calling the special attribute `.__doc__` on the function, method, or class name.
223+
Docstrings can also function as [lightweight unit tests][doctests], which will be covered in a later exercise.
224+
They are recommended for programs of any size where documentation is needed, and their conventions are laid out in [PEP257][PEP257].
225+
172226

173227
```python
174228
# An example on a user-defined function.
@@ -185,6 +239,7 @@ They are recommended for programs of any size where documentation is needed, and
185239
return number_one ** number_two
186240
...
187241

242+
# Calling the .__doc__ attribute of the function and printing the result.
188243
>>> print(number_to_the_power_of.__doc__)
189244
Raise a number to an arbitrary power.
190245

@@ -194,7 +249,7 @@ Raise a number to an arbitrary power.
194249

195250
Takes number_one and raises it to the power of number_two, returning the result.
196251

197-
# __doc__() for the built-in type: str.
252+
# Printing the __doc__ attribute for the built-in type: str.
198253
>>> print(str.__doc__)
199254
str(object='') -> str
200255
str(bytes_or_buffer[, encoding[, errors]]) -> str
@@ -212,27 +267,21 @@ errors defaults to 'strict'.
212267
[assignment statements]: https://docs.python.org/3/reference/simple_stmts.html#assignment-statements
213268
[calls]: https://docs.python.org/3/reference/expressions.html#calls
214269
[comments]: https://realpython.com/python-comments-guide/#python-commenting-basics
215-
[default arguments]: https://docs.python.org/3/tutorial/controlflow.html#default-argument-values
216270
[docstring]: https://docs.python.org/3/tutorial/controlflow.html#tut-docstrings
217271
[doctests]: https://docs.python.org/3/library/doctest.html
218272
[duck typing]: https://en.wikipedia.org/wiki/Duck_typing
219273
[dynamic typing in python]: https://stackoverflow.com/questions/11328920/is-python-strongly-typed
220274
[everythings an object]: https://docs.python.org/3/reference/datamodel.html
275+
[facts-and-myths-about-python-names]: https://nedbatchelder.com/text/names.html
221276
[function definition]: https://docs.python.org/3/tutorial/controlflow.html#defining-functions
222277
[gradual typing]: https://en.wikipedia.org/wiki/Gradual_typing
223278
[indentation]: https://docs.python.org/3/reference/lexical_analysis.html#indentation
224279
[module]: https://docs.python.org/3/tutorial/modules.html
225280
[more on functions]: https://docs.python.org/3/tutorial/controlflow.html#more-on-defining-functions
226-
[naming and binding]: https://docs.python.org/3/reference/executionmodel.html#naming-and-binding
227281
[none]: https://docs.python.org/3/library/constants.html
228282
[object oriented programming]: https://en.wikipedia.org/wiki/Object-oriented_programming
229283
[parameters]: https://docs.python.org/3/glossary.html#term-parameter
230-
[pep8]: https://www.python.org/dev/peps/pep-0008/
231-
[python docs]: https://docs.python.org/3/
232284
[return]: https://docs.python.org/3/reference/simple_stmts.html#return
233-
[significant indentation]: https://docs.python.org/3/reference/lexical_analysis.html#indentation
234285
[snake case]: https://en.wikipedia.org/wiki/Snake_case
235-
[the zen of python]: https://www.python.org/dev/peps/pep-0020/
236286
[type hints]: https://docs.python.org/3/library/typing.html
237287
[variables]: https://realpython.com/python-variables/
238-
[what is pythonic]: https://blog.startifact.com/posts/older/what-is-pythonic.html

0 commit comments

Comments
 (0)