Skip to content

Commit

Permalink
.md test
Browse files Browse the repository at this point in the history
  • Loading branch information
Uday Hiwarale authored and Uday Hiwarale committed Apr 16, 2020
1 parent fbd6920 commit f4c4701
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 67 deletions.
78 changes: 78 additions & 0 deletions 1.basics/0.must-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
## Comments
```py
# This is a comment.

# python editor
# https://repl.it/languages/python3
```

```py
'''
This is not a
multi line comment (also tripple double-quotes).
This is still compiled by the interpreter
but does nothing.
https://www.python.org/dev/peps/pep-0257/#multi-line-docstrings
'''
```

## `print` function

```py
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
```

Print objects to the text stream file, separated by sep and followed by end. `sep`, `end`, `file` and `flush`, if present, must be given as keyword arguments.

https://docs.python.org/3/library/functions.html#print


```py
print(123)
# => 123

print("Hello World!")
# Hello World!

print("Hello", "World!")
# => Hello World!

print("Hello", "World!", sep=" - ")
# => Hello - World!

print( 4 * 2 )
# => 8

a, b = 4, 2
print( "a * b =", a * b )
# => a * b = 8

# https://docs.python.org/3/tutorial/inputoutput.html
print( "%d * %d = %d" %( a, b, (a * b) ) )
# => 4 * 2 = 8

print("{} * {} = {}".format( a, b, a * b ))
# => 4 * 2 = 8

print("{2}, {1} and {0}".format("mango", "bananas", "apple"))
# => apple, bananas and mango

print(1 == 1)
# => True

print( 1 != 1 )
# => False
```

## Multi-line code
The `\` character is used to put sequential code on the next line.

```py
a, b = 1, 2
c = a + \
b
print( "c =", c )

# => c = 3
```

67 changes: 0 additions & 67 deletions 1.basics/iterator.py

This file was deleted.

67 changes: 67 additions & 0 deletions 5.advanced/iterator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# an iterator is an object which produces a value on succesive iteration

# sample iterator class
# https://www.geeksforgeeks.org/iterators-in-python/
class MyItr:
def __init__(self, limit):
self.limit = limit

# when iteration is initialized
def __iter__(self):
self.current = 0
return self

# get values sequentially
def __next__(self):
if self.current > self.limit:
raise StopIteration
else:
returnVal = self.current
self.current = self.current + 1
return returnVal

# create iterator
myItr5 = MyItr(5)

# for loop
print('for loop on interable', end=" :=> ")
for elem in myItr5:
print( elem, end=" " )
print('')

# convert iterable to a list
print( 'iterator: list(myItr5)', list(myItr5) )

# check element exist
print('iterator: 4 in myItr5', 4 in myItr5)

# convert list to an iterable
itr_list = iter([1,2,3,4,5])
print('itr_list', end=" :=> ")
for elem in itr_list:
print( elem, end=" " )
print('')

# https://www.geeksforgeeks.org/python-range-method/
# https://stackoverflow.com/questions/31227536/what-is-the-difference-between-range0-2-and-listrange0-2
# https://pynative.com/python-range-function/

# in python 2.x, range() returns a list, which stores entire list in the memory.
# in python 3, range() returns a class of immutable iterable object that lets you iterate over it. This does not store the list in memory. This produces the element on the fly as we iterate over them. (borrowd from: https://stackoverflow.com/questions/31227536/what-is-the-difference-between-range0-2-and-listrange0-2)
# doc: https://docs.python.org/3/library/stdtypes.html#ranges

# range(stop)
a = range(5) # start from 0 to 5 (excluded)
print( 'range(5)', list(a) )

# range(start, stop)
a = range(2, 5) # exclude 5
print( 'range(2, 5)', list(a) )

# range(start,stop,step)
print( 'range(2,10)', list(range(2,10)) ) # default step: 1
print( 'range(2,10,2)', list(range(2,10,2)) ) # ignores 9
print( 'range(2,-10,-2)', list(range(2,-10,-2)) ) # ignores -9
print( 'range(2,10,-2)', list(range(2,10,-2)) ) # empty
#print( 'range(2,10,0)', list(range(2,10,0)) ) # ValueError for step 0

0 comments on commit f4c4701

Please sign in to comment.