一件事重复做多次,可以使用多种方式实现
while循环:
count = 0
while count < 3:
print('='*10)
print('步骤1')
print('步骤2')
print('步骤3')
count+=1
for循环
for x in 'a'*100:
print('='*10)
print('步骤1')
print('步骤2')
print('步骤3')
这两种方法都不够简洁,我们可以使用for+range来实现
# range(a,b,c) a代表起始位置,b代表结束位置,c代表步长
# a,c都可省略,起始位置默认为0,步长默认为1
for x in range(3):
print('='*10)
print('步骤1')
print('步骤2')
print('步骤3')
值改变,id也变,证明是产生了新值,并没有改变原值,原值是不可变类型
x = 123
print(x,id(x)) # 123 140728794076640
x = 456
print(x,id(x)) # 456 2156057766608
l = [111,222,333]
print(l,id(l)) #[111, 222, 333] 2234697360896
l[0] = 444
print(l,id(l)) #[444, 222, 333] 2234697360896
记录年龄,个数,号码,出生年,等级等
age = 18 # age = int(18)
int功能可以把纯数字的字符串转换成int类型
res = int('18')
# res1 = int('1.8')
print(res,type(res)) # 18
# print(res1,type(res1)) # 报错
进制转换(十转二,十转八,十转十六)
print(bin(11)) # 0b1011
print(oct(11)) # 0o13
print(hex(11)) # 0xb
算数运算与比较运算
记录薪资,身高,体重等
salary = 3.1 # salary = float(3.1)
float功能可以把浮点数组成的字符串转换成float类型
res = float('1.8')
print(type(res))
算数运算与比较运算
整型,浮点型都是只能存一个值,且为不可变类型
记录描述性质的状态,例如名字,性别,国籍等
在引号(’’,"",’’’ ‘’’,""" “”")内包含一串字符
s = 'hello' # s = str('hello')
str功能可以把任意类型转换成str类型
res = str([1,2,3]) # '[1,2,3]'
print(type(res))
①按索引取值(正向取+反向取) : 只能取
s = 'hello world'
print(s[0],type(s[0])) # h
print(s[-1]) # d
# s[1] = 'E' # 报错 ,不能修改
②切片(顾头不顾尾,步长) ==> 属于拷贝操作
s = 'hello world'
new_s = s[1:7]
print(new_s) # ello w
print(s) # hello world
new_s1 = s[:7:2]
print(new_s1) # hlow
new_s2 = s[::2]
print(new_s2) # hlowrd
new_s3 = s[::] # 完整拷贝字符串,只留一个冒号就可以 new_s3 = s[:]
print(new_s3)
③长度:len
s = 'hello world'
print(len(s))
④成员运算in和not in
s = 'hello world'
print('h' in s) # True
# 语义明确,推荐使用
print('he' not in s) # False
print(not 'he' in s) # False
⑤strip:移除空白符
s = ' \n hel lo \t'
new_s = s.strip()
print(new_s) # hel lo
print(s) # hel lo 没有改变原字符
取出左右两边的非空白字符
print('*+=%^hel**llo*-+'.strip('*+=%^-')) # hel**llo
⑥split:切分,把字符串按照某个分隔符切成一个列表
userinfo = 'egon_dsb:123:18:3.1'
res = userinfo.split(':')
print(res[0]) # egon_dsb
print(res) # ['egon_dsb', '123', '18', '3.1']
# join:把列表拼接成字符串
print('-'.join(res)) # egon_dsb-123-18-3.1
# 纯字符串组成的列表
l = ['aaaa','bbbb','cccc']
res = l[0] + ':' + l[1] + ':' + l[2]
res1 = ':'.join(l)
print(res,type(res))
print(res,type(res))
⑦循环
for i in 'hello':
print(i)
# 去左右两边的*号
print('***hello***'.strip('*')) # hello
# 去左边的*号
print('***hello***'.lstrip('*')) # hello***
# 去右边的*号
print('***hello***'.rstrip('*')) # ***hello
msg = 'KAFoow'
# 字符变小写
res = msg.lower()
print(res) # kafoow
# 字符变大写
res1 = msg.upper()
print(res1) # KAFOOW
# 大小写互换
res2 = msg.swapcase()
print(res2) # kafOOW
msg = 'ab is ab'
print(msg.startswith('ab'))
print(msg.endswith('ab'))
userinfo = 'egon:123:18'
print(userinfo.split(':')) # ['egon', '123', '18']
print(userinfo.split(':',1)) # ['egon', '123:18']
print(userinfo.rsplit(':',1)) # ['egon:123', '18']
msg = '***egon hello***'
res = msg.replace('*','').replace(' ','')
print(res) # egonhello
res1 = msg.strip('*').replace(' ','')
print(res1) # egonhello
按照位置传值
# %s 的方式
name = 'egon'
age = 18
res1 = 'my name is %s my age is %s' % (name,age)
print(res1)
# format
name = 'egon'
age = 18
res1 = 'my name is{} my age is {}'.format(name,age)
print(res1)
按照索引传值
name = 'egon'
age = 18
res1 = '{0}{0}{0}{1}'.format(name,age)
print(res1)
按照关键字传值
res = 'my name is{name} my age is {age}'.format(name='egon',age=18)
print(res)
了解:
name = 'egon'
age = 18
res1 = f'my name is{name} my age is {age}'
print(res1)
f 搭配{}可以执行字符串中的代码
res = f'{len("hello")}'
print(res) # 5
f'{print("hello")}' # hello
f包含的字符串可以放到多行
name = 'egon'
age = 18
res1 = f'my name is {name}' \
f'my age is {age}'
print(res1)
{} 内不能有\以及#
print(f'my name is {{egon}}') # my name is {egon}
print('胜率是 %s%%' % 70) # 胜率是 70%
print('asdf123'.isdigit()) # False
print('123'.isdigit()) # True
print('12.3'.isdigit()) # False
age = input('>>>: ')
if age.isdigit():
age=int(age)
if age > 18:
print('猜大了')
elif age < 18:
print('猜小了')
else:
print('猜对了')
else:
print('必须输入数字')