Last active
April 26, 2018 00:05
-
-
Save vsxen/c2b6c9977f0f9cdb602f1a9867c143d8 to your computer and use it in GitHub Desktop.
cluste
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
# False def if raise | |
# None del import return | |
# True elif in try | |
# and else is while | |
# as except lambda with | |
# assert finally nonlocal yield | |
# break for not | |
# class from or | |
# continue global pass | |
# https://www.programiz.com/python-programming/keyword-list | |
import datetime | |
number = 2 | |
going = True | |
str1 = 'string' | |
a='\u4e2d\u6587' | |
print("---------------base type--------------------") | |
print(number,going,str1,a) | |
shoplist = ['apple', 'mango', 'carrot', 'banana'] | |
mylist = shoplist[:] | |
del mylist[0] | |
dictdemo = {'qq': 'tencent', 'alipay': 'ali', 'linux': 'linus'} | |
print("qq is ", dictdemo['qq']) | |
del dictdemo['qq'] | |
zoo = ('python', 'elephant', 'penguin') | |
new_zoo = 'monkey', 'camel', zoo | |
setdemo = set([1, 2, 3]) | |
print('list',shoplist) | |
print('slice',mylist) | |
print('dict',dictdemo) | |
print('tuple',new_zoo) | |
print('set',setdemo) | |
print("13/2=", 13 / 2,"13//2=", 13 // 2,"13%2=", 13 % 2) | |
print("2<<=", 2 << 2,"11>>=", 11 >> 1,"5&3=", 5 & 3) | |
print("5|3=", 5 | 3, "5~3=", ~5,"5<3", 5 < 3) | |
print("---------------flow----------------------") | |
running = True | |
while running: | |
nowdate = datetime.datetime.now().hour | |
if nowdate > 12: | |
print('afternoon') | |
running = False | |
elif nowdate < 12: | |
print('mornning') | |
running = False | |
else: | |
print('noon') | |
running = False | |
print("---------------function------------------") | |
def say(mes, times=1): | |
print(mes * times) | |
say('hello') | |
say('world', 5) | |
def total(a=5, *numbers, **kw): | |
'''Func can | |
this is a test doc | |
''' | |
print('a', a) | |
for single_item in numbers: | |
print('single_item', single_item) | |
for first, second in kw.items(): | |
print(first, second) | |
print(total(10, 1, 2, 3, Jack=1123, John=2231, Inge=1560)) | |
print("-----------------file--------------------") | |
poem = '''test word | |
''' | |
try: | |
f = open('file.txt', 'w') | |
f.write(poem) | |
except Exception as e: | |
raise e | |
finally: | |
f.close() | |
f = open('file.txt') | |
while True: | |
line = f.readline() | |
if len(line) == 0: | |
break | |
print(line, end = '') | |
f.close() | |
with open('file.txt', 'r') as f: | |
word = f.read() | |
f.close() | |
print("-----------------lamba---------------------") | |
g = [x * x for x in range(1, 11)] | |
for n in g: | |
print(n,end=' ') | |
print('') | |
l=(x for x in range(9)) | |
while True: | |
try: | |
print(next(l),end=' ') | |
except StopIteration: | |
break | |
def fib(max): | |
n, a, b = 0, 0, 1 | |
while n < max: | |
yield b | |
a, b = b, a + b | |
n = n + 1 | |
return 'done' | |
for i in fib(6): | |
print(i,end=' ') | |
print('') | |
print(list(map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9]))) | |
f = lambda x: x * x | |
print(f(5)) | |
def lazy_sum(*args): | |
def sum(): | |
ax = 0 | |
for n in args: | |
ax = ax + n | |
return ax | |
return sum | |
print(lazy_sum(1,3,5,7)) | |
print(lazy_sum(1,3,5,7)()) | |
print("-----------------oop---------------------") | |
class Person: | |
def __init__(self, name): | |
self.name = name | |
def say_hi(self): | |
print('hello', self.name) | |
print() | |
p = Person('python') | |
p.say_hi() | |
class Animal(object): | |
def run(self): | |
print('Animal is running...') | |
class Dog(Animal): | |
pass | |
class Cat(Animal): | |
def run(self): | |
print('cat is running') | |
dog1=Dog() | |
dog1.run() | |
cat1=Cat() | |
cat1.run() | |
print(isinstance(cat1,Animal)) | |
class Robot: | |
number = 0 | |
def __init__(self, name): | |
self.name = name | |
Robot.number += 1 | |
def die(self): | |
print(self.name) | |
Robot.number -= 1 | |
if Robot.number == 0: | |
print(self.name) | |
else: | |
print(Robot.number) | |
def say_hi(self): | |
print('hi this is',self.name) | |
@classmethod | |
def how_many(cls): | |
print(cls.number) | |
droid = Robot("R2-D2") | |
droid.say_hi() | |
Robot.how_many() | |
droid1 = Robot("c-3PO") | |
droid1.say_hi() | |
Robot.how_many() | |
droid.die() | |
Robot.how_many() | |
if __name__ == '__main__': | |
print('run by itself') | |
else: | |
print('import by other') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment