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 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
-
Imperative, declarative (e.g., functional), and object-oriented programming _styles_ are all supported, 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
6
7
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
8
9
-
10
-
The [zen of Python (PEP 20)][the zen of python] and [What is Pythonic?][what is pythonic] lay out additional philosophies
11
-
12
9
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].
13
10
14
11
@@ -21,59 +18,77 @@ Complete documentation for the current release can be found at [docs.python.org]
21
18
-[Python FAQs][python faqs]
22
19
-[Python Glossary of Terms][python glossary of terms]
23
20
21
+
24
22
This concept introduces 4 major Python language features: Name Assignment (_variables and constants_), Functions (_and the return keyword_), Comments, and Docstrings.
25
23
26
24
27
25
~~~~exercism/note
28
26
29
27
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.
30
28
31
-
~~~~
29
+
The [zen of Python (PEP 20)][the zen of python] and [What is Pythonic?][what is pythonic] lay out additional philosophies.
30
+
31
+
On the Python track, [variables][variables] are always written in [`snake_case`][snake case], and constants in `SCREAMING_SNAKE_CASE`
[what is pythonic]: https://blog.startifact.com/posts/older/what-is-pythonic.html
37
+
[the zen of python]: https://www.python.org/dev/peps/pep-0020/
38
+
~~~~
35
39
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
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.
41
+
## Name Assignment (Variables & Constants)
42
+
43
+
In Python, there are no keywords used in creating variables or constants.
44
+
Instead, programmer defined [_names_][facts-and-myths-about-python-names] (also called _variables__) can be bound to any type of object using the assignment `=` operator: `<name> = <value>`.
45
+
A name can be reassigned (or re-bound) to different values (different object types) over its lifetime.
42
46
43
47
For example, `my_first_variable` can be re-assigned many times using `=`, and can refer to different object types with each re-assignment:
44
48
49
+
45
50
```python
46
-
# Assigning my_first_variable to a numeric value.
47
-
>>> my_first_variable =1
51
+
>>> my_first_variable =1# Name bound to an integer object of value one.
52
+
>>> my_first_variable =2# Name re-assigned to integer value 2.
53
+
48
54
>>>print(type(my_first_variable))
49
55
<class'int'>
50
56
51
57
>>>print(my_first_variable)
52
-
1
58
+
2
53
59
54
-
# Reassigning my_first_variable to a new string value.
55
-
>>> my_first_variable ="Now, I'm a string."
60
+
>>> my_first_variable ="Now, I'm a string."# You may re-bind a name to a different object type and value.
56
61
>>>print(type(my_first_variable))
57
62
<class'str'>
58
63
59
64
>>>print(my_first_variable)
60
-
"Now, I'm a string."
65
+
"Now, I'm a string."# Strings can be declared using single or double quote marks.
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.
79
+
Constants are names meant to be assigned only once in a program.
80
+
They should be defined at a [module][module] (file) level, and are typically visible to all functions and classes in the program.
81
+
Using `SCREAMING_SNAKE_CASE` signals that the name should not be re-assigned, or its value mutated.
82
+
69
83
70
84
```python
71
85
# All caps signal that this is intended as a constant.
72
86
MY_FIRST_CONSTANT=16
73
87
74
88
# Re-assignment will be allowed by the compiler & interpreter,
75
89
# but this is VERY strongly discouraged.
76
-
# Please don't: MY_FIRST_CONSTANT = "Some other value"
90
+
# Please don't do this, it could create problems in your program!
91
+
MY_FIRST_CONSTANT="Some other value"
77
92
```
78
93
79
94
@@ -85,49 +100,45 @@ Functions can be executed by themselves, passed as arguments to other functions,
85
100
When functions are bound to a [class][classes] name, they're referred to as [methods][method objects].
86
101
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.
87
102
88
-
The keyword `def` begins a [function definition][function definition].
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
+
The `def` keyword begins a [function definition][function definition].
104
+
Each function can have zero or more formal [parameters][parameters] in `()` parenthesis, followed by a `:` colon.
105
+
Statements for the _body_ of the function begin on the line following `def` and must be _indented in a block_.
103
106
104
107
105
108
```python
106
-
#Function definition on first line.
109
+
#The body of this function is indented by 2 spaces, & prints the sum of the numbers
107
110
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.
111
+
total = number_one + number_two
112
+
print(total)
109
113
110
114
>>> add_two_numbers(3, 4)
111
115
7
112
-
```
113
116
114
117
118
+
# Inconsistent indentation in your code blocks will raise an error.
... result = number_one + number_two + number_three # Indented by 4 spaces.
145
-
...return result #this was only indented by 3 spaces
146
-
...
147
-
...
148
-
File "<stdin>", line 3
149
-
return result
150
-
^
151
-
IndentationError: unindent does not match any outer indentation level
152
-
```
153
-
154
150
155
151
### Calling Functions
156
152
157
-
Functions are [_called_][calls] using their name followed by `()`.
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.
153
+
Functions are [_called_][calls]or invoked using their name followed by `()`.
154
+
Dot (`.`) notation is used for calling functions defined inside a class or module.
:param number_two: int the power to raise the base number to.
191
-
:return: int - number raised to power of second number
192
-
193
-
Takes number_one and raises it to the power of number_two, returning the result.
194
-
"""
195
-
196
-
return number_one ** number_two
197
-
198
-
# Because there was a default value, this call with only one argument does not throw an error.
199
-
>>> number_to_the_power_of_default(4)
200
-
16
201
-
```
202
-
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.:
205
-
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.
173
+
# Calling methods or functions in classes and modules.
209
174
>>> 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)
175
+
>>>str.upper(start_text) # Calling the upper() method for the built-in str class.
213
176
"MY SILLY SENTENCE FOR EXAMPLES."
214
177
178
+
# Importing the math module
179
+
import math
215
180
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."]
224
-
225
-
import string
226
-
227
-
# This is a constant provided by the *string* module.
228
-
>>> alphabet = string.ascii_lowercase
229
-
>>>print(alphabet)
230
-
"abcdefghijklmnopqrstuvwxyz"
181
+
>>> math.pow(2,4) # Calling the pow() function from the math module
0 commit comments