Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion concepts/none/about.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# About
# TODO: Add about for this concept.


20 changes: 12 additions & 8 deletions concepts/none/introduction.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
# Introduction

In Python, `None` is frequently used to represent the absence of a value -- a placeholder to define a `null` (empty) variable, object, or argument.
In Python, `None` is frequently used to represent the absence of a value — a placeholder to define a `null` (empty) variable, object, or argument.

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:

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.

```python
a = None
print(a)
#=> None

type(a)
#=> <class 'NoneType'>

# Adding a Default Argument with `None`
# Adding a default argument with `None`
def add_to_todos(new_task, todo_list=None):
if todo_list is None:
todo_list = []
if todo_list is None:
todo_list = []
todo_list.append(new_task)

return todo_list

```

`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.
`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.

```python
a = None
if a: #=> a will be evaluated to False when its used in a conditional check.
if a: #<-- a will be evaluated to False when it is used in a conditional check.
print("This will not be printed")
```
26 changes: 14 additions & 12 deletions concepts/random/about.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# About

Many programs need (apparently) random values to simulate real-world events.
Many programs need (_seemingly_) random values to simulate real-world events.

Common, familiar examples include:
- A coin toss: a random value from `('H', 'T')`.
- A coin toss: a random value from `('Heads', 'Tails')`.
- The roll of a die: a random integer from 1 to 6.
- Shuffling a deck of cards: a random ordering of a card list.

Expand All @@ -18,7 +18,7 @@ We encourage you to explore the full `random` documentation, as there are many m

~~~~exercism/caution

The `random` module should __NOT__ be used for security and cryptographic applications.
The `random` module should __NOT__ be used for security or cryptographic applications.

Instead, Python provides the [`secrets`][secrets] module.
This is specially optimized for cryptographic security.
Expand Down Expand Up @@ -60,17 +60,17 @@ To avoid typing the name of the module, you can import specific functions by nam

# Using choice() to pick Heads or Tails 10 times
>>> tosses = []
>>> for side in range(10):
>>> tosses.append(choice(['H', 'T']))
... for side in range(10):
... tosses.append(choice(['H', 'T']))

>>> print(tosses)
['H', 'H', 'H', 'H', 'H', 'H', 'H', 'T', 'T', 'H']


# Using choices() to pick Heads or Tails 8 times
>>> picks = []
>>> picks.extend(choices(['H', 'T'], k=8))
>>> print(picks)
... picks.extend(choices(['H', 'T'], k=8))
... print(picks)
['T', 'H', 'H', 'T', 'H', 'H', 'T', 'T']
```

Expand All @@ -94,8 +94,9 @@ Possible results from `randint()` _include_ the upper bound, so `randint(a, b)`

# Select 10 numbers at random between 0 and 9 two steps apart.
>>> numbers = []
>>> for integer in range(10):
>>> numbers.append(random.randrange(0, 10, 2))
... for integer in range(10):
... numbers.append(random.randrange(0, 10, 2))

>>> print(numbers)
[2, 8, 4, 0, 4, 2, 6, 6, 8, 8]

Expand All @@ -108,10 +109,10 @@ Possible results from `randint()` _include_ the upper bound, so `randint(a, b)`

## Working with sequences

The functions in this section assume that you are starting from some [sequence][sequence-types], or other container.
The functions in this section assume that you are starting from some [sequence][sequence-types] or other container.


This will typically be a `list`, or with some limitations a `tuple` or a `set` (_a `tuple` is immutable, and `set` is unordered_).
This will typically be a `list`, or with some limitations, a `tuple` or a `set` (_a `tuple` is immutable, and `set` is unordered_).



Expand All @@ -124,9 +125,10 @@ At its simplest, this might be a coin-flip:
# This will pick one of the two values in the list at random 5 separate times
>>> [random.choice(['H', 'T']) for _ in range(5)]
['T', 'H', 'H', 'T', 'H']
```

We could accomplish essentially the same thing using the `choices()` function, supplying a keyword argument with the list length:

We could accomplish essentially the same thing using the `choices()` function, supplying a keyword argument with the list length:

```python
>>> random.choices(['H', 'T'], k=5)
Expand Down
20 changes: 10 additions & 10 deletions concepts/random/introduction.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Introduction

Many programs need (apparently) random values to simulate real-world events.
Many programs need (_seemingly_) random values to simulate real-world events.

Common, familiar examples include:
- A coin toss: a random value from `('H', 'T')`.
- A coin toss: a random value from `('Heads', 'Tails')`.
- The roll of a die: a random integer from 1 to 6.
- Shuffling a deck of cards: a random ordering of a card list.
- The creation of trees and bushes in a 3-D graphics simulation.
Expand All @@ -18,7 +18,7 @@ We encourage you to explore the full [`random`][random] documentation, as there

~~~~exercism/caution

The `random` module should __NOT__ be used for security and cryptographic applications!!
The `random` module should __NOT__ be used for security or cryptographic applications!!

Instead, Python provides the [`secrets`][secrets] module.
This is specially optimized for cryptographic security.
Expand Down Expand Up @@ -57,17 +57,17 @@ To avoid typing the name of the module, you can import specific functions by nam

# Using choice() to pick Heads or Tails 10 times
>>> tosses = []
>>> for side in range(10):
>>> tosses.append(choice(['H', 'T']))
... for side in range(10):
... tosses.append(choice(['H', 'T']))

>>> print(tosses)
['H', 'H', 'H', 'H', 'H', 'H', 'H', 'T', 'T', 'H']


# Using choices() to pick Heads or Tails 8 times
>>> picks = []
>>> picks.extend(choices(['H', 'T'], k=8))
>>> print(picks)
... picks.extend(choices(['H', 'T'], k=8))
... print(picks)
['T', 'H', 'H', 'T', 'H', 'H', 'T', 'T']
```

Expand All @@ -89,10 +89,10 @@ Possible results from `randint()` _include_ the upper bound, so `randint(a, b)`

## `choice()` and `choices()`

These two functions assume that you are starting from some [sequence][sequence-types], or other container.
This will typically be a `list`, or with some limitations a `tuple` or a `set` (_a `tuple` is immutable, and `set` is unordered_).
These two functions assume that you are starting from some [sequence][sequence-types] or other container.
This will typically be a `list`, or with some limitations, a `tuple` or a `set` (_a `tuple` is immutable, and `set` is unordered_).

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.
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.
In the examples shown above, we assumed a fair coin with equal probability of heads or tails, but weights can also be specified.

For example, if a bag contains 10 red balls and 15 green balls, and we would like to pull one out at random:
Expand Down
Loading