python基本语法练习记录

# -*- coding: UTF-8 -*-
# a = int(input())
# if a > 100:
#   print 100
# else:
#   print 0


# print('''
# line1
# line2
#   ''')

# print("growth rate: %d%% %s" % (7,"minping"))

# classmates = ["aaa","bbb","ccc"]
# print(len(classmates))
# classmates.append("ddd")
# classmates.insert(1,"aaa1")
# res = classmates.pop()
# res = classmates.pop(1)
# classmates[1] = "bbbb1"
# L = ["apple",123, True]
# classmates[1] = L
# # print(classmates)
# # print(classmates[1][1])
# classmatessss = ("AAA","BBB","CCC")
# classmates
# # print(classmatessss)#tuple不可变,不能被二次赋值,也没有append,insert,pop等操作

# t = ('a',('b','c'),['A','B'])
# # print(len(t))
# # t[2][0] = 'a'
# # t[2][1] = 'b'
# # print(t)
# classmates[1] = t
# print(classmates)
# # classmates[1][0] = 'A'
# # classmates[1][1][0] = 'B'
# classmates[1][2][1] = 'b'
# # print(classmates)


# res1 = 'a'
# res2 = 'b'
# te = (res1, res2)
# print(te)
# res2 = 'B'
# print(te)


# classmates = ['a','b','c']
# t = (1, 2, classmates, 4)
# print(t)
# t[2][0] = 'A'
# print(t)
# t[2] = t[2].append('d')
# print(t)


# classmates = ["aaa","bbb","ccc",'ddd']
# print(len(classmates))
# classmates.append("eee")
# print(classmates)
# classmates.insert(1,"aaa1") #指定位置insert,后面的元素顺势后移
# print(classmates)
# res = classmates.pop()
# print(classmates)
# res = classmates.pop(1)  #指定位置pop,后面的元素顺势前移
# print(classmates)
# classmates[1] = "bbbb1" #指定位置赋值
# L = ["apple",123, True] #slice里每个元素类型可以不一样
# classmates[1] = L #slice里的元素也可以为slice
# print(classmates)


# birth = input('birth: ')
# birth = int(birth)
# if birth < 2000:
#     print('00前')
# else:
#     print('00后')

# name = ['a','b','c']
# for k in name:
#   print(k)
# sums = 0
# num = 99
# while num > 0:
#   if num%2 == 0:
#       continue
#   sums += num
#   num -=1
#   if num == 95:
#       break
# # nums = list(range(101))
# # for num in nums:
# #     sums += num
# print(sums)

# d = {'minping':99, 'shuxin':98}
# print(d)
# print(d["minping"])
# print(d.get("minping1"))
# d.pop("minping")
# print(d)

# s = set([1,1,1,2,3,2])
# print(s)
# s.add(4)
# s.add(-1)
# print(s)
# s.remove(3)
# print(s)
# s.remove(-2)
# print(s)
# s1 = set([1,2,3])
# s2 = set([2,3,4])

# # print(s1 & s2) #交集
# # print(s1 | s2) #并集
# # print(s1 - s2) #差集
# s1.add([1,2])
# print(s1)

# print(max(1,2,3))

# def my_abs(x):
#   if not isinstance(x,(int,float)):
#       raise TypeError('bad param type')
#   if x >= 0:
#       return x
#   return -x



# def nop():
#   pass

# ddd = my_abs
# print(ddd('A'))

# import math

# def move(x, y, step, angle=0):
#   nx = x + step*math.cos(angle)
#   ny = y - step*math.sin(angle)
#   return nx, ny
    

# x, y = move(100, 100, 60, math.pi/6)
# print(x, y)

# val = move(100, 100, 60, math.pi/6)
# print(val)

# def power(x, n=2):
#   if not isinstance(x, (int, float)):
#       raise TypeError('input param type error, must be a int or float')
#   res = 1
#   while n > 0:
#       res = res * x
#       n -= 1
#   return res


# print(power(3,2))
# print(power(5))

# def stuinfo(name, gender, age=6, city='Beijing'):
#   print('name:', name)
#   print('gender:', gender)
#   print('age:', age)
#   print('city:', city)

# stuinfo('minp', 'male', city='shanghai')

# def add_end(l=[]):
#   l.append('end')
#   return l

# print(add_end([1,2,3]))
# print(add_end(['x','y','z']))
# print(add_end())
# print(add_end())
# print(add_end())

# def calc(*numbers):
#   sums = 0
#   for num in numbers:
#       sums += num
#   return sums

# res1 = calc(1,2,3) #传入多个参数
# res2 = calc(*[1,2,3])  #传入一个list
# res3 = calc(*(1,2,3))  #传入一个tuple

# print(res1)
# print(res2)
# print(res3)

# def person(name, age, **kw):
#   print('name is :', name, 'age is :', age, 'other is :',kw)

# person('Michael', 30)
# person('Bob', 35, city='Beijing')
# person('Adam', 45, gender='M', job='Engineer')

# from collections import Iterable


# extra = {'city':'Beijing', 'job':'Engineer'}
# extra = '123'
# if (isinstance(extra, Iterable)):
#   for v in extra:
#       print(v)
# else:
#   print("eeee")
# person('Jack', 24, **extra)


# val = [(1,1), (2,4), (3,9)]

# for x, y in val:
#   print(x,"ddd", y)

# val = [x*x for x in range(2,11) if x %2 == 0]
# print(val)

# val = [m+n for m in 'abc' for n in '123']
# print(val)

# val = [m+n+p for m in 'abc' for n in '123' for p in 'IVM']
# print(val)

# import os

# val = [d for d in os.listdir('.')]
# print(val)

# d = {'city':'Beijing', 'job':'Employer'}
# val = [k+'='+v for k,v in d.items()]
# print(val)

# L = ['Hello', 'World', 'IBM', 'Apple']
# val = [v.lower() for v in L]
# print(val)

# g = (x*x for x in range(1,3))
# # print(next(g))
# # print(next(g))
# # print(next(g))
# # print(next(g))
# for v in g:
#   print(v)

# def fib(max):
#   a, b = 1, 1
#   while b < max:
#       yield b 
#       a, b = b, a+b
#   return 'done'

# f = fib(6)

# while True:
#   try:
#       v = next(f)
#       print(v)
#   except StopIteration as e:
#       print('Gengerator return value is: ',e.value)
#       break

# def f(x):
#   return x*x

# r = map(f, list(range(10)))
# print(list(r))
# # for v in r:
# #     print(v)

# print(list(map(str, [1,2,3,4,5])))

# def add(x, y):
#   return x+y

# r = reduce(add, [1,2,3,4,5])
# print(r)

# def fn(x, y):
#   return 10*x +y

# def str2int(strstr):
#   return reduce(fn,list(map(int, list(strstr))))

# print(str2int('123456789'))

# def is_odd(n):
#   return n%2 == 0

# r = filter(is_odd,[1,2,3,4,5])
# print(next(r))


# def _odder():
#   n = 3
#   while True:
#       yield n 
#       n += 2

# def primes():
#   yield 2
#   it = _odder() 
#   while True:
#       v = next(it)
#       yield v 

# p = primes()
# print(p)
# print(next(p))
# print(next(p))


# r = sorted([1,5,-3,6,2], key=abs)
# print(r)

# l = ['bob', 'about', 'Zoo', 'Credit']
# r = sorted(l, key = str.lower, reverse = True)
# print(r)

# def lazy_sum(*args):
#   def my_sum():
#       sums = 0
#       for v in args:
#           sums += v
#       return sums
#   return my_sum

# r = lazy_sum(*[1,2,3,4,5])
# print(r)
# print(r())

# def count():
#   fs = []
#   for i in range(1,4):
#       def f():
#           return i*i
#       fs.append(f)
#   return fs 

# f1,f2,f3 = count()
# print(f1())
# print(f2())
# print(f3())



# def count_new():
#   fs = []
#   def f(i):
#       def g():
#           return i*i
#       return g 
#   for i in range(1,4):
#       fs.append(f(i))
#   return fs

# f1,f2,f3 = count_new()
# print(f1())
# print(f2())
# print(f3())

# f = lambda x: x*x
# print(f)

# ' a test module '

# __author__ = 'mp'

# import sys



# # def test():
# #     arg = sys.argv
# #     if len(arg) == 1:
# #         print('hello world')
# #     elif len(arg) == 3:
# #         print('Hello, %s %s' % (arg[1], arg[2]))
# #     else:
# #         print('too many param')

# def _private1(name):
#   return 'hello %s' % name

# def _private2(name):
#   return 'hi %s' % name 

# def greeting(name):
#   if len(name) < 3:
#       print _private1(name)
#   else:
#       print _private2(name)
#   return
    

# if __name__=='__main__':
#   greeting("shuxin")

# class Student(object):
#   def __init__(self, name, score):
#       self.__name = name 
#       self.__score = score

#   def print_score(self):
#       print('%s:%s' % (self.__name, self.__score))

# bart = Student("minping", 99)
# lisa = Student("shuxin", 100)
# bart.__name = 'ddd'
# print(bart.__name)
# print(bart.Student.__name)
# # lisa.print_score()

# class Animal(object):
#   def run(self):
#       print('Animal is running...')

# class Dog(Animal):
#   def run(self):
#       print('Dog is running...')

# class Cat(Animal):
#   def run(self):
#       print('Cat is running...')

# dog = Dog()
# dog.run()
# cat = Cat()
# cat.run()

# print(isinstance(dog, Dog))
# print(isinstance(dog, Animal))
# print(isinstance(dog, Cat))

# def run_t(animal):
#   animal.run()

# run_t(Animal())
# run_t(Dog())
# run_t(Cat())

# print(dir(cat))

# import logging

# def foo(s):
#   return 10/int(s)

# def main():
#   try:
#       foo(0)
#   except Exception as e:
#       logging.exception(e)

# main()
# print('END')

# with open('2.py', 'a') as f:
#   res = f.write('shuxin')

# with open('2.py') as f:
#   res = f.read()
#   print(res)

# from io import BytesIO as StringIO
# f = StringIO()
# f.write('minping')
# print(f.read())
# while True:
#   s = f.readline()
#   if s == '':
#       break
#   print(s)


# from io import BytesIO
# f = BytesIO()
# f.write('中文'.encode('utf-8'))
# print(f.getvalue())

# import os
# print(os.environ)

# import json
# d = {'name':'minping', 'age':20}
# r = json.dumps(d)
# print(r)
# print(isinstance(r, dict))

# re = json.loads(r)
# print(re)
# print(isinstance(re, dict))


# import json
# class Student(object):
#   def __init__(self, name, score):
#       self.name = name
#       self.score = score

# # def obj2json(obj1):
# #         return {'name':obj1.name, 'score':obj1.score}

# s = Student('shuxin', 99)
# print(json.dumps(s, default = lambda obj: obj.__dict__))

from datetime import datetime
now = datetime.now()
# print(now)
# dt = datetime(2013, 12, 12, 14, 15, 00)
# print(dt.timestamp())
print(datetime.fromtimestamp(1386828900.0))

你可能感兴趣的:(python基本语法练习记录)