一、变量定义的规则:
1) 变量名只能是 字母、数字或下划线的任意组合
2) 变量名的第一个字符不能是数字
3) 以下关键字不能声明为变量名(都是后续需要用到的关键字)
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
默认都是字符串类型(str)。
eg:
1 # Author : Lyr 2 3 name = "Lei Yan rui" 4 5 name2 = name #name2不指向name,而是指向字符串"Lei Yan rui",所以name变了,name2并没有变 6 7 print("My name is ",name,name2) 8 9 name = "PaoChe Ge" 10 11 12 gf_of_oldboy = "Chen rong hua" 13 14 15 16 print(name,name2) 17 18 PIE = 123 #大写相当于常量,不应该改,但是可以改
二、分支语句结构
包括:
1) if
2) if...elif...
3) if...elif...else
eg:
1 # Author : Lyr 2 3 age_of_oldboy = 34 4 5 guess_old = (int)(input("guess age: ")) 6 7 if guess_old==age_of_oldboy: 8 print("Yes,you got it.") 9 elif guess_old > age_of_oldboy: 10 print("Think bigger") 11 else: 12 print("Think smaller")
三、循环结构
1)for循环
2)while循环
eg:for
1 # Author : Lyr 2 3 for i in range(0,10): # 0可省 4 print(i) 5 6 for i in range(0,10,2): # 每次都加2输出 7 print(i)
eg:while
# Author : Lyr age_of_oldboy = 34 num = 3 while num>0: guess_old = (int)(input("guess age: ")) if guess_old==age_of_oldboy: print("Yes,you got it.") break elif guess_old > age_of_oldboy: print("Think bigger") else: print("Think smaller") num -= 1 if num==0: countine_confirm = input("do you want to you keep guessing..?") if countine_confirm != 'n': num = 3
四、总结
代码几乎都是一层扣一层的,都运用了前面的知识,如果小编给的例子有什么问题的话希望大家及时评论出来,我将加以改正,有什么不明白的也可以评论,小编会尽量帮大家解决的。