Skip to content

Commit 7dcd007

Browse files
authored
Fix minor inconsistencies in introduction.md of guidos-gorgeous-lasagna (#4073)
* Fix minor inconsistencies in introduction.md - Update output to be consistent with the Python interactive-interpreter (e.g. print() prints its argument string without quotes). - Replace "Python terminal" with "Python shell" for accuracy. While informally acceptable, they are technically different (a shell runs inside a terminal). - Change parenthesis to plural form: parentheses - Replace "return expression" with "return statement" for accuracy. * Refine basic concepts after forum discussions * Clarify comment on the call to str.upper()
1 parent 9443eb1 commit 7dcd007

File tree

3 files changed

+115
-72
lines changed

3 files changed

+115
-72
lines changed

concepts/basics/about.md

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,17 @@ For example, `my_first_variable` can be re-assigned many times using `=`, and ca
6868
>>> print(type(my_first_variable))
6969
<class 'str'>
7070

71+
>>> my_first_variable = 'You can call me "str".' # Strings can be declared using single or double quote marks.
7172
>>> print(my_first_variable)
72-
"Now, I'm a string." # Strings can be declared using single or double quote marks.
73+
You can call me "str".
7374

7475
import collections
7576
>>> my_first_variable = collections.Counter([1,1,2,3,3,3,4,5,6,7]) # Now my_first_variable has been re-bound to a Counter object.
7677
>>> print(type(my_first_variable))
7778
<class 'collections.Counter'>
7879

7980
>>> print(my_first_variable)
80-
>>> Counter({3: 3, 1: 2, 2: 1, 4: 1, 5: 1, 6: 1, 7: 1})
81+
Counter({3: 3, 1: 2, 2: 1, 4: 1, 5: 1, 6: 1, 7: 1})
8182
```
8283

8384

@@ -108,7 +109,7 @@ When functions are bound to a [class][classes] name, they're referred to as [met
108109
Related functions and classes (_with their methods_) can be grouped together in the same file or module, and imported in part or in whole for use in other programs.
109110

110111
The `def` keyword begins a [function definition][function definition].
111-
Each function can have zero or more formal [parameters][parameters] in `()` parenthesis, followed by a `:` colon.
112+
Each function can have zero or more formal [parameters][parameters] in `()` parentheses, followed by a `:` colon.
112113
Statements for the _body_ of the function begin on the line following `def` and must be _indented in a block_:
113114

114115

@@ -144,7 +145,7 @@ def add_two_numbers(number_one, number_two):
144145
return number_one + number_two
145146

146147

147-
# Calling the function in the Python terminal returns the sum of the numbers.
148+
# Calling the function in the Python shell returns the sum of the numbers.
148149
>>> add_two_numbers(3, 4)
149150
7
150151

@@ -155,28 +156,42 @@ def add_two_numbers(number_one, number_two):
155156
11
156157
```
157158

158-
Functions that do not have an _explicit_ `return` expression will _implicitly_ return the [`None`][none] object.
159+
Functions that do not have an _explicit_ expression following a `return` will _implicitly_ return the [`None`][none] object.
159160
The details of `None` will be covered in a later exercise.
160161
For the purposes of this exercise and explanation, `None` is a placeholder that represents nothing, or null:
161162

162163

163164
```python
164-
# This function does not have an explicit return.
165-
def add_two_numbers(number_one, number_two):
166-
result = number_one + number_two
167165

166+
# This function will return `None`
167+
def square_a_number(number):
168+
square = number * number
169+
return # <-- note that this return is not followed by an expression
168170

169-
# Calling the function in the Python terminal appears
171+
# Calling the function in the Python shell appears
170172
# to not return anything at all.
171-
>>> add_two_numbers(5, 7)
173+
>>> square_a_number(2)
172174
>>>
173175

174176

175177
# Using print() with the function call shows that
176178
# the function is actually returning the **None** object.
177-
>>> print(add_two_numbers(5, 7))
179+
>>> print(square_a_number(2))
178180
None
181+
```
182+
183+
Functions that omit `return` will also _implicitly_ return the [`None`][none] object.
184+
This means that if you do not use `return` in a function, Python will return the `None` object for you.
185+
186+
```python
187+
188+
# This function omits a return keyword altogether
189+
def add_two_numbers(number_one, number_two):
190+
result = number_one + number_two
179191

192+
>>> add_two_numbers(5, 7)
193+
>>> print(add_two_numbers(5, 7))
194+
None
180195

181196
# Assigning the function call to a variable and printing
182197
# the variable will also show None.
@@ -192,32 +207,32 @@ Functions are [_called_][calls] or invoked using their name followed by `()`.
192207
Dot (`.`) notation is used for calling functions defined inside a class or module.
193208

194209
```python
195-
>>> def number_to_the_power_of(number_one, number_two):
196-
return number_one ** number_two
210+
>>> def raise_to_power(number, power):
211+
return number ** power
197212
...
198213

199-
>>> number_to_the_power_of(3,3) # Invoking the function with the arguments 3 and 3.
214+
>>> raise_to_power(3,3) # Invoking the function with the arguments 3 and 3.
200215
27
201216

202217

203218
# A mis-match between the number of parameters and the number of arguments will raise an error.
204-
>>> number_to_the_power_of(4,)
219+
>>> raise_to_power(4,)
205220
...
206221
Traceback (most recent call last):
207222
File "<stdin>", line 1, in <module>
208-
TypeError: number_to_the_power_of() missing 1 required positional argument: 'number_two'
223+
TypeError: raise_to_power() missing 1 required positional argument: 'power'
209224

210225

211226
# Calling methods or functions in classes and modules.
212227
>>> start_text = "my silly sentence for examples."
213-
>>> str.upper(start_text) # Calling the upper() method for the built-in str class.
214-
"MY SILLY SENTENCE FOR EXAMPLES."
228+
>>> str.upper(start_text) # Calling the upper() method from the built-in str class on start_text.
229+
'MY SILLY SENTENCE FOR EXAMPLES.'
215230

216231
# Importing the math module
217232
import math
218233

219234
>>> math.pow(2,4) # Calling the pow() function from the math module
220-
>>> 16.0
235+
16.0
221236
```
222237

223238

@@ -273,32 +288,32 @@ Testing and `doctest` will be covered in a later concept.
273288

274289
```python
275290
# An example on a user-defined function.
276-
>>> def number_to_the_power_of(number_one, number_two):
291+
>>> def raise_to_power(number, power):
277292
"""Raise a number to an arbitrary power.
278293
279-
:param number_one: int the base number.
280-
:param number_two: int the power to raise the base number to.
281-
:return: int - number raised to power of second number
294+
:param number: int the base number.
295+
:param power: int the power to raise the base number to.
296+
:return: int - number raised to the specified power.
282297
283-
Takes number_one and raises it to the power of number_two, returning the result.
298+
Takes a number and raises it to the specified power, returning the result.
284299
"""
285300

286-
return number_one ** number_two
301+
return number ** power
287302
...
288303

289304
# Calling the .__doc__ attribute of the function and printing the result.
290-
>>> print(number_to_the_power_of.__doc__)
305+
>>> print(raise_to_power.__doc__)
291306
Raise a number to an arbitrary power.
292307

293-
:param number_one: int the base number.
294-
:param number_two: int the power to raise the base number to.
295-
:return: int - number raised to power of second number
308+
:param number: int the base number.
309+
:param power: int the power to raise the base number to.
310+
:return: int - number raised to the specified power.
296311

297-
Takes number_one and raises it to the power of number_two, returning the result.
312+
Takes a number and raises it to the specified power, returning the result.
298313

299314

300315

301-
# Printing the __doc__ attribute for the built-in type: str.
316+
# Printing the __doc__ attribute of the built-in type: str.
302317
>>> print(str.__doc__)
303318
str(object='') -> str
304319
str(bytes_or_buffer[, encoding[, errors]]) -> str

concepts/basics/introduction.md

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ A name can be reassigned (or re-bound) to different values (different object typ
3838
>>> print(type(my_first_variable))
3939
<class 'str'>
4040

41+
>>> my_first_variable = 'You can call me "str".' #<-- Strings can be declared using single or double quote marks.
4142
>>> print(my_first_variable)
42-
"Now, I'm a string." # Strings can be declared using single or double quote marks.
43+
You can call me "str".
4344
```
4445

4546

@@ -54,7 +55,7 @@ Constants should be defined at a [module][module] (file) level, and are typicall
5455
## Functions
5556

5657
The `def` keyword begins a [function definition][function definition].
57-
Each function can have zero or more formal [parameters][parameters] in `()` parenthesis, followed by a `:` colon.
58+
Each function can have zero or more formal [parameters][parameters] in `()` parentheses, followed by a `:` colon.
5859
Statements for the _body_ of the function begin on the line following `def` and must be _indented in a block_.
5960

6061

@@ -90,7 +91,7 @@ def add_two_numbers(number_one, number_two):
9091
return number_one + number_two
9192

9293

93-
# Calling the function in the Python terminal returns the sum of the numbers.
94+
# Calling the function in the Python shell returns the sum of the numbers.
9495
>>> add_two_numbers(3, 4)
9596
7
9697

@@ -102,29 +103,42 @@ def add_two_numbers(number_one, number_two):
102103
```
103104

104105

105-
Functions that do not have an _explicit_ `return` expression will _implicitly_ return the [`None`][none] object.
106-
This means that if you do not use `return` in a function, Python will return the `None` object for you.
106+
Functions that do not have an _explicit_ expression following a `return` will _implicitly_ return the [`None`][none] object.
107107
The details of `None` will be covered in a later exercise.
108108
For the purposes of this exercise and explanation, `None` is a placeholder that represents nothing, or null:
109109

110110

111111
```python
112-
# This function does not have an explicit return.
113-
def add_two_numbers(number_one, number_two):
114-
result = number_one + number_two
115112

113+
# This function will return `None`
114+
def square_a_number(number):
115+
square = number * number
116+
return # <-- note that this return is not followed by an expression
116117

117-
# Calling the function in the Python terminal appears
118+
# Calling the function in the Python shell appears
118119
# to not return anything at all.
119-
>>> add_two_numbers(5, 7)
120+
>>> square_a_number(2)
120121
>>>
121122

122123

123124
# Using print() with the function call shows that
124125
# the function is actually returning the **None** object.
125-
>>> print(add_two_numbers(5, 7))
126+
>>> print(square_a_number(2))
126127
None
128+
```
129+
130+
Functions that omit `return` will also _implicitly_ return the [`None`][none] object.
131+
This means that if you do not use `return` in a function, Python will return the `None` object for you.
127132

133+
```python
134+
135+
# This function omits a return keyword altogether
136+
def add_two_numbers(number_one, number_two):
137+
result = number_one + number_two
138+
139+
>>> add_two_numbers(5, 7)
140+
>>> print(add_two_numbers(5, 7))
141+
None
128142

129143
# Assigning the function call to a variable and printing
130144
# the variable will also show None.

0 commit comments

Comments
 (0)