Skip to content

Commit 54129fe

Browse files
basics - replace about.md file with concept files (exercism#2350)
1 parent f945fe3 commit 54129fe

File tree

3 files changed

+282
-148
lines changed

3 files changed

+282
-148
lines changed

concepts/basics/about.md

Lines changed: 146 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,146 @@
1-
TODO: add information on basics concept
1+
[Python](https://docs.python.org/3/) is a [dynamic and strongly](https://stackoverflow.com/questions/11328920/is-python-strongly-typed) typed [object-oriented](https://en.wikipedia.org/wiki/Object-oriented_programming) programming language. As of version 3.5, it employs both [duck typing](https://en.wikipedia.org/wiki/Duck_typing) and [gradual typing](https://en.wikipedia.org/wiki/Gradual_typing), via [type hints](https://docs.python.org/3/library/typing.html). It supports both imperative (_object-oriented, procedural_) and declarative (_functional, concurrent_) programming paradigms. But do not be fooled: while programming in other paradigms is fully _supported_, [everything in Python is an object](https://docs.python.org/3/reference/datamodel.html).
2+
3+
Python was created by Guido van Rossum and first released in 1991. The [Python Software Foundation](https://www.python.org/psf/) manages and directs resources for Python and CPython development and receives proposals for changes to the language from [members](https://www.python.org/psf/membership/) of the community via [Python Enhancement Proposals or PEPs](https://www.python.org/dev/peps/).
4+
5+
Python puts a strong emphasis on code readability and (_similar to Haskell_) uses [significant indentation](https://docs.python.org/3/reference/lexical_analysis.html#indentation) to denote function, method, and class definitions. The [zen of Python (PEP 20)](https://www.python.org/dev/peps/pep-0020/) lays out additional inspiration and philosophy.
6+
7+
Objects are [assigned](https://docs.python.org/3/reference/simple_stmts.html#assignment-statements) to [names](https://docs.python.org/3/reference/executionmodel.html#naming-and-binding) in Python via the `=` or _assignment operator_. [Variables](https://realpython.com/python-variables/) are written in [`snake_case`](https://en.wikipedia.org/wiki/Snake_case), and _constants_ usually in `SCREAMING_SNAKE_CASE`. A name (_variable or constant_) is not itself _typed_, and can be attached or re-attached to different objects over its lifetime. For extended naming conventions and advice, see [PEP 8](https://www.python.org/dev/peps/pep-0008/).
8+
9+
```python
10+
>>> my_first_variable = 1
11+
>>> my_first_variable = "Last one, I promise"
12+
>>> print(my_first_variable)
13+
"Last one, I promise"
14+
```
15+
16+
Constants are usually defined on a [module](https://docs.python.org/3/tutorial/modules.html) or _global_ level, and although they _can_ be changed, they are _intended_ to be named only once. Their `SCREAMING_SNAKE_CASE` is a message to other developers that the assignment should not be altered:
17+
18+
```python
19+
# All caps signal that this is intended as a constant
20+
MY_FIRST_CONSTANT = 16
21+
22+
# Re-assignment will be allowed by the compiler & interpreter,
23+
# but is VERY strongly discouraged.
24+
# Please don't do: MY_FIRST_CONSTANT = "Some other value"
25+
```
26+
27+
In Python, units of functionality are encapsulated in [functions](https://docs.python.org/3/reference/compound_stmts.html#function), which are themselves [objects](https://docs.python.org/3/reference/datamodel.html#the-standard-type-hierarchy) (_Its [turtles all the way down](https://en.wikipedia.org/wiki/Turtles_all_the_way_down)_).
28+
29+
Functions can be executed by themselves, passed as arguments to other functions, nested, or bound to a class. When functions are bound to a [class](https://docs.python.org/3/reference/datamodel.html#classes) name, they're referred to as [methods](https://docs.python.org/3/c-api/method.html#method-objects). Related functions and classes (_with their methods_) can be grouped together in the same file or [module](https://docs.python.org/3/reference/datamodel.html#modules), and imported in part or in whole for use in other programs.
30+
31+
The keyword `def` begins a [function definition](https://docs.python.org/3/tutorial/controlflow.html#defining-functions). It must be followed by the function name and a parenthesized list of zero or more formal [parameters](https://docs.python.org/3/glossary.html#term-parameter), which can be of several different varieties, and even [vary](https://docs.python.org/3/tutorial/controlflow.html#more-on-defining-functions) in length.
32+
33+
The `def` line is terminated with a colon. Statements for the _body_ of the function begin on the next line, and must be _indented in a block_. There is no strict indentation amount (_either space **OR** [tab] characters are acceptable_), but [indentation](https://docs.python.org/3/reference/lexical_analysis.html#indentation) must be _consistent for all indented statements_. Functions explicitly return a value or object via the [`return`](https://docs.python.org/3/reference/simple_stmts.html#return) keyword. Functions that do not have an explicit `return` expression will return [`None`](https://docs.python.org/3/library/constants.html):
34+
35+
```python
36+
#function definition on first line.
37+
def add_two_numbers(number_one, number_two):
38+
return number_one + number_two #returns the sum of the numbers
39+
40+
>>> add_two_numbers(3, 4)
41+
7
42+
43+
#this function has no return keyword, and so will return None
44+
def multiply_two_numbers(number_one, number_two):
45+
result = number_one * number_two
46+
47+
>>> print(multiply_two_numbers(6, 8))
48+
None
49+
```
50+
51+
Functions are [called](https://docs.python.org/3/reference/expressions.html#calls) using their name followed by `()`. The number of arguments passed in the parentheses must match the number of parameters in the original function definition unless [default arguments](https://docs.python.org/3/tutorial/controlflow.html#default-argument-values) have been used:
52+
53+
```python
54+
def number_to_the_power_of(number_one, number_two):
55+
'''Returns float or int.
56+
57+
Takes number_one and raises it to the power of number_two, returning the result.
58+
'''
59+
60+
return number_one ** number_two
61+
62+
>>> number_to_the_power_of(3,3)
63+
27
64+
65+
>>> number_to_the_power_of(4,)
66+
Traceback (most recent call last):
67+
File "<stdin>", line 1, in <module>
68+
TypeError: number_to_the_power_of() missing 1 required positional argument: 'number_two'
69+
70+
...
71+
72+
def number_to_the_power_of_default(number_one, number_two=2):
73+
'''Returns float or int.
74+
75+
Takes number_one and raises it to the power of number_two, returning the result.
76+
'''
77+
78+
return number_one ** number_two
79+
80+
>>> number_to_the_power_of_default(4)
81+
16
82+
```
83+
84+
Methods bound to class names are invoked via _dot notation_ (`.`), as are functions, constants, or global names imported as part of a _module_.:
85+
86+
```python
87+
import string
88+
89+
#this is a constant provided by the *string* module
90+
>>> print(string.ascii_lowercase)
91+
"abcdefghijklmnopqrstuvwxyz"
92+
93+
#this is a method call of the str *class*
94+
>>> start_text = "my silly sentence for examples."
95+
>>> str.upper(start_text)
96+
"MY SILLY SENTENCE FOR EXAMPLES."
97+
98+
#this is a method call of an *instance* of the str *class*
99+
>>> start_text.upper()
100+
"MY SILLY SENTENCE FOR EXAMPLES."
101+
```
102+
103+
[Comments](https://realpython.com/python-comments-guide/#python-commenting-basics) in Python start with a `#` that is not part of a string, and end at line termination. Unlike many other programming languages, Python does not support multi-line comment marks. Each line of a comment block must start with the `#` character. Comments are ignored by the interpreter:
104+
105+
```python
106+
#this is a single line comment
107+
108+
x = "foo" #this is an in-line comment
109+
110+
#this is a multi-line
111+
#comment block over multiple lines
112+
#these should be used sparingly
113+
```
114+
115+
The first statement of a function body can optionally be a [docstring](https://docs.python.org/3/tutorial/controlflow.html#tut-docstrings), which concisely summarizes the function or object's purpose. These docstrings are read by automated documentation tools, and are returned by calling `__doc__` on the function, method, or class. They are recommended for programs of any size where documentation is needed:
116+
117+
```python
118+
#an example on a user-defined function
119+
def number_to_the_power_of(number_one, number_two):
120+
'''Returns float or int.
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
126+
127+
>>> print(number_to_the_power_of.__doc__)
128+
Returns float or int.
129+
130+
Takes number_one and raises it to the power of number_two, returning the result.
131+
132+
#an example for a built-in type: str
133+
>>> print(str.__doc__)
134+
str(object='') -> str
135+
str(bytes_or_buffer[, encoding[, errors]]) -> str
136+
137+
Create a new string object from the given object. If encoding or
138+
errors is specified, then the object must expose a data buffer
139+
that will be decoded using the given encoding and error handler.
140+
Otherwise, returns the result of object.__str__() (if defined)
141+
or repr(object).
142+
encoding defaults to sys.getdefaultencoding().
143+
errors defaults to 'strict'.
144+
```
145+
146+
Docstrings can also include [doctests](https://docs.python.org/3/library/doctest.html), which are interactive examples of how a method or function should work. Doctests can be read and run by PyTest, or by importing the `doctest` module.

concepts/basics/links.json

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,136 @@
1-
[]
1+
[
2+
{ "url": "https://docs.python.org/3/", "description": "Python" },
3+
{
4+
"url": "https://stackoverflow.com/questions/11328920/is-python-strongly-typed",
5+
"description": "dynamic and strongly"
6+
},
7+
{
8+
"url": "https://en.wikipedia.org/wiki/Object-oriented_programming",
9+
"description": "object-oriented"
10+
},
11+
{
12+
"url": "https://en.wikipedia.org/wiki/Duck_typing",
13+
"description": "duck typing"
14+
},
15+
{
16+
"url": "https://en.wikipedia.org/wiki/Gradual_typing",
17+
"description": "gradual typing"
18+
},
19+
{
20+
"url": "https://docs.python.org/3/library/typing.html",
21+
"description": "type hints"
22+
},
23+
{
24+
"url": "https://docs.python.org/3/reference/datamodel.html",
25+
"description": "everything in Python is an object"
26+
},
27+
{
28+
"url": "https://www.python.org/psf/",
29+
"description": "Python Software Foundation"
30+
},
31+
{ "url": "https://www.python.org/psf/membership/", "description": "members" },
32+
{
33+
"url": "https://www.python.org/dev/peps/",
34+
"description": "Python Enhancement Proposals or PEPs"
35+
},
36+
{
37+
"url": "https://docs.python.org/3/reference/lexical_analysis.html#indentation",
38+
"description": "significant indentation"
39+
},
40+
{
41+
"url": "https://www.python.org/dev/peps/pep-0020/",
42+
"description": "zen of Python (PEP 20)"
43+
},
44+
{
45+
"url": "https://docs.python.org/3/reference/simple_stmts.html#assignment-statements",
46+
"description": "assigned"
47+
},
48+
{
49+
"url": "https://docs.python.org/3/reference/executionmodel.html#naming-and-binding",
50+
"description": "names"
51+
},
52+
{
53+
"url": "https://realpython.com/python-variables/",
54+
"description": "Variables"
55+
},
56+
{
57+
"url": "https://en.wikipedia.org/wiki/Snake_case",
58+
"description": "TODO: add description"
59+
},
60+
{
61+
"url": "https://www.python.org/dev/peps/pep-0008/",
62+
"description": "PEP 8"
63+
},
64+
{
65+
"url": "https://docs.python.org/3/tutorial/modules.html",
66+
"description": "module"
67+
},
68+
{
69+
"url": "https://docs.python.org/3/reference/compound_stmts.html#function",
70+
"description": "functions"
71+
},
72+
{
73+
"url": "https://docs.python.org/3/reference/datamodel.html#the-standard-type-hierarchy",
74+
"description": "objects"
75+
},
76+
{
77+
"url": "https://en.wikipedia.org/wiki/Turtles_all_the_way_down",
78+
"description": "turtles all the way down"
79+
},
80+
{
81+
"url": "https://docs.python.org/3/reference/datamodel.html#classes",
82+
"description": "class"
83+
},
84+
{
85+
"url": "https://docs.python.org/3/c-api/method.html#method-objects",
86+
"description": "methods"
87+
},
88+
{
89+
"url": "https://docs.python.org/3/reference/datamodel.html#modules",
90+
"description": "module"
91+
},
92+
{
93+
"url": "https://docs.python.org/3/tutorial/controlflow.html#defining-functions",
94+
"description": "function definition"
95+
},
96+
{
97+
"url": "https://docs.python.org/3/glossary.html#term-parameter",
98+
"description": "parameters"
99+
},
100+
{
101+
"url": "https://docs.python.org/3/tutorial/controlflow.html#more-on-defining-functions",
102+
"description": "vary"
103+
},
104+
{
105+
"url": "https://docs.python.org/3/reference/lexical_analysis.html#indentation",
106+
"description": "indentation"
107+
},
108+
{
109+
"url": "https://docs.python.org/3/reference/simple_stmts.html#return",
110+
"description": "TODO: add description"
111+
},
112+
{
113+
"url": "https://docs.python.org/3/library/constants.html",
114+
"description": "TODO: add description"
115+
},
116+
{
117+
"url": "https://docs.python.org/3/reference/expressions.html#calls",
118+
"description": "called"
119+
},
120+
{
121+
"url": "https://docs.python.org/3/tutorial/controlflow.html#default-argument-values",
122+
"description": "default arguments"
123+
},
124+
{
125+
"url": "https://realpython.com/python-comments-guide/#python-commenting-basics",
126+
"description": "Comments"
127+
},
128+
{
129+
"url": "https://docs.python.org/3/tutorial/controlflow.html#tut-docstrings",
130+
"description": "docstring"
131+
},
132+
{
133+
"url": "https://docs.python.org/3/library/doctest.html",
134+
"description": "doctests"
135+
}
136+
]

0 commit comments

Comments
 (0)