基础篇:
(基础知识+实例+项目开发)
2.7版本:用户交互: name = raw_input("please input your name:") 1.3: 3.0版本: eval(变量) 找到相应类型的变量
2.7版本: input
2、条件判断与缩进: 2.1: if...: + 语句 + elif...: + 语句 + else: + 语句 2.2: 缩进确定作用域 例: #简单条件选择 sex = input("please input your gender:") if sex == "girl":
print("I would like to have a little monkey with you!")
elif sex == "man":
print("Going to homosexual!...")
else:
print("Porver!")
3、循环控制 3.1: while True
while False
while "条件"
break 退出
while "条件" else:"语句" 例: #猜数字(限制猜3次)
Luckynum = 19
guessnum = -1
count = 0 while count < 3: guessnum = int(input("plaese input the number:")) if guessnum > Luckynum:
print("the real is smaller")
elif guessnum < Luckynum:
print("the real is bigger")
else:
print("bingo")
break
count += 1 else:
print("too many retrys") 3.2: for i in rang(3):
else "语句" 例: #猜数字(限制猜3次)
Luckynum = 19
guessnum = -1
count = 0 for i in range(3): guessnum = int(input("plaese input the number:")) if guessnum > Luckynum:
print("the real is smaller")
elif guessnum < Luckynum:
print("the real is bigger")
else:
print("bingo")
break else:
print("too many retrys")
4、字符串的格式化: 4.1: +变量+ 4.2: 格式控制字符 %(变量名) 4.3: msg = '''
格式控制字符
'''
print(msg) 4.4: .strip() 移除功能 里边可以加字符,表示去掉相应字符;默认则是移除两边空白 例:name = input("name:").strip()
age = input("age:").strip()
job = input("job:").strip() print("Information of[]:" + name + "\nName:[]" + name + "\nAge[]:" + age + "\nJob:[]" + job ) print("Information of %s:\nName:%s\nAge:%s\nJob:%s" %(name,name,age,job)) msg = '''
Information of %s
Name:%s
Age:%s
Job:%s
''' %(name,name,age,job)
print(msg)
5、列表功能 5.1:索引: 格式:变量[] 去除字符串变量中的相应的值 5.2:追加: 格式:变量.append() 在字符串变量后增加想加入的元素 5.3 寻找: 格式:变量.index("想寻找的元素") 找到想找的元素的下标 5.4 计算: 格式:变量.count("想知道的元素") 计算想知道元素的个数 5.5: 插入: 格式:变量.insert(索引值) 在想要的位置插入一个元素 5.2:删除: 格式:变量.remove / 变量.pop() 总是删除最后一个 5.3:反转: 格式:变量.reverse() 将各元素的值反转 5.4:排序: 格式:变量.sort() 将元素的值按照ASCL码值进行排序 5.4:切片: 格式:变量[0:2] 表示从数组中切出前两个值 5.5:循环: 格式:for i in range(变量.count('指定的元素')) 5.6: 拓展: 格式:变量1.extend(变量2) 将两个变量拼接 5.7:包含: 格式:元素1 in 元素2 会返回True或者False 例1:
name_list = ["alex","65brother",tenglan]
取元素(索引值):name_list[0] 例2:
同时删除多个相同元素
for i in range(name_list.count('65brother')):
name_list.remove('65brother')
6、元组 6.1:元组类型:tuple 6.2:相互转换: tuple(列表变量) 将列表转换成元组 / list(元组变量) 将元组转换为列表 6.3:功能有限
7、运算符 7.1:算数运算符 7.11: == 比较是否相等 7.12: != 比较是否不相等 7.13: % 求余 7.14: // 整除后取整数部分 7.15: ** 幂 7.16: 按位运算符
7.2:按位运算符 128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 A=10
0 0 1 1 0 0 1 0 B=50 0 0 0 0 0 0 1 0 A&B=2(要真全真)
0 0 1 1 1 0 1 0 A|B=58(一真全真)
0 0 1 1 1 0 0 0 A^B=56(一真一假) 左移:整体左移
右移:整体右移
7.3:逻辑运算符 7.31:and 7.32: or 7.33: not
7.4:身份运算符 7.41 is 7.42 is not
8、文件操作 8.1: write 只写 8.2: read 只读 8.3: write+ 先写后读(不建议使用,会冲掉原先的文件内容) 例: #写文件
# f = open("test.log","w")
#
# f.write("This is the first line.\n")
# f.write("This is the second line.\n")
# f.write("This is the third line.\n")
# f.write("This is the fourth line\n")
#
# f.close() #读文件
f = open("test.log","r") # print(f.read()) 读所有 # print(f.readlines()) 列表方式 '''for line in f:
print(line)
''' for line in f:
if "3" in line:
print("This is the third line")
else:
print("line") f.close()