一个人的Python学习旅程(二)

while

while 条件:

while True:
    print('我们')
    print('在')
    print('KFC')
    quit()
    
count=1
while count<20:
    print(count)
    count=count+1

循环体,无限循环,需要一个退出键,如quit,或如下:

终止循环的方法:

  • 1、改变条件,使其不成立。
  • 2、break,终止循环
  • 3、pass,跳过循环

while else

当while循环被break打断,就不会执行else的结果
continue :结束本次循环,开始下一循环

无限输出1

print(111)
count=1
whlie count<20:
	print(count)
	continue(#间接终止,返回while)
	count=count+1

跳出continue死循环,可加 if 条件

count=0
whlie count<=100:
	count+=1
	if count >5 and count<95:
		continue
	print(‘loop’,count)
print(‘-------out of while loop-----’)
代码输入内容:

loop 1
loop 2
loop 3
loop 4
loop 5
loop 95
loop 96
loop 97
loop 98
loop 99
loop 100
loop 101
-------out of while loop-----

从1数到100

count = 1
flag = True
while flag:
    print(count)
    count = count + 1
    if count > 100:
        flag = False


count=1
whlie count<=100:
	print(count)
	count=count+1

1-100求和

count=1
sum=0
while count <=100:
	sum=sum+count
	count=count+1
print(sum)

pycharm 解释器

辅助解释,提示错误

  • 官网下载不是免费试用,需网上找激活码
  • 建议下载专业版,不要下载社区版本
  • 建议下载英文版,不要下载中文版

格式化输出

% 占位符
s(表示替换的是str)
d(表示替换的是digit数字)

动态替换

name=input('你叫什么名字:')
age=input('你的年龄是:')
high=input('你的身高是:')
mas = "我叫%s,今年%s,我身高%s" %(name,age,high)
print(mas)

如需要使用%可连续%%表示该位置为真正的%

初始编码

电脑的传输,还有储存的实际都是010101010101

美国:
ascii码(阿斯克码,256位)
为了解决全球化的文字问题,创建了一个万国码(Unicode)

中文: 9万多字

Unicode: 1个字节表示所有的英文,特殊字符,数字等

最开始: 2个字节,即16位表示一个中文
后来: 4个字节,即32位表示一个中文

升级版utf-8: 一个中文用3个字节表示,24位,包含所有的中文

gbk
中国自己创建,只供国内使用,1个中文用2个字节表示,包含英文及大部分中文

00000001 8位bit = 1个字节(byte)

1024字节=1KB
1024KB = 1MB
1024MB = 1GB
1024GB = 1TB
1024TB =

运算符

逻辑运算 and or not

优先级

( )> not > and > or

  • x or y ,x为非零,则返回x
    print(1 or 2)
    print(3 or 2)
    print(0 or 100)

  • x and y ,x为非零,则返回y

    例:

    print(2 or 1 < 3 and 2)

    输出为2

数据类型

  • int 1-9 用于计算
  • bool 用于判断
  • str ‘fjdlk’‘34235’‘二哥’,小型的数据,存储少量数据进行操作
  • list 列表 [1,2,3,4,‘泰哥’,‘23423’,[1,2,3] ]可以储存大量数据
  • 元祖 (1,2,3,‘第三方’) 又叫只读列表,不能修改
  • dict 字典 键值对 {‘name’:‘云姐’,‘age’:14}
    {‘云姐’:[ ],‘二哥’:[2,2,2], } 大量关系型数据存储
  • 集合 {1,2,3,4,‘234’ } 主要用于求交集、差集

int的方法

i=2
i.bit_length( ) 将该数字转换成二进制最少的位数(有效)

例:

1 0000 0001 1
2 0000 0010 2
3 0000 0011 2

bool True False 无方法

说在前面:转换

int——>bool

非零的数字转换为bool都为True,其他为Flase

bool——>int

True 1,Flase 0

int——>str

i=1
s=str(i)

int——>bool

非零为True 零为False

i=3
b=bool(i)
print(b)

bool——>int

0101010

判断:谁的效率高

while True:
	pass
while 1:
	pass

str——>bool

s=‘ ’ False 无数据

s
if s:
	print(‘你的输入为空,请重新输入’)
else:
	pass

字符串的操作

s=‘dfddd’
s1=s.capitalize()     #首字母大写
        upper              #全部大写
        lower              #全部小写

例子:

s_str= 'dD'
you_input= input('请输入验证码,不区分大小写')
if s_str.upper() == you_input.upper():
	print(‘输入成功’)
else:
	print('重新输入')

索引切片

s=‘ABCDEFG’
#索引
s1=s[0]
printl(s1)
print(s)
#ABC
s2=s[0:2]
print(s2)

切片:顾头不顾尾

最后一位 -1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值