神奇的python系列2:python基础一

第一步:Python的while循环

  while循环的基本结构:

   while 条件:

   缩进 循环体

具体如下:

 while 3>2:
     print("好嗨哟")
     print("你的骆驼")
     print("再活五百年")
     print("在人间")
     print("")
1 #打印输出1-100以内的所有数
2  num = 1
3  while num<101:
4      print(num)
5      num = num + 1

接下来说一下break和continue

  1.break:终止循环,break以下代码都不执行

1 while 3>2:
2      print(1)
3      break
4      print(2)
5  print(4)
1 #用while循环打印输出0-50的所有数
2 num = 0
3 while True:
4     if num > 50:
5         break
6     print(num)
7     num = num + 1

  2.continue:跳出本次循环,继续下次循环

1 while 3>2:
2      print(1)
3      continue
4      print(2)
5  print(5)
 1 # 打印输出1,2,3,4,5,6,7,8,10
 2 
 3 
 4  num = 1
 5  while num<11:
 6      if num == 9:
 7          num = num + 1
 8          continue
 9      print(num)
10      num = num + 1

第二步:格式化输出

  

msg = '你好%s,我是%s'%('少年','meet')
print(msg)

  格式化输出中%表示一个占位,比如说上学时帮人占位置这样的情况,那%后面的s则是表示这是一个字符串类型。

1  name = input("姓名:")
2  age = input("年龄:")
3  msg = '姓名:%s,年龄:%d'%(input("姓名:"),int(input("年龄:")))
4  print(msg)
5 # %d== %i d和i必须放入的是整型 

  格式化输出的另一种写法:

name = input("姓名:")
age = input("年龄:")
msg = f'姓名:{name},年龄:{age}'  #需要注意的是这种写法必须是python3.6版本以上
print(msg)

 格式化输出要注意:   

  1.数量要一一对应
  2.在格式化中使用%的时候需要转义 %%

第三步:运算符

1.比较运算符

>  <  >=  <=  ==  !=

2.赋值运算符

+= -= *= /= //= **= %=

3.成员运算符

in  not in

4.逻辑运算符

and  or  not

逻辑运算的优先级:( )>not>and>or,同一优先级从左往右计算。这里需要注意的是 and 和 or 运算的结果:

5.算数运算符

+ - * / ** % //

 

第四步:初识编码(一)

  python2的默认编码是ascill,而python3对内容进行编码的默认为utf-8。

  

ascii 美国 256个字节 没有中文
            一个字节 8位
gbk 中国
            中文 2字节 16位
            英文 1字节 8位
unicode 万国码
            2个字节 16位
            4个字节 32位
utf-8 可变编码
            英文 1字节 8位
            欧洲 2字节 16位
            亚洲 3字节 24位

单位的转换

8bit = 1byte

1024byte = 1KB

1024KB = 1MB

1024MB = 1GB

1024GB = 1TB

1024TB = 1PB

1024TB = 1EB

1024EB = 1ZB

1024ZB = 1YB

1024YB = 1NB

1024NB = 1DB

常⽤到TB就够了 

  

原文地址:https://www.cnblogs.com/xianyuwangzhe/p/10197392.html