加油
简单回忆
注释: 单行 # 多行‘’‘ ’‘’
#输出
age = 10
print(我今年%d岁了%age)
print("我的名字是%s,我的国籍是%s"%("小张","中国"))
print("aaa","bbb","ccc") #输出 aaa bbb ccc
print("www","baidu","com",sep=".")#输出www.baidu.com
print("hello",end="") #end="\t" end="\n"
#输入
password = input("请输入密码")
print("您输入的密码是:",password)
#注意input输入的是字符串
#若需要修改为数字,需要强制类型转换
a = int(input("输入一个数字:"))
print("输入了一个数字%d"%a)
#判断
注意冒号和缩进,一个项里的内容必须对齐
score = 81
if score >=90 and score <=100:
print("等级A")
elif score >=80 and score <90:
print("等级B")
elif score >=70 and score <80:
print("等级C")
else:
print(等级D)
库的引入
import random #随机库
x = random.randint(0,2)
print(x)
将整个模块(somemodule)导入,格式为:import somemoule
从某个模块中导入某个函数:from somemoule imort somefunction
从某个模块中导入多个函数,格式为:from somemodule import firstfuc,secondfuc
将某个模块中的全部函数导入,格式为:from somemodule import \*
for循环
for i in range(5): #输出0-4
print(i)
#左闭右开
for i in range(0,12,3): #从0开始,到12结束,步进值为3 (不包含12)
#输出0、3、6、9
print(i)
for i in range(-10,-100,-30): #输出-10 -40 -70
print(i)
name="jinan"
for i in name:
print(i,end="\t") #输出j i n a n
a = ["aa","bb","cc","dd"]
for i in range(len(a)): #注意len的使用
print(a[i])
while循环
#while不同于其它语言地方
count = 0
while count < 5:
print(count,"小于5")
count += 1
else:
print(count,"大于或等于5")
'''输出
0 小于5
1 小于5
2 小于5
3 小于5
4 小于5
5 大于或等于5'''
break #结束整个while循环
continue #跳过本次循环continue后面语句
pass #空语句,一般用作占位语句,不做任何事情
#九九乘法表
for i in range(1,10,1):
for j in range(1,i+1,1):
print("%d*%d=%d"%(i,j,i*j),end=" ")
print("\n")