<jupyter_start><jupyter_text>选择 布尔类型、数值和表达式![](../Photo/33.png)- 注意：比较运算符的相等是两个等到，一个等到代表赋值- 在Python中可以用整型0来代表False，其他数字来代表True- 后面还会讲到 is 在判断语句中的用发<jupyter_code>money = 500
a = eval(input('输入取钱数量：'))
if a <= 500:
    print('ok')
    b = money - a 
    money = b
    print('剩余的钱数',b)
else :
    print('失败，余额不足')<jupyter_output>输入取钱数量：20
ok
剩余的钱数 -20<jupyter_text>字符串的比较使用ASCII值  Markdown - https://github.com/younghz/Markdown  EP:- - 输入一个数字，判断其实奇数还是偶数  产生随机数字- 函数random.randint(a,b) 可以用来产生一个a和b之间且包括a和b的随机整数   其他random方法- random.random 返回0.0到1.0之间前闭后开区间的随机浮点- random.randrange(a,b) 前闭后开  EP：- 产生两个随机整数number1和number2，然后显示给用户，使用户输入数字的和，并判定其是否正确- 进阶:写一个随机序号点名程序<jupyter_code>import random
a = random.randint(0,10)
b = random.randint(0,10)
print (a)
print (b)
number = eval(input('输入a+b的值:'))
if number == a+b :
    print ('正确')
else :
    print ('错误')
import random
h = random.randint(1,10)
l = random.randint(1,5)
print(h)
print(l)<jupyter_output>6
4<jupyter_text>if语句- 如果条件正确就执行一个单向if语句，亦即当条件为真的时候才执行if内部的语句- Python有很多选择语句:> - 单向if   - 双向if-else  - 嵌套if  - 多向if-elif-else  - 注意：当语句含有子语句的时候，那么一定至少要有一个缩进，也就是说如果有儿子存在，那么一定要缩进- 切记不可tab键和space混用，单用tab 或者 space- 当你输出的结果是无论if是否为真时都需要显示时，语句应该与if对齐  EP：- 用户输入一个数字，判断其实奇数还是偶数- 进阶：可以查看下4.5实例研究猜生日<jupyter_code>num = eval(input('输入一个数：'))
if num %2 == 0:
    print('os')
else :
    print('js')
cj = eval(input('输入你的成绩：'))
if cj >= 90 :
    print('A')
elif  80 < cj <= 90:
    print('B')
elif  70 < cj <= 80:
    print('C')
elif  60 < cj <= 70:
    print('D')
else :
    print('不及格')<jupyter_output>输入你的成绩：78
C<jupyter_text>双向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>kg = eval(input('输入您的体重：'))
m = eval(input('输入你的身高：'))
BMI = kg / (m * m)
if BMI < 18.5:
    print('超轻')
elif 18.5<= BMI < 25.0 :
    print('标准')
elif 25.0 <= BMI < 30.0 :
    print('超重')
elif BMI >= 30.0 :
    print('痴肥','再不减肥就GG了')
year = eval(input('输入年份：'))
if year == 0:
    print('猴')
elif year ==1:
    print('鸡')
elif year ==2:
    print('狗')
elif year ==3:
    print('猪')
elif year ==4:
    print('鼠')
elif year ==5:
    print('牛')
elif year ==6:
    print('虎')
elif year ==7:
    print('兔')
elif year ==8:
    print('龙')
elif year ==9:
    print('蛇')
elif year ==10:
    print('马')
elif year ==11:
    print('羊')<jupyter_output>输入年份：2
狗<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('runnain')
else :
    print('not runnian')
max_num = int(input('请输入最大范围'))
# 获取小于指定数的阿姆斯特朗数
for num in range(100, max_num):
    sum = 0
    length = len(str(num))
    temp = num
    for i in range(length):
        sum += (temp % 10) ** length
        temp //= 10
    if sum == num:
        print(num)<jupyter_output>请输入最大范围10000
153
370
371
407
1634
8208
9474<jupyter_text>实例研究：彩票![](../Photo/41.png)<jupyter_code>import random
a = str(random.randint(0,10))
b = str(random.randint(0,10))
str = a + b
num = str(eval(input('输入一个两位数：')))
if num == str :
    print('奖励10000￥')
65 % 56<jupyter_output><empty_output><jupyter_text>Homework- 1![](../Photo/42.png)<jupyter_code>import math
a,b,c = eval(input('Enter a,b,c:'))

if b*b -4*a*c > 0 :
    r1 = (-b + math.sqrt(b*b - 4*a*c)) / (2*a)
    r2 = (-b - math.sqrt(b*b - 4*a*c)) / (2*a)
    print('The roots are ',r1,'and',r2)
elif b*b - 4*a*c ==0:
    r=r1=r2
    print('The root is ',r)
elif b*b - 4*a*c < 0:
    print('The equation has no real roots')<jupyter_output>Enter a,b,c:1,2.0,1
The root is  -1.0<jupyter_text>- 2![](../Photo/43.png)<jupyter_code>import random
a = random.randint(0,100)
b = random.randint(0,100)
c = a + b
num = eval(input('输入和为：'))
if num == c :
    print('true')
else :
    print('false')<jupyter_output>输入和为：48
false<jupyter_text>- 3![](../Photo/44.png)<jupyter_code>today = eval(input('Enter today :'))
later = eval(input('Enter the number of days elapsed since today:'))
x = (today + later) % 7 
xx  = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
print ('today is ',today,'and the future day is ',xx[x])<jupyter_output>Enter today :0
Enter the number of days elapsed since today:31
today is  0 and the future day is  Wednesday<jupyter_text>- 4![](../Photo/45.png)<jupyter_code>a,b,c = eval(input('输入a,b,c'))
q = [a,b,c]
q.sort()
print (q)<jupyter_output>输入a,b,c5,7,6
[5, 6, 7]<jupyter_text>- 5![](../Photo/46.png)<jupyter_code>kg1,mon1 = eval(input('Enter weight and price 1 for package:'))
kg2,mon2 = eval(input('Enter weight and price 2 for package:'))
if (kg1 / mon1) > (kg2 / mon2):
    print('package 1 has the better price')
elif (kg1 / mon1) < (kg2 / mon2):
    print('package 2 has the better price')
elif (kg1 / mon1) == (kg2 / mon2):
    print('both ok')<jupyter_output>Enter weight and price 1 for package:50,24.59
Enter weight and price 2 for package:25,11.99
package 2 has the better price<jupyter_text>- 6![](../Photo/47.png)<jupyter_code>month,year = eval(input('输入月，年:'))
if month == 2 and  ((year % 4 ==0 and year % 100 !=0) or (year % 400 == 0)):
    print(year,'年',month,'月28天')
elif (month == 1,3,5,7,8,10,12) :
    print(year,'年',month,'月31天')
elif (month == 4,6,9,11) :
    print(year,'年',month,'月30天')<jupyter_output>输入月，年:3,2005
2005 年 3 月31天<jupyter_text>- 7![](../Photo/48.png)<jupyter_code>import random
a = random.randint(0,1)
num = eval(input('输入0或1:'))
if num == a:
    print('正确')
else :
    print('错误')<jupyter_output>输入0或1:0
错误<jupyter_text>- 8![](../Photo/49.png)<jupyter_code>import random
x = random.randint(0,2)
a = eval(input('输入一个0-2之间的数：'))
if (a == x) and a =<jupyter_output><empty_output><jupyter_text>- 9![](../Photo/50.png)<jupyter_code>year = eval(input('输入年份：'))
months = eval(input('输入月份：'))
days = eval(input('输入当前月份的某天：'))
hdayDict = {0:'星期六',1:'星期日', 2: '星期一', 3: '星期二', 4: '星期三', 5: '星期四', 6: '星期五'}
monDict = {1: '13', 2: '14', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9', 10: '10', \
             11: '11', 12: '12'}
if months == 1 or months == 2:
    year = year - 1 
shijishu = year // 100
shijiyear = year % 100
h = (days + (26 * (eval(monDict[months]) + 1)) // 10 + shijiyear + shijiyear // 4 + shijishu // 4 + 5 * shijishu) % 7
print('Day of the week is ' + hdayDict[h])<jupyter_output>输入年份：2012
输入月份：5
输入当前月份的某天：12
Day of the week is 星期六<jupyter_text>- 10![](../Photo/51.png)<jupyter_code>import random
x = ['Ace','2','3','4','5','6','7','8','9','10','Jack','Queen','King']
y = random.sample(x,1)
z = ['梅花','红桃','方块','黑桃']
g = random.sample(z,1)
print(y,g)<jupyter_output>['King'] ['梅花']<jupyter_text>- 11![](../Photo/52.png)<jupyter_code>num = eval(input('输入一个三位数：'))
if (num //100) == (num %10):
    print(num,'is a palindrome')
elif (num //100) != (num %10):
     print(num,'is not a palindrome')<jupyter_output>输入一个三位数：121
121 is a palindrome<jupyter_text>- 12![](../Photo/53.png)<jupyter_code>a,b,c = eval(input('a,b,c:'))
if (a+b > c) and (a+c > b ) and (b+c > a ):
    print ('合法')
    print('The perimeter',a+b+c)
elif   (a+b< c) or (a+c <b) or (b+c < a)  :
    print('非法的')<jupyter_output>a,b,c:1,3,5
非法的
{'fraction_non_alphanumeric': 0.20948509485094852, 'fraction_numerical': 0.05907859078590786, 'mean_word_length': 4.383661560904449, 'pattern_counts': {'":': 0, '<': 74, '<?xml version=': 0, '>': 65, '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': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '189773', 'license': 'Apache-2.0', 'n_tokens_mistral': 4026, 'n_tokens_neox': 3956, 'n_words': 662, 'path': '7.18.ipynb', 'repo_name': 'wekjka/lyy', 'size': '7380'}