首页 > 代码库 > python 学习之 PythonBasic1
python 学习之 PythonBasic1
#!/usr/bin/python #coding=utf-8 class switch(object): def __init__(self, value): self.value = value self.fall = False def __iter__(self): yield self.match raise StopIteration def match(self, *args): if self.fall or not args: return True elif self.value in args: self.fall = True return True else: return False operator = "+" x = 1 y = 2 for case in switch(operator): if case(‘+‘): print x + y break if case(‘-‘): print x - y break if case(‘*‘): print x * y break if case(‘/‘): print x / y break if case(): print "" #基本数据类型 print(‘Hello World !‘) #变量不需要声明 a = 1.3 #回收变量名 print(a),type(a) #常用数据类型 #变量数据类型 #a=10 int 整数 #a=1.3 float 浮点数 #a=True 真值(True/False) #a=‘Hello!‘ 字符串 #序列 #tuple定值表 s1 = (2,1.3,10,‘who‘,5.8,12,False) #list 表 s2 = [True,5,‘smile‘] #元素的引用 print(s1),(s2),type(s1),type(s2) s2[0] = 3.1415926 print(s2) print s1[:5] print s1[2:] print s1[0:5:2] print s1[0:8:1] print s1[7::-1] #字符串是元组 str = ‘abcdefghijklmnopqrstuvwxyz‘ print str[2:4] print str[0::1] #数学运算 print 1+1 print 1-32 print 1.34*26 print 199/15 print 198%55 print 3**9 #判断 print 5 == 6 print 8.0!=8.0 print 3<3, 3<=3 print 4>5, 4>=0 c = 2.5 #逻辑运算 print a is not c print True and True, True and False print True or False print not True #缩进 i = 1 if i > 0: x = 1 y = 1 print x+y #if语句 i = 1 if i != 1: x = 5 y = 2 print x*y else: print ‘somthing worry‘ i = -2 x = 1 if i > 0: x = x+1 print x i = 6 if i > 0: print ‘positive i‘ i = i + 1 if i > 2: print ‘i bigger than 2‘ print ‘even better‘ elif i == 0: print ‘i is 0‘ i = i * 10 else: print ‘negative i‘ i = i - 1 print ‘new i:‘,i
本文出自 “小陌成长之路” 博客,谢绝转载!
python 学习之 PythonBasic1
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。