Skip to content

Commit b997794

Browse files
additional (advanced ) topic added + Cheatsheet
1 parent 89b4994 commit b997794

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+570
-1
lines changed

10. Generators/fibonacci_list.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
def fib(num):
3+
a = 0
4+
b= 1
5+
li=[]
6+
for i in range(num):
7+
li.append(a)
8+
temp = a
9+
a = b
10+
b = temp + b
11+
print(li)
12+
13+
num = int(input("Enter a number: "))
14+
fib(num)

10. Generators/fibonacci_range.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
def fib(num):
3+
a = 0
4+
b= 1
5+
for i in range(num):
6+
yield a
7+
temp = a
8+
a = b
9+
b = temp + b
10+
11+
num = int(input("Enter a number: "))
12+
13+
for i in fib(num):
14+
print(i)

10. Generators/generator.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
# here we are generating our own generator function, just like a range().
3+
4+
def generator_fn(num):
5+
print("check")
6+
#yield
7+
for i in range(num):
8+
print("****")
9+
yield i*2
10+
print("####")
11+
12+
g = generator_fn(3)
13+
print(g)
14+
print(next(g))
15+
print(next(g))
16+
print(next(g))
17+
#print(next(g)) # StopIteration error
18+
print(g)
19+
20+
for item in generator_fn(5):
21+
print(item)
22+
23+
# here it goes to the generator_fn(), gets the 'i' value, pauses the function, until called for the 2nd time,
24+
# and so on, it doesn't store all the no.s in the memory (just the most recent one).
25+
26+
'''
27+
'yield' pauses the function and comes back to it when we do something to it, which is called 'next'.
28+
29+
if there is the keyword 'yield' written inside the function, then python recognises that its a
30+
generator function, and won't run the function until the function is being iterated.
31+
'''

10. Generators/our_own_forloop.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
def my_own_forloop(iterable):
3+
iterator = iter(iterable)
4+
while True:
5+
try:
6+
print(iterator)
7+
print(next(iterator))
8+
except StopIteration:
9+
break
10+
11+
my_own_forloop([1,2,3,4,5])

10. Generators/our_own_range.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
class OurOwnRange():
3+
current = 0
4+
def __init__(self,first,last):
5+
self.first = first
6+
self.last = last
7+
8+
def __iter__(self):
9+
return self
10+
11+
def __next__(self):
12+
print("hehehheh")
13+
# if self.current < self.last:
14+
# num = OurOwnRange.current
15+
# OurOwnRange.current += 1
16+
# return num
17+
# raise StopIteration
18+
19+
gen = OurOwnRange(0,10)
20+
print(gen)
21+
22+
for i in gen:
23+
print(i)
24+
25+
'''
26+
loops by default deal with StopIteration error. they have build in functionality to handle them.
27+
'''
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
import re
3+
4+
email_pattern = re.compile(r"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)")
5+
check_email = email_pattern.fullmatch('saurabh_1089@gmail.com')
6+
7+
password_patter = re.compile(r"([a-zA-Z0-9@#$%]{8,}$)")
8+
check_password = password_patter.fullmatch('12345678')
9+
10+
if check_email and check_password:
11+
print("Both email and password are correct.")
12+
else:
13+
print("Try again.")
14+
15+
'''
16+
password is also checking for minimum 8 chars
17+
'''
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import re
2+
3+
string = "this is a really cool string really!"
4+
5+
a = re.search('really',string)
6+
print(a)
7+
8+
# the below 4 commands will give error if the searching string does not exist.
9+
print(a.span())
10+
print(a.start())
11+
print(a.end())
12+
print(a.group())
13+
14+
pattern = re.compile('really')
15+
16+
b = pattern.search(string)
17+
c = pattern.findall(string)
18+
19+
pattern = re.compile('this is a really cool string really!')
20+
d = pattern.fullmatch('this is a really cool string really!')
21+
e = pattern.fullmatch('hello this is a really cool string really!') # this should be exact match, otherwise returns none
22+
23+
pattern = re.compile('really')
24+
f = pattern.match('really cool feature') # it starts matching from the first character otherwise returns none
25+
g = pattern.match('yo really')
26+
27+
print(f"b: {b}")
28+
print(f"c: {c}")
29+
print(f"d: {d}")
30+
print(f"e: {e}")
31+
print(f"f: {f}")
32+
print(f"g: {g}")
Binary file not shown.
Binary file not shown.
392 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)