zoukankan      html  css  js  c++  java
  • 学习python的day1之变量和数据类型

    1.注释

    python的的注释方式有单行注释,有多行注释

    单行注释(用#注释):

    #单行注释
    print('hello python!')

    多行注释:

    1)用6个单引号注释

    '''
    多行注释
    6个单引号
    '''
    print('hello word!')

    2)用6个双引号注释

    """
    多行注释
    6个双引号
    """
    print("hello as_scheduled!")

    2.变量

    定义变量要满足标识符命名规则:

    1)由下划线、字符、数字组成

    2)不能数字开头

    3)不能使用内置关键字

    4)严格区分大小写

    命名习惯:大驼峰(MyPython)、小驼峰(myPython)、下划线(my_python)

    3.认识数据类型

    常用的数据类型:int--整型、float--浮点型、str--字符串、list--列表、tuple--元组、dict--字典、set--集合

    list--列表

    list1 =  [1,2,3]

    tuple--元组

    num = (1,2,3)

    dict--字典

    stu = {"name':'zhangsan",'id':'123','sex':''}

    set--集合

    num = {1,2,3}

    检测数据类型的关键字--type

    num =123
    type(num)

    4.格式化输出

    1)int--%d

    age = 18 ;
    print("我的年龄是%d岁"%age)

    2)float--%f

    weight = 68.3
    print("我的体重是%f公斤"%weight)

     这样格式化化输出浮点数是默认小数点后有6位小数,若是我们想要控制输出小数点后只有两位小数便可以用%.2f

    weight = 68.13
    print("我的体重是%.2f公斤"%)

    3)str--%s

    name = 'as_scheduled'
    print("我的名字是%s"%name)

    4)多个变量同时输出

    
    
    name = 'as_scheduled'
    age = 18
    weight = 68.3
    print("我的名字是%s,今年%d岁,体重%.2f公斤"%(name,age,weight))

    还有一种输出格式为f'{表达式}'

    name = 'as_scheduled'
    age = 18
    weight = 68.3
    print(f'我的名字是{name},今年{age}岁,体重{weight}公斤')

    5.转义字符

    是换行, 是制表符(一个tab键的距离即4个空格)

    输出函数print的默认格式为print(“hello python”,end=' '), 是print默认的结束符。我们可以根据需要更改结束符,例如:

    print('hello python',end='...')
  • 相关阅读:
    C# 关键字 default
    nopCommerce_3.00-Nop.Core.Caching
    汉字转拼音
    ASP.NET MVC性能调试工具-MvcMiniProfiler
    开源项目
    UVA 11374 Airport Express 最短路
    UVA 10319 Manhattan 2-sat
    UVA 1357 Cells
    UVA 610 Street Directions 双连通分量
    UVA 11504 Dominos 强连通分量
  • 原文地址:https://www.cnblogs.com/scheduled/p/13574744.html
Copyright © 2011-2022 走看看