zoukankan      html  css  js  c++  java
  • python基础--基础入门

    一,   注释

    1,注释的作用:通过⽤自己熟悉的语言,在程序中对某些代码进⾏标注说明,这就是注释的作用,能够增强程序的可读,方便后期维护。

    解释器不执行注释内容

    2,注释的分类及语法

    注释分两类:单行注释和多行注释

    单行注释:

    # 输出hello world
    print('hello world')
    print('hello Python') # 输出(简单的说明可以放到⼀一⾏行行代码的后⾯面,⼀一般习惯代码后⾯面添加
    两个空格再书写注释⽂文字)
    View Code

    多行注释

    """
    下⾯面三⾏行行都是输出的作⽤用,输出内容分别是:
    hello Python
    hello itcast
    hello itheima
    """
    print('hello Python')
    print('hello itcast')
    print('hello itheima')
    '''
    下⾯面三⾏行行都是输出的作⽤用,输出内容分别是:
    hello Python
    hello itcast
    hello itheima
    '''
    print('hello Python')
    print('hello itcast')
    print('hello itheima')
    View Code

    快捷键ctrl  + /

    二,变量

    变量作用:是一个存储数据的的时候当前数据所在的内存地址的名字而已。

    定义变量:变量名 = 值

    标识符:

    标识符命名规则是Python中定义各种名字的时候的统⼀一规范,具体如下:

    1,由数字、字母、下划线组成

    2,不能数字开头

    3,不能使用内置关键字

    4,严格区分⼤小写

    False None True and as assert 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
    View Code

    命名习惯:

    1,见名知义

    2,大驼峰:即每个单词首字母都大写,例如:MyName

    3,  小驼峰:第二个(含)以后的单词首字母大写,例如:myName

    4,  下划线:例如:my_name

    使用变量:

    #定义变量:存储数据TOM
    my_name = 'TOM'
    print(my_name)
    schoolName = '程序员'
    print(schoolName)

    数据类型:在 Python 里为了应对不同的业务需求,也把数据分为不同的类型

     检测数据类型的方法:type()

    a = 1
    print(type(a)) # <class 'int'> -- 整型
    b = 1.1
    print(type(b)) # <class 'float'> -- 浮点型
    c = True
    print(type(c)) # <class 'bool'> -- 布尔型
    d = '12345'
    print(type(d)) # <class 'str'> -- 字符串串
    e = [10, 20, 30]
    print(type(e)) # <class 'list'> -- 列列表
    f = (10, 20, 30)
    print(type(f)) # <class 'tuple'> -- 元组
    h = {10, 20, 30}
    print(type(h)) # <class 'set'> -- 集合
    g = {'name': 'TOM', 'age': 20}
    print(type(g)) # <class 'dict'> -- 字典
    View Code

    三,格式化输出

     技巧:

    %06d,表示输出的整数显示位数,不足以0补全,超出当前位数则原样输出
    %.2f,表示⼩数点后显示的⼩数位数。

    格式化字符串除了了%s,还可以写为f'{表达式}'

    测试代码

    转义字符:

      :换行

       :制表符,一个tab键(4个空格)的距离

    结束符:

    print('输出的内容', end="
    ")

    四,输入

    语法:

    input("提示信息")

    特点:

    1,当程序执⾏行行到input ,等待⽤用户输入,输⼊完成之后才继续向下执⾏。
    2,在Python中, input 接收⽤用户输入后,一般存储到变量量,⽅便使用。
    3,在Python中, input 会把接收到的任意⽤用户输入的数据都当做字符串处理。

    password = input('请输⼊入您的密码:')
    print(f'您输入的密码是{password}')
    # <class 'str'>
    print(type(password))

     五,运算符

    运算符的分类:

    1,算数运算符
    2,赋值运算符
    3,符合赋值运算符
    4,比较运算符
    5,逻辑运算符

    1,算数运算符:

    注意:

    混合运算符优先级顺序:()高于**高于*  /  //  % 高于 + -

    2,赋值运算符:

     单个变量赋值:

    num = 1
    print(num)

    多个变量赋值:

    num1, float1, str1 = 10, 0.5, 'hello world'
    print(num1)
    print(float1)
    print(str1)

    结果如下:

     多变量赋相同值:

    a = b = 10
    print(a)
    print(b)

    结果如下:

     3,复合赋值运算符:

    a = 100
    a += 1
    #输出101 a = a + 1 ,最终a = 100 + 1
    print(a)
    
    b = 2
    b *= 3
    #输出6  b = b * 3,最终b = 2 * 3
    print(b)
    
    c = 10
    c += 1 + 2
    #输出13 ,先算运算符右侧1+2=3,c += 3,推导出c = 10 + 3
    print(c)

    4,比较运算符

    比较运算符也叫关系运算符,通常用来判断

    a = 7
    b = 5
    print(a == b) # False
    print(a != b) # True
    print(a < b) # False
    print(a > b) # True
    print(a <= b) # False
    print(a >= b) # True

    5,逻辑运算符

    a = 1
    b = 2
    c = 3
    print((a < b) and (b < c)) # True
    print((a > b) and (b < c)) # False
    print((a > b) or (b < c)) # True
    print(not (a > b)) # True

    数字间的逻辑运算

    a = 0
    b = 1
    c = 2
    # and运算符,只要有一个值为0,则结果为0,否则结果为最后一个非0数字
    print(a and b)   # 0
    print(b and a)   # 0
    print(a and c)   # 0
    print(c and a)   # 0
    print(b and c)   # 2
    print(c and b)   # 1
    
    # or运算符,只有所有值为0结果才为0,否则结果为第一个⾮0数字
    print(a or b)  # 1
    print(a or c)  # 2
    print(b or c)  # 1
  • 相关阅读:
    sql推断时间
    COCOFrame
    第41周一
    第40周日
    第40周六
    第40周五
    第40周四
    第40周三国庆
    第40周二晚
    第40周二
  • 原文地址:https://www.cnblogs.com/chenpeng03/p/13439967.html
Copyright © 2011-2022 走看看