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
Copy file name to clipboardExpand all lines: exercises/concept/guidos-gorgeous-lasagna/.docs/introduction.md
+20-15Lines changed: 20 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,48 +2,48 @@
2
2
3
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
-
Python supports Imperative, declarative (e.g., functional), and object-oriented programming _styles_, but internally [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
+
This exercise introduces 4 major Python language features: Name Assignment (_variables and constants_), Functions (_and the return keyword_), Comments, and Docstrings.
6
8
7
-
This exercise introduces 4 major Python language features: Names (_variables and constants_), Functions (_and the return keyword_), Comments, and Docstrings.
8
9
9
10
~~~~exercism/note
10
11
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.
12
13
13
14
~~~~
14
15
16
+
15
17
## Name Assignment and Re-assignment
16
18
17
-
There are no keywords in Python to define variables or constants and there is no difference in the way Python treats them.
18
-
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.
19
+
In Python, there are no keywords to define variables or constants.
20
+
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.
19
21
On Exercism, [variables][variables] are always written in [`snake_case`][snake case], and _constants_ in `SCREAMING_SNAKE_CASE`.
20
22
21
-
Names are assigned to values via `=`, or the [_assignment operator_][assignment statements]: `<name> = <value>`.
22
-
A name (_variable or constant_) can be assigned or re-assigned over its lifetime to different values/different object types.
23
-
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:
23
+
Names are assigned to values using `=`, or the [_assignment operator_][assignment statements] (`<name> = <value>`).
24
+
A name (_variable or constant_) can be re-assigned over its lifetime to different values/object types.
25
+
26
+
For example, `my_first_variable` can be re-assigned many times using `=`, and can refer to different object types with each re-assignment:
24
27
25
28
```python
26
29
# Assigning my_first_variable to a numeric value.
27
30
>>> my_first_variable =1
28
31
>>>print(type(my_first_variable))
29
-
...
30
32
<class'int'>
31
33
32
34
>>>print(my_first_variable)
33
-
...
34
35
1
35
36
36
37
# Reassigning my_first_variable to a new string value.
37
38
>>> my_first_variable ="Now, I'm a string."
38
39
>>>print(type(my_first_variable))
39
-
...
40
40
<class'str'>
41
41
42
42
>>>print(my_first_variable)
43
-
...
44
43
"Now, I'm a string."
45
44
```
46
45
46
+
47
47
### Constants
48
48
49
49
Constants are typically defined at a [module][module] level, being values that are accessible outside function or class scope.
@@ -59,23 +59,26 @@ MY_FIRST_CONSTANT = 16
59
59
# Please don't: MY_FIRST_CONSTANT = "Some other value"
60
60
```
61
61
62
+
62
63
## Functions
63
64
64
65
The keyword `def` begins a [function definition][function definition].
65
66
It must be followed by the function name and a parenthesized list of zero or more formal [parameters][parameters].
66
67
Parameters can be of several different varieties, and can even [vary][more on functions] in length.
67
-
The `def` line is terminated with a colon.
68
+
69
+
The `def` line is terminated with a colon:
68
70
69
71
```python
70
-
#function definition
72
+
#Function definition.
71
73
defmy_function_name(parameter, second_parameter):
72
74
<function body>
73
75
74
76
```
75
77
76
78
77
79
Statements for the _body_ of the function begin on the line following `def` and must be _indented in a block_.
78
-
There is no strict indentation amount (_either space **OR**[tab] characters are acceptable_), but [indentation][indentation] must be _consistent for all indented statements_.
80
+
There is no strict indentation amount (_either space **OR**[tab] characters are acceptable_), but [indentation][indentation] must be _consistent_ for all indented statements.
81
+
79
82
80
83
```python
81
84
# Function definition on first line.
@@ -92,7 +95,7 @@ Functions explicitly return a value or object via the [`return`][return] keyword
92
95
```python
93
96
# Function definition on first line.
94
97
defadd_two_numbers(number_one, number_two):
95
-
return number_one + number_two # Returns the sum of the numbers.
98
+
return number_one + number_two # Returns the sum of the numbers.
96
99
97
100
>>> add_two_numbers(3, 4)
98
101
7
@@ -185,6 +188,7 @@ Each line of a comment block must start with the `#` character.
185
188
186
189
Comments are ignored by the interpreter:
187
190
191
+
188
192
```python
189
193
# This is a single line comment.
190
194
@@ -201,6 +205,7 @@ x = "foo" # This is an in-line comment.
201
205
The first statement of a function body can optionally be a [_docstring_][docstring], which concisely summarizes the function or object's purpose.
202
206
Docstrings are declared using triple double quotes (""") indented at the same level as the code block:
203
207
208
+
204
209
```python
205
210
206
211
# An example from PEP257 of a multi-line docstring.
0 commit comments