python菜鸟入坑day02

基本循环

"""
#while 条件:

# 循环体

# 如果条件为真,那么循环体则执行
# 如果条件为假,那么循环体不执行
while 3>2:
  print("花有重开日")
  print("人无再少年")
"""
"""
#例子:1
while True:
  print("花油重开日")
  print("人无再少年")
"""

如何终止循环

#循环终止方法一
#break在后面加break这个循坏将不再允许了
while True:
   print("花有重开日")
   print("人无再少年")
   break
   #终止循环的第二个方法利用改变条件,终止循坏
fale=True
while fale:
   print("花有重开日")
   print("人无再少年")
   fale=False
   #方法3 continue 用于终止本次循环,继续下一次循环。举例说明:
fale=True
print(111)
while fale:
   print("花有重开日")
   print("人无再少年")
   continue
   print("你好呀")
print(你好)

循环四个练习题

#练习1 打印1-100所有的数字
coun=1
coun=True
while coun<101:
   print(coun)
   coun=coun+1
#练习2 打印1-100所有的和
coun=1
s=0
while coun<101:
   s=s+coun
   coun=coun+1
print(s)
#练习题3
coun=0
while coun<10:
   coun=coun+1
   if coun==7: #当到了7的时候开始if 成立 运行continue 又回到最初始的while开始判断
       continue
   print(coun)
#练习题4
coun=0
while coun<=100:
   coun=coun+1
   if coun>5 and coun<95:   #只要coun在6-94之间,就不走下面的print语句,直接进入下一次循环
       continue
   print(coun)

2.4,while ... else ..

#与其他语言else一般只能跟if搭配不同的,在python中还有个while ...else语句
#我while 后面的else作用是指,当while循环忠诚执行完,中间木有被break终止的话,就会执行else后面的语句
count=0
while count<5:
   count=count+1
   print("loop",count)
else:
   print("循环运行成功了")
print("-------out of while loop------------")
运行成功之后
loop 1
loop 2
loop 3
loop 4
loop 5
循环运行成功了
-------out of while loop------------


#如果执行过程中被break啦 ,就不会运行else的语句啦
count=0
while count<5:
   count=count+1
   if count==3: break
   print("loop",count)
  运行结果
loop 1
loop 2
----------ou of while loop---------

基本运算符

'''
#str----->只能纯数字转字符串  
s1='100'
print(int(s1),type(int(s1)))

s1=100
print(str(s1),type(str(s1))) #字符串转数字

#int ---->bool 非零则True ,0为False

print(int(True))
print(int(False))
'''
#字符串转数字
s1='100'
print(int(s1),type(int(s1)))
#数字转字符串
s2=100
print(str(s2),type(str(s2)))
#数字转布尔列型
s3=100
print(bool(s3),type(bool(s3)))
#布尔类型转数字
print(int(False))
print(int(True))

编码知识

UTF-8:是对Unicode编码的压缩和优化,他不再使用最少使用2个字节,而是将所有的字符和符号进行分类:ascii码中的内容用1个字节保存、欧洲的字符用2个字节保存,东亚的字符用3个字节保存...

UTF-16: 每个字符最少占16位.

GBK: 每个字符占2个字节, 16位. 

utf-8 最少用8位数,去表示一个字符

英文 : 8位 一个字节表示

欧文 : 16位2个字节表示

中文:24位3个字节表示

例题1

python中一个字符串表示‘aaa中国’utf-8编码表示,这个字符串站几个字符呢
答:三个英文一个表示一个字跟两个汉字每个汉字3个字节表示也就是说一共9个字节

 

猜你喜欢

转载自www.cnblogs.com/luomou/p/13197793.html
今日推荐