关键词搜索

源码搜索 ×
×

Python学习:基础知识

发布2019-01-02浏览565次

详情内容

  1. 类型转换
a =3
a = str(a)
type(a)	#查看类型
<class 'str'>
    1. 输出语句
    a = 33
    b = 44
    print('a的值为%d,b的值为%d' % (a,b))
    #一行输出多个数据,各个数据间用空格隔开
    set1 = {1,2,3,4,5,2,3,1}
    for each in set1:
        print(each,end=' ')
     # 结果值	1 2 3 4 5 
    print("Hello"+","+"World"+"!!!")
    # Hello,World!!!
    
      5
    • 6
    • 7
    • 8
    • 9
    • 10
    1. 断言
      assert 这个关键字我们称之为“断言”,当这个关键字后边的条件为假的时候,程序自动崩溃并抛出AssertionError的异常。
      同时,我们也可以添加字符串来解释断言
    assert 5>6
    #抛出的错误
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    AssertionError
    
      5

    在这里插入图片描述
    4. range()

    语法:range(strat,stop,[step =1])
            # range(2,10,3)
     a. 这个BIF有三个参数,其中用中括号括起来的两个表示这两个参数是可选的。
     b. step = 1表示第三个参数的值默认值是1. 
     c.range这个BIF的作用是生成一个从start参数的值开始到stop参数的值结束的数字序列。
     
    
      5
    • 6
    #for 语句
    for i in range(1,10,3):
        print(i)
    
    • 1
    • 2
    • 3
    1. 如果字符串中需要出现单引号或双引号:
    # 使用转义符号(\)对字符串中的引号进行转义
    'Let\'s go'
    # 使用不同引号
    "Let's go"
    # 使用三引号
    '''Let's go'''
    
      5
    • 6
    1. 输出原始字符串
    #只需要在字符串前边加一个英文字母 r 即可
    path = r'C:\Program Files\FishC\Good'
    # 输出结果
    'C:\\Program Files\\FishC\\Good'
    #如果非要在字符串的最后输入反斜杠,可以在最后加上‘\\’
    path = r'C:\Program Files\FishC\Good' '\\'
    # 输出结果
    'C:\\Program Files\\FishC\\Good\\'
    
      5
    • 6
    • 7
    • 8
    1. 循环和分支
    import random
    #random模块里randint(),它会返回一个随机函数。
    secret = random.randint(1,10)
    temp = input("输入一个数值:")
    guess = int(temp)
    # while语句
    while guess !=secret:
        temp = input("input again")
        guess = int(temp)
        # if - else 语句
        if guess == secret:
            print("you are right")
        else:
            if guess > secret:
                print("more big")
            else:
                print("more small")
    print("Game over")
    
      5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    # break 语句终止当前循环,跳出循环体
    bingo = "right"
    answer = input("Please enter your answer:")
    
    while True:
        if answer == bingo:
            break
        answer = input('Please again enter your answer:')
    
      5
    • 6
    • 7
    • 8
    #continue语句终止本轮循环,并开始下一轮循环
    for i in range(10):
        if i%2 !=0:
            print(i)
            continue
        i += 2
        print(i)
    
      5
    • 6
    • 7
    1. Python中的逻辑运算符or,and
    # or 或的意思,左右两边有一边为真就往下执行
    def fun(x):
        if x==1 or x==2:
            return x
        else:
            return fun(x-1)+fun(x-2)
    # and 与的意思,左右两边均为真,猜往下执行操作
    if (a==3 and b==4):
        print('you are right')
    
      5
    • 6
    • 7
    • 8
    • 9
    1. 分行打印语句
    html = """
    <li><a href="https://blog.csdn.net/nanhuaibeian/article/details/86066888" rel="nofollow">网页基础</a></li>
    <li><a href="https://blog.csdn.net/nanhuaibeian/article/details/85521071" rel="nofollow">文件存储</a></li>
    <li><a href="https://blog.csdn.net/nanhuaibeian/article/details/85505387" rel="nofollow">文件命名</a></li>
    """
    for each in html:	
    	print(each,end='')
    
      5
    • 6
    • 7

    在这里插入图片描述
    10. 输入语句 input得到的是字符串类型

    在这里插入图片描述
    因此如果 想要得到一个 整型的数值 a ,需要进行格式变换

    1. 序列解包
      多个赋值操作可以同时进行
      也可以同时交换两个(或更多个)变量
    x,y,z = 1,2,3
    # x = 1
    x,y = y,x
    # x = 2
    values = (1,2,3)
    x,y,z = values
    # x = 1
    
      5
    • 6
    • 7
    1. 增量赋值
      增量赋值不止对整型数据适合,也适用于其他数据类型
      在这里插入图片描述
    2. elif 子句:是else if 的简写

    在这里插入图片描述

    1. is :同一性运算符
      == 判定两个对象是否相等不同,使用 is 判定两者是否等同(同一个对象)
      在这里插入图片描述

    2. 字符串和序列比较
      字符串可以按照字母顺序排列进行比较
      其他序列也可以用同样的方式进行比较,不过比较的不是字符而是其他类型的元素
      在这里插入图片描述

    3. 一些迭代工具
      并行迭代zip 函数,可以把两个序列“压缩”在一起,然后返回一个元祖的列表,是一个可迭代对象,可以使用 list(zip(names,ages)) 转换返回的对象
      在这里插入图片描述
      当处理不等长的序列时,当最短的序列“用完”的时候就会停止
      在这里插入图片描述
      按索引迭代内建函数 enumerate 函数 在提供索引的地方迭代索引-值对
      在这里插入图片描述
      翻转和排序迭代reversed 函数和 sorted 函数,不是原地修改对象,而是返回翻转或排序后的版本,可以作用在任何序列或可迭代对象上,其中 sorted 方法返回列表,reversed 方法返回一个迭代对象,可以使用 list 类转换返回的对象
      在这里插入图片描述

    4. pass 语句:什么都不用做的语句,可以称作占位符,进行测试用
      在这里插入图片描述

    5. 使用 del 删除
      python 中是没有办法删除值的(也不需要过多考虑删除值的问题),因为在某个值不再使用的时候,Python解释器会负责内存的回收
      del 语句不仅会移除一个对象的引用,也会移除那个名字本身
      在这里插入图片描述

    相关技术文章

    点击QQ咨询
    开通会员
    返回顶部
    ×
    微信扫码支付
    微信扫码支付
    确定支付下载
    请使用微信描二维码支付
    ×

    提示信息

    ×

    选择支付方式

    • 微信支付
    • 支付宝付款
    确定支付下载