You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[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.
4
4
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].
6
6
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.
9
8
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`.
12
9
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:
15
27
16
28
```python
29
+
# Assigning my_first_variable to a numeric value.
17
30
>>> my_first_variable =1
18
-
>>> my_first_variable ="Last one, I promise"
31
+
>>>print(type(my_first_variable))
32
+
...
33
+
<class'int'>
34
+
19
35
>>>print(my_first_variable)
20
36
...
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."
22
48
```
23
49
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.
25
50
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
+
27
57
28
58
```python
29
59
# All caps signal that this is intended as a constant.
30
60
MY_FIRST_CONSTANT=16
31
61
32
62
# Re-assignment will be allowed by the compiler & interpreter,
33
63
# 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"
35
65
```
36
66
67
+
68
+
## Functions
69
+
37
70
The keyword `def` begins a [function definition][function definition].
38
71
It must be followed by the function name and a parenthesized list of zero or more formal [parameters][parameters].
39
72
Parameters can be of several different varieties, and can even [vary][more on functions] in length.
40
73
The `def` line is terminated with a colon.
41
74
75
+
```python
76
+
# function definition
77
+
defmy_function_name(parameter, second_parameter):
78
+
<function body>
79
+
80
+
```
81
+
42
82
Statements for the _body_ of the function begin on the line following `def` and must be _indented in a block_.
43
83
There is no strict indentation amount (_either space **OR**[tab] characters are acceptable_), but [indentation][indentation] must be _consistent for all indented statements_.
44
84
85
+
86
+
```python
87
+
# Function definition on first line.
88
+
defadd_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
+
45
95
Functions explicitly return a value or object via the [`return`][return] keyword.
46
96
47
97
```python
48
98
# Function definition on first line.
49
99
defadd_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.
... result = number_one + number_two + number_three # Indented by 4 spaces.
73
123
...return result #this was only indented by 3 spaces
124
+
...
125
+
...
74
126
File "<stdin>", line 3
75
127
return result
76
128
^
77
129
IndentationError: unindent does not match any outer indentation level
78
130
```
79
131
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..
: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."
126
163
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."
131
167
132
-
Methods bound to class names are invoked via dot notation (.), as are functions, constants, or global names imported as part of a module.:
133
168
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 = []
135
172
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
+
```
137
178
138
-
# This is a constant provided by the *string* module.
139
-
>>>print(string.ascii_lowercase)
140
-
"abcdefghijklmnopqrstuvwxyz"
141
179
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
146
181
147
-
# This is a method call of an *instance* of the str *class*.
148
-
>>> start_text.upper()
149
-
"MY SILLY SENTENCE FOR EXAMPLES."
150
-
```
151
182
152
183
[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.
154
185
Each line of a comment block must start with the `#` character.
155
186
156
187
Comments are ignored by the interpreter:
@@ -165,10 +196,33 @@ x = "foo" # This is an in-line comment.
165
196
# these should be used sparingly.
166
197
```
167
198
199
+
## Docstrings
200
+
168
201
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
+
defcomplex(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.0and 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
+
172
226
173
227
```python
174
228
# An example on a user-defined function.
@@ -185,6 +239,7 @@ They are recommended for programs of any size where documentation is needed, and
185
239
return number_one ** number_two
186
240
...
187
241
242
+
# Calling the .__doc__ attribute of the function and printing the result.
188
243
>>>print(number_to_the_power_of.__doc__)
189
244
Raise a number to an arbitrary power.
190
245
@@ -194,7 +249,7 @@ Raise a number to an arbitrary power.
194
249
195
250
Takes number_one and raises it to the power of number_two, returning the result.
196
251
197
-
# __doc__() for the built-in type: str.
252
+
#Printing the __doc__ attribute for the built-in type: str.
198
253
>>>print(str.__doc__)
199
254
str(object='') ->str
200
255
str(bytes_or_buffer[, encoding[, errors]]) ->str
@@ -212,27 +267,21 @@ errors defaults to 'strict'.
0 commit comments