zoukankan      html  css  js  c++  java
  • 1.Python学习笔记:[Print()格式;缩进;变量命名;注释;字符串拼接;表达式和运算符;变量赋值;输入;强制类型转换;IF语句]

    一、Print()格式:

      1、   

        print()     #等价于print(end=" ")

      2、

        print('hello word')    

           print("hello word")        #单双引号都可以

      3、  

        print('hello word '*8)

        或

    print('hello word')

    print('hello word')

    print('hello word')

    msg='''hello word

    hello word

    hello word'''

    print(msg)

    #运行结果:

    hello word

    hello word

    hello word

    ----------

      4、

    print('hello word',end=" ")

    print('hello word',end=" ")

    print('hello word',end=" ") #输出在同一行

               #运行结果:hello word hello word hello word

      5、

    print('helloworld'[2:])    #运行结果:lloworld  

      6、

    print('el' in 'hello')      #运行结果:True   如果字符串中包含给定的字符返回 True 

    二、缩进:

    1. Tab不等于四个空格
    2. 缩进级别一致(官方建议四个空格)

    三、变量命名:

    1. 具有描述性:Student_number(良好习惯)
    2. 支持中文命名,但不推荐
    3. 不能以数字开头
    4. 变量名只能数字字母组成,不可一是空格或特殊字符(#?<,./$%^#等)
    5. 保留字符不用做变量:print=5(不可)
    6. Python不分常量,变量。全部大写默认常量(STUDENT_NUMBER)
    7. Python区分大小写

    四、注释:

    1、

    #print() #单行注释

    2、

    '''print()

    print()

    print()'''      #多行注释:(三引号。单引,或双引) 

     五、字符串类型(string):

    1、

     print("good"+"well")   #运行结果:goodwell

    2、

    a='123'
    b='abc'
    c='789'
    d2='---'.join([a,b,c])
    print(d2)        #运行结果123---abc---789  

    六、表达式和运算符:

    1、

    算术运算符 : +   -    *   /     //(取整除,3//2=1)    %(取余)   **(次方)

    赋值运算符: = 、+= -= *= /= %= //= **=

    >>> num = 2    

    >>> num += 1   # 等价于 num = num + 1

    >>> num -= 1   # 等价于 num = num - 1

    >>> num *= 1   # 等价于 num = num * 1

    >>> num /= 1   # 等价于 num = num / 1

    >>> num //= 1   # 等价于 num = num // 1

    >>> num %= 1   # 等价于 num = num % 1

    >>> num **= 2   # 等价于 num = num ** 2

    2、

    比较运算符:>、 <、 >=、 <=、 ==、!=

    逻辑运算符: not 、and、 or

    >>> a > b and  a < b  # 如果两个操作数都是True,那么结果为True,否则结果为False。

    False

    >>> a > b or  a < b   # 如果有两个操作数至少有一个为True, 那么条件变为True,否则为False。

    True

    >>> not a > b       # 反转操作的状态,操作数为True,则结果为False,反之则为True

    False

    3、

    成员运算符: not in 、in (判断某个单词里是不是有某个字母)

    >>> "h" in "hello"  # 这里的意思是 “h” 在“Hello” 中,判断后结果为True

    True

    >>> "h" not in "hello"  # 这里的意思是 “h” 不在“Hello” 中,判断后结果为False

    False

    4、

    身份运算符: is、is not(讲数据类型时讲解,一般用来判断变量的数据类型)

    >>> a=123

    >>> b=123

    >>> a is b

    True>>> a is not b

    False

    5、

    ()优先级高:      >>> (((2+3)*2+3)/2)*5

    七、变量赋值:

    name="梁志伟"

    name2="小李"

    print(name,name2)   #运行结果:梁志伟 小李

     

    print('3*4=',3*4)     #运行结果:3*4= 12

     

      

    x=3

    y=4

    print('x*y=',x*y)     #运行结果:x*y= 12

    八、输入:

    age=input('你的年龄:')  #默认输入的是字符串
    change=int(age)

    九、强制类型转换

    >>> a=3 

    >>> int(a) 

    3

    >>> str(a)

    '3' 

    >>> float(3)3.0

     十、IF语句:

    1、

    if (3>2) and (4>2) :   #and  or

    2、 

    if a<b<c :   #在python可以 

    3、 

    if 1<=z<=5 :

        print('') 

    else :

        print('')

     

    if score>90 :

        print('优秀')

    elif score>80 :

        print('良好')

    elif score>60 :

        print('及格')

    else :

        print('滚') #score=91   结果为:优秀

     

  • 相关阅读:
    说说该死的Google Android Market
    由HTML5绘制地图说开去
    unicode解码小工具
    IntelliJ IDEA 的安装、配置与使用
    再次强调!考试必带的十几样物品,一样也不能少
    Redis 实现限流的三种方式
    Linux配置/etc/resolv.conf详解
    人生哲学
    一文教你如何高效使用 IDEA !
    Mysql5.7.30_配置参数
  • 原文地址:https://www.cnblogs.com/LiangZhiWei/p/9185083.html
Copyright © 2011-2022 走看看