1.长整型表示数的范围比整型更大。在python中,整型、长整型趋于统一,普通用户不用特别关注两者区别,仅当需引用C语言时需要特别注意。
2.操作符
(a)
def product(x,y):
return x * y
(b)
>>> print product(23,24)
552
3.
def result(x):
if 90 <= x <= 100:
return "A"
elif 80 <= x <= 89:
return "B"
elif 70 <= x <= 79:
return "C"
elif 60 <= x <= 69:
return "D"
else:
return "F"
4.
# -*- coding:utf-8 -*- def year_judge(year): if (int(year) % 4 == 0 and int(year) % 100 != 0) or int(year) % 400 == 0:
return u"该年是闰年!"
else:
return u"该年是平年!"
5.
# -*- coding:utf-8 -*-
def exchange(x):
divide_25,mod_25 = divmod(x,25)[0],divmod(x,25)[1]
if mod_25 == 0:
return u"%d 美分换算结果是:%d枚25美分。" \
% (x,divide_25)
else:
divide_10,mod_10 = divmod(mod_25,10)[0],divmod(mod_25,10)[1]
if mod_10 == 0:
return u"%d美分换算结果是:%d枚25美分,%d枚10美分。" \
% (x,divide_25,divide_10)
else:
divide_5,mod_5 = divmod(mod_10,5)[0],divmod(mod_10,5)[1]
if mod_5 == 0:
return u"%d美分换算结果是:%d枚25美分,%d枚10美分,%d枚5美分。" \
% (x,divide_25,divide_10,divide_5)
else:
return u"%d美分换算结果是:%d枚25美分,%d枚10美分,%d枚5美分,%d枚1美分。"\
% (x,divide_25,divide_10,divide_5,mod_5) def cents_exchange(x):
if x < 1:
x = x * 100
else:
x = int(x)
print exchange(x)
6.
# -*- coding:utf-8 -*- def calculator():
global expression
expression = raw_input("请输入一个表达式,可以计算加减乘除取余和幂运算,按Q退出。".decode("utf-8").encode("gbk"))
if expression == "Q":
return u"程序退出,再见"else:
for i in ["+","-","**","/","%","*"]:
if i in expression:
numbers = expression.split(i)
if "." in expression:
N1 = float(numbers[0])
N2 = float(numbers[1])
else:
N1 = int(numbers[0])
N2 = int(numbers[1])
if i == "+":
return N1 + N2
elif i == "-":
return N1 - N2
elif i == "*":
return N1 * N2
elif i == "**":
return N1 ** N2
elif i == "%":
return N1 % N2
elif i == "/":
return N1 / N2 while True:
print calculator()
if expression == "Q":
break
7.
# -*- coding:utf-8 -*-
sales = float(raw_input("请输入营业额".decode("utf-8").encode("gbk")))
rate = float(raw_input("请输入税率,以两位小数表示".decode("utf-8").encode("gbk")))
print u"应缴税额为:%f 元人民币。" % (sales * rate)
8.
# -*- coding:utf-8 -*-
def geometry():
prompt = raw_input("计算正方形面积请输入1,立方体体积请输入2,圆面积请输入3,球体积请输入4".\
decode("utf-8").encode("gbk"))
if prompt == "":
side = raw_input("请输入边长".decode("utf-8").encode("gbk"))
print u"该正方形的面积是:%d" % (int(side) ** 2)
elif prompt == "":
side = raw_input("请输入边长".decode("utf-8").encode("gbk"))
print u"该正方体的体积是:%d" % (int(side) ** 3)
elif prompt == "":
radius = raw_input("请输入半径".decode("utf-8").encode("gbk"))
print u"该圆的面积是:%s" % (float(radius) ** 2 * 3.14)
elif prompt == "":
radius = raw_input("请输入半径".decode("utf-8").encode("gbk"))
print u"该球的体积是:%s" % (float(radius) ** 3 * 4 * 3.14 / 3) geometry()
9.
a.进制问题 python中0开头的数字表示八进制的数字
b.56l + 78l 代表的是长整数的和
10.
# -*- coding:utf-8 -*-
def F_to_C(F):
print u"%s℉等于%f℃" % (F,(float(F) - 32) * 5 / 9)
11.(1)
#取偶数
for i in range(0,21):
if i % 2 == 0:
print i, print #取奇数
for i in range(0,21):
if i % 2 == 1:
print i,
(2)对2进行取余运算,余数0是偶数,否则是奇数
(3)
# -*- coding:utf-8 -*- def judge_divide(x,y):
if x % y == 0:
print u"%d可以被%d整除!" % (x,y)
else:
print u"%d不能被%d整除!" % (x,y) def judge():
number1 = int(raw_input("请输入第一个整数".decode("utf-8").encode("gbk")))
number2 = int(raw_input("请输入第二个整数".decode("utf-8").encode("gbk")))
if number1 < number2:
judge_divide(number2,number1)
else:
judge_divide(number1,number2)
12.
# -*- coding:utf-8 -*-
import sys
print sys.maxint
print -sys.maxint - 1
print sys.float_info.max
print sys.float_info.min
print sys.long_info
13.
# -*- coding:utf-8 -*-
time = raw_input("请输入时间,英文冒号:前为小时数,英文冒号:后为分钟数".decode("utf-8").encode("gbk"))
hour = int(time.split(":")[0])
minute = int(time.split(":")[1])
print u"%d:%d等于%d分钟。" % (hour,minute,hour * 60 + minute)
14.
# -*- coding:utf-8 -*-
def Annual_return_rate(rate):
per_day_rate = rate / 365.0
investment = 1
interest = 1 * (1 + per_day_rate) ** 365 - 1
print u"年回报率为:",interest * 100,"%" Annual_return_rate(0.5)
15.
# -*- coding:utf-8 -*-
def greatest_common_divisor(x,y):
divisor = max(x,y)
while divisor >= 1:
if x % divisor != 0 or y % divisor != 0:
divisor -= 1
else:
print u"%d和%d的最大公约数是%d。" % (x,y,divisor)
break def least_common_multiple(x,y):
multiple = max(x,y)
while multiple <= x * y:
if multiple % x == 0 and multiple % y == 0:
print u"%d和%d的最小公倍数是%d。" % (x,y,multiple)
break
else:
multiple += 1
16.略
17.
# -*- coding:utf-8 -*-
import random N = random.randint(2,100)
i = 1
random_list1 = []
while i <= N:
random_list1.append(random.randint(0,2 ** 31 - 1))
i += 1 h = 1
N2 = random.randint(1,100)
random_list2 = []
while h <= N2:
random_list2.append(random.choice(random_list1))
h += 1 print sorted(random_list2)