Python语言(一)

自动化测试
        写代码帮助测试
 
 
python
 
    python3.x
 
代码:
print("Hello world!")
 
 
 
 
 
1、Python数据类型
代码
print("Let's go")
print('吴彦祖"很帅"')
print("""'''Let's go 吴彦祖"很帅"'''""")
 
 
注释:  '''  '''          #
 
代码
name = '小黑' # 字符串,tring 字符串必须加引号
age = 18 # 整数,int
score = 99.3 # 浮点数,float
user = 'wangxiaohei'
 
2、 条件判断
代码
if age >= 18:
print('成年人')
else:
print('你还是个宝宝')
 
if score >=90 and score<=100:  #等于要用两个等号 ==
print('优秀')
elif score<90 and score>=80:
print('良好')
elif score <80 and score>=60:
print('及格')
else:
print('不及格')
 
 
3、输入
input输入的都是string类型
代码
score = input('请输入你的分数:')
print('score的类型',type(score)) # type看一个变量的数据类型
score = int(score) # 类型转换
print('转换之后的score类型',type(score))
if score>=90 and score<=100:
print('优秀')
elif score<90 and score>=80:
print('良好')
elif score <80 and score>=60:
print('及格')
elif score<60:
print('不及格')
else:
print('分数不合法')
报错:
 
 
4、循环
循环、遍历、迭代
 
whil、for
 
1>用while 首先要定义一个计数器
2>循环就是在重复执行循环体里面的代码
 
 
代码
count = 0
while count<20:
print('添加了一个用户',count)
count = count+1
# count+=1 和上面是一样的
 
3>break,在循环里面遇到break,立马结束
 
4> continue,结束本次循环,继续进行下一次循环
猜数字游戏
#1、随机产生一个1-100之间的数字
#2、输入一个1-100之间的数字
#3、总共7次机会
#4、如果猜大了,提示猜大了继续猜
#5、如果猜对了,就结束游戏
代码
import random
number = random.randint(1,100) #随机产生一个1-100的数字
count=0
while count<7:
count=count+1
guess = input('请输入一个数字:')
guess = int(guess)
if guess == number:
print('恭喜你猜对了,游戏结束')
break
elif guess<number:
print('猜小了')
continue
else:
print('猜大了')
continue
代码
import random
number = random.randint(1,100) #随机产生一个1-100的数字
count=0
while count<7:
count = count + 1
guess = input('请输入一个数字:')
guess = int(guess)
if guess == number:
print('恭喜你猜对了,游戏结束')
break
elif guess<number:
print('猜小了')
continue
else:
print('猜大了')
continue
else: # while 对应的else的作用是,循环正常结束之后,会执行else里面的代码
print('错误次数已用完')
 
break、continue后面的代码执行不到
 
5、字符串格式化
代码
import datetime
 
user = '金刚'
today = datetime.datetime.today()
msg = '欢迎'+user+','+'今天的日期是'+ str(today)
msg2 = '欢迎 %s登录,今天的日期是 %s'%(user,today)
msg3 = '欢迎%s登录' % user
print(msg)
print(msg2)
print(msg3)
 
 
import datetime
 
user = '金刚'
today = datetime.datetime.today()
msg = '欢迎'+user+','+'今天的日期是'+ str(today)
msg2 = '欢迎 %s登录,今天的日期是 %s'%(user,today)
msg3 = '欢迎%s登录' % user
 
age = 18
score=97.5
msg4 = '你的名字是%s,你的年龄是 %d,你的分数是 %.1f' %(user,age,score)
 
print(msg4)
 
 
s = 'ab C '
 
result = s.count('h') # 统计个数
print(s.strip()) # 去空格,和换行符
print(s.lstrip()) # 去左边的空格
print(s.rstrip()) # 去右边的空格
 
print('lower',s.lower()) #把字符串变成小写的
print('upper',s.upper()) #把字符串变成大写的
import random
 
 
s = 'abcd123456'
result = random.choice(s) # 随机选一个元素
print(result)
 
print(len(s))
 
 
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/zibinchen/p/11444031.html