python学习笔记Part2-6
学习代码+注释
part2:
变量和简单的数据类型:
#part 2 变量和简单的数据类型
#print("hello world!")
idea="hello wwwworld!"
print (idea)
idea=" no hEllo!"
print("normal: ",idea)
print("Title: ",idea.title())
#输出时,使用[ 变量名.title() ]方法 可以使每个单词的首字母大写**并且其他字母必定小写
print("Upper: ",idea.upper(),"\nLower: ",idea.lower())
#另外upper全员大写 lower全员小写
part1="mynameis"
part2="mr.handsome"
whole=part1+part2
whole2=part1,part2 #有疑问
print(part1+part2)
print(part1,part2)
print(whole)
print(whole2)
#合并与拼接字符串 教材是p20,用[+] * 在print内直接用[,]的话会有空格
#制表符是\t 换行符是\n
test1=" space is before!"
test2="space is behind! "
test3=" space are both l&r! "
print("'"+test1.lstrip()+"'")#不需要转义/?
print("'"+test2.rstrip()+"'")
print("'"+test3.strip()+"'")
#.rstrip() .lstrip() .strip() 分别删除右r 左l 两端的空格
num1=0.202
num2=1.75
print(num1+num2)
print(round(num1+num2,1))
print("%.1f" %(num1+num2))
from decimal import Decimal
print(Decimal(num1+num2).quantize(Decimal("0.0")))
print("Happy "+str(round(num1+num2))+" BirthDay!")
#浮点数的小数位数 1.round() 2."%.xf" %a 3.decimal模块<精度高>
#py的浮点数小数保留算法很有意思,大多数都是向下取整,暂时未发现规律。
#str()函数 将其他类型转换为字符串
part3+4:
列表和列表的操作:
#part3 列表
#py中用[]表示列表,当中的元素逗号隔开。
#调用当中的<某个>元素时,可使用第二章学到的.tittle()等方法。
example=["one","two",3.222]
print(example)
#打印整个列表是带方括号和引号的
print(example[0].title())
print(round(example[2],2))
#下标索引从零开始,最后一个也指定为-1
print(example[-1])
#修改列表元素用索引然后重定义
example[2]=66.666
print(example)
#插入元素
#在列表尾插入 用.append()方法
example.append("new adds")
print(example)
#在列表中插入
#.insert()方法
example.insert(0,"insert to 0")
print(example)
#这里我有个问题,那些不改变原变量的函数/方法(eg:.titlt())应该是可以直接在print中用的,怎么判断是那些函数?
#删除列表元素
#1.已知目标元素位置,用del语句 <不是方法>
del example[0]
print("After delete: "+str(example))
#2.使用.pop()方法 默认从列表尾弹出一个元素并存储/如果已知位置,可以在括号里加上索引,精准弹出
popdata=example.pop()
print(popdata)
#3.根据数值删除元素(不知道要删除元素的位置,只知道它的值)使用方法.remove()
example.remove("one")
print("After Remove: "+str(example))
#备注ps:.remove()方法只会删除第一个匹配的值,如果要全部删除,就需要循环
#.sort()方法和 sorted()函数 前者是永久性排序,即修改原列表,后者则是临时性的
newlist=["add","ddd","cdd","bdd"]
print(sorted(newlist))
print(newlist)
newlist.sort()
print(newlist)
#反转列表元素的排列顺序.reverse()方法 也是永久性的
newlist.reverse()
print(newlist)
#len()函数获得列表长度
#part4 第四章操作列表
#1.遍历列表 for
names=["aaa","bbb","vvv"," ccc"]
for one in names:#勿忘冒号
print(one.title())
print("i want to see u")
print("for loop is over")
#感觉Python中代替c中{}的是缩进
#2.创建数值列表
#range(m,n,q)上限不包括n。 <q为步长,不填则为默认值1,即每个元素加q> range函数和list()函数
#range函数只有一个参数时默认为n(从0开始),两个参数为m,n
for num in range(2,6):
print(num)
numlist=list(range(2,6))
print(numlist)
numlist2=list(range(3,8,3))
print(numlist2)
#几个简单的对数字列表进行处理的函数 min max sum
print(str(min(numlist2))+"\t"+str(max(numlist2))+"\t"+str(sum(numlist2)))
#3.列表解析
#首先写一个只有1-10的平方的列表
normal=[]
for value in range(1,11):
value=value**2
normal.append(value)
print(normal)
#如果用解析写的话就是(这部分内容书中没有做具体讲解******
jiexi=[value**2 for value in range(1,11)]
print(jiexi)
#效果相同
#3.使用列表的一部分 切片 通过用冒号指定索引的方式,方括号
new=jiexi[2:5]#冒号前后如果空值则为默认索引,即头、尾
print(new)
#遍历切片 即之遍历访问切片里的内容
for son in jiexi[:2]:
print(son)
#4.复制列表
#不能使用简单的a=b,这只是让a也指向b的列表,实则为同一列表
#应使用a=b[:]
copy=jiexi[:]
copy.append("hello")
print(copy)
print(jiexi)
#5.元组 即不可变得列表
nochange=(0,1,2,3,4,5,6)
#不可变指的是元祖中的每个元素 精确度到元素 但是可以重定义整个元组列表
part5:
if语句:
#part 5 第五章 if语句
pills=["red","yellow","white","black","pink"]
for pill in pills:
if pill!="red":
print("Wrong "+pill.upper())
else:
print("Right "+pill.upper())
#and 和 or 判断多个条件
nums=[0,1,2,3,4,5,6,7,8,9]
for num in nums:
if num>2 and num<6:
print("Middle: "+str(num))
elif num<2 or num>6:
print("Head&Last: "+str(num))
else:
print("NULL")
#not in 检查一个值是否包含 在列表中 if...not in /if...in
if 77 not in nums:
print("Yes")
#布尔表达式
for num in nums:
print(num==7)#判断返回布尔型
#if if-else if-elif-else
for num in nums:#用的elif
if num<4:
print("(1)Less than 4 : "+str(num))
elif num<8:
print("(1)Less than 8 : "+str(num))
else:
print("(1)Else : "+str(num))
for num in nums:#用ififif else
if num<4:
print("(2)Less than 4: "+str(num))
if num<8:
print("(2)Less than 8: "+str(num))
else:
print("(2)Else: "+str(num))
#if 操作列表
#检查列表是否为空 if 变量名 in 列表名
if num in nums:
print("somoething is in this list")
else:
print("it is a empty list")
#使用多个列表
list1s=[2,4,6,8]
list2s=[0,2,4,5,7]
for list1 in list1s:
if list1 in list2s:
print("Two lists both have this num: "+str(list1))
else:
print("This only belongs to list1: "+str(list1))
continue
part6:
字典:
#part 6 第六章 字典(感觉可能很类似哈希)
alien0={'name':'e.t','sex':'none','age':'18'}
print(alien0['sex'])
#键值对
#添加键值对
alien0['xplace']=0
alien0['yplace']=0
print(alien0)
#创建空字典 {}
#修改字典中的值类似于添加赋值
#删除键值对 del语句
del alien0['sex']#方括号里跟的是键名
print(alien0)
#格式
hash1={
'one':1,
'two':2,
'second':2,}
print(hash1)
#遍历字典 for循环 结合.items()方法
user_0={
'name':'aaabbb',
'password':'admin',
'copy':'null',}
for firstone,lastone in user_0.items():
print("key: "
+firstone)
print("value: "
+lastone)
#py不关心键值对的存储顺序,只关心键值之间的对应关系
#只遍历键 .keys()方法
for one in user_0.keys():
print(one)
#只遍历值 .values()方法
for one in user_0.values():
print(one)
#按顺序遍历 sorted()函数
for one in sorted(user_0.keys()):
print(one+" "+user_0[one])
#嵌套(类似于perl中hash的哈希的数组和数组的哈希)
# 字典列表 列表中存储的每个元素都是字典
username_0={1:'one',2:'two',3:'three',}
password_0={1:'onepass',2:'twopass',3:'threepass',}
data_0={1:'empty',2:'some',3:'full',}
accounts=[username_0,password_0,data_0]
i=0
for account in accounts:
print(i)
i=i+1
for key_0 in sorted(account.keys()):
print("key: "+str(key_0)+" values: "+account[key_0])
#在字典中存储列表
steam={
'name_':[0,1,2,3,4],
'price_':[6,7,8,9,9],}
for one in sorted(steam.keys()):
print(one+" "+str(steam[one]))
for sth in steam[one]:
print("gps: "+str(sth))
#列表和字典的嵌套层数不宜过多
#在字典中存储字典
hanyu={'c1':2001,'c2':2002,}
ingyu={'C1':201,'C2':202,}
xinhua={'one':hanyu,'two':ingyu,}
for keyy in xinhua.keys():
for keyyy in xinhua[keyy].keys():
print("last floor's key:" + keyyy )
print("and its value is: "+ str(xinhua[keyy][keyyy]))#这不就很像二维数组吗
#也可以直接在字典里赋值字典
zidians={
'zidian1':
{'a':'A',
'b':'B',
'c':'C',}
}
print(zidians)
for kee in zidians.keys():
for keee in zidians[kee]:
print(keee+' -> '+zidians[kee][keee])
前面的内容已经'overcopy'。
开始今天的学习,告辞!