zoukankan      html  css  js  c++  java
  • Python的输入指令、格式化输出、基本运算符

    Python的输入指令、格式化输出、基本运算符

    Python的输入指令input

    name = input('Could I know your name please?')
    

    在Python3版本下,输入的所有内容都视为字符串,所以此时name的类型是字符串。如果输入年龄,需要进行转换

    age = int(input('Could I know your age please?'))
    

    在Python2版本下,使用input()输入的内容不会被自动转成字符串,所以需要在输入时指定数据类型。

    而Python2下的raw_input()等于Python3下的input()

    Python的格式化输出

    我们先定义3个变量

    name = '李隆基'
    height = 175
    weight = 80
    
    使用占位符输出信息
    print('My name is %s, height is %d cm, weight is %d kg' % (name, height, weight))
    
    使用format输出
    print('My name is {0}, height is {1} cm, weight is {2} kg'.format(name, height, weight))
    
    使用f-string输出
    print(f'My name is {name}, height is {height} cm, weight is {weight} kg')
    

    仔细观察下列代码,看看3句print的差别(虽然他们的输出结果是一样的)

    name = '李隆基'
    height = 1.75
    weight = 80.5
    print('My name is %s, height is %.2f m, weight is %.2f kg' % (name, height, weight))
    print('My name is {0}, height is {1:.2f} m, weight is {2:.2f} kg'.format(name, height, weight))
    print(f'My name is {name}, height is {height:.2f} m, weight is {weight:.2f} kg')
    

    Python的基本运算类型

    算术运算符

    关于算术运算符,下面的代码就能说明一切了

    x = 11
    y = 7
    plus = x + y
    minute = x - y
    multiply = x * y
    divide = x / y
    divide_exactly = x // y
    mod = x % y
    exponent = x ** y
    print(f'plus is {plus}')
    print(f'minute is {minute}')
    print(f'multiply is {multiply}')
    print(f'divide is {divide}')
    print(f'divide_exactly is {divide_exactly}')
    print(f'mod is {mod}\nexponent is {exponent}')
    

    比较运算符

    代码的执行结果说明了一切

    x = 10
    y = 11
    print(x == y)
    print(x > y)
    print(x < y)
    print(x >= y)
    print(x <= y)
    print(x != y)
    

    赋值运算符

    运算后直接赋值的缩写法

    # x = x + y
    x += y
    # x = x - y
    x -= y
    # x = x * y
    x *= y
    # x = x / y
    x /= y
    # x = x // y
    x //= y
    # x = x % y
    x %= y
    

    逻辑运算符

    老样子,通过代码来学习

    # and 与运算,所有条件都为True,结果才是True。相当于一串布尔值分别做乘法
    print(True and False)
    print(False and False)
    print(True and True)
    # or 或运算,有一个条件为True,结果即True。相当于一串布尔值分别做加法
    print(True or False)
    print(False or False)
    print(True or True)
    # not 把True转成False,把False 转成True
    print(not True)
    print(not False)
    

    身份运算符

    通过两个值的存储单元(即内存地址)进行比较

    is 如果相同则返回True

    is not 如果不同则返回True

    == 用来判断值相等,is 是内存地址

    换言之,

    == 是True,is 不一定是True

    == 是False,is 一定也是False

    is 是True,== 一定是True

    is 是False,== 不一定是False

    逻辑有点饶,关键是理解

    运算符的优先级

    平时多用括号,方便别人,方便自己

    链式赋值

    # 若数字一样时,可以如此
    a = b = c = 10
    print(a, b, c)
    # 若数字不同时,可以如此
    x, y, z = (10, 11, 12)
    print(x, y, z)
    

    交叉赋值

    变量直接互换,就是这么任性

    num_a = 100
    num_b = 200
    print(num_a,num_b)
    num_a,num_b = num_b,num_a
    print(num_a,num_b)
    

    解压缩

    可以快速取出列表中的值,可以使用占位符_ 和 *_

    执行一下下面的代码,效果很棒

    boy_list = ['apple', 'banana', 'pear', 'gripe', 'cherry']
    a, b, c, d, e = boy_list
    print(a, b, c, d, e)
    a, _, _, b, c = boy_list
    print(a, b, c)
    a, *_, b = boy_list
    print(a, b)
    
  • 相关阅读:
    使用正则表达式,取得点击次数,函数抽离
    爬取校园新闻首页的新闻
    网络爬虫基础练习
    [PHP类] 分享一个强大的HTTP访问类(可做采集)
    php程序检测页面是否被百度收录
    php文章相似度计算 不用similar_text()函数
    php实现天干地支计算器示例 php算命程序
    Nginx 1.5.2 + PHP 5.5.1 + MySQL 5.6.10 在 CentOS 下的编译安装
    call和apply实现的继承
    Html语义化标签
  • 原文地址:https://www.cnblogs.com/heroknot/p/10901633.html
Copyright © 2011-2022 走看看