zoukankan      html  css  js  c++  java
  • 与用户交互与运算符

    一、与用户交互

    1.接收用户的输入

    1.1在Python3:input会将用户输入的所有内容都存成字符串类型

    username = input("请输入您的账号:") # "egon"

    print(username, type(username))

    age = input("请输入的你的年龄: ") # age="18"

    print(age, type(age))

    age=int(age) # int只能将纯数字的字符串转成整型

    print(age > 16)

    #1.2 在python2中:

    raw_input():用法与python3的input一模一样

    input(): 要求用户必须输入一个明确的数据类型,输入的是什么类型,就存成什么类型

    age=input(">>>>>>>>>>>>>>>>>>>>>: ")

    : 18

    age,type(age)

    (18, <type 'int'>)

    2.字符串的格式化输出

    1.%S

    1.1 值按照位置与%s一一对应,少一个不行,多一个也不行

    res="my name is %s my age is %s" %('egon',"18")

    res="my name is %s my age is %s" %("18",'egon')

    res="my name is %s" %"egon"

    print(res)

    1.2以字典的形式传值,打破位置的限制

    res="我的名字是 %(name)s 我的年龄是 %(age)s" %{"age":"18","name":'egon'}

    print(res)

    1.3 %s可以接收任意类型

    print('my age is %s' %18)

    print('my age is %s' %[1,23])

    print('my age is %s' %{'a':333})

    print('my age is %d' %18) # %d只能接收int

    2 str.format:兼容性好(推荐)

    2.1按照位置传值

    res='我的名字是 {} 我的年龄是 {}'.format('egon',18)

    print(res)

    res='我的名字是 {0}{0}{0} 我的年龄是 {1}{1}'.format('egon',18)

    print(res)

    2.2打破位置的限制,按照key=value传值

    res="我的名字是 {name} 我的年龄是 {age}".format(age=18,name='egon')
    print(res)

    3. f :python3.5以后才推出

    x = input('your name: ')
    y = input('your age: ')
    res = f'我的名字是{x} 我的年龄是{y}'
    print(res)

    新增

    输出中有%时

    # 格式化输出
    # print('my name is %s age is %s' %('egon',18))
    # print('成功的概率 %s%% ' % (97,))
    

    format新增

    print('{x}=============='.format(x='开始执行')) # 开始执行******
    print('{x:=<10}'.format(x='开始执行')) #   左对齐
    print('{x:=>10}'.format(x='开始执行')) #   右对齐
    print('{x:=^10}'.format(x='开始执行')) #   中间对齐
    开始执行==============
    开始执行======
    ======开始执行
    ===开始执行===
    

    保留小数(四舍五入)

    print('{salary:.3f}'.format(salary=1232132.12351))  
    1232132.124
    

    f的新用法:{}内的字符串可以当做表达式运行

    res=f'{10+3}'
    print(res)
    13
    

    二、基本运算符

    1、算数运算符

    print(10 + 3)

    print(10 / 3) # 结果带小数

    print(10 // 3) # 只保留整数部分

    print(10 % 3) # 取模、取余数

    print(10 ** 3) # 次方 1000

    2、比较运算符: >、>=、<、<=、==、!=

    print(10 > 3)

    print(10 == 10)

    print(10 >= 10)

    print(10 >= 3)

    name=input('your name: ')

    print(name == 'egon')

    3、赋值运算符

    3.1 =:变量的赋值

    name="egon"

    3.2 增量赋值:

        age += 1  # age=age + 1
    
    ​    print(age)
    
    ​    age*=3
    
    ​    age/=3
    
    ​    age%=3
    
    ​    age**=3   #age=age**3
    

    3.3 链式赋值

    x=10

    y=x

    z=y

    z = y = x = 10 # 链式赋值

    3.4 交叉赋值

    m=10
    n=20

    m,n=n,m # 交叉赋值

    print(m,n)

    3.5 解压赋值

    salaries=[111,222,333,444,555]

    3.5.1取前三个值

    x,y,z,*=salaries=[111,222,333,444,555] # *会将没有对应关系的值存成列表然后赋值给紧跟其后的那个变量名,此处为

    print(x,y,z)

    print(_)

    3.5.2取后三个值

    *_,x,y,z=salaries=[111,222,333,444,555]

    print(x,y,z)

    3.5.3解压字典默认解压出来的是字典的key

    x,y,z=dic={'a':1,'b':2,'c':3}
    print(x,y,z) #a ,b, c

    4、逻辑运算符

    4.1 not、and、or的基本使用

    not:就是把紧跟其后的那个条件结果取反

    ps:not与紧跟其后的那个条件是一个不可分割的整体

        print(not 16 > 13)
    
    ​    print(not True)
    
    ​    print(not False)
    
    ​    print(not 10)
    
    ​    print(not 0)
    
    ​    print(not None)
    
    ​    print(not '')
    

    and:逻辑与,and用来链接左右两个条件,两个条件同时为True,最终结果才为真

    只要有一个是假,最终结果就为假

    print(True and 10 > 3)   #true
    
    print(True and 10 > 3 and 10 and 0) # 条件全为真,最终结果才为True
    false
    
    print( 10 > 3 and 10 and 0 and 1 > 3 and 4 == 4 and 3 != 3)  # 偷懒原则
    false
    

    or:逻辑或,or用来链接左右两个条件,两个条件但凡有一个为True,最终结果就为True,

    两个条件都为False的情况下,最终结果才为False

    print(3 > 2 or 0)   true
    
    print(3 > 4 or False or 3 != 2 or 3 > 2 or True) # 偷懒原则 true
    

    4.2优先级not>and>or

    如果单独就只是一串and链接,或者说单独就只是一串or链接,按照从左到右的顺讯依次运算即可(偷懒原则)

    如果是混用,则需要考虑优先级了

    # res=3>4 and not 4>3 or 1==3 and 'x' == 'x' or 3 >3
    # print(res)
    #上面代码可以按优先级用kua号放在一起
    # #       False                 False              False
    # res=(3>4 and (not 4>3)) or (1==3 and 'x' == 'x') or 3 >3
    # print(res)
    

    5.成员运算符

    # print("egon" in "hello egon") # 判断一个字符串是否存在于一个大字符串中
    # print("e" in "hello egon") # 判断一个字符串是否存在于一个大字符串中
    
    # print(111 in [111,222,33]) # 判断元素是否存在于列表
    
    # 判断key是否存在于字典
    # print(111 in {"k1":111,'k2':222})
    # print("k1" in {"k1":111,'k2':222})
    
    # not in
    # print("egon" not in "hello egon") # 推荐使用
    # print(not "egon" in "hello egon") # 逻辑同上,但语义不明确,不推荐
    

    6.身份运算符

    # is # 判断的是id是否相等
    
  • 相关阅读:
    Mysql Dump
    Amazon的Dynamo中用到的几种技术
    一致性哈希
    eclipse下提交job时报错mapred.JobClient: No job jar file set. User classes may not be found.
    eclipse中java heap space问题解决方法
    hadoop的启动步骤
    java根据经纬度计算距离
    eclipse创建Enterprise Application project没有web.xml
    cygwin+hadoop安装步骤和问题
    java程序员修炼
  • 原文地址:https://www.cnblogs.com/chenyoupan/p/12422365.html
Copyright © 2011-2022 走看看