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
In Python, `None` is frequently used to represent the absence of a value -- a placeholder to define a `null` (empty) variable, object, or argument.
3
+
In Python, `None` is frequently used to represent the absence of a value — a placeholder to define a `null` (empty) variable, object, or argument.
4
+
5
+
If you've heard about or used a `NULL` or `nil` type in another programming language, then this usage of `None` in Python will be familiar to you.
6
+
`None` helps you to declare variables or function arguments that you don't yet have values for.
7
+
These can then be re-assigned to specific values later as needed:
4
8
5
-
If you've heard about or used a `NULL` or `nil` type in another programming language, then this usage of `None` in Python will be familiar to you. `None` helps you to declare variables or function arguments that you don't yet have values for. These can then be re-assigned to specific values later as needed.
6
9
7
10
```python
8
11
a =None
9
12
print(a)
10
13
#=> None
14
+
11
15
type(a)
12
16
#=> <class 'NoneType'>
13
17
14
-
# Adding a Default Argument with `None`
18
+
# Adding a default argument with `None`
15
19
defadd_to_todos(new_task, todo_list=None):
16
-
if todo_list isNone:
17
-
todo_list = []
20
+
if todo_list isNone:
21
+
todo_list = []
18
22
todo_list.append(new_task)
23
+
19
24
return todo_list
20
-
21
25
```
22
26
23
-
`None` will evaluate to `False` when used in a conditional check, so it is useful for validating the "presence of" or "absence of" a value -_any_ value -- a pattern frequently used when a function or process might hand back an errorobject or message.
27
+
`None` will evaluate to `False` when used in a conditional check, so it is useful for validating the "presence of" or "absence of" a value —_any_ value — a pattern frequently used when a function or process might hand back an `error`, `object`, or message.
24
28
25
29
```python
26
30
a =None
27
-
if a: #=> a will be evaluated to False when its used in a conditional check.
31
+
if a: #<-- a will be evaluated to False when it is used in a conditional check.
@@ -89,10 +89,10 @@ Possible results from `randint()` _include_ the upper bound, so `randint(a, b)`
89
89
90
90
## `choice()` and `choices()`
91
91
92
-
These two functions assume that you are starting from some [sequence][sequence-types], or other container.
93
-
This will typically be a `list`, or with some limitations a `tuple` or a `set` (_a `tuple` is immutable, and `set` is unordered_).
92
+
These two functions assume that you are starting from some [sequence][sequence-types] or other container.
93
+
This will typically be a `list`, or with some limitations, a `tuple` or a `set` (_a `tuple` is immutable, and `set` is unordered_).
94
94
95
-
The `choice()` function will return one entry chosen at random from a given sequence, and `choices()` will return `k`number of entries chosen at random from a given sequence.
95
+
The `choice()` function will return one member chosen at random from a given sequence, and `choices()` will return a specified number of members (`k`) chosen at random from a given sequence.
96
96
In the examples shown above, we assumed a fair coin with equal probability of heads or tails, but weights can also be specified.
97
97
98
98
For example, if a bag contains 10 red balls and 15 green balls, and we would like to pull one out at random:
0 commit comments