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
-
It supports multiple programming paradigms including both imperative (_object-oriented, procedural_) and declarative (_functional, concurrent_) flavors.
6
-
But do not be fooled: while programming across paradigms is fully _supported_, [everything in Python is an object][everythings an object].
5
+
Imperative, declarative (e.g., functional), and object-oriented programming _styles_ are all supported, but internally [everything in Python is an object][everythings an object].
6
+
7
+
Python puts a strong emphasis on code readability and (_similar to Haskell_) uses [significant indentation][significant indentation] to denote function, method, and class definitions.
8
+
9
+
10
+
The [zen of Python (PEP 20)][the zen of python] and [What is Pythonic?][what is pythonic] lay out additional philosophies
7
11
8
12
Python was created by Guido van Rossum and first released in 1991. The [Python Software Foundation][psf] manages and directs resources for Python and CPython development and receives proposals for changes to the language from [members][psf membership] of the community via [Python Enhancement Proposals or PEPs][peps].
9
13
10
-
Python puts a strong emphasis on code readability and (_similar to Haskell_) uses [significant indentation][significant indentation] to denote function, method, and class definitions.
11
-
The [zen of Python (PEP 20)][the zen of python] and [What is Pythonic?][what is pythonic] lay out additional philosophies.
12
14
13
15
Complete documentation for the current release can be found at [docs.python.org][python docs].
14
16
@@ -19,59 +21,110 @@ Complete documentation for the current release can be found at [docs.python.org]
19
21
-[Python FAQs][python faqs]
20
22
-[Python Glossary of Terms][python glossary of terms]
21
23
24
+
This concept introduces 4 major Python language features: Name Assignment (_variables and constants_), Functions (_and the return keyword_), Comments, and Docstrings.
25
+
26
+
27
+
~~~~exercism/note
22
28
23
-
## Getting Started
29
+
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.
24
30
25
-
Objects are [assigned][assignment statements] to [names][naming and binding] in Python via the `=` or _assignment operator_. [Variables][variables] are written in [`snake_case`][snake case], and constants usually in `SCREAMING_SNAKE_CASE`.
31
+
~~~~
26
32
27
-
A `name` (_variable or constant_) is not itself typed, and can be attached or re-attached to different objects or values over its lifetime.
28
-
For extended naming conventions and formatting advice, see [PEP 8][pep8].
33
+
34
+
## Name Assignment and Re-assignment
35
+
36
+
In Python, there are no keywords to define variables or constants.
37
+
Both are [_names_][facts-and-myths-about-python-names] that help programmers reference values (_objects_) in a program and are written differently only by convention.
38
+
On Exercism, [variables][variables] are always written in [`snake_case`][snake case], and _constants_ in `SCREAMING_SNAKE_CASE`.
39
+
40
+
Names are assigned to values using `=`, or the [_assignment operator_][assignment statements] (`<name> = <value>`).
41
+
A name (_variable or constant_) can be re-assigned over its lifetime to different values/object types.
42
+
43
+
For example, `my_first_variable` can be re-assigned many times using `=`, and can refer to different object types with each re-assignment:
29
44
30
45
```python
46
+
# Assigning my_first_variable to a numeric value.
31
47
>>> my_first_variable =1
32
-
>>> my_first_variable ="Last one, I promise"
48
+
>>>print(type(my_first_variable))
49
+
<class'int'>
50
+
33
51
>>>print(my_first_variable)
52
+
1
53
+
54
+
# Reassigning my_first_variable to a new string value.
55
+
>>> my_first_variable ="Now, I'm a string."
56
+
>>>print(type(my_first_variable))
57
+
<class'str'>
34
58
35
-
"Last one, I promise"
59
+
>>>print(my_first_variable)
60
+
"Now, I'm a string."
36
61
```
37
62
38
-
Constants are usually defined on a [module][module] or `global` level, and although they _can_ be changed, they are _intended_ to be assigned only once.
39
63
40
-
Their `SCREAMING_SNAKE_CASE` is a message to other developers that the assignment should not be altered.
64
+
### Constants
65
+
66
+
Constants are typically defined at a [module][module] level, being values that are accessible outside function or class scope.
67
+
Constant names **_can be reassigned to new values_**, but they are _intended_ to be named only once.
68
+
Using `SCREAMING_SNAKE_CASE` warns other programmers that these names should not be mutated or reassigned.
41
69
42
70
```python
43
-
# All caps signal that this is intended as a constant
71
+
# All caps signal that this is intended as a constant.
44
72
MY_FIRST_CONSTANT=16
45
73
46
74
# Re-assignment will be allowed by the compiler & interpreter,
47
-
# but is VERY strongly discouraged.
48
-
# Please don't do: MY_FIRST_CONSTANT = "Some other value"
75
+
# but this is VERY strongly discouraged.
76
+
# Please don't: MY_FIRST_CONSTANT = "Some other value"
49
77
```
50
78
79
+
80
+
## Functions
81
+
51
82
In Python, units of functionality are encapsulated in [_functions._][functions], which are themselves [objects][objects] (_it's [turtles all the way down][turtles all the way down]_).
52
83
53
84
Functions can be executed by themselves, passed as arguments to other functions, nested, or bound to a class.
54
85
When functions are bound to a [class][classes] name, they're referred to as [methods][method objects].
55
86
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.
56
87
57
88
The keyword `def` begins a [function definition][function definition].
58
-
`def` must be followed by the function name and a parenthesized list of zero or more formal [parameters][parameters].
59
-
Parameters can be of several different varieties, and can even [vary][more on functions] in length.
60
-
The `def` line is terminated with a colon (`:`).
89
+
It must be followed by the function name and a parenthesized list of zero or more formal [parameters][parameters].
90
+
Parameters can be of several different varieties, and can even [vary][more on functions] in length.
91
+
92
+
The `def` line is terminated with a colon (`:`):
93
+
94
+
```python
95
+
# Function definition.
96
+
defmy_function_name(parameter, second_parameter):
97
+
<function body>
98
+
99
+
```
100
+
101
+
Statements for the `function body` begin on the line following `def` and must be _indented in a block_.
102
+
There is no strict indentation amount (_either space **OR**[tab] characters are acceptable_), but [indentation][indentation] must be _consistent_ for all indented statements.
103
+
104
+
105
+
```python
106
+
# Function definition on first line.
107
+
defadd_two_numbers(number_one, number_two):
108
+
print(number_one + number_two) # Prints the sum of the numbers, and is indented by 2 spaces.
109
+
110
+
>>> add_two_numbers(3, 4)
111
+
7
112
+
```
113
+
61
114
62
-
Statements for the `function body` begin on the line following `def`, and must be _indented in a block_.
63
-
There is no strict indentation amount (_either space **OR**[tab] characters are acceptable_), but [indentation][indentation] must be _consistent for all indented statements_.
64
115
Functions explicitly return a value or object via the [`return`][return] keyword.
65
116
66
117
```python
67
118
# Function definition on first line.
68
-
>>>defadd_two_numbers(number_one, number_two):
69
-
...return number_one + number_two # Returns the sum of the numbers, and is indented by 2 spaces.
119
+
defadd_two_numbers(number_one, number_two):
120
+
return number_one + number_two # Returns the sum of the numbers.
70
121
71
122
>>> add_two_numbers(3, 4)
72
123
7
73
124
```
74
125
126
+
127
+
75
128
Functions that do not have an explicit `return` expression will return [`None`][none].
... result = number_one + number_two + number_three # Indented by 4 spaces.
92
145
...return result #this was only indented by 3 spaces
146
+
...
147
+
...
93
148
File "<stdin>", line 3
94
149
return result
95
150
^
96
151
IndentationError: unindent does not match any outer indentation level
97
152
```
98
153
154
+
155
+
### Calling Functions
156
+
99
157
Functions are [_called_][calls] using their name followed by `()`.
100
158
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.
# Because there was a default value, this call with only one argument does not throw an error.
145
199
>>> number_to_the_power_of_default(4)
146
200
16
147
201
```
148
202
149
-
Methods bound to class names are invoked via dot notation (`<class_name>.<method_name>()`), as are functions, constants, or global names imported as part of a module.:
203
+
204
+
Methods bound to class names are invoked via dot notation (`<class name>.<method name>(<parameters>))`, as are functions (`<module name>.<function name>(<parameters>)`), constants (`<module name>.<constant name>`), or any other global names imported as part of a module.:
150
205
151
206
```python
207
+
# This is an example of a method call of the built in str class.
208
+
# Define a variable and assign it to a string.
209
+
>>> start_text ="my silly sentence for examples."
210
+
211
+
# Uppercase the string by calling the upper method from the str class.
212
+
>>>str.upper(start_text)
213
+
"MY SILLY SENTENCE FOR EXAMPLES."
214
+
215
+
216
+
# Below is an example of a method call of the built in list class.
217
+
# Define an empty list
218
+
>>> my_list = []
219
+
220
+
# Add an element to the list by calling the append method from the list class.
221
+
>>> my_list.append(start_text)
222
+
>>>print(my_list)
223
+
["my silly sentence for examples."]
152
224
153
225
import string
154
226
155
227
# This is a constant provided by the *string* module.
156
-
>>>print(string.ascii_lowercase)
228
+
>>> alphabet = string.ascii_lowercase
229
+
>>>print(alphabet)
157
230
"abcdefghijklmnopqrstuvwxyz"
231
+
```
158
232
159
-
# This is a method call of the str *class*.
160
-
>>> start_text ="my silly sentence for examples."
161
-
>>>str.upper(start_text)
162
-
"MY SILLY SENTENCE FOR EXAMPLES."
163
233
164
-
# This is a method call of an *instance* of the str *class*.
165
-
>>> start_text.upper()
166
-
"MY SILLY SENTENCE FOR EXAMPLES."
167
-
```
234
+
## Comments
168
235
169
236
[Comments][comments] in Python start with a `#` that is not part of a string, and end at line termination.
170
-
Unlike many other programming languages, Python does not support multi-line comment marks.
237
+
Unlike many other programming languages, Python **does not support** multi-line comment marks.
171
238
Each line of a comment block must start with the `#` character.
239
+
172
240
Comments are ignored by the interpreter:
173
241
242
+
174
243
```python
175
244
# This is a single line comment.
176
245
@@ -181,25 +250,53 @@ x = "foo" # This is an in-line comment.
181
250
# these should be used sparingly.
182
251
```
183
252
253
+
254
+
## Docstrings
255
+
184
256
The first statement of a function body can optionally be a [_docstring_][docstring], which concisely summarizes the function or object's purpose.
185
-
Docstrings are read by automated documentation tools and are returned by calling `.__doc__` on the function, method, or class name.
186
-
They are recommended for programs of any size where documentation is needed, and their conventions are laid out in [PEP257][PEP257]:
257
+
Docstrings are declared using triple double quotes (""") indented at the same level as the code block:
:param number_two: int the power to raise the base number to.
196
-
:return: int - number raised to power of second number
197
-
198
-
Takes number_one and raises it to the power of number_two, returning the result.
261
+
262
+
# An example from PEP257 of a multi-line docstring.
263
+
defcomplex(real=0.0, imag=0.0):
264
+
"""Form a complex number.
265
+
266
+
Keyword arguments:
267
+
real -- the real part (default 0.0)
268
+
imag -- the imaginary part (default 0.0)
199
269
"""
200
270
201
-
return number_one ** number_two
271
+
if imag ==0.0and real ==0.0:
272
+
return complex_zero
273
+
274
+
```
275
+
276
+
277
+
Docstrings are read by automated documentation tools and are returned by calling the special attribute `.__doc__` on the function, method, or class name.
278
+
They are recommended for programs of any size where documentation is needed, and their conventions are laid out in [PEP257][pep257].
279
+
280
+
Docstrings can also function as [lightweight unit tests][doctests], which can be read and run by PyTest, or by importing the `doctest` module.
281
+
Testing and `doctest` will be covered in a later concept.
0 commit comments