Day 6 Understand the variable type and the immutable type int, the basic use of float str, the basic use of list, the basic use of

Today's focus:

for+range()

#起始位置,结束位置,步长
for i in range(10,3,-1):
    print(i)
        一个数结束位置
        两个数起始位置结束位置
        三个数起始位置结束位置加步长

for+enumerate

a = [1, 2, 3]
for i in enumerate(a):
    print(i)	# enumerate给列表格式加了索引。0开头,打印结果为(01)(12)(23)

enumerate枚举
a = {
    
    "name": "nana", "age": 18, "gender": "female"}
for x in enumerate(a):
    print(x)    # (0,"name")  (1,"age")  (2,"gender")
    print(x[1])   # name  age  gender
    print(a[x[1]])  # nana 18  female

What is a mutable type

值改变,  内存地址不变(id不变)证明就是在改变原值,原址是可变类型
a=[111,222,333]
print(id(a))
a[0]=666
print(a)	# [666,222,333]
print(id(a))	# id 不变

What is an immutable type

值改变, 内存地址也变(id也变)证明就是产生新值,原址是不可变类型
x = 10
print(id(x))	# 2033297472
x = 11
print(x)    # x = 11(python代码是从上往下运行的,x=11直接覆盖x=10)
print(id(x))	# 2033297488  两次打印id不一样,是不可变类型

Basic use of int:

age = int("18")
print(age,type(age))	# 18 <class 'int'>
int数据类型转换:可以把纯数字组成的字符串转成整型

res=int("      1819   ")
print(res,type(res)) #数字开头或者结尾可以是空格,如果数字中间是空格,那么转换失败.

常用操作+内置方法
数学运算+比较运算

类型总结,存一个值,是不可变量

Basic use of float

定义方式:float
a = 3.1  	# a = float(3.1)
float数据类型转换:可以把小数组成的字符串转成浮点型

整型转成浮点型
res = float(111)
print(res, type(res))	# 111.0 <class 'float'>

字符串转成浮点型
res = float("3.3")
print(res,type(res))	# 3.3 <class 'float'>

常用操作+内置方法
数学运算+比较运算
类型总结,存一个值,是不可变量
x = 3.1
print((id(x)))	# 51528960
x = 4.3
print(x)	# x = 4.3
print(id(x))	# 51528112	两次打印id不一样,是不可变类型

Priority operations

1. Get the value by index (take forward + take reverse): only take

字符串:字符串是个不可分割的整体
msg = "hello world"
print(msg[0], type(msg))  # h <class 'str'>
print(msg[-1])  # d
msg[0]="H"  # 直接报错,字符串的字母不能进行修改,不可变类型

2. Slicing (head and tail, step size)

切边是复制了一份,并不是真的切走
msg="hello world"

msg[1:7]
# 计算机是二进制,所以开头的字母h是0,切片操作最后一个值是不取的(顾头不顾尾)。所以最后的切片取值结果为:
ello w,没有写步长,那么步长默认为1。其中空格也算一个值。

msg[1:7:2]  # (1取到7,步长为2)  取值结果为el ,取值的值为1 3 5

res = msg[ : ]   # 复制字符串 (起始位置没写默认为0,中间位置为结尾位置,没写默认结尾+1,默认步长为1)
写全是res = msg[ : : 1] ,还有一种写法[ : : ]
print(res)  # 打印结果为hello world

倒过来写res = msg[ : :-1]
print(res)    # 打印结果为dlrow olleh

3. Length len

msg="hello world"
print(len(msg))统计字符串长度,注意里面的空格也算是字符

4. Member operations in and not in

判断子字符串是否存在于大字符串中
msg="hello world"
print("he" in msg) True
print("he" not  in msg)  False
print (not "he" in  msg) 跟上面的运用一样,结果也为False,推荐用上面的哪一种。

5. Remove blank strip

.调出了方法;函数是直接调名字调出来的
msg="     hello    "
res=msg.strip()
”     hello    ”.strip()
print(res)   hello移除了空白

msg="******hello******"
print(msg.strip("*"))   移除了hello中的 *,如果字符串里面左右是其他的符号,也可以通过strip移除。
strip不能移除范围以外中间的字符

strip案例:
inp_name = input("username")
inp_pwd = input("password")
if inp_name.strip() = "nana" and inp_pwd.strip() = "123":
	print("pass")
else:
	print"(fail")

6. Split

通过切片的方式可以取出nana的字符串:
msg = "nana:123:30000"
print((msg[0:3]))

针对有规律的字符串,我们可以用split来进行切割。
res = msg.split(":")这样会变成列表的形式res = ["nana","123","30000"]
如果res = msg.split(":",1)这样会变成res = ["nana","123":"30000"],只会把第一个切割开

7, loop

msg = "nana:123:30000"
for x in msg:
    print(x)	# 	会把字符串里面的每一个字符都取出来

Some functions of string need to be mastered

strip,lstrip,rstrip
msg="*****hello******"
print(msg.strip("*"))    	 # 去所有的*print(msg.lstrip("*"))   	 # 去左边的*print(msg.rstrip("*"))   	 # 去右边的*
lower upper
msg="aAbA"
print(msg.lower())  	# 所有的都变成小写
print(msg.upper() ) 	# 所有的都变成大写
startswith,endswith
msg="egon is ok"
 print(msg.startswith("egon"))	 # 判断"egon"是不是在msg这个字符串开头
 print(msg.startswith("eg"))   	# 判断"eg"是不是在msg这个字符串里面开头
 print(msg.endswith("ok")) 		# 判断"ok"是不是在msg这个字符的结尾
format 的三种用法
msg = ("my name is %s,age is %s" % ("nana", 18))
msg = "my name is {x},age is {y}".format(y=18,x="nana")  	# my name is nana ,age is18
msg = "my name is {} ,age is{}".format("nana",18) 	 # my name is nana ,age is18
msg = "my name is {1},age is {0} {0} {0}".format(18,"nana")	 # my name is nana,age is 18 18 18
msg = "my name is {x},age is {y}".format(**{
    
    "x":18,"y":"egon"}) 	 # my name is 18,age is egon
msg = "my name is %(x)s,age is %(y)s" % {
    
    "x":"nana", "y":18}	 # my name is nana,age is 18
print(msg)

使用f也可以实现同样的功能:
x = "nana"
y = 18
print(f"my name is {x},age is {18}")
split,resplit
msg="nana:18:3000"
print(msg.split(":",1)) 	 # ['nana', '18:3000']
print(msg.rsplit(":",1))	 # ['nana:18', '3000']

join (跟split正好相反)
msg = "nana:18:3000"
l = msg.split(":")
print(l)   # ['nana', '18', '3000']
print(l[0]+":"+l[1]+":"+l[2])  # nana:18:3000
print(":".join(l))  # nana:18:3000
replace
msg = "nana  xxx  nana  yyyy  nana"
print(msg.replace("nana", "NANA", 1)) 	# NANA  xxx  nana  yyyy  nana
print(msg.replace("nana","NANA")) 	 # 括号末尾不写值的话,默认是全部 NANA  xxx  NANA  yyyy  NANA
isdigit 	# 判断用户的输入是不是数字
a = input("输入数字")
if a.isdigit():
    a = int(a)
    print(a,type(a))
else:
    print("必须输入数字")

Some features of strings need to know

find,rfind,indiex,rindex,count 字符串的查找功能
msg = "hello xxelxabc"
res = msg.find("el")  	# 从左往右,第一个字母的位置,打印结果为1
res = msg.rfind("el")  	 # 从右往左,第一个字母的位置,打印结果为8
print((res))

indiex 作用与find作用一样。不过如果字符串中没有我们需要查找的值,find会显示-1,
indiex会直接报错

res = msg.count("el")	 # 计算字符串中需要查找的数字出现的次数
print(res)

center,ljust,rjust,zfill 		# 字符串的一些美化功能
print("hello".center(20, "*"))  	# *分别在两边,hello居中  *******hello********
print("hello".ljust(20, "*"))  	 # *在右边,hello居左  hello***************
print("hello".zfill(20)) 	 # 默认操作等同于.ljust,不过zfill是用0填充的,在右边,hello居左  000000000000000hello
print("hello".rjust(20, "*"))  	 # *在左边,hello居右  ***************hello

captalize, seapcase, title
print("hello world".capitalize())  	# 首字母变大写Hello world
print("aAbB".swapcase()) 	# 大写变小写,小写变大写AaBb
print("helLo world".title()) 	# 将首字母变成大写,其他的字符都变成小写Hello World

is number series

bytes ,unciode
num1=b"4"  # bytes(字符)
num2=u"4"  # unicode,python3中加u就是unciode(统一字符标准)
num3="四"     # 中文数字
num4="Ⅳ"      # 罗马数字

bytes ,unciode
print(num1.isdigit())	# True
print(num2.isdigit())	# True
print(num3.isdigit())	# False
print(num4.isdigit())	# False

unciode
print(num2.isdecimal())		# True
print(num3.isdecimal())		# False
print(num4.isdecima())	# False

unicode,中文数字,罗马数字(适用于银行软件使用)
print(num2.isnumeric())		# True
print(num3.isnumeric())		# True
print(num4.isnumeric())		# True

is其他
name = "nana123"
print(name.isalpha())    # 判断字符串里面是不是纯字母组成False
print(name.isalnum())    # 判断字符串里面是不是字母或数字组成True
print(name.islower())    # 判断字符串里面是不是纯小写字母组成True
print(name.isupper())    # 判断字符串里面是不是纯大写字母组成False

name="  "
print((name.isspace()))     # 判断字符串里面是不是空格组成True

name="Hello World"
print(name.istitle())   # 判断字符串里面首字母是不是都是大写True

List

list 数据类型转换:把可迭代的类型转成列表,可以被for循环遍的类型都是可迭代的类型
例如:字符串,字典都可以。

1.按索引存取值(正向存取+反向存取);即可存也可取。
列表是可变类型的值
案例:
l=[111,222,333]
print(id(l))
l[0]=666
print(l)
print(id(i))	# 同上面的l id一样
l=[111,222,333]
l[0]=666  索引里面的值可以更改,但是不能往索引里面添加值
l[3]=777   列表索引不能超出范围,超出范围会直接报错

2. Slicing

l = [11,12,13,14,15,16,17]
res = [1:7:2]
print(res) 结果为:12,14 ,16

3. Length

print(len[111,22,33,444,555,"AAA"])    # 打印结果为5,len是统计列表里有多少个元素

4. Member operations in and not in

l=[111,222,333,“AAA”]
print(111 in l) 	# 结果为Ture

5. Add

l=[111,222,333]
l.append(444)
l.append(555)
print(l)    # 这样l就会变成[111, 222, 333, 444, 555]

插入值
l=[111,222,333]
l.insert(1,6666)
print(l) 	# 这样l就会变成了[111, 6666, 222, 333]

6. Delete

万能删除:
l=[111,222,333]
del l[0]
print(l)
直接删除元素

l.remove 指定元素删除:
res=l.remove(222)
print(l)
直接删除元素
print(res)

l.pop (指定索引删除)
res=l.pop(1)
print(l)
print(res)
这一种相当于把元素取出来

7. Loop

for x in msg:
	print(x)
	会把列表里面的值按序列都取出来

Guess you like

Origin blog.csdn.net/Yosigo_/article/details/111563101