加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

day 2

发布时间:2020-12-20 12:51:24 所属栏目:Python 来源:网络整理
导读:Python 3? 不可信 File Edit View Insert Cell Kernel Widgets Help ? ? ? 运行 ? ? ? 选择 布尔类型、数值和表达式 注意:比较运算符的相等是两个等号,一个等到代表赋值 在Python中可以用整型0来代表False,其他数字来代表True 后面还会讲到 is 在判断语句

Python 3?
不可信
File
Edit
View
Insert
Cell
Kernel
Widgets
Help
?
?
?
运行
?
?
?
选择
布尔类型、数值和表达式
注意:比较运算符的相等是两个等号,一个等到代表赋值
在Python中可以用整型0来代表False,其他数字来代表True
后面还会讲到 is 在判断语句中的用发
In [32]:

a = id(1)
b = id(1)
?
print(a,b)
# 因为a和b并不是同一个对象
a is b
?
4370970016 4370970016
Out[32]:
False
In [33]:

a = id(1)
b = a
a is b

Out[33]:
True
In [22]:

a = True
b = False
?
In [31]:

id(True)

Out[31]:
4370566624
In [ ]:

?
?
In [23]:

a == b

Out[23]:
False
In [24]:

a is b

Out[24]:
False
字符串的比较使用ASCII值
In [37]:

a = "jokar"
b = "jokar"
a > b

Out[37]:
False
Markdown
https://github.com/younghz/Markdown
肯定会给发数据划分啊

??
??=1
??
??
∑j=1Nxj
In [ ]:

?
?

EP:
输入一个数字,判断其实奇数还是偶数
产生随机数字
函数random.randint(a,b) 可以用来产生一个a和b之间且包括a和b的随机整数
In [38]:

import random
?
In [40]:

random.randint(0,1)

Out[40]:
1
In [ ]:

if condition:
??? do someething
else:
??? other
?
In [ ]:

for iter_ in xxx:
??? do something
?
?
In [46]:

age = 10
Joker = eval(input(‘Name‘))
print(Joker)
?
Nameage
10
产生一个随机数,你去输入,如果你输入的数大于随机数,那么就告诉你太大了,反之,太小了, 然后你一直输入,知道它满意为止
In [55]:

number =random.randint(0,5)
?
In [58]:

for i in range(5):
??? input_? = eval(input(‘>>‘))
??? if input_ > number:
??????? print(‘太大啦‘)
??? if input_ < number:
??????? print(‘太小啦‘)
??? if number == input_:
??????? print(‘正好‘)
??????? break
?
>>0
太小啦
>>0
太小啦
>>0
太小啦
>>0
太小啦
>>0
太小啦
In [59]:

for i in range(5):
??? print(i)
?
0
1
2
3
4
其他random方法
random.random 返回0.0到1.0之间前闭后开区间的随机浮点
random.randrange(a,b) 前闭后开
In [60]:

random.random()

Out[60]:
0.5903384303938066
In [61]:

import matplotlib.pyplot as plt
?
In [64]:

image=plt.imread(‘/Users/huwang/Downloads/cat.jpeg‘)
print(image*random.random())
plt.imshow(image)
. . .
EP:
产生两个随机整数number1和number2,然后显示给用户,使用户输入数字的和,并判定其是否正确
进阶:写一个随机序号点名程序
In [69]:

number_1 = random.randrange(0,10)
number_2 = random.randrange(0,10)
?
while 1:
??? sum_ = eval(input(‘>>‘))
??? if sum_ == (number_1 + number_2):
??????? print(‘Congratulations! Correct~‘)
??? else:
??????? print(‘Sorry~SB.‘)
. . .
In [ ]:

?
?

if语句
如果条件正确就执行一个单向if语句,亦即当条件为真的时候才执行if内部的语句
Python有很多选择语句:
单向if
双向if-else
嵌套if
多向if-elif-else
注意:当语句含有子语句的时候,那么一定至少要有一个缩进,也就是说如果有儿子存在,那么一定要缩进
切记不可tab键和space混用,单用tab 或者 space
当你输出的结果是无论if是否为真时都需要显示时,语句应该与if对齐
In [72]:

input_? = eval(input(‘>>‘))
if input_ > number:
??? print(‘太大啦‘)
if input_ < number:
??? print(‘太小啦‘)
if number == input_:
??? print(‘正好‘)
print(‘不要灰心‘)
?
>>1
正好
不要灰心
李文浩相亲测试树
? 年龄
老 年轻
拜拜
帅 否 是 考虑一下 老婆 没有 有 马上结婚 回家的诱惑
代码写不出来的立马分手,从此社会上有多出一个渣男/渣女.
In [4]:

age = input(‘年轻嘛[y/n]‘)
if age == ‘y‘:
??? handsome = input(‘帅否[y/n]‘)
??? if handsome == ‘y‘:
??????? wife = input(‘有没有老婆[y/n]‘)
??????? if wife == ‘y‘:
??????????? print(‘回家的诱惑‘)
??????? else:
??????????? print(‘立马结婚‘)
??? else:
??????? print(‘考虑一下‘)
else:
??? print(‘拜拜~‘)
?
年轻嘛[y/n]y
帅否[y/n]y
有没有老婆[y/n]y
回家的诱惑
EP:
用户输入一个数字,判断其实奇数还是偶数
进阶:可以查看下4.5实例研究猜生日
双向if-else 语句
如果条件为真,那么走if内部语句,否则走else内部语句
EP:
产生两个随机整数number1和number2,然后显示给用户,使用户输入数字,并判定其是否正确,如果正确打印“you‘re correct”,否则打印正确错误
嵌套if 和多向if-elif-else
In [ ]:

if score >= 80:
??? gread = ‘B‘
elif score>=90:
??? gread = ‘A‘
?

EP:
提示用户输入一个年份,然后显示表示这一年的动物
计算身体质量指数的程序
BMI = 以千克为单位的体重除以以米为单位的身高的平方
In [9]:

tizhong? = eval(input(‘体重‘))
shengao = eval(input(‘身高‘))
BMI = tizhong / shengao ** 2
if BMI<18.5 :
??? print(‘超清‘)
elif 18.5<=BMI<25 :
??? print(‘标准‘)
elif 25<=BMI<30 :
??? print(‘超重‘)
else:
??? print(‘超级无敌胖‘)
?
体重500
身高10000
超清
逻辑运算符

?
EP:
判定闰年:一个年份如果能被4整除但不能被100整除,或者能被400整除,那么这个年份就是闰年
提示用户输入一个年份,并返回是否是闰年
提示用户输入一个数字,判断其是否为水仙花数
实例研究:彩票
In [16]:

import random
?
In [25]:

number = random.randint(10,99)
print(number)
N = input(‘>>‘)
number_shi = number // 10
number_ge = number % 10
?
if N[0] == ‘0‘:
??? N_shi = 0
else:
??? N_shi = int(N) // 10
??? N_ge = int(N) % 10
?
if number == int(N):
??? print(‘10000‘)
# elif (number_shi == N_shi or number_shi==N_ge) and (number_ge == N_shi or number_ge==N_ge):
elif number_shi + number_ge == N_shi + N_ge:
??? print(‘3000‘)
elif (number_ge ==N_ge or number_ge == N_shi) or (number_shi == N_ge or number_shi == N_shi):
??? print(‘1000‘)
?
24
>>40
1000
In [3]:

a = "05"
a[0]

Out[3]:
‘o‘
In [22]:

05 // 10

Out[22]:
‘J‘
In [14]:

Number = eval(input(‘>>‘))
?
bai = Number // 100
shi = Number // 10 % 10
ge = Number % 10
?
if bai**3 + shi **3 + ge **3 == Number:
??? print(‘水仙花‘)
else:
??? print(‘不是水仙花‘)
?
>>123
不是水仙花
In [15]:

223 // 10

Out[15]:
2
Homework
1
In [3]:

import math
for i in range(3):
??? a,b,c=map(float,input(‘Enter a,c:‘).split(‘,‘))
??? final1=(-b+(b**2-4.0*a*c)**0.5)/2*a
??? final2=(-b-(b**2-4.0*a*c)**0.5)/2*a
??? final=b**2-4.0*a*c
??? if final > 0:
??????? print(‘The roots are %.6f and %.5f‘%(final1,final2))
??? if final == 0:
??????? print(‘The root is%d‘%(final1))
??? if final < 0:
??????? print(‘The equation has no real roots‘)
?
Enter a,c:1.0,3,1
The roots are -0.381966 and -2.61803
Enter a,c:1,2.0,1
The root is-1
Enter a,2,3
The equation has no real roots
2
In [10]:

import random
A = int(input(‘请输入这两个整数的和:‘))
a = random.randint(0,100)
b = random.randint(0,100)
B = a+b
if A==a+b:
??? print(‘真‘)
else:
??? print(‘假,且结果为:{}‘.format(B))
?
请输入这两个整数的和:19
假,且结果为:139
3
In [4]:

today = int(input("Enter today‘s day:"))
future = int(input("Enter the number of days elapsed since today:"))
week = [‘Sunday‘,‘Monday‘,‘Tuesday‘,‘Wednesday‘,‘Thursday‘,‘Friday‘,‘Saturday‘]
futureweek = today + future
ok=0
ok = futureweek % 7
print("Today is %s and the future day is %s"%(week[today],week[ok]))
???
?
Enter today‘s day:0
Enter the number of days elapsed since today:31
Today is Sunday and the future day is Wednesday
4
In [19]:

a,input(‘请输入3个整数‘).split(‘,‘))
if a<b<c:
??? print(‘从小到大的顺序为%d %d %d‘%(a,c))
elif b<a<c:
??? print(‘从小到大的顺序为%d %d %d‘%(b,a,c))
elif b<c<a:
??? print(‘从小到大的顺序为%d %d %d‘%(b,c,a))
elif a<c<b:
??? print(‘从小到大的顺序为%d %d %d‘%(a,b))?
elif c<a<b:
??? print(‘从小到大的顺序为%d %d %d‘%(c,b))
elif c<b<a:
??? print(‘从小到大的顺序为%d %d %d‘%(c,a))???
???
?
请输入3个整数3,1
从小到大的顺序为1 2 3
5
In [22]:

a,b=map(float,input(‘Enter weight and price for package 1:‘).split(‘,‘))
c,d=map(float,input(‘Enter weight and price for package 2:‘).split(‘,‘))
f=b/a
e=d/c
if e<f:
??? print(‘package 2 has the better price‘)
if e==f:
??? print(‘package 2 and package are equle‘)
if f<e:
??? print(‘package 1 has the better price‘)
?
Enter weight and price for package 1:50,24.59
Enter weight and price for package 2:25,11.99
package 2 has the better price
6
In [25]:

y = int(input(‘输入月份:‘))
n = int(input(‘输入年份:‘))
if n % 4 == 0 and n % 100 != 0 or n % 400 == 0:
??? if y == 2:
??????? print(‘%d年%s月份有29天‘%(n,y))
??? elif y in (1,5,7,8,10,12):
??????? print(‘%d年%s月份有31天‘%(n,y))
??? else:
??????? print(‘%d年%s月份有30天‘%(n,y))
else:
??? if y == 2:
??????? print(‘%d年%s月份有28天‘%(n,y))in
??? elif y in (1,y))
?
?
输入月份:3
输入年份:2005
2005年3月份有31天
7
In [26]:

import random
A=str(input(‘请猜测硬币是正面还是反面:‘))
B=random.choice([‘正,反‘])
if A==B:
??? print(‘恭喜你猜对了‘)
else:
??? print(‘很可惜猜错了‘)
?
?
请猜测硬币是正面还是反面:正
很可惜猜错了
8
In [30]:

import numpy as np
x = np.random.choice([‘0‘,‘1‘,‘2‘])
n = str(input(‘Scissor (0),rock (1),paper (2): ‘))
def jg(x):
??? if x == 0:
??????? str1 == ‘scissor‘
??? elif x == 1:
??????? str1 == ‘rock‘
??? elif x == 2:
??????? str1 == ‘paper‘
str2 = jg(n)
if x == ‘0‘:
??? if n == ‘0‘:
??????? print(‘The computer is %s. You are %s too.It is draw.‘%(x,n))
??? elif n? == ‘1‘:
??????? print(‘The computer is %s. You are %s.You won.‘%(x,n))
??? elif n == ‘2‘:
??????? print(‘The computer is %s. You are %s.It is lose‘%(x,n))
if x == ‘1‘:
??? if n == ‘0‘:
??????? print(‘The computer is %s. You are %s.It is lose‘%(x,n))
??? elif n? == ‘1‘:
??????? print(‘The computer is %s. You are %s too.It is draw.‘%(x,n))
??? elif n == ‘2‘:
??????? print(‘The computer is %s. You are %s.,You won.‘%(x,n))
if x == ‘2‘:
??? if n == ‘0‘:
??????? print(‘The computer is %s. You are %s.,n))
??? elif n? == ‘1‘:
??????? print(‘The computer is %s. You are %s.It is lose‘%(x,n))
??? elif n == ‘2‘:
??????? print(‘The computer is %s. You are %s too.It is draw.‘%(x,n))
?
Scissor (0),paper (2): 1
The computer is 1. You are 1 too.It is draw.
9
In [ ]:

def calculateDayofWeek(q,m,k,j):
??? h = (q + (26*(m + 1) // 10) + k + (k // 4) + (j // 4) + 5*j) % 7#泽勒一致性
??? return h
?
def info_enter():
??? year = eval(input("请输入年份:"))
??? j = year // 100#世纪数
??? month = eval(input("请输入月份:"))
??? if month == 1:
??????? m = 13
??????? k = year % 100 - 1#年份
??? elif month == 2:
??????? m = 14
??????? k = year % 100 - 1#年份
??? else:
??????? m = month
??????? k = year % 100#年份
??? day = eval(input("请输入月份中哪一天:"))
??? q = day
??? return q,j,day,month,year
if __name__ == "__main__":
??? q,year = info_enter()
??? #求出日期对应着一个星期的哪一天
??? h = calculateDayofWeek(q,j)
??? if h == 0:
??????? week = ‘Saturday‘
??? elif h == 1:
??????? week = ‘Sunday‘
??? elif h == 2:
??????? week = ‘Monday‘
??? elif h == 3:
??????? week = ‘Tuesday‘
??? elif h == 4:
??????? week = ‘Wednesday‘
??? elif h == 5:
??????? week = ‘Thursday‘
??? elif h == 6:
??????? week = ‘Friday‘
??? print(str(year)+‘-‘+str(month)+‘-‘+str(day)+" is "+week+‘ of the week.‘)
?

10
In [34]:

import random
A=random.choice([‘Ace‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘10‘,‘Jack‘,‘Queen,King‘])
B=random.choice([‘梅花‘,‘红桃‘,‘方块‘,‘黑桃‘])
print(‘The card you picked is the {} of {}‘.format(A,B))
?
The card you picked is the 10 of 红桃
11
In [1]:

A=float(input(‘Enter a three-digit integer:‘))
def is_palindrome(num):
??? temp = num
??? total = 0
??? while temp > 0:
??????? total = total * 10 + temp % 10
??????? temp //= 10
??? return total == num
num = int(input(‘Enter a three-digit interger : ‘))
a = is_palindrome(num)
if a == True:
??? print(‘%d is a palindrome‘ %num)
elif a == False:
??? print(‘%d is not a palindrome‘ %num)
?
Enter a three-digit integer:123
Enter a three-digit interger : 121
121 is a palindrome
12
In [2]:
?

import math
a,c = map(int,input(‘Enter three edges: ‘).split(‘,‘))
if a + b > c and a + c > b and b + c > a:
??? l = a + b + c
??? print(‘The perimeter is %d‘ % (a + b + c))
else:
??? print(‘Error‘)
?
Enter three edges: 1,1,1
The perimeter is 3
In [ ]:
?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读