变量,数据类型,运算符

python运行原理:

变量,数据类型,运算符


1、变量不需要实现声明
2、变量不需要指定类型
3、不必关心内存,变量名会被回收,python只需关注如何解决问题,内存的事情解释器解决




-------------------------------------------变量------------------------------------------------
变量名对英文字母大小写敏感


命名以字母数字下划线命名,首字母不能用数字




------------------------------------------数据类型------------------------------------------


整形,浮点数类型,字符串之间可以互相转换
---------------------------------------------------------------------------------------------
引入随机变量random 需先import random才能调用,提供了random.randrange(x,y)  randint()等方法。
import random
guess=int(random.randrange(1,10))
print(guess)


times=4
n=int(input("please input a number"))
while n!=guess and times!= 0:
    n=int(input('plieae input a number:'))
    if n==guess:
        print('Right')
    else:
        if(n>guess):
            print('too big')
        else:
            print('too small')
    times=times-1
print('END')




-----------------------------------------运算符---------------------------------------------------
not 1 为:False
not 0 为:True
0 or 2 为:0      False or 0 为:0       如果是true则值为前面的,false值为后面的
0 and 3 为:3     False and 0 为:False   如果true则值为后面的,false值为前面的。
---------------------------------------------------------------------------------------------------
三元运算符:
small = x if x<y else y  如果x<y成立结果为small = x,否则为small = y
---------------------------------------------------------------------------------------------------
断言:
assert  3>4  当asssert后面的条件是错的,报AssertionError
当需要确保程序中的某个条件一定为真才能让程序正常工作的话,可用到assert
------------------------------------------------------------------------------------------------------
x=1,y=2,z=3,如何将x和y的值互换?
答:

变量,数据类型,运算符
--------------------------------------------------------------------------------------------
rang()函数的使用
rang(起始值,末尾值,叠加值),其中末尾值不是必填的
例:for i in range(1,8,2):
    print(i)
结果输出:1 3 5 7


-----------------------------------------运算符优先级--------------------------------------------
 优先级:  (幂运算操作符比其左侧的一元操作符优先级高,比其右侧的一元操作符优先级低。-3**2,先算-号)
  幂运算                        **
  正负号                     +x   -x
  算数操作符               + - * / %  //
  比较运算符            < <=  >  >=  ==  !=
  逻辑运算符           not      and        or    其中not>and>or




------------------------------------------python中,加号+和乘号*的作用------------------------------------------------------
+:起来连接字符:[1, 2, 3] + [4, 5, 6]
*:重复运算:3**2 返回9,起到平方的作用
         列表重复,s='abc',s*3 返回:abcabcabc
------------------------------------------------------------------------------------------------------------------------------------------------------------