@@ -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
7475import 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
108109Related 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
110111The ` 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.
112113Statements 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 )
1491507
150151
@@ -155,28 +156,42 @@ def add_two_numbers(number_one, number_two):
15515611
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.
159160The details of ` None ` will be covered in a later exercise.
160161For 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 ))
178180None
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 `()`.
192207Dot (` . ` ) 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.
20021527
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...
206221Traceback (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
217232import 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__ )
291306Raise 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__ )
303318str (object = ' ' ) -> str
304319str (bytes_or_buffer[, encoding[, errors]]) -> str
0 commit comments