zoukankan      html  css  js  c++  java
  • 基础-变量的基本使用

    变量篇

    变量名 只有在 第一次 出现 才是 定义变量

    变量名 再次 出现,不是定义变量,而是 直接使用之前定义过得变量

    price = 7.5
    
    weight = 8.5
    
    money = weight * price
    
    money = money -5
    
    print(money)

    2、变量的类型

     

    变量名称  =   变量保存的数据

    """
    在python中,定义变量时是不需要指定变量的类型的
    运行的时候,python解释器会根据 = 右侧的数据 自动推导出变量中保存数据的准确类型
    """
    # str 表示 字符串类型
    name = '小明'
    
    # int 表示整数型
    age = 18
    
    # bool 表示布尔类型,真True,假False
    gender = True
    
    # float 表示 小数类型==浮点型
    hight = 1.75
    
    # 带小数点则表示 浮点型 不带则表示 整数型
    weigh = 75.0
    
    print(type(age))
    print(type(name))
    print(type(hight))
    print(type(gender))
    print(type(weigh))
    # 输出结果:
    <class 'int'> <class 'str'> <class 'float'> <class 'bool'> <class 'float'>

      

    变量的类型列举:

     

    ipython 交互的方式查看变量类型

    在python2.x 的解释器中 整数 根据长度 分为:

    int (整数)

    long (长整数)

    在python3.x的解释器中做了优化,整数类型只有只有一种:

    int (整数)

    不同类型变量之间的运算

     

     转化合并:

    f = "小明"
    
    print(f + "10")

    :小明10

    变量的输入

    • 程序是用来处理数据的
    • 变量是用来存储程序处理的数据的

    input 函数介绍:

     2020-03-26  08:35:39

    # 类型转换
    password = input('请输入密码:')
    
    print(password)
    
    print(type(password))
    
    change = (int(password))
    print(type(change))
    输出结果:

    请输入密码:21345 21345 <class 'str'> <class 'int'>

    # 买苹过增强版 - 类型转换

    两个字符串之间是不能直接用 乘法 

    # 1 输入苹过的单价
    price_str = input('苹过的单价:')
    
    # 2 输入购买的重量
    weight_str = input('苹过的重量')
    # 3计算支付金额
    money = price_str * weight_str
    输出结果:  
    Traceback (most recent call last): File
    "D:/Desktop/project/01-变量.py", line 63, in <module> money = price_str * weight_str TypeError: can't multiply sequence by non-int of type 'str'

    正确版:

    # 买苹过增强版 - 类型转换

    # 1 输入苹过的单价 price_str = input('苹过的单价:')

    # 将字符串转化为小数类型 price
    = float(price_str)
    # 2 输入购买的重量 weight_str = input('苹过的重量') weight = float(weight_str) # 3计算支付金额 money = price * weight print(money)

    #可以将一个完整的函数放在一个函数的小括号里面

    # 这样就减掉了 上面的类型转换 多定义的变量了

    tall = int(input("请输入:"))

    变量的格式化字输出

    字符串

    name = '大小明'
    print('我的名字叫 %s ,请多观战' % name)
    
    #输出
    
    我的名字叫 大小明 ,请多观战

    整数     # %06d 意思是 数值长度不到6位 用0来代替   >=6位就是原数值 (可以控制字符的长度)

    # 我的学号是 000001
    
    student_nu = 1
    print('我的学号是 %06d' % student_nu)
    
    student_no = 12343
    print('我的学号是 %06d' % student_no)
    
    
    
    student_ne = 12343234
    print('我的学号是 %06d' % student_ne)
    
    
    # 输出
    
    我的学号是 000001
    我的学号是 012343
    我的学号是 12343234

    小数   %.2 意思是:可以控制小数点的位数 (小数点后两位)

    # 买苹果 输出 price weight money 分别对应 苹过单价 重量 支付金额
    
    price = 7.5
    weight = 8.9
    money = price * weight
    print('苹过单价 %.2f,苹果重量 %.3f,支付金额 %.4f' % (price,weight,money))
    
    
    #输出 
    
    苹过单价 7.50,苹果重量 8.900,支付金额 66.7500

    百分数的定义 ,%% 对应的是%     %f 代表小数   .2f 可以控制小数的位数

    # 定义个小数 scale ,输出数据比例是 10.00%
    scale = 0.1
    print('输出数据比例是 %.2f%%' % (scale *100))
    
    #输出
    
    输出数据比例是 10.00%

    变量的标识符

    变量标识符就是 变量名和函数名

    名字要求:

      见名之意

    关键字

     查看python中定义的关键字

    import keyword
    
    print(keyword.kwlist)
    
    
    #输出:
    
    ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 

    'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

    变量命名规则

    三种方式:first_name lass_name   (python常用—严格区分大小写)

    大驼峰法: FirstName LassName

    小驼峰法: firstName  lassNmae

    附:  python之父定义的标准命名方式

  • 相关阅读:
    第二阶段冲刺第七天
    学习进度表_十五周
    第二阶段冲刺第六天
    第二阶段冲刺第五天
    第二阶段冲刺第四天
    第二阶段冲刺第三天
    Beta阶段项目总结
    Alpha阶段项目总结
    第二次冲刺站立会议10
    第二次冲刺站立会议09
  • 原文地址:https://www.cnblogs.com/leading-net/p/12550125.html
Copyright © 2011-2022 走看看