<jupyter_start><jupyter_text>选择 布尔类型、数值和表达式![](../Photo/33.png)- 注意：比较运算符的相等是两个等号，一个等到代表赋值- 在Python中可以用整型0来代表False，其他数字来代表True- 后面还会讲到 is 在判断语句中的用发<jupyter_code>a = 1
b = '2'
a == b
0 == 0
0 == 100
0 is 1<jupyter_output><empty_output><jupyter_text>字符串的比较使用ASCII值<jupyter_code>a = 'Joker'
b = 'hahaha'
a > b
a = 'Joker'
b = 'Jahaha'
a > b<jupyter_output><empty_output><jupyter_text>Markdown - https://github.com/younghz/Markdown  EP:- - 输入一个数字，判断其实奇数还是偶数<jupyter_code>int(False)
bool(0)
x = eval(input('输入一个数字'))
if x%2==0:
    print(x,'是偶数')
else:
    print(x,'是奇数')
x = eval(input('输入一个数字'))
if bool(x%2):
    print(x,'是奇数')
else:
    print(x,'是偶数')<jupyter_output>输入一个数字45
45 是奇数<jupyter_text>产生随机数字- 函数random.randint(a,b) 可以用来产生一个a和b之间且包括a和b的随机整数<jupyter_code>import random
random.randint(2,3)<jupyter_output><empty_output><jupyter_text>产生一个随机数，你去输入，如果你输入的数大于随机数，那么就告诉你太大了，反之，太小了，然后你一直输入，知道它满意为止<jupyter_code>x = random.randint(0,10)
while 1:
    y = eval(input('y'))
    if y > x:
        print('太大了')
    if y < x:
        print('太小了')
    if y == x:
        print('满意')
        break<jupyter_output>y1
太小了
y2
满意<jupyter_text>其他random方法- random.random 返回0.0到1.0之间前闭后开区间的随机浮点- random.randrange(a,b) 前闭后开<jupyter_code>random.random()
random.randrange(0,10)<jupyter_output><empty_output><jupyter_text>EP：- 产生两个随机整数number1和number2，然后显示给用户，使用户输入数字的和，并判定其是否正确- 进阶:写一个随机序号点名程序<jupyter_code>number1 = random.randint(0,10)
number2 = random.randint(0,10)
print(number1,number2)
x = eval(input('x='))
if x ==(number1 +number2):
    print('Correct')
else:
    print('Error')
for i in range(5):
    x = eval(input('x='))
    if x == (number1 + number2):
        print('Correct')
        break
    else:
        print('Error')
x = eval(input('x='))
for i in range(2,x):
    if x%i == 0:
        print(i,'可以被整除')<jupyter_output>x=10
2 可以被整除
5 可以被整除<jupyter_text>if语句- 如果条件正确就执行一个单向if语句，亦即当条件为真的时候才执行if内部的语句- Python有很多选择语句:> - 单向if   - 双向if-else  - 嵌套if  - 多向if-elif-else  - 注意：当语句含有子语句的时候，那么一定至少要有一个缩进，也就是说如果有儿子存在，那么一定要缩进- 切记不可tab键和space混用，单用tab 或者 space- 当你输出的结果是无论if是否为真时都需要显示时，语句应该与if对齐  EP：- 用户输入一个数字，判断其实奇数还是偶数- 进阶：可以查看下4.5实例研究猜生日<jupyter_code>x = eval(input('>>'))
if x % 2 == 0:
    print(x,'是奇数')
else:
    print(x,'是偶数')
print('运行完毕')<jupyter_output>>>16
16 是奇数
运行完毕<jupyter_text>双向if-else 语句- 如果条件为真，那么走if内部语句，否则走else内部语句<jupyter_code>money = input('Money[y/n]:')
if money == 'y':
    print('再问一个问题')
    handsome = input('Handsome[y/n]:')
    if handsome == 'y':
        print('非常关键的一个问题')
        wife = input('Wife[y/n]:')
        if wife == 'y':
            print('不拒绝')
        else:
            print('马上结婚')
    else:
        print('考虑一下')
else:
    print('Gun ')<jupyter_output>Money[y/n]:y
再问一个问题
Handsome[y/n]:y
非常关键的一个问题
Wife[y/n]:n
马上结婚<jupyter_text>EP：- 产生两个随机整数number1和number2，然后显示给用户，使用户输入数字，并判定其是否正确,如果正确打印“you‘re correct”，否则打印正确错误  嵌套if 和多向if-elif-else![](../Photo/35.png)<jupyter_code>score = 90
if score >= 90:
    grad = 'A'
elif score >= 80:
    grad = 'B'
elif score >= 70:
    grad = 'C'
else:
    grad = 'D'
print(grad)
x1 = eval(input('钱？[0/1]')) 
x2 = eval(input('帅？[0/1]')) 
x3 = eval(input('老婆？[0/1]')) 
x = x1 + x2 + x3
if x == 0:
    print('滚')
if x == 2:
    if x1 == 0:
        print('滚')
    elif x2 == 0:
        print('考虑一下')
    elif x3 == 0:
        print('马上结婚')
elif x == 3:
    print('有老婆，也要考虑一下')
x1 = eval(input('钱？[0/1]')) 
if x1 == 1:
    x2 = eval(input('帅？[0/1]')) 
    if x2 == 1:
        x3 = eval(input('老婆？[0/1]'))
        if x3 == 1:
            print('原配变小三')
        elif x3 == 0:
            print('马上结婚')
    elif x2 == 0:
        print('考虑一下')
elif x1 == 0:
    print('滚')<jupyter_output>钱？[0/1]1
帅？[0/1]1
老婆？[0/1]1
原配变小三<jupyter_text>EP：- 提示用户输入一个年份，然后显示表示这一年的动物![](../Photo/36.png)- 计算身体质量指数的程序- BMI = 以千克为单位的体重除以以米为单位的身高的平方![](../Photo/37.png)<jupyter_code>year = eval(input('YEAR：'))
res = year % 12
if res == 0:
    print('猴')
elif res == 1:
    print('鸡')
elif res == 2:
    print('狗')
elif res == 3:
    print('猪')
elif res == 4:
    print('鼠')
elif res == 5:
    print('牛')
elif res == 6:
    print('虎')
elif res == 7:
    print('兔')
elif res == 8:
    print('龙')
elif res == 9:
    print('蛇')
elif res == 10:
    print('马')
elif res == 11:
    print('羊')
身高 = eval(input('身高'))
体重 = eval(input('体重'))
BMI = 体重 / 身高**2
if BMI<18.5:
    print('超轻')
elif 18.5<=BMI<25:
    print('标准')
elif 25<=BMI<30:
    print('超重')
else:
    print('超级胖胖')<jupyter_output>身高1.60
体重45
超轻<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('闰年')
else:
    print('平年')
number = eval (input('数字'))
bai = number // 100
shi = number // 10 % 10
ge = number % 10
if bai **3 +shi **3 +ge **3 == number:
    print('是水仙花数')
else:
    print('不是水仙花数')
for number in range(100,1000):
    bai = number // 100
    shi = number // 10 % 10
    ge = number % 10
    if bai **3 +shi **3 +ge **3 == number:
        print(number,'是水仙花数')<jupyter_output>153 是水仙花数
370 是水仙花数
371 是水仙花数
407 是水仙花数<jupyter_text>实例研究：彩票![](../Photo/41.png)<jupyter_code>import random
number = random.randint(10,99)
print('中奖数字为：',number)
num1=eval(input("Please input number:"))
number_t=number//10
number_g=number%10
num1_t=num1//10
num1_g=num1%10
if number == num1 :#判断两个数是否相等
    print('恭喜您中奖了!奖金为10000美元')
elif number_t + number_g == num1_t + num1_g: 
    print('恭喜您中奖了!奖金为3000美元')
elif (number_g == num1_g or number_g == num1_t) or (number_t == num1_g or number_t == num1_t):
    print('恭喜您中奖了!奖金为1000美元')<jupyter_output>中奖数字为： 84
Please input number:84
恭喜您中奖了!奖金为10000美元<jupyter_text>Homework- 1![](../Photo/42.png)<jupyter_code>a,b,c = eval(input('Enter a, b, c,:'))
res = b**2 - 4*a*c
if res > 0:
    r1 = (-b + (b**2-4*a*c)**0.5) / 2*a
    r2 = (-b - (b**2-4*a*c)**0.5) / 2*a
    print('The roots are',r1,'and',r2)
elif res == 0:
    r3 = (-b + (b**2-4*a*c)**0.5) / 2*a
    print('The roots is',r3)
elif res < 0:
    print('The equation has no real roots')<jupyter_output>Enter a, b, c,:1.0,3,1
The roots are -0.3819660112501051 and -2.618033988749895<jupyter_text>·2          （游戏：学习加法）编写一个程序产生两个100以下的整数，然后提示用户输入这两个整数的和。             如果答案是正确的，程序报告结果为真，否则为假。这个程序类似于程序清单4-1。<jupyter_code>import random
number1 = random.randint(0,100)
number2 = random.randint(0,100)
x = eval(input('x='))
if x ==(number1 +number2):
    print('Correct')
else:
    print('Error')<jupyter_output>x=68
Error<jupyter_text>- 3![](../Photo/44.png)<jupyter_code>today = int(input('Enter today\'day:'))
plus = eval(input('Enter the numer of days elapsed since today:'))
I=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
if today == 0:
    x = plus%7+today
    print('Today is',I[today-1],'and the Future day is',I[x-1])
elif today == 1:
    x = plus%7+today
    print('Today is',I[today-1],'and the Future day is',I[x-1])
elif today == 2:
    x = plus%7+today
    print('Today is',I[today-1],'and the Future day is',I[x-1])
elif today == 3:
    x = plus%7+today
    print('Today is',I[today-1],'and the Future day is',I[x-1])
elif today == 4:
    x = plus%7+today
    print('Today is',I[today-1],'and the Future day is',I[x-1])
elif today == 5:
    x = plus%7+today
    print('Today is',I[today-1],'and the Future day is',I[x-1])
elif today == 6:
    x = plus%7+today
    print('Today is',I[today-1],'and the Future day is',I[x-1])<jupyter_output>Enter today'day:1
Enter the numer of days elapsed since today:3
Today is Monday and the Future day is Thursday<jupyter_text>- 4![](../Photo/45.png)<jupyter_code>a,b,c = eval(input('a,b,c:'))
if a>b>c:
    print(c,b,a)
elif a>c>b:
    print(b,c,a)
elif b>a>c:
    print(c,a,b)
elif b>c>a:
    print(a,c,b)
elif c>a>b:
    print(b,a,c)
elif c>b>a:
    print(a,b,c)<jupyter_output>a,b,c:5,1,8
1 5 8<jupyter_text>- 5![](../Photo/46.png)<jupyter_code>price1,weight1 = eval(input('Enter weight and price for package 1:'))
price2,weight2 = eval(input('Enter weight and price for package 2:'))
if (price1/weight1)>(price2/weight2):
    print('Package 1 has the better price.')
elif (price1/weight1)<(price2/weight2):
    print('Paclage 2 has the better price.')<jupyter_output>Enter weight and price for package 1:50,24.59
Enter weight and price for package 2:25,11.99
Paclage 2 has the better price.<jupyter_text>- 6![](../Photo/47.png)<jupyter_code>month = eval(input('输入月份'))
year = eval(input('输入年份'))
if ((year%4==0) and (year%100!=0)) or (year%400==0):
    if month<=7:
        if month%2==0:
            if month==2:
                print(year,'年',month,'月','有29天')
            else:
                print(year,'年',month,'月','有30天')
        else:
            print(year,'年',month,'月','有31天')
    else:
        if month%2==0:
            print(year,'年',month,'月','有31天')
        else:
            print(year,'年',month,'月','有30天')
else :
    if month<=7:
        if month%2==0:
            if month==2:
                print(year,'年',month,'月','有28天')
            else:
                print(year,'年',month,'月','有30天')
        else:
            print(year,'年',month,'月','有31天')
    else:
        if month%2==0:
            print(year,'年',month,'月','有31天')
        else:
            print(year,'年',month,'月','有30天')<jupyter_output>输入月份5
输入年份1999
1999 年 5 月 有31天<jupyter_text>- 7![](../Photo/48.png)<jupyter_code>x = random.randint(0,1)
y = eval(input('正面为1，反面为0 输入猜测数字'))
if y == x:
    print('猜测正确')
else :
    print('猜测错误')<jupyter_output>正面为1，反面为0 输入猜测数字1
猜测正确<jupyter_text>- 8![](../Photo/49.png)<jupyter_code>import random
computer = random.randint(0,2)
m = eval(input('剪刀为0，石头为1，布为2：'))
if computer>m:
    print('coumputer is',computer)
    print('computer won')
if computer<m:
    print('coumputer is',computer)
    print('mine won')
if computer==m:
    print('coumputer is',computer)
    print('it is a draw')<jupyter_output>剪刀为0，石头为1，布为2：2
coumputer is 1
mine won<jupyter_text>- 9![](../Photo/50.png)<jupyter_code>import math
y = int(input("请输入年份"))
m = int(input("请输入月份"))
q = int(input("请输入这是这个月的第几天"))
a = q + ((26 * (m + 1)) / 10 )
j = y // 100
k = y % 100
b = k + (k / 4) +(j / 4) + 5 * j
h = (a+b)%7
print(h)
x = math.floor(h)
if x == 0: 
    print("今天是星期六")
elif x == 2: 
            print("今天是星期一")
elif x == 3:
            print("今天是星期二")
elif x == 4:
            print("今天是星期三")
elif x == 5:
            print("今天是星期四")
elif x == 6:
            print("今天是星期五")
elif x == 1:
            print("今天是星期日")<jupyter_output>请输入年份2019
请输入月份4
请输入这是这个月的第几天29
2.75
今天是星期一<jupyter_text>- 10![](../Photo/51.png)<jupyter_code>import random
colour=random.randint(1,4)
value=random.randint(1,12)
if colour==1:
    h='梅花'
elif colour==2:
    h='红桃'
elif colour==3:
    h='方块'
elif colour==4:
    h='黑桃'
elif value==1:
    value='Ace'
elif value==2:
    value='2'
elif value==3:
    value='3'
elif value==4:
    value='4'
elif value==5:
    value='5'
elif value==6:
    value='6'
elif value==7:
    value='7'
elif value==8:
    value='8'
elif value==9:
    value='9'
elif value==10:
    value='10'
elif value==11:
    value='Jack'
elif value==2:
    value='Queen'
elif value==2:
    value='king'

print('The card you picked is the',h,'of',value)<jupyter_output>The card you picked is the 红桃 of 8<jupyter_text>- 11![](../Photo/52.png)<jupyter_code>x = eval(input('请输入一个三位整数:'))
if x%10==x//100:
    print(x,'是回文数')
else:
    print(x,'不是回文数')<jupyter_output>请输入一个三位整数:565
565 是回文数<jupyter_text>- 12![](../Photo/53.png)<jupyter_code>a,b,c = eval(input('Enter three edges:'))
if (a+b>c) and (a+c>b) and (b+c>a):
    print('The perimeter is',a+b+c)
else:
    print('输入的边长是非法的')<jupyter_output>Enter three edges:3,5,6
The perimeter is 14
{'fraction_non_alphanumeric': 0.19402475458813487, 'fraction_numerical': 0.05309432351685873, 'mean_word_length': 3.7052208835341367, 'pattern_counts': {'":': 0, '<': 91, '<?xml version=': 0, '>': 109, 'https://': 1, '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': '25', 'dir': 'jupyter-structured-clean-dedup', 'id': '646969', 'license': 'Apache-2.0', 'n_tokens_mistral': 6118, 'n_tokens_neox': 6056, 'n_words': 967, 'path': '7.18.ipynb', 'repo_name': 'xym99/mysql', 'size': '11715'}