This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def factorial(n): | |
if n == 1: | |
return 1 | |
return factorial(n - 1) * n | |
print(factorial(4)) | |
def tailRecursionFactorial(n, acc): | |
if n == 1: | |
return acc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
re.match(r'\d{5}','12345') | |
# <_sre.SRE_Match object; span=(0, 5), match='12345'> | |
re.match(r'\d{5}','1234') | |
# None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 'a b c'.split(' ') | |
# ['a', 'b', '', '', 'c'] Can't split by consecutive spaces. | |
>>> re.split(r'\s+', 'a b c') | |
# ['a', 'b', 'c'] | |
>>> re.split(r'[\s\,]+', 'a,b, c d') | |
# ['a', 'b', 'c', 'd'] | |
>>> re.split(r'[\s\,\;]+', 'a,b;; c d') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
time='18:05' | |
matched = re.match(r'^(0[0-9]|1[0-9]|2[0-3])\:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])$', time) | |
matched.groups() | |
# ('18', '05') show all groups(substrings) | |
matched.group() | |
# '18:05' group will always be the original string | |
matched.group(0) | |
# '18:05' group(0) will always be the original string | |
matched.group(1) | |
# '18' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
time='18:05' | |
matched = re.match(r'^(0[0-9]|1[0-9]|2[0-3])\:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])$', time) | |
matched.groups() | |
# ('18', '05') | |
matched.group() | |
# '18:05' | |
matched.group(0) | |
# '18:05' group(0) returns the original string | |
matched.group(1) | |
# '18' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
re_three_numbers = re.compile(r'^\d{3}$') | |
re_three_numbers.match('123') | |
# <_sre.SRE_Match object; span=(0, 3), match='123'> | |
re_three_numbers.match('12345') | |
# None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass(object): | |
class_attr = 1 | |
def __init__(self, value): | |
self.instance_attr = value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass(object): | |
class_attr = 0 | |
def __init__(self, instance_attr): | |
self.instance_attr = instance_attr | |
MyClass.class_attr | |
# 0 | |
MyClass.instace_attr | |
# Traceback (most recent call last): | |
# File "/usr/lib/python3.6/code.py", line 91, in runcode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass(object): | |
class_attr = 0 | |
def __init__(self, instance_attr): | |
self.instance_attr = instance_attr | |
print(MyClass.__dict__) | |
# {'__module__': '__main__', 'class_attr': 0, '__init__': <function MyClass.__init__ at 0x7f00cb7fcea0>, '__dict__': <attribute '__dict__' of 'MyClass' objects>, '__weakref__': <attribute '__weakref__' of 'MyClass' objects>, '__doc__': None} | |
my_instance = MyClass(1) | |
print(my_instance.__dict__) | |
# {'instance_attr': 1} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HandsomeGuy(object): | |
name = 'Yang' | |
def __init__(self, value): | |
self.name = value | |
guy = HandsomeGuy('Jack') | |
HandsomeGuy.name | |
# 'Yang' | |
guy.name | |
# 'Jack' |
OlderNewer