Skip to content

Instantly share code, notes, and snippets.

@ericxyan
Created November 5, 2015 21:25
Show Gist options
  • Save ericxyan/a977303b655723680edb to your computer and use it in GitHub Desktop.
Save ericxyan/a977303b655723680edb to your computer and use it in GitHub Desktop.
My study notes
# Python 2.7
# 1. Iterator
def f(n):
return [x**4 for x in range(n)]
def f2(n):
for x in range(n):
yield x**3
# Check if is a prime
def isPrime(n):
if n == 1:
return False
for t in range(2,n):
if n % t == 0:
return False
return True
# Iterator
def primes(x=1):
n = 1
while n < x:
if isPrime(n): yield n
n += 1
# 2. __init__.py: is usually an empty py file, the hierarchy gives us a covenient way of organizing the files.
# 3. xrange
# 4. backquote `: ''.join([`x` for x in xrange(10)])
# ''.join(str(x) for x in xrange(11))
# 5. assert: assert condition, check the condition
# 6. multi-line statements: \
# 7. str[2:4]: [2,4)
# 8. print str * 2, print twice
# 9. print lst * 2, [1, 2, 1, 2] return one list
# 10. tuple (): cannot be updated, read-only list.
# 11. list(s): convert to list
# 12. //: floor division
# 13. max(x1, x2,...)
# 14. pow(x,y)
# 15. round(x[,n])
# String
# 16. String +, *, in, not in, [:]-> concatenation, repetition
# 17. print "my name is %s and age is %d" % ('Eric', 25)
# 18. str.count(substr); str.find(substr); str.index(substr); str.join(
# 19. str.replace(old,new[,max])
# 20. str.split(delimiter);
# 21. str.splitlines();
# List
# 22. Delete: del list[2]
# 23. Length: len([1,2,3])
# 24. Concatenation: [1,2] + [3,4] = [1,2,3,4]
# 25. Repetition: [1,2]*2 = [1,2,1,2]
# 26. Membership: 3 in [1,2,3]: true
# 27. list[1:], list[-1]->the right most one
# 28. list1, list2 = [1,'xyz'], [2,'abc']
# 29. max(lst)
# 30. lst.append(x); lst.count(x), lst.index(x), lst.insert(index,x);
# 31. lst.pop(index): pop and remove, default index = -1
# 32. lst.remove(index)
# 33. lst.reverse()
# 34. lst.sort(func)
# Dict
# 35. del dic['key']; del dict; dic.clear();
# 36. dict.has_key(key)
# 37. dict.keys(); dict.values();
# I/O
# 38. str = input('Enter your input: ')
# 39. file fd = open(filename [, access mode][, buffering])
# 40. file.name; file.closed
# 41. fl.write(string)
# 42. fl.read([byte#])
# 43. fl.tell(): cursor position
# 44. fl.seek(offset [,from]), from=0->beginning, from=1->current, from=2->end
# Class
# 45. Constructor: __init__(slef, arg1, arg2)
# 46. Method: as normal function except that the 1st arg is self
# 47. Instance: instance1 = Class1(arg1, arg2)
# 48. geattr(obj1, attri1); hasattr(obj1,attri1); setattr(obj,attr1,val)
# 49. __doc__, __dict__, __name__, __str__(self),
# 50. Inheritance: class child(Parent1, Parent2):
# 51. issubclass(sub, sup); isinstance(obj,class)
# 52. def __ad__(self,other): return Vector(self.a + other.a, self.b + other.b)
# v1 + v2
# 53. Data Hiding: __attribute
# 54. obj._className__attrName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment