zoukankan      html  css  js  c++  java
  • 学习python的day2之数据转换和运算符

    1.输入

    语法:input('提示信息')

    注意:input()接收到的数据都是字符串

    password = input('请输入密码:')
    print(f'你的密码是{password}')
    print(type(password))

    2.转换数据类型

    int(x):将数据类型转换为int型

    num1 = input('请输入:')
    print(num1)
    print(type(num1))
    print(type(int(num1)))

    float(x):将数据类型转换为float型

    num2 = 1
    str1 = '12'
    print(type(num2))
    print(type(float(num2)))
    print(type(str1))
    print(type(float(str1)))

    str(x):将数据类型转换为字符串

    #str()
    str1 = 123
    print(type(str1))
    print(type(str(str1)))

    tuple(s):将一个序列转换为元组

    list1 = [1,2,3,4]
    print(type(list1))
    print(list1)
    print(type(tuple(list1)))
    print(tuple(list1 ))

    list(s):将一个序列转换为列表

    list1 = (1,2,3,4)
    print(type(list1))
    print(list1)
    print(type(list(list1)))
    print(list(list1 ))

    eval(str):用来计算字符串中的有效表达式,并返回一个对象

    str1 = '1'
    str2 = '1.1'
    str3 = '(1,2,3)'
    str4 = '[1,2,3]'
    str5 = '1+4.7'
    print(eval(str1))
    print(type(eval(str1)))
    print(type(eval(str2)))
    print(type(eval(str3)))
    print(type(eval(str4)))
    print(eval(str5))
    print(type(eval(str5)))

    3.多变量赋值

    在python中,可以多个变量同时赋多个不同的值

    a,b,c,d,e = 1,2.7,"hello python",(1,2,3),[1,2,3]
    print(a)
    print(b)
    print(c)
    print(d)
    print(e)

    也可以多个变量赋相同的值

    c = d = 1
    a = b = (1,2,3)
    print(a)
    print(b)
    print(c)
    print(d)

    4.复合赋值运算符

    在python中,//表示整除,%表示求余,**表示乘方

    在复合运算中,先算复合运算符右面的表达式,再算复合运算符

    a = b = c =d  = 12
    a += 1+2+3
    print(a)
    b -= 1+2+3
    print(b)
    c *= 1+2+3
    print(c)
    d /= 1+2+3
    print(d)
    e = 3
    e **= 3
    print(e)

    5.数字逻辑运算

    逻辑运算符:and表示与,or表示或,not表示非

    1)and运算符,只要一个值为0,则结果为0,否则结果为最后一个数字

    print(a and b) #   0 and 1
    print(b and a) #   1 and 0
    print(a and c) #   0 and 2
    print(c and a) #   2 and 0
    print(b and c) #   1 and 2
    print(c and b) #   2 and 1
    print(b and c and d) #   1 and 2 and 3

    2)or运算符,只有所有值为0,结果才为0,否则结果为第一个非0数字

    a,b,c,d = 0,1,2,0
    print(a or b)
    print(b or a)
    print(a or d)
    print(d or a)
    print(a or b or c or d)
    print(c or a or d or b)

    3)not运算符,是改变布尔值,若布尔值为true,则会变为false

    a = 3
    print(not(a))
    b = 0
    print(not(b))

    6.if语句

    语法格式:

    if 条件语句:{

      当条件语句的布尔值为true时,执行的代码

    }

    if 18>10:
        print('hello python!')

    if 条件语句:{

      当条件语句的布尔值为true时,执行此处的代码

    }

    else:{

      当条件语句的布尔值为false时,执行此处的代码

    }

    if 10>10:
        print('hello python!')
    else:
        print('hello word!')

    7.多重if条件判断语句

    if 条件语句1:{

      当条件语句1的布尔值为true时,执行此处的代码

    }

    elif 条件语句2:{

      当条件语句2的布尔值为true时,执行此处的代码

    }

    else:{

      当条件语句1和条件语句2的布尔值为false时,执行此处的代码

    }

    if 10>14:
        print('hello python')
    elif 18>13:
        print('hello word')
    else:
        print('as_scheduled')

    8.三目运算符

    三目运算符也称为三元运算符或三元表达式

    语法如下:

      条件成立执行的表达式 if 判断条件 else 条件不成立执行的表达式

    例:

    a,b = 1,2
    c = a-b if a>b else a+b   #变量C用来接收执行表达式返回的对象
    print(f'c={c}')

    注意:三目运算符只能用于简单的if-else条件判断语句

  • 相关阅读:
    164 Maximum Gap 最大间距
    162 Find Peak Element 寻找峰值
    160 Intersection of Two Linked Lists 相交链表
    155 Min Stack 最小栈
    154 Find Minimum in Rotated Sorted Array II
    153 Find Minimum in Rotated Sorted Array 旋转数组的最小值
    152 Maximum Product Subarray 乘积最大子序列
    151 Reverse Words in a String 翻转字符串里的单词
    bzoj3994: [SDOI2015]约数个数和
    bzoj 4590: [Shoi2015]自动刷题机
  • 原文地址:https://www.cnblogs.com/scheduled/p/13580370.html
Copyright © 2011-2022 走看看