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))

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,340评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,762评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,329评论 0 329
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,678评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,583评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,995评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,493评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,145评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,293评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,250评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,267评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,973评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,556评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,648评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,873评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,257评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,809评论 2 339

推荐阅读更多精彩内容