zoukankan      html  css  js  c++  java
  • python语法入门之与用户交互,常用运算符

    python语法入门之与用户交互,常用运算符

    一、用户与程序的交互

    1.1什么是用户交互

    用户交互就是往计算机input/输入数据,计算机print/输出结果。

    1.2为什么要与用户交互

    交互时为了能够像人一样与用户沟通方便。

    1.3 如何与用户交互

    交互的本质就是输入输出。

    3.1 输入input:

    在python3中input无论输入的什么类型的内容,都会存为字符串类型。

    name = input('请输入用户名:')
    请输入用户名:123
    >>> print(type(name))
    <class 'str'>
    >>> print(name)
    123
    >>> name = input('请输入用户名:')
    请输入用户名:nnnn
    >>> print(type(name))
    <class 'str'>

    在python2中由一个raw_input功能与python3中的input功能一样但是2中还有一个input功能,需要用户明确输入的数据类型,输入什么类型,就会存成什么类型。

       input一定要声明你输入的类型
               >>> input(">>:")
               >>:sean
               Traceback (most recent call last):
                 File "<stdin>", line 1, in <module>
                 File "<string>", line 1, in <module>
               NameError: name 'sean' is not defined
               >>> input(">>:")
               >>:"sean"
               'sean'
               >>> input(">>:")
               >>:1
               1
               >>> input(">>:")
               >>:[1,2]
              [1, 2]
               >>>

               -------------------------------
               >>> raw_input(">>:")
               >>:sean
               'sean'
               >>> raw_input(">>:")
               >>:12
               '12'

    3.2 输出print:
    >>> print('hello,world')
    hello,world

     

    3.3 输出之格式化输出

    3.3.1 什么时格式化输出?

    把一段字符串里面的内容替换掉之后再输出,就是格式化输出。

    3.3.2 为什么要格式化输出

    将输出具体某种格式的内容替换成集体的内容。

    3.4 如何格式化输出

    使用占位符

    3.41 %S :可以接受任意类型的占位符
    >>> name = 'hao'
    >>> age = 21
    >>> print('my name is %s,age is %s'%(name,age))
    my name is hao,age is 21
    3.42 %d:只能接受数字
    >>> print('my name is %s,age is %s'%(name,age)
    ... )
    my name is hao,age is 21
    >>> print('my name is %d,age is %d'%(name,age)
    ... )
    Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
    TypeError: %d format: a number is required, not str
    >>> print('my name is %s,age is %d'%(name,age))
    my name is hao,age is 21
    3.43 formart: 可以接受不限个参数,位置可以不按顺序 .
    print('name:{user},age:{age}'.format(user=name,age=age))
    name:hao,age:21
    >>> print('name:{user}, age:{age}'.format(age=age, user=name))
    name:hao, age:21
    >>> name = hao
    3.44 f-string: 用大括号 {} 表示被替换字段,其中直接填入替换内容
    >>> name = 'hao'
    >>> f'my name is {name}'
    'my name is hao'
    >>> print(f'my name is {name}')
    my name is hao
    3.45 %f 小数,%.(2)f表示小数点后保留(两位)小数
    >>> number = 123456
    >>> print('我的学号是%.2f.' %number)
    我的学号是123456.00.

     

    二、基本数据类型

    1.数据:描述衡量数据的状态

    2.类型:不同的事物需要不同的类型存储

    3.字符串:存一些描述性信息

    python2中str本质是一个拥有8个bit位的序列

    python3中str本质是一个unicode序列

    4.列表:存一个或多个不同类型的值

    5.字典类型:通过大括号存储数据,通过key-value这种映射关系定义键值对,每个键值对通过逗号隔开。

    数据的类型:

    1.数字型:

    整数类型:int

    小数类型:float

    2.布尔类型(bool):True /False主要用于判断事物的对错一般不会单独定义

    3.复数型:comples(主要用于科学计算由实数和虚数组成)

    4.非数字型:

    字符串:str

    元组: 元组与列表类似,不同之处在于元组的元素不能修改

    字典:

    查看变量的类型:type(变量)

    >>> print(type(name))
    <class 'str'>
    >>>

    三、运算符

    1.算术运算符

    img

    2.比较运算符

    img

    3.赋值运算符

    3.1增量赋值

    img

    3.2链式赋值

    >>> x=y=z=1
    >>> print(x,y,z)
    1 1 1
    >>>

    3.3交叉赋值

    >>> m =10
    >>> n = 20
    >>> m,n = n,m
    >>> print(n,m)
    10 20

    3.4解压赋值

    >>> numbers = [1,2,3,4,5,6,7]
    >>> a,b,c,d,e,*_ = numbers
    >>> print(a,b,c,d,e)
    1 2 3 4 5
    >>> *_,a,b,c,d,e,= numbers #*_可以接受溢出的元素
    >>> print(a,b,c,d,e)
    3 4 5 6 7
    >>>

    4.逻辑运算符

    and or not

    and满足全部条件

    >>> a = 1
    >>> b = 2
    >>> c = 3
    >>> a > b and b < c
    False
    >>> a < b and b < c
    True

     

    or至少满足一个条件

    >>> a = 1
    >>> b = 2
    >>> c = 3
    >>> a > b or b < c
    True

     

    not 反向满足条件

    >>> a = 1
    >>> b = 2
    >>> c = 3
    >>> not a > b
    True
    >>>
    >>> not a < b
    False

     

  • 相关阅读:
    set集合
    作业(2)
    字典
    列表功能
    字符串
    while循环
    pb加密转换成C#
    NET在64位系統使用32位oracle客户端访问数据库
    SQL Server中查找包含某个文本的存储过程
    SQLServer 错误: 15404,无法获取有关 Windows NT 组/用户
  • 原文地址:https://www.cnblogs.com/cyfdtz/p/11783515.html
Copyright © 2011-2022 走看看