Python基础(Day 4)(string拓展赋值玩法 分支 循环)

知识点总结:

  • string拓展赋值
#序列赋值
>>> a,b,c = 'uke'
>>> print(a,b,c)
u k e

#拓展序列解包赋值
>>> a,b,*c = 'test data'
>>> print(a,b,c)
t e ['s', 't', ' ', 'd', 'a', 't', 'a']


>>> a,*b,c = 'test data'
>>> print(a,b,c)
t ['e', 's', 't', ' ', 'd', 'a', 't'] a

# 当前b为list,用‘’连接b的元素
>>> b = ''.join(b)
>>> b
'est dat'

# 变量个数与赋值数相同
>>> a,b,*c = 'abc'
>>> print(a,b,c)
a b ['c']

# 变量个数多与赋值内容
>>> a,b,*c = 'ab'
>>> print(a,b,c)
a b []

# 相同string对象 is 比较 为True
>>> a = 'a'
>>> b = 'a'
>>> a==b
True
>>> a is b
True

# 相同string对象 is 比较 为False
>>> a = 'a.a'
>>> b = 'a.a'
>>> a is b
False

这里一会为True一会为False很懵逼,于是查了一下,涉及到string的驻留机制,即什么时候才会真正的创建一个新的对象:
链接:牛客网

字符串驻留定义:在计算机科学中,字符串驻留一种仅保存一份相同且不可变字符串的方法。不同的值被存放在字符串驻留池中。
字符串驻留限制:仅包含下划线(_)、字母和数字的字符串会启用字符串驻留机制驻留。因为解释器仅对看起来像python标识符的字符串使用intern()方法,而python标识符正是由下划线、字母和数字组成。python只会针对整数范围为[-5,
256]的整数启用字符串驻留 字符串驻留机制的优缺点如下: 优点:能够提高一些字符串处理任务在时间和空间上的性能,
缺点:在创建或驻留字符串时的会花费更多的时间。

举例:string1 = “aabbcc”
string2 = “aabbcc” 使用id(string1)和id(string2)得到的内存地址是一样的。

这里测试了几个不同版本,机制上还是有些差别:

# Python 3.7.3 每次执行id均不同
>>> id('a.')
1887803840752
>>> id('a.')
1887803840696
>>> id('a.')
1887803840024
>>> id('a.')
1887803838904

# Python 3.6.6 每次均不同
id('a.')
1977457401672
id('a.')
1977457397864
id('a.')
1977457398032

# Python 3.6.2 id值在表达式操作后改变
>>> id('a.')
1967580871400
>>> id('a.')
1967580871400
>>> 'a.'
'a.'
>>> id('a.')
1967580871456
>>> id('a.')
1967580871456

如上,新版本表现符合引用中回答。

  • print 操作:
def print(self, *args, sep=' ', end='\n', file=None): 

# 默认空格分割,定义分隔符|
a = 'a'
b = 'b'
c = 'c'
print(a,b,c)
a b c
print(a,b,c,sep='|')
a|b|c


# 默认结尾\n, 定义末尾
>>> print(a,b,c,sep='|',end='...\n')
a|b|c...

# 打印到文件
>>> print(a,b,c,sep='|',end='...\n'file open('result.txt','w',encoding = 'utf8'))
  • 条件分支
score = 75
if score < 60:
    print('not pass')
elif score < 90:
    print('pass')
else:
    print('grate')
#通过dict实现分支
operation = {
    'add':'增加',
    'update':'更新',
    'delete':'删除'
}

print(operation.get('add'))
# lambda 方式
def add(v):
    print(v+10)
    
# 定义一个默认函数
def default_methomd(v):
   print('nothing to do')

# 将方法定义到dict
operation = {
    'add':add,
    'update':'更新',
    'delete':'删除'
}

operation.get('add',defalut_method)(10)
  • 三元运算
# A if condition else B
result = 'pass' if score >= 60 else 'not pass'
  • 循环操作
x = 'learn-python'
while x:
    print(x, end=' ')
    x = x[1:]
    
learn-python earn-python arn-python rn-python n-python -python python ython thon hon on n


a, b = 0, 10
while a < b:
    print(a)
    a += 1

# break 跳出循环
# continue 跳出本次循环
# pass 占位

# 输入stop跳出循环
while True:
    name = input("请输入姓名:")
    if name == 'stop':
        break
    age = input("请输入年龄:")
    print('您好,{},您的年龄是{}'.format(name, age))
else:
	# 循环可嵌套else
	pass

# if循环
for x in [1,2,3,4]:
    print(x)

1
2
3
4

for x in 'python':
    print(x)

p
y
t
h
o
n

# 迭代dict,只会迭代key
>>> emp = {
... 'name':'Tom',
... 'dept':'technology',
... 'job':'developer',
... 'salary':9000.00}
>>>
>>> for key in emp:
...     print('{} => {}'.format(key, emp[key]))

name => Tom
dept => technology
job => developer
salary => 9000.0

# 推导方式打印两个string交集
s1 = 'abcdef'
s2 = 'aceg'

l = [x for x in s1 if x in s2]
print(l)

['a', 'c', 'e']

# 打印 1-100 (不会取到100) range返回迭代器
# range(start, stop[, step]) -> range object
for x in range(1,100):
    print(x)

# 字符串按下标转枚举,(idx, item)元组
s = 'abcdef'
for idx, item in enumerate(s):
    print('{}){}'.format(idx, item))

0)a
1)b
2)c
3)d
4)e
5)f


发布了45 篇原创文章 · 获赞 2 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_22096121/article/details/103186634