python自学第二课!

  • 编码

#/usr/bin/u/ubv/a python
# -*- coding:utf-8 -*-
补充:
字节,位
unicode utf8 gbk
utf8: 3字节
gbk : 2字节

  • while 可以与else一起使用
count = 0
while count < 10:
    print(count)
    count = count + 1
else:
    print("else")
  • continue用法
count = 0
while count < 10:
    if count == 7:
        count = count + 1
        continue #遇见continue 下面的代码块不执行,直接回while
    print(count) 
    count = count + 1
  • break用法 遇见break不执行下面语句块,退出while
count = 0
while count < 10:
    count = count + 1
    print(count)
    break
    print(123)
print("end")
  • 运算符

算数运算符:+    -     *     /     %     **    //

赋值运算符: =     +=      -=       *=      /=      %=      **=     //=

成员运算符:in        not in    IN 判断某个东西是否在某个里面包含

结果:布尔值

逻辑运算符:==    <       >        <=        >=        !=    

not 非

优先级:

先计算括号内

执行顺序:

从前到后   结果 true遇见 or 为true

          结果true 遇见 and 继续走

       结果false 遇见and 为flase

       结果false 遇见 or 继续走

user = 'star'
pwd = '123'
v = user == 'star' and pwd == '123' or 1 == 1 and pwd == '222'
print(v)
# and 前有假的 结果就是false  or 前有真的 结果为true
# "张明星" 字符串
#"张" 字符
#"明星" 子字符串
#ctrl+? 选中后全部注释
# name = "张明星"
# if "张星" in name:  #连续的字符和字符串 在不在里面
#     print("ok")
# else:
#     print("error")

if "" not in name:  #连续的字符和字符串 在不在里面
    print("1")
else:
    print("2")

布尔值:true 真 false 假

if true:

  pass

while true:

  pass

  • 基本数据类型

数字 int  python3里int不限长度

-int   

  将字符串转换为数字int() 、type()查看数据类型、int(num,base=2)强制转换为几进制、bit_length 当前数据的二进制,至少用几位显示

a = '123'
print(type(a),a)
b = int(a)
b = b + 1000
print(type(b),b)  #type()查看数据类型

num = '0011'
v = int(num,base=2)  #base 可以转换为2,8,10,16进制,默认为10进制
print(v)
age =5
r
= age.bit_length() #当前数据的二进制位数,至少用几位 print(r)

字符串 str  

test = 'Starara'
v = test.capitalize() #首字母大写
print(v)

v1 = test.casefold() #变小写,但更牛
print(v1)

v2 = test.lower() #变小写
print(v2)
# def test.center(self,width,fillchar=none)
#设置宽度,并将内容居中
#20代表总长度
# * 空白位置填充,一个字符,可有可无
v3 = test.center(100,"*")
print(v3)

v4 = test.count('a',4,7) #去字符串子序列出现的次数,可以设置起始和结束的位置
print(v4)

v5 = test.endswith('ra') #判断是否以子字符结尾
print(v5)

v6 = test.startswith('S') #判断是否以子字符开头,结果真假
print(v6)

v7 = test.find('ar',4,8) #从开始往后找,找到第一个后,获取位置,从0开始
print(v7)

test1 = "i am {name},age{a}"
v8 = test1.format(name='star',a=19) #格式化,将一个字符串中的占位符替换为指定的值,从0开始
print(v8)

test2 = "i am {0},age{1}"
v9 = test2.format('star',19) #格式化,将一个字符串中的占位符替换为指定的值,从0开始
print(v9)

#格式化只能这种类型{"name" : "star","a":10}
test3 = "i am {name},age{a}"
v10 = test3.format_map({"name" : "star","a":10})
print(v10)

v11 = test.index('a') #index与find功能一样,index找不到报错
print(v11)

test4 = 'star224——'
v12 = test4.isalnum()  #判断字符串中是否只包含字符和数字
print(v12)
Starara
starara
starara
**********************************************starara***********************************************
2
True
False
4
i am star,age19
i am star,age19
i am star,age10
2
False

列表 list

元祖 tuple

字典 dict

布尔值 bool

布尔值

猜你喜欢

转载自www.cnblogs.com/starz224/p/9112046.html