• Python 入门学习 -----变量及基础类型(元组,列表,字典,集合)


    Python的变量和数据类型

        1 、python的变量是不须要事先定义数据类型的。能够动态的改变

        2、 Python其中一切皆对象,变量也是一个对象,有自己的属性和方法

           我们能够通过 

           来查看变量的类型:变量名.__class__ 

           调用变量的方法:变量名.方法()

    #!/bin/env python
    #coding:utf-8
    
    #type 打印出数据的类型
    
    print type(1)
    print type(1.0)
    
    print type("helloworld")
    
    #虚数 如12j 
    a = 12j + 1
    print a
    print type(a)
    
    # 除法和求余数
    print "5 / 3 = %d" % ( 5 / 3)
    print "5 %% 3 = %d" %( 5 % 3)
    
    #进制数字打印
    print "%d,%o,%x" %(10,10,10)
    
    #查看变量的类型
    a = "hello  world"
    print a.__class__
    #调用变量的方法
    print a.split()

    Tuple(元组)

    #!/bin/env python
    #coding:utf-8
    
    #除了字符串和数值之外,Python还提供了另外4中重要的基本类型:
    #元组、列表、集合和字典。
    
    #元组(tuple) :不可更改的数据序列
    
    a = ("first","second","third")
    
    #输出元组
    print (a)
    #打印元组的长度
    print ("a len : %d" % len(a))
    #遍历元组
    print ("%s %s %s" % (a[0],a[1],a[2]))
    #元组中的一个元素被还有一个元组引用
    b = (a,"four")
    print (b)
    print("%s %s %s %s" % (b[0][0],b[0][1],b[0][2],b[1]))
    
    # 元组能够包括各种类型的数据。可是在创建之后。就不能再改变
    #元组是不可变的。

    (字符串在创建之后也是不可变的,那些看起来改变 #他们的操作实际上创建了新的字符串) #以下的书写将会报错 a[1] = 3


    列表  ---可更改数据的值

    #!/bin/env python
    #coding:utf-8
    
    #列表---可更改数据的内容
    list = ["coffee","tea","toast"]
    
    #list 长度
    print ("list len is %d" %  (len(list)))
    
    #打印list数值
    print ("%s,%s,%s" % (list[0],list[1],list[2]))
    
    #改动list[2]的数值
    list[len(list) - 1] = "sausages"
    print ("list[2] is %s" % (list[2]))
    
    #list 追加
    list.append("waffles")
    print ("list[%d] is %s" % (len(list) - 1,list[len(list) - 1]))
    
    #list 一次性追加多个元素
    list.extend(["juice","decaf"])
    
    print(list)


    字典--

    #!/bin/env python
    #coding:utf-8
    
    
    #字典---以名称索引的分组数据
    dict = {}#空字典
    print (dict)
    
    dict["a"] = "1"
    dict["b"] = "2"
    
    print (dict)
    
    #从字典里获得它全部的键
    key=dict.keys()
    print (list(key))
    #打印键和值
    print ("key[%s] : value[%s]" % (key[0],dict.get(key[0])))
    #从字典里获得它全部的值
    value=dict.values()
    print (list(value))
    
    #字典的工作原理是每一个键是不同的(不能够有全然同样的两个键)
    #可是能够有多个反复的值

    集合

    #!/bin/env python
    #coding:utf-8
    
    #集合 是不包括反复数据的数据集合
    list = ['a','b','a','a','b','b']
    
    print ("list is %s" % (list))
    print ("set is %s " % (set(list)))

    输入例如以下:
    list is ['a', 'b', 'a', 'a', 'b', 'b']
    set is set(['a', 'b']) 



    序列相关的一些操作

    #!/bin/env python
    #coding:utf-8
    
    #序列的其它共同拥有属性
    #字典代表一组数据,但它不是序列。由于它没有衣蛾从头至尾的特定顺序
    
    #1、引用最后一个元素
    
    last_names = ["Dogglass","Jefferson","Williams","Frank"]
    
    print ( "last_names len is %d" % (len(last_names)))
    
    #python 提供了一个捷径,能够通过使用数值-1訪问一个序列的最后一个元素
    
    len = len(last_names)
    print ("last_names[%d] = %s" % ( len -1,last_names[len -1]))
    print ("last_names[%d] = %s" % (  -1,last_names[-1]))
    print ("last_names[%d] = %s" % ( len -2,last_names[len -2]))
    print ("last_names[%d] = %s" % (  -2,last_names[-2]))
    
    #2、序列的范围
    
    #序列分片
    slice_me = ("the","next","time","we","meet","drinks","are","on","me")
    print  (slice_me[5:9])
    slice_me_list = ["the","next","time","we","meet","drinks","are","on","me"]
    print  (slice_me_list[5:9])
    slice_this_string="the next time we meet drinks are on me"
    print (slice_this_string[5:9])
    
    #通过附加序列增长列表
    #append 方法是将一个序列附加到还有一个序列的末端
    slice_me = ("the","next","time","we","meet","drinks","are","on","me")
    apartment = []
    apartment.append(slice_me)
    print ("apartment.append(slice_me) is %s" % (apartment))
    #extend 方法将给定序列中的么个元素插入到调用它的列表中
    apartment=[]
    apartment.extend(slice_me)
    print ("apartment.extend(slice_me) is %s" % (apartment))
    
    #使用列表暂时存储数据
    #为了防止列表变得笨重,能够使用pop方法在处理完列表的一个数据之后
    #将其引用从列表中删除。当删除引用之后。它原来在列表的位置将被兴许元素
    #填上,列表较少的元素个数等于已经弹出的元素个数。

    todays_temperatures = [23,32,33,31] todays_temperatures.append(29) print ("berfore todays_temperatures is :%s" % (todays_temperatures)) print ("todays_temperatures.pop(1) is %d" % (todays_temperatures.pop(1))) print ("end todays_temperatures.pop(1) is :%s" % (todays_temperatures))

    输出例如以下:

    last_names len is 4
    last_names[3] = Frank
    last_names[-1] = Frank
    last_names[2] = Williams
    last_names[-2] = Williams
    ('drinks', 'are', 'on', 'me')
    ['drinks', 'are', 'on', 'me']
    ext 
    apartment.append(slice_me) is [('the', 'next', 'time', 'we', 'meet', 'drinks', 'are', 'on', 'me')]
    apartment.extend(slice_me) is ['the', 'next', 'time', 'we', 'meet', 'drinks', 'are', 'on', 'me']
    berfore todays_temperatures is :[23, 32, 33, 31, 29]
    todays_temperatures.pop(1) is 32
    end todays_temperatures.pop(1) is :[23, 33, 31, 29]


  • 相关阅读:
    Spring(03)Spring IOC 概述
    Spring IoC Bean 创建方法总结
    Spring Boot 目录
    Spring 循环引用(三)AbstractFactoryBean 如何解决循环依赖
    Spring(02)重新认识 IoC
    极客时间
    Spring(01)特性总览
    Spring 核心编程思想目录
    Spring IOC 前世今生之 JDNI
    sharding-jdbc-core 源码分析
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7066594.html
Copyright © 2020-2023  润新知