Python3 基础语法 从入门到精通二

符号 阅读:670 2021-03-31 21:21:04 评论:0
#Python3  变量赋值 
i = 100; #整型变量 
d = 100.0; #浮点型 
s = "Python3"; #字符串 
 
print(i); 
print(d); 
print(s); 
 
#Python3 列表 
list = ['one','two','three','four','five']; 
 
print(list); 
print(list[0]); 
print(list[1:3]); 
print(list[2:]); 
print(list * 2); 
 
#Python3 元组 
arraylist = ('1','2','3','4','5'); 
print(arraylist); 
print(arraylist[0]); 
print(arraylist[1:3]); 
print(arraylist[2:]); 
print(arraylist * 2); 
 
# Python3 字典 
dirt ={}; 
dirt['one'] = 'one'; 
dirt['two'] = 'two'; 
dirt['thress'] = 'three'; 
dirt['four'] = 'four'; 
dirt['five'] = 'five'; 
dirt[0] =0; 
dirt[1] =1; 
dirt[2] =2; 
dirt[3] =3; 
 
print(dirt['one']); 
print(dirt[0]); 
print(dirt); 
print(dirt.keys()); 
print(dirt.values()); 
 
#Python3 数据类型转换 
a = int('1'); 
print("type is {0}".format(type(a))); #type 基础数据类型判断 
 
b = float(1); 
print("type is {0}".format(type(b))); #type 基础数据类型判断 
 
c = str(1); 
print("type is {0}".format(type(c))); #type 基础数据类型判断 
 
d = repr(1.0); 
print("type is {0}".format(type(d))); #type 基础数据类型判断 
 
#Python3 成员运算符 
e = 1; 
f = 6; 
lists = [1,2,3,4,5]; 
 
def whether(a,b): 
    if(a in b): 
        return 'true'; 
    else: 
        return 'false'; 
 
print("e is it inside {0}".format(whether(e,lists))); 
print("f is it inside {0}".format(whether(f,lists))); 
 
#Python3 身份运算符(基于对象) 
class Dog: 
    def __init__(self,weight): 
        self.weight = weight; 
    def getWeight(self): 
        print("my weight is {0}".format(self.weight)); 
 
toady = Dog(10); 
boogie = Dog(15); 
 
if(toady is boogie): 
    print("true"); 
else: 
    print("false"); 
 
#Python3 身份运算符(基于数据类型) 
a = 10; 
b = 10; 
if(a is b): 
    print("true"); 
else: 
    print("false"); 
 
 
 
 
 
 
 
 
 

标签:Python
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号