python数据类型,,变量以及类型变量:存


变量以及类型


变量:存储程序运行中的数据,变量有3个要素:变量名、变量类型、变量值。python属于弱类型语言,不需要声明变量类型。

[root@localhost python]# ipython3In [1]: a=1  //变量名=变量值;在堆内存中的一个区域存了一个值为1,内存分为堆内存和栈内存,栈内存的是引用。指向堆内存中的值。In [4]: b=88In [5]: c=a+bIn [7]: cOut[7]: 89In [8]: aOut[8]: 1In [9]: type(a)Out[9]: intIn [10]: type(c)Out[10]: intIn [11]: str="hello"In [12]: strOut[12]: ‘hello‘In [13]: x,y=1,2In [14]: xOut[14]: 1In [15]: yOut[15]: 2In [16]:In [16]: type(str)Out[16]: str

变量类型


技术分享图片

标识符:自定义的一些符号和名称,标识符是自己定义的,如变量名、函数名等。


规则:由字母、下划线和数字组成,且不能以数字开头;不能包含一些有特殊意义的符号,如#.&!()等;同时区分大小写。

大小写规则:

驼峰命名法:如小驼峰:userName / userLoginFlag 大驼峰:UserName;类的名字一般首字母大写。

下划线规则:user_name user_login_flag

保留字/关键字:在计算机中有特殊含义的单词。

In [1]: import keywordIn [2]: keyword.kwlistOut[2]: [‘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‘]In [3]: 

输出和输出格式

普通的输出,print()

In [4]: print(‘hello‘,‘world‘,‘zyj‘,‘hello‘) //输出多个变量hello world zyj helloIn [5]: print(111+222) //输出表达式333In [6]: exit[root@localhost python]#vim 3.py   1 a=35  2 print("my age is:%d"%a )  //变量替换[root@localhost python]# python3 3.py my age is:35[root@localhost python]#vim 3.py    1 a=35  2 b=10  3 print("my age is:%d"%a+b)[root@localhost python]# python3 3.py Traceback (most recent call last):  File "3.py", line 3, in <module>    print("my age is:%d"%a+b) //错误行TypeError: must be str, not int  //错误类型[root@localhost python]#vim 3.py    1 a=35  2 b=10  3 print("my age is:%d"%(a+b)) //表达式需要用括号括起来[root@localhost python]# python3 3.py my age is:45[root@localhost python]#vim 3.py   1 a=35  2 b=10  3 my_name="zyj"  4 print("my age is:%d,my name is:%s" %(a+b,my_name)) //如果需要多个变量替换也要加小括号,并且每个变量用逗号隔开,%d代表整数类型替换,%s代表所有的类型变量替换,可以只记住%s[root@localhost python]# python3 3.py my age is:45,my name is:zyj
[root@localhost python]#vim 3.py money=2print("I have money:%4d",%money) //输出为4位,默认位空格[root@localhost python]# python3 3.py I have money: 2[root@localhost python]#vim 3.py money=2print("I have money:%04d" %money) //不够4位前面用0填充[root@localhost python]# python3 3.py I have money:0002

python数据类型

评论关闭