zoukankan      html  css  js  c++  java
  • Python——语言基础

    1.数据类型

      1.1.字符串

        1.1.1.变量声明

        1.1.2.相关函数

      1.2.布尔类型

        1.2.1.变量声明

        1.2.2.相关函数

      1.3.数字类型

        1.3.1.变量声明

        1.3.2.相关函数

      1.4.列表(list)

        1.4.1.创建列表

        1.4.2.获取元素和切片

        1.4.3.添加/替换元素

        1.4.4.移除元素

        1.4.5.其他方法

      1.5.元组(tuple)
        1.5.1.创建元组
        1.5.2.访问元素
        1.5.3.相关方法
      1.6.集合(Set)
        1.6.1.创建集合
      1.7.字典(Dictionary)
        1.7.1.创建字典
        1.7.2.访问字典元素
        1.7.3.删除元素
        1.7.4.其他方法

    1.数据类型

    在Python中的变量不需要声明,直接在使用时赋值即可,变量的数据类型根据其所赋值的类型来确定。

    1.1.字符串

    1.1.1.变量声明

    在Python中声明字符串有三种方式,使用单引号和双引号声明单行字符串,使用三个单引号可以声明多行字符串。

    使用单引号声明:

    str = 'this is a string'

    使用双引号声明:

    str = "this is a string"

    使用三个单引号声明:

    str = '''this is a 
    multi-lines
    strings  
    '''

    1.1.2.相关函数

    将变量转为字符串类型

    strVar = str(x)

    1.2.布尔类型

    1.2.1.变量声明

    boolVar = False
    
    boolVar = True

    1.2.2.相关函数

    将变量转为布尔类型

    boolVar = bool(x)

    1.3.数字类型

    1.3.1.变量声明

    Python中的数字类型分为整型和浮点型。

    intVar = 10
    
    floatVar = 2.5

    1.3.2.相关函数

    将变量转为整型:

    intVar = int(x)

    将变量转为浮点型:

    floatVar = float(x)

    返回绝对值:

    value = abs(x)

    以进一法转换浮点数为整数,注意这个函数是math包下面的,因此要先导入math包:

    import math
    value = math.ceil(4.3)
    
    # result: 5

    舍去小数部分直接返回浮点数的整数部分为整型:

    import math
    value = math.floor(4.9)
    
    # result: 4

    返回常数e的x次幂:

    import math
    
    math.exp(0)          # 1.0
    math.exp(1)          # 2.718281828459045
    math.exp(2)          # 7.38905609893065

    计算对数函数:

    import math
    
    math.log(1)             # 0
    math.log(math.e)        # 1
    math.log(10, 100)       # 0.5
    math.log(100, 10)       # 2

    返回给定参数中的最大值/最小值,可以接收序列:

    max(1, 8, 23, 10)    # 23
    min(1, 8, 23, 10)    # 1

    计算指数函数:

    pow(2,2)        # 4
    pow(2,3)        # 8

    计算开平方:

    sqrt(4)        # 2

    求和:

    sum([1,2,3])        # 6
    sum([1,2,3], 1)    # 7
    
    # 最多接收两个参数,第一个参数是可迭代的对象

    1.4.列表(list)

    Python中的列表用来组织一系列元素的值,通过将这些元素放置在一对中括号中,元素之间使用逗号间隔。列表中的元素可以是不同类型的,不过大多数情况下使用时,都是相同类型的。

    1.4.1.创建列表

    list = [1, 4, 9, 16, 25]
    list = [1, 4, 9, 16, '1', 'b']

    1.4.2.获取元素和切片

    list中的元素可以使用下标获取,下标从0开始

    list = [1, 4, 9, 16, 25]
    
    list[0]        # 1
    list[3]        # 16

    可以通过切片的方式从list中获取一个元素构成的子集,形成一个新的list:

    list = [1, 4, 9, 16, 25]
    
    list[0:3]          # [1, 4, 9]
    list[-3:-1]        # [9, 16]
    list[-3:]          # [9, 16, 25]
    list[:]            # [1, 4, 9, 16, 25] 返回一份新的拷贝

    1.4.3.添加/替换元素

    list = [1, 4, 9, 16, 25]
    
    # 在末尾添加单个元素
    list.append(6)
    print(list)            # [1, 4, 9, 16, 25, 6]
    
    list.append([1, 2, 3])
    print(list)            # [1, 4, 9, 16, 25, 6, [1, 2, 3]]
    
    # 在末尾添加多个元素
    list.extend([0, 1, 2])
    print(list)            # [1, 4, 9, 16, 25, 6, 0, 1, 2]
    
    # 在某个位置插入一个元素
    list.insert(0, 7)
    print(list)            # [7, 1, 4, 9, 16, 25, 6, 0, 1, 2]
    
    # 替换列表中的元素
    list[2:4] = [0, 1, 0]
    print(list)            # [1, 4, 0, 1, 0, 25, 6]

    1.4.4.移除元素

    list = [1, 4, 9, 16, 25]
    
    # 移除最后一个元素并返回该元素
    list.pop()
    print(list)        # [1, 4, 9, 16]

    1.4.5.其他方法

    list = [1, 4, 9, 16, 25]
    
    # 列表长度
    len(list)            # 5
    
    # 统计某个元素出现的次数
    list.count(4)        # 1
    
    # 反转列表
    list.reverse()        # [25, 16, 9, 4, 1]
    
    #排序
    list.sort()            # [1, 4, 9, 16, 25]

    1.5.元组(tuple)

    元组与列表十分相似,不同的是元组中的元素不能修改。列表使用中括号,元组使用小括号。

    1.5.1.创建元组

    tuple = ('this', 'is', 'a', 'tuple', 666)

    1.5.2.访问元素

    元组中元素的访问与列表基本一致。

    1.5.3.相关方法

    seq = [1, 2, 5, 6, 25]
    print(seq)        # [1, 2, 5, 6, 25]
    
    tuple(seq)
    print(seq)        # (1, 2, 5, 6, 25)

    1.6.集合(Set)

    Python中还包含一种可以用来存放无重复元素的数据结构,集合。

    1.6.1.创建集合

    fruitSet = {'apple', 'orange', 'pear', 'apple'}
    
    print(fruitSet)        # {'apple', 'orange', 'pear'}

    1.7.字典(Dictionary)

    字典与列表的区别是字典使用key来作为元素的索引,而列表使用下标作为元素的索引。

    1.7.1.创建字典

    dic = {'key1': 'value1', 'key2': 2, 'key3': [1, 2, 3]}

    1.7.2.访问字典元素

    dic['key1']        # 'value1'
    dic['key1']        # 2
    dic['key1']        # [1, 2, 3]

    1.7.3.删除元素

    del dic['key2']
    
    print(dic)        # {'key1': 'value1', 'key3': [1, 2, 3]}

    1.7.4.其他方法

    dic.clear() 删除字典内所有元素
    dic.copy() 返回一个字典的浅复制
    dic.fromkeys() 创建一个新字典,以序列seq中元素做字典的键,val为字典所有键对应的初始值
    dic.get(key, default=None) 返回指定键的值,如果值不在字典中返回default值
    dic.has_key(key) 如果键在字典dict里返回true,否则返回false
    dic.items() 以列表返回可遍历的(键, 值) 元组数组
    dic.keys() 以列表返回一个字典所有的键
    dic.setdefault(key, default=None) 和get()类似, 但如果键不已经存在于字典中,将会添加键并将值设为default
    dic.update(dict2) 把字典dict2的键/值对更新到dict里
    dic.values() 以列表返回字典中的所有值

    2.流程控制

    2.1.IF语句

    x = 1
    if x > 0:
        print('A')
    elif x == 0:
        print('B')
    else:
        print('C')

    2.2.FOR语句

    遍历一个列表

    fruits = ['apple', 'orange', 'pear']
    
    for f in fruits:
        print(f)

    遍历一个数字序列

    for i in range(5):
        print(i)
    
    # results : 0 1 2 3 4
    
    for i in range(5, 10):
        print(i)
    
    # results : 5 6 7 8 9
    
    for i in range(0, 10, 2):
        print(i)
    
    # results : 0 2 4 6 8
    
    
    fruits = ['apple', 'orange', 'pear']
    
    for i in range(len(fruits)):
        print(fruits[i])
        
    # results : apple orange pear

    2.3.函数

    def aFun(n):
        '''
        this is a function
        :param n:
        :return:
        '''
        for i in range(n):
            print(i)
    
    aFun(10)
  • 相关阅读:
    侧边工具栏
    proguard 使用说明
    人员组成
    google play
    大数据平台相关
    HttpClient
    库克
    html5 开发 android 注意点
    htm5 动画制作 工具
    JAVA取得WEBROOT物理路径
  • 原文地址:https://www.cnblogs.com/weilu2/p/python_basic_syntax.html
Copyright © 2011-2022 走看看