<jupyter_start><jupyter_text>选择 布尔类型、数值和表达式![](../Photo/33.png)- 注意：比较运算符的相等是两个等到，一个等到代表赋值- 在Python中可以用整型0来代表False，其他数字来代表True- 后面还会讲到 is 在判断语句中的用发<jupyter_code>a=100
b=100
a<=b
c='joker'
d='hahha'
c>d
i=int(True)
j=int(False)
b1=bool(4)
b2=bool(0)
print(i,j,b1,b2)<jupyter_output>1 0 True False<jupyter_text>EP:- - 输入一个数字，判断其实奇数还是偶数  产生随机数字- 函数random.randint(a,b) 可以用来产生一个a和b之间且包括a和b的随机整数  EP：- 产生两个随机整数number1和number2，然后显示给用户，使用户输入数字，并判定其是否正确- 进阶:写一个随机序号点名程序   其他random方法- random.random 返回0.0到1.0之间前闭后开区间的随机浮点- random.randrange(a,b) 前闭后开<jupyter_code>import random
a1=random.randrange(0,10)
print(a1)
#a2=random.randint()<jupyter_output>3<jupyter_text>随机点名<jupyter_code>import time
print(time.time())
sen=int(time.time())
shi=sen//3600%24
fen=sen//60%60
miao=sen%60
shijian=str(shi)+"时"+str(fen)+"分"+str(miao)+"秒"
#print("%d时%d分%d秒",end=''%(shi%24,fen%(60),miao))
lie=random.randint(1,4)
hang=random.randint(1,4)
print(shijian,"位置为：",lie,hang,"的老师请给Git账号：")
lie=random.randint(1,4)
if lie<=2 :
    hang=random.randint(1,4)
else:
    hang=random.randint(1,3)
print("北京时间"+time.strftime("%H:%M:%S"),"位置为："+str(lie)+str(hang)+"的老师请给Git账号")<jupyter_output>北京时间12:36:27 位置为：33的老师请给Git账号<jupyter_text>if语句- 如果条件正确就执行一个单向if语句，亦即当条件为真的时候才执行if内部的语句- Python有很多选择语句:> - 单向if   - 双向if-else  - 嵌套if  - 多向if-elif-else  - 注意：当语句含有子语句的时候，那么一定至少要有一个缩进，也就是说如果有儿子存在，那么一定要缩进- 切记不可table键和space混用，单用table 或者 space- 当你输出的结果是无论if是否为真时都需要显示时，语句应该与if对齐  模拟抢火车票加油包 1、0-10代表总进度 2、随机数字（0-100） 3、input("加油包"） 4、随机数的范围-1 5、随机设定一些姓名，用户输入一个姓名（a,b,c），如果不在那名单中，问是否升级vip，如果升级需要提交50元，<jupyter_code>import random
name1=['polly','lilei','hanmeimei']
#qiangpiaonum=random.randint(0,100)
name=input('请输入你的姓名')
if name in name1:
    qiangpiaonum=random.randint(0,50)
    print('vip会员你好，马上为你抢票,抢票成功率为：'+str(100-qiangpiaonum)+"%")
else:
    queren=input('你是否要加入vip？（Y或N）')
    if queren=='Y':
        m=eval(input("请缴纳vip费用50元:"))
        if m==50:
            qiangpiaonum=random.randint(0,50) 
            print('vip加入成功，马上为你抢票,抢票成功率为：'+str(100-qiangpiaonum)+"%")
            
    else:
        print('普通会员你好，马上为你抢票,抢票成功率为：'+str(100-qiangpiaonum)+"%")
while True:
    if qiangpiaonum<=5:
        print("抢票成功")
        break
    else:
        str1=input("请输入加油口号‘加油包’为他加油：")
        
        if str1=="加油包":
            new=random.randint(1,5)
            qiangpiaonum-=new
            print('你为他加油：'+str(new)+'点，目前抢票成功率为'+str(100-qiangpiaonum)+"%")
        else:
            print('你输入的口号不正确，请重新输入：')<jupyter_output>请输入你的姓名韩梅梅
你是否要加入vip？（Y或N）Y
请缴纳vip费用50元:50
vip加入成功，马上为你抢票,抢票成功率为：80%
请输入加油口号‘加油包’为他加油：加油包
你为他加油：4点，目前抢票成功率为55%
请输入加油口号‘加油包’为他加油：加油包
你为他加油：3点，目前抢票成功率为58%
请输入加油口号‘加油包’为他加油：加油包
你为他加油：2点，目前抢票成功率为60%
请输入加油口号‘加油包’为他加油：加油包
你为他加油：3点，目前抢票成功率为63%
请输入加油口号‘加油包’为他加油：加油包
你为他加油：1点，目前抢票成功率为64%
请输入加油口号‘加油包’为他加油：加油包
你为他加油：4点，目前抢票成功率为68%
请输入加油口号‘加油包’为他加油：加油包
你为他加油：4点，目前抢票成功率为72%
请输入加油口号‘加油包’为他加油：加油包
你为他加油：3点，目前抢票成功率为75%
请输入加油口号‘加油包’为他加油：加油包
你为他加油：3点，目前抢票成功率为78%
请输入加油口号‘加油包’为他加油：加油包
你为他加油：5点，目前抢票成功率为83%
请输入加油口号‘加油包’为他加油：加油包
你为他加油：3点，目前抢票成功率为86%
请输入加油口号‘加油包’为他加油：加油包
你为他加油：4点，目前抢票成功率为90%
请输入加油口号‘加油包’为他加油：加油包
你为他加油：3点，目前抢票成功率为93%
请输入加油口号‘加油包’为他加油：加油包
你为他加油：5点，目前抢票成功率为98%
抢票成功<jupyter_text>EP：- 用户输入一个数字，判断其实奇数还是偶数- 进阶：可以查看下4.5实例研究猜生日  双向if-else 语句- 如果条件为真，那么走if内部语句，否则走else内部语句  EP：- 产生两个随机整数number1和number2，然后显示给用户，使用户输入数字，并判定其是否正确,如果正确打印“you‘re correct”，否则打印正确答案  嵌套if 和多向if-elif-else![](../Photo/35.png)  EP：- 提示用户输入一个年份，然后显示表示这一年的动物![](../Photo/36.png)- 计算身体质量指数的程序- BMI = 以千克为单位的体重除以以米为单位的身高![](../Photo/37.png)<jupyter_code>year=eval(input('请输入一个年份：'))
shengxiao='猴鸡狗猪鼠牛虎兔龙蛇马羊'
print(str(year)+'年是'+shengxiao[year%12]+'年')
height,weight=eval(input("请输入身高（M为单位）和体重（kg为单位）"))
bmi=weight/height
if bmi<18.5:
    print('超轻')
elif bmi<25:
    print('标准')
elif bmi<30:
    print('超重')
else:
    print('痴肥')<jupyter_output>请输入身高（M为单位）和体重（kg为单位）1.78,76
痴肥<jupyter_text>逻辑运算符![](../Photo/38.png) ![](../Photo/39.png)![](../Photo/40.png)  EP：- 判定闰年：一个年份如果能被4整除但不能被100整除，或者能被400整除，那么这个年份就是闰年- 提示用户输入一个年份，并返回是否是闰年- 提示用户输入一个数字，判断其是否为水仙花数<jupyter_code>year=eval(input('请输入一个年份：'))
if (year%4==0 and year%100!=0 )or (year %400==0) :
    print(str(year)+"是闰年")
else:
    print(str(year)+'不是闰年')<jupyter_output>请输入一个年份：2000
2000是闰年<jupyter_text>实例研究：彩票![](../Photo/41.png)<jupyter_code>num=eval(input('请输入一个两位数的数字'))
num1=random.randint(10,99)
numa=num//10
numb=num%10
num1a=num1//10
num1b=num1%10
if num==num1:
    print("中奖号码为"+str(num1)+"你的奖金为$10000!")
elif numa==num1b and numb==num1a:
    print("中奖号码为"+str(num1)+"你的奖金为$3000!")
elif numa==num1b or numb==num1a:
     print("中奖号码为"+str(num1)+"你的奖金为$1000!")
else:
     print("中奖号码为"+str(num1)+"你没有中奖，奖金为0!")<jupyter_output>请输入一个两位数的数字39
中奖号码为95你的奖金为$1000!<jupyter_text>Homework- 1![](../Photo/42.png)<jupyter_code>import math
a,b,c=eval(input('enter a,b,c:'))
data=b**2-4*a*c
if data>0:
    x1=(-b+math.sqrt(data))/(2*a)
    x2=(-b-math.sqrt(data))/(2*a)
    print('The roots are ',round(x1,5),'and',round(x2,5))
elif data==0:
    x1=(-b+math.sqrt(data))/(2*a)
    print('The roots are ',x1)
else:
    print('The equation has no real roots ')<jupyter_output>enter a,b,c:1.0,3,1
The roots are  -0.38197 and -2.61803<jupyter_text>- 2![](../Photo/43.png)<jupyter_code>import random
x1=random.randint(0,100)
x2=random.randint(0,100)
res=eval(input("请输入%d与%d的和：\n"%(x1,x2)))
if res==x1+x2:
    print("正确！")
else:
    print("输入错误")<jupyter_output>请输入55与89的和：
144
正确！<jupyter_text>- 3![](../Photo/44.png)<jupyter_code>weekday=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
today=eval(input("Enter today's day:"))
nextday=eval(input('Enter the number of days elapsed since today:'))
print('Today is '+weekday[today]+' and the future day is '+weekday[(today+nextday)%7])<jupyter_output>Enter today's day:1
Enter the number of days elapsed since today:31
Today is Monday and the future day is Thursday<jupyter_text>- 4![](../Photo/45.png)<jupyter_code>a,b,c=eval(input('请输入三个整数'))

if a>b:
    a,b=b,a
if a>c:
    a,c=c,a
if b>c:
    b,c=c,b
print("升序后的结果为：",a,b,c)
a,b,c=eval(input('请输入三个整数'))

if a>b:
    d=a
    a=b
    b=d
if a>c:
    a,c=c,a
if b>c:
    b,c=c,b
print("升序后的结果为：",a,b,c)<jupyter_output>请输入三个整数3,2,1
升序后的结果为： 1 2 3<jupyter_text>- 5![](../Photo/46.png)<jupyter_code>weight1,price1=eval(input('enter weight and price for package 1:'))
weight2,price2=eval(input('enter weight and price for package 2:'))
pc1=price1/weight1
pc2=price2/weight2
if pc1<pc2:
    print('Package 1 has the better price.')
else:
    print('Package 2 has the better price.')<jupyter_output>enter weight and price for package 1:50,24.99
enter weight and price for package 2:25,11.99
Package 2 has the better price.<jupyter_text>- 6![](../Photo/47.png)<jupyter_code>month,year=eval(input('please input month and year:'))
ls1=[1,3,5,7,8,10,12]
ls2=[4,6,9,11]
if month in ls1:
    print(str(year)+'年'+str(month)+'月份有31天')
if month in ls2:
    print(str(year)+'年'+str(month)+'月份有30天')
if month==2:
    if (year%4==0 and year%100!=0 )or (year %400==0) :
        print(str(year)+'年'+str(month)+'月份有29天')
    else:
        print(str(year)+'年'+str(month)+'月份有28天')<jupyter_output>please input month and year:3,2005
2005年3月份有31天<jupyter_text>- 7![](../Photo/48.png)<jupyter_code>import random
caicezhi=input('请输入正面还是反面：')
yinbi=random.randint(0,1)
if caicezhi=='正面' and yinbi==1:
    print("你猜对了")
else:
    print('你猜错了')<jupyter_output>请输入正面还是反面：正面
你猜错了<jupyter_text>- 8![](../Photo/49.png)<jupyter_code>shitou=eval(input('scissor(0),rock(1)，paper(2):'))
computer=random.randint(0,3)
if computer ==0:
    if shitou ==0:
        print('The computer is scissor,you are scissor too, it is a draw')
    if shitou ==1:
        print('The computer is scissor,you are rock ,you won')
    if shitou ==2:
        print('The computer is scissor,you are paper, you loss')
if computer ==1:
    if shitou ==0:
        print('The computer is rock,you are scissor,you loss')
    if shitou ==1:
        print('The computer is rock,you are rock too, it is a draw')
    if shitou ==2:
        print('The computer is rock,you are paper, you won')
if computer ==2:
    if shitou ==0:
        print('The computer is paper,you are scissor, you won')
    if shitou ==1:
        print('The computer is paper,you are rock ,you loss')
    if shitou ==2:
        print('The computer is paper,you are paper too, it is a draw')<jupyter_output>scissor(0),rock(1)，paper(2):0
The computer is paper,you are scissor, you won<jupyter_text>- 9![](../Photo/50.png)<jupyter_code>year=eval(input('enter year:(e.g.,2008):'))
m=eval(input('enter month:1-12:'))
q=eval(input('enter the day of the month:1-31:'))
if m==1 or m==2:
    m+=12
    year-=1
j=year//100
k=year%100
part1=(26*(m+1))//10
part2=k//4
part3=j//4
h=(q+part1+k+part2+part3+5*j)%7
#h=(q+(26*(m+1)//10)+k+k//4+j//4+5*j)%7
weekd=["Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"]
print(h,'day of the week is ',weekd[h])<jupyter_output>enter year:(e.g.,2008):2013
enter month:1-12:1
enter the day of the month:1-31:25
6 day of the week is  Friday<jupyter_text>- 10![](../Photo/51.png)<jupyter_code>import random
ls1=['Ace','2','3','4',',5','6','7','8','9','10','Jack','Queen','King']
ls2=['梅花','红桃','方块','黑桃']
rand1=random.randint(0,11)
rand2=random.randint(0,3)
print("The card you picked is the ",ls1[rand1],'of',ls2[rand2])<jupyter_output>The card you picked is the  9 of 梅花<jupyter_text>- 11![](../Photo/52.png)<jupyter_code>num=eval(input('Enter a three-digit integer:'))
if num%10==num//100:
    print(num,'is a palindrome')
else:
    print(num,'is not a palindrome')<jupyter_output>Enter a three-digit integer:123
123 is not a palindrome<jupyter_text>- 12![](../Photo/53.png)<jupyter_code>import math
a,b,c=eval(input('enter three edges:'))
if a+b>c and a+c>b and b+c>a and abs(a-b)<c and abs(a-c)<b and abs(b-c)<a:
    print('The perimeter is ',a+b+c)
else:
    print('the data error!')<jupyter_output>enter three edges:1,1,1
The perimeter is  3
{'fraction_non_alphanumeric': 0.1922222222222222, 'fraction_numerical': 0.05787878787878788, 'mean_word_length': 5.952949438202247, 'pattern_counts': {'":': 1, '<': 68, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '151381', 'license': 'Apache-2.0', 'n_tokens_mistral': 5713, 'n_tokens_neox': 5804, 'n_words': 665, 'path': '7.18.ipynb', 'repo_name': 'jastermax/mypython', 'size': '9900'}