zoukankan      html  css  js  c++  java
  • Python的简单语法(一)

    import sys
    a=3
    b=4
    
    c=5.66
    d=8.0
    
    e=complex(c,d)
    f=complex(float(a),float(b))
    
    print("a is type:",type(a))
    print("c is type:",type(c))
    print("e is type:",type(e))
    
    
    
    print(sys.float_info)
    
    age=3
    name="sunliyuan"
    print("{0} was {1} years old".format(name, age))
    
    '''保证所有的变量都是字符串'''
    print(name+"was"+ str(age) +" years old")
    

      List结构列表:

    #创建list
    number_list=[1,3,5,7,9]
    print("number_list:"+ str(number_list))
    
    string_list=["abc","bbc","python"]
    
    #创建混合的元素列表
    mixed_list=["python",".net",3,12]
    print("string_list"+str(string_list))
    print("mixed_list"+ str(mixed_list))
    
    #访问列表中的
    second_num=number_list[1]
    third_string=string_list[2]
    fourth_mixed=mixed_list[3]
    
    print("second_num:{0} third_string:{1} fourth_mixed:{2}".format(second_num,third_string,fourth_mixed)) 
    
    
    #更新列表中的元素
    number_list[1]=30
    print("number_list after: "+str(number_list))
    
    #删除list 的元素
    del number_list[1]
    print("number_list after del: "+ str(number_list))
    
    #判断是否在元素内
    print(len([1,2,3]))
    print([1,2,3]+[4,5,6])
    print(['Hellow']* 4)
    print(3 in [1,2,3])
    
    abcd_list=['a','b','c','d']
    print(abcd_list[1])
    #除右边数
    print(abcd_list[-2])
    #: 代表剩下的所有元素
    print(abcd_list[1:])
    

      换行:

    print("what's your name? 
     Tom")
    

      Tuple:一旦创建不能修改

    number_tuple=(1,3,5,7,9)
    print("number_list:"+ str(number_tuple))
    
    string_tuple=("abc","bbc","python")
    
    mixed_tuple=("python",".net",3,12)
    
    print("number_list:"+ str(number_tuple))
    print("string_list"+str(string_tuple))
    print("mixed_list"+ str(mixed_tuple))
    
    
    #Tuple(元组)一旦被创建就不可以更改        List可以
    
    #删除  Tuple不可以删除其中的元素    但是可以删除整个Tuple
    del mixed_tuple
    
    #打印长度
    print(len((1,2,3)))
    print((1,2,3)+(4,5,6))
    print(('Hellow') * 4)
    print(3 in (1,2,3))
    
    abcd_list=('a','b','c','d')
    print(abcd_list[1])
    #除右边数
    print(abcd_list[-2])
    #: 代表剩下的所有元素
    print(abcd_list[1:])
    

      Tuple和List

    #创建只包含一个元素的tuple
    a_tuple=(2,)
    
    #Tuple中的list
    mixed_tuple=(1,2,['a','b'])
    print("mixed_tuple: " +str(mixed_tuple))
    
    #更改tuple中的list
    mixed_tuple[2][0]='c'
    mixed_tuple[2][1]='d'
    print("mixed_tuple after " +str(mixed_tuple))
    
    #Tuple比List操作速度要快。如果定义了一个值得常量集,并且唯一要做的不断地遍历他,使用Tuple代替list    Tuple比较安全(不能修改)
    

      

  • 相关阅读:
    十个能让你成为牛逼前端程序猿的特征
    一道Javascript面试题引发的血案
    程序员实现财务自由的9个阶段,你达到了哪一段?
    程序员进阶路上不能错过的史上最全技术知识图谱秘籍
    清华大学研发神技能:用意念回复微信
    机器学习原来如此有趣:用深度学习识别人脸
    【代码片段】如何使用CSS来快速定义多彩光标
    Android自定义一款带进度条的精美按键
    现在的人工智能逆天到什么地步了?
    分享几套生成iMac相关高逼格免费mockup的素材和在线工具
  • 原文地址:https://www.cnblogs.com/sunliyuan/p/6262933.html
Copyright © 2011-2022 走看看