美文网首页
python语法入门一

python语法入门一

作者: 是我真的是我 | 来源:发表于2019-10-10 14:15 被阅读0次
    • 输出
    • 循环分支
    • random模块
    • 类型
    • 运算
    • 列表

    python下载版本说明 web-based、executable 、 embeddable zipfile
    1、X86:对应系統是32位。X86-64对64位系统。
    2、web-based、executable 、 embeddable zipfile是什么意思?
    web-based: 透过网络安装的,就是执行安装后才透过网络下载python
    executable: 可执行文件安装,既把要安装的python全部下载好在本机安装
    embeddable zipfile: zip 压缩档,就是python打包成zip压缩包

    输出

    示例1
    示例2

    循环分支

    print("第一个程序")
    temp = input("猜数字:")
    guess = int(temp)
    while guess != 8:
        if guess == 8:
            print("猜对了")
            print("没奖励")
        else:
            print("猜错了")
        temp = input("猜数字:")
        guess = int(temp)
    print("结束")
    
    str = "abcd"
    for i in str:
        print(i, end=' ')
    # a b c d
    
    strs = ["ab", 'abc', '123']
    for i in strs:
        print(i, len(i))
    # ab 2
    # abc 3
    # 123 3
    
    print(range(5))
    # range(0, 5)
    
    print(list(range(5)))
    # [0, 1, 2, 3, 4]
    
    for i in range(5):
        print(i)
    # 0
    # 1
    # 2
    # 3
    # 4
    
    for i in range(3, 6):
        print(i)
    # 3
    # 4
    # 5
    
    for i in range(1, 10, 2):
        print(i)
    # 1
    # 3
    # 5
    # 7
    # 9
    
    x, y = 4, 5
    if x < y:
        small = x
    else:
        small = y
    print(small)
    # 4
    
    small = x if x < y else y
    print(small)
    # 4
    

    random模块

    import random
    
    num1 = random.randint(1, 3) # [1, 3]
    print(num1)
    
    num2 = random.random() # 随机浮点数 0 <= num2 < 1.0
    print(num2)
    

    类型

    类型转换
    类型判断

    运算


    列表

    arr = ["a", 'bc', '列表']
    print(arr)
    # ['a', 'bc', '列表']
    
    num = [1, 2, 3, 5]
    print(num)
    # [1, 2, 3, 5]
    
    mix = [1, 2, 'aa', "bb", [3, 4]]
    print(mix)
    # [1, 2, 'aa', 'bb', [3, 4]]
    
    num.append(66)
    print(len(num))
    # 5
    print(num)
    # [1, 2, 3, 5, 66]
    
    num.extend([333, 444])
    print(num)
    # [1, 2, 3, 5, 66, 333, 444]
    
    num.insert(1, 'abc')
    print(num)
    # [1, 'abc', 2, 3, 5, 66, 333, 444]
    
    print(num[0])
    # 1
    
    num.remove('abc')
    print(num)
    # [1, 2, 3, 5, 66, 333, 444]
    
    del num[0]
    print(num)
    # [2, 3, 5, 66, 333, 444]
    
    num.pop()
    print(num)
    # [2, 3, 5, 66, 333]
    num.pop(1)
    print(num)
    # [2, 5, 66, 333]
    
    print(num[1:3])
    # [5, 66]
    print(num[:])
    # [2, 5, 66, 333]
    print(num[:3])
    # [2, 5, 66]
    print(num[2:])
    # [66, 333]
    
    l1 = [123]
    l2 = [321]
    print(l1 < l2)
    # True
    l1 = [123, 321]
    l2 = [321, 123]
    print(l1 > l2)
    # False
    print(l1 == l2)
    # False
    print(l1 + l2)
    # [123, 321, 321, 123]
    
    print(123 in l1)
    # True
    print(123 not in l2)
    # False
    
    l1 = [123, 345, 456]
    l1 *= 5;
    print(l1)
    # [123, 345, 456, 123, 345, 456, 123, 345, 456, 123, 345, 456, 123, 345, 456]
    print(l1.count(123))
    # 5
    
    print(l1.index(345))
    # 1
    print(l1.index(123, 2, 10))
    # 3
    
    l1.reverse()
    print(l1)
    # [456, 345, 123, 456, 345, 123, 456, 345, 123, 456, 345, 123, 456, 345, 123]
    
    l1 = [2, 3, 7, 6, 0, 3, 5]
    l1.sort()
    print(l1)
    # [0, 2, 3, 3, 5, 6, 7]
    
    l1.sort(reverse=True)
    print(l1)
    # [7, 6, 5, 3, 3, 2, 0]
    
    l2 = l1[:]
    print(l2)
    # [7, 6, 5, 3, 3, 2, 0]
    
    l3 = l1
    print(l3)
    # [7, 6, 5, 3, 3, 2, 0]
    
    l1.sort()
    print(l1)
    # [0, 2, 3, 3, 5, 6, 7]
    print(l2)
    # [7, 6, 5, 3, 3, 2, 0]
    print(l3)
    # [0, 2, 3, 3, 5, 6, 7]
    

    相关文章

      网友评论

          本文标题:python语法入门一

          本文链接:https://www.haomeiwen.com/subject/zsmnpctx.html