Python3学习(2),,字符串赋值引用特性同


字符串赋值引用特性
同一个字符串赋值给不同的变量,所有变量都是同一个对象

>> s = "abc"
>> s1 = "abc"
>> id(s)
34707248

>> id(s1)
34707248

>> id("abc")
34707248

>> s is s1
True

变量赋值

>> a = b = c = 3
>> a,b,c
(3, 3, 3)

>> a,b,c = 1,2,3
>> a,b,c
(1, 2, 3)

变量特性
变量可以重新赋值,变量保存的是值的引用,即值在内存中的地址,当变量被重新赋值后变量指向的地址就会变;会指向一个新的对象;

>> a = 5
>> id(a)
499805328
>> id(5)
499805328
>> a = 1000
>> id(a)
34452592

交换两个变量的值

>> a,b = b,a

其他语言:

>> a,b = 1,2
>> temp = a
>> a = b
>> b = temp
>> a,b
(2, 1)

查看保留字,关键字模块keyword

>> import keyword
>> print(keyword.kwlist)
[‘False‘, ‘None‘, ‘True‘, ‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘,
‘else‘, ‘except‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘nonloc
al‘, ‘not‘, ‘or‘, ‘pass‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘with‘, ‘yield‘]

>> keyword.iskeyword("yield")
True

一行写多个表达式,”;”

>> a = 1;b = 2;c = 3

代码换行

>> a = 3\
... +3
>> a

判断字符类型

>> isinstance(s,str)
True
>> isinstance(s,(str,bytes))
True

help 和 dir 命令
help可以查看对象的使用方法
dir 可以查看模块或对象包含的属性和方法

python3中的数据类型
Numbers 数字 ,python3中没有long
--int
--float
--complex
str 字符串

list 列表

tuple 元组

dict 字典
set 集合
注释
单行注释用#
多行注释用三引号”” ”””

Python3学习(2)

评论关闭