Skip to content
This repository was archived by the owner on Apr 1, 2021. It is now read-only.
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
41 changes: 41 additions & 0 deletions Python-Function-Slice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Python slice(start:stop[:step])

`slice(start:stop[:step])` is an object usually containing a portion of a sequence. A slice is created using the subscript notation, [] with colons between numbers when several are given, such as in variable_name[1:3:5].

## Arguments

This function can be used to slice tuples, arrays and lists.

The value of the `start` parameter (or None if not provided)

The value of the `stop` parameter (or last index of sequence)

The value of the `step` parameter (or None if not provided). It cannot be 0.

All three must be of integer type.


## Return

If only `stop` is provided, it generates portion of sequence from index `0` till `stop`.

If only `start` is provided, it generates portion of sequence after index `start` till last element.

If both `start` and `stop` are provided, it generates portion of sequence after index `start` till `stop`.

If all three `start`, `stop` and `step` are provided, it generates portion of sequence after index `start` till `stop` with increment of index `step`.



## Example

```python
a = [ 1, 2, 3, 4, 5, 6, 7, 8]
print(a[:5]) # prints [1, 2, 3, 4, 5]
print(a[2:]) # prints [3, 4, 5, 6, 7, 8]
print(a[2:5]) # prints [3, 4, 5]
print(a[2:7:2]) # prints [3, 5, 7]
```
:rocket: [Run Code](https://repl.it/CT5h)

[Official Documentation](https://docs.python.org/3/library/functions.html#slice)
1 change: 1 addition & 0 deletions Python-Functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Several built-in functions have been discussed and used previous examples. Just
- [Nested functions](Python-Functions-Nested)
- [Pow](Python-Function-Pow)
- [Resources](Python-Functions-Resources)
- [Resources](Python-Functions-Slice)
- [`global`](Python-Functions-Statements-Global)
- [`nonlocal`](Python-Functions-Statements-Nonlocal)
- [`return` Statement](Python-Functions-Statements-Return)
4 changes: 2 additions & 2 deletions Python-Range.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Python Range

**TODO: `range` basic info**

[Python Docs - Ranges](https://docs.python.org/3/library/stdtypes.html#ranges)

Rather than being a function, range is actually an [immutable sequence type](https://docs.python.org/3/library/stdtypes.html#immutable-sequence-types) and is commonly used for looping a specific number of times in for loops.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-Rather than being a function, range is actually an [immutable sequence type](https://docs.python.org/3/library/stdtypes.html#immutable-sequence-types) and is commonly used for looping a specific number of times in for loops.
+Rather than being a function, a range is actually an [immutable sequence type](https://docs.python.org/3/library/stdtypes.html#immutable-sequence-types) and is commonly used for looping a specific number of times in for loops.


**Creation:**

`ranges` are created using the `range` constructor. The parameters for the constructor are:
Expand Down