Skip to content
Open
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
47 changes: 43 additions & 4 deletions python.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ In Python, declaring variables is not required. Means you don't need to specify

```py
# declaring a function
def function-name(parameters){ # here parameters are optional
#code
}
function-name(parameters); # calling a function
def function-name(parameters): # here parameters are otional
pass

function-name(parameters) # calling a function
```
## Collections

Expand All @@ -88,6 +88,45 @@ print(mylist[-1]) # prints Samsung
|lst.sort()| sort the given list items|
|lst.reverse() |reverse the given list items|

#### list comprehension
```py
# instead of populating list like this
l = list()
for i in range(100):
l.append(i)
# you can do this in one liner
l = [x for x in range(100)]
# one more example
# suppose you want to create a list of even numbers from
# 1 to 100
evennums = [x if x%2==0 for x in range(100)]
```
#### list slicing
```py
# suppose there is a list and you wanna access only
# certain subarray
subarr = arr[2:5]
# subarr is the subarray of arr from 2 to 4 considering 0 based index
subarr1 = arr[:5]
subarr2 = arr[2:]
# if nothing is given in the left of colon , the default accepted is 0
# if nothing is given in right , the default is accepted as len(arr)-1


# accessing from backword
# let n be an natural number
# where n can't be 0
ele = arr[-n]
# ele is the element in the arr[len(arr)-n]
ele = arr[-x:-n]
# ele is the subarray of arr from arr[len(arr)-x] to arr[len(arr)-n]

# suppose you wanna access some perticular indexes

ele = arr[2:8:2]
# ele gives the subsequence of arr starting from index 2 to index 7 but only iterates over even position
```

### 2. Tuple

Tuple is ordered collection of items and can't be changed. `()` are used to represent Tuples.
Expand Down