变量
变量就是变化的量
变量的赋值过程
>>> Name = ‘zhang‘
>>> Age = 20
>>> print (Name,Age)
zhang 20
加引号为字符串,不加字符串为变量
>>> name,age
(‘zhang‘, 18)
变量起名:
1.显式
2.起名通俗易懂
3.不能以数字开头
4.不能以点开头,中间不能带-
5.不能加空格
6.关键字不能为变量
获取用户输入:input与raw_input区别
python3x
python2x
raw_Input(在py3中已舍弃)
记住,python2.x版本中,使用input时,需要强制降他转换为字符串,所以需要自己加引号
不加引号为变量,加引号为字符串
条件判断与缩进
if: print elif: print else: print 结构为这样 代码块: sex = input("Please input you gender: ") if sex == "girl": print ("I would like to hava a little money with Tenlan") elif sex == "boy": print ("I would like you ") else: print ("I like you life") 自己写的代码块: num = 6 num2 = int(input("Please input you num:")) if num2 == num: print ("Yes") elif num2 >= num: print ("It‘s bigger") else: print ("It‘s smaller")
while循环
lucky_num = 19 #定义一个主参数,做比较数 input_num = -100 #定义个参数,为比较数 while lucky_num != input_num: #while循环开始,19与-100不相等,为真,继续下面的代码 input_num = int(input("Please input you num:")) #正式接收用户传来的参数 # if lucky_num < input_num: # print ("bingo!") if lucky_num < input_num: #做小于比较 print ("It‘s bigger") elif lucky_num > input_num: #做大于比较 print ("It‘s samller") print ("Bingo") #最后做相等比较 #在一个等式中,只要三种可能,大于、小于、等于 将等于写在最外面,做等于
猜数字大小与关系且次数问题
lucky_num = 19 input_num = -100 guess_count = 0 while guess_count < 3: input_num = int(input("Please input you num:")) if lucky_num < input_num: print ("It‘s bigger") elif lucky_num > input_num: print ("It‘s smaller") else: print ("Bingo") break guess_count += 1 else: print ("Try angin")
for循环
lucky_num = 19 input_num = -100 for i in range(3): input_num = int(input("Please input you num:")) if lucky_num < input_num: print ("It‘s bigger") elif lucky_num > input_num: print ("It‘s smaller") else: print ("Bingo") break else: print ("Try angin")
字符串的拼接
name = input("Nmae:") age = input("Age:") job = input("Job:") 接收用户传入的参数 print ("Your name is "+name+" age is "+age+" job is "+job+"",(name,age,job)) print ("Your name is %s age is %s job is %s" %(name,age,job)) 注意:传入参数必须一一对应
列表
append 追加 count 统计 extend 扩展 index 索引 insert 插入 pop 删除一个 remove 删除指定的 reverse 反转 sort 排序 >>> name_list = ["zhang","wang","li","zhao"] >>> name_list [‘zhang‘, ‘wang‘, ‘li‘, ‘zhao‘] >>> name_list.append("sun") 追加一个sun >>> name_list [‘zhang‘, ‘wang‘, ‘li‘, ‘zhao‘, ‘sun‘] >>> name_list.index("li") 查看索引位 2 >>> name_list.count("sun") 统计出现次数 1 >>> name_list.pop() 删除最后一位 ‘sun‘ >>> name_list [‘zhang‘, ‘wang‘, ‘li‘, ‘zhao‘] >>> name_list.append("sun") 追加一个sun >>> name_list.insert(2,"zhou") 在第二个索引位加入一个zhou >>> name_list [‘zhang‘, ‘wang‘, ‘zhou‘, ‘li‘, ‘zhao‘, ‘sun‘] >>> name_list.pop() 删除最后一位 ‘sun‘ >>> name_list.remove("zhou") 制定删除zhou >>> name_list [‘zhang‘, ‘wang‘, ‘li‘, ‘zhao‘] >>> for i in range(name_list.count("zhang")): 一个for循环,利用count找到一共有多少个zhang ... name_list.remove("zhang") 删除zhang ... >>> name_list [‘li‘, ‘wang‘, ‘zhao‘] 切片操作: >>> a[1:4] [2, 3, 4] >>> a[-1:] [‘ge‘] extend >>> a = [1,2,3,4,5,6,‘li‘] >>> b = [77,99,‘ge‘] >>> a.extend(b) >>> a [1, 2, 3, 4, 5, 6, ‘li‘, 77, 99, ‘ge‘] 判断包含: >>> if 5 in a: ... print ("dada") ... dada
元祖
count 统计 index 索引 tuple(x)转换为元祖
文件操作
file_obj = file(“文件路径”,“模式”) file_obj = open(“文件路径”,“模式”) 打开文件的模式有: r,以只读打开文件 w,打开一个文件只用于写入,如果该文件存在则覆盖。如果不存在,则创建 a,打开一个文件用于追加,如果该文件已存在,文件指针将会放在文件的结尾 w+,打开一个文件用于读写,如果存在存在则覆盖,不存在则创建 #一次性加载所有内容到内存 obj.read() #一次性加载所有内用到内存,并根据行分割成字符串 obj.readlines() #每次仅读取一行数据 for line in obj(): print line 写文件内容: obj.write(“内容”) 关闭文件句柄 obj.close() 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 4 line\\n") f.colse() f = open("test.log",‘r‘) for i in f: print (i), print (f.readlines()) f.close() f = open("test.log",‘a‘) f.write("This is the 5 line\\n") f.write("This is the 5 line") f.close() print () ‘‘‘