########
########
print("#" * 8)
print("#" + " " * 6 + "#")
print("#" * 8)
width = 6
hight = 4
perimeter = (width + hight) * 2
area = width * hight
print("矩形的周长是%s, 面积是%s" % (perimeter, area))
方法一:
a = 1000
b = 2000
c = a
a = b
b = c
print(a, b)
方法二:
a = 1000
b = 2000
a = a + b
b = a - b
a = a - b
print(a, b)
方法三:
a = 1000
b = 2000
a, b = b, a # 序列赋值(解构/解包),python独有的方法
print(a, b)
'''规律: 1 3 5 7 每一行小星星个数=x*2-1
*
***
*****
*******
'''
方法一:
x = 1
while x <= 4:
res = "*" * (x * 2 - 1)
print(res.center(7)) # center字符居中,两边自动填充字符
x += 1
方法二:
for x in range(1, 5):
res = "*" * (x * 2 - 1)
print(res.center(7))
i = 216 // 16
j = 216 % 16
print("古代的216两是%d斤%d两" % (i, j))
t = 63320 // 3600
m = 63320 % 3600 // 60
s = 63320 % 3600 % 60
print("%d:%d:%d" % (t, m, s))
pi = 3.14
i = int(pi)
f = pi - i
print(f == 0.14) # False
del pi, i, f # 删除变量
help("__main__") # 查看当前作用域内变量
year = input("请输入年份:")
print("%s有%s周" % (year, 365 // 7))
time_t = int(input("时"))
time_m = int(input("分"))
time_s = int(input("秒"))
print("距离0:0:0过了%s秒" % (time_t * 3600 + time_m * 60 + time_s))
i = int(input("请输入一个正整数:"))
if i%2 == 0:
print("%s是偶数" % i)
else:
print("%s是奇数" % i)
quarter = int(input("季度(1~4): ") or "")
if quarter == 1:
print("1月, 2月, 3月")
elif quarter == 2:
print("4月, 5月, 6月")
elif quarter == 3:
print("7月, 8月, 9月")
elif quarter == 4:
print("10月, 11月, 12月")
else:
print("输入有误!")
mouth = int(input("请输入月份(1~12): ") or "")
if mouth in [1, 2, 3]:
print("%s月是春季" % mouth)
elif mouth in [4, 5, 6]:
print("%s月是夏季" % mouth)
elif mouth in [7, 8, 9]:
print("%s月是秋季" % mouth)
elif mouth in [10, 11, 12]:
print("%s月是冬季" % mouth)
else:
print("输入有误!")
i = int(input("输入一个数字: "))
if i >= 0:
print(i)
else:
print(-i)
print(i if i >= 0 else -i)
kilometer = int(input("请输入公里数: "))
if kilometer <= 3:
money = 13
elif 3 < kilometer <= 15:
money += (kilometer - 3) * 2.3
elif kilometer > 15:
money += (kilometer - 15) * 1.15
else:
print("输入有误!")
print("费用为%.2f" % money)
a = int(input("请输入成绩a: "))
b = int(input("请输入成绩b: "))
c = int(input("请输入成绩c: "))
if a > b:
a, b = b, a
if b > c:
b, c = c, b
if a > b:
a, b = b, a
print("最高分为%s, 最低分为%s" % (c, a))
方法一:
year = int(input("请输入年份: "))
if year % 4 == 0 and year % 100 != 0:
print("%s年是闰年" % year)
elif year % 400 == 0:
print("%s年是闰年" % year)
else:
print("%s年不是闰年" % year)
方法二:
year = int(input("请输入年份: "))
# if条件表达式实现: 成立执行 if 真值表达式 else 不成立执行
print("闰年" if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0) else "不是闰年")
'''
BIM = 体重(公斤)/身高的平方(米)
体重过轻: BIM < 18.5
正常范围: 18.5 <= BIM <= 24
体重过重: BIM > 24
'''
height = int(input("身高: ")) / 100
weight = int(input("体重: "))
bim = weight / (height ** 2)
print("体重过轻" if bim < 18.5 else "体重过重" if bim > 24 else "正常范围")
'''
输入10时打印如下,输入5时打印如下
########## #####
# # # #
# # # #
########## #####
'''
方法一:
width = int(input("请输入矩形宽度: "))
for i in range(1, 5):
display = '#' * width if i == 1 or i == 4 else '#' + ' ' * (width - 2) + '#'
print(display)
方法二:
width = int(input("请输入矩形宽度: "))
t = '%' + str(width - 1) + 's'
for i in range(1, 5):
# 替换原理: print("#%4s" % "*")
display = '#' * width if i == 1 or i == 4 else '#' + t % '#'
print(display)
s1 = input("请输入一个字符串: ")
print("第一个字符是%s,最后一个字符是%s" % (s1[0], s1[-1]))
print("|" if len(s1) % 2 == 0 else s1[len(s1) // 2])
s1 = input("请输入一个字符串: ")
print("是回文" if s1 == s1[::-1] else "不是回文")
s1 = input("请输入字符串或正整数: ")
print(ord(s1[0]) if s1.isalpha() is True else chr(int(s1)))
'''
请输入第一行: hello world!
请输入第二行: my name is Coco.
请输入第三行: today is Sunday!
显示如下:
+----------------------+
| hello world! |
| my name is Coco. |
| today is Sunday! |
+----------------------+
'''
line = 3
temp_list = list(input("请输入字符串%d: " % (i + 1)) for i in range(line))
for i in range(temp_list.count('')):
# 循环查找列表中的空字符串后删除空字符串
temp_list.remove('')
length = max(len(i) for i in temp_list)
for i in range(len(temp_list) + 2):
print('+' + '-' * length + '+') if i == 0 or i == len(temp_list) + 1 else \
print('|' + temp_list[i - 1].center(length) + '|')
s1 = input("请输入字符串: ")
print("%s个字符e" % s1.count('e'))
print("%s个空格" % s1.count(' '))
print("字符串以?结尾: %s" % s1.endswith('?'))
'''
请输入第1行: hello world!
请输入第2行: my name is Coco.
请输入第3行: today is Sunday!
打印结果如下
hello world!
my name is Coco.
today is Sunday!
'''
line = 3
temp_list = list(input("请输入第%d行: " % (i + 1)) for i in range(line))
for i in range(temp_list.count('')):
temp_list.remove('')
length = max(len(i) for i in temp_list)
temp_str = "%" + str(length) + "s"
for i in range(len(temp_list)):
print(temp_str % temp_list[i])
方法一:
i = 1
j = int(input("请输入一个整数: "))
while i < j:
if i % 2 == 0:
print(i)
i += 1
方法二:
for i in range(2, int(input("请输入一个整数: "))):
if i % 2 == 0:
print(i)
for i in range(1, 21):
print(i, end=' ')
for i in range(1, 21):
if i in [6, 11, 16]:
print()
print("%d\t" % i, end=" ")
i = 0.5
while i < 10:
print(i, end=' ')
i += 0.5
i = 10
while i > 0:
print(i, end=' ')
i -= 1
'''请输入三角形宽度: 4
*
**
***
****
'''
width = int(input("请输入三角形宽度: "))
i = 1
while i <= width:
j = 1
while j <= i:
print("*", end='')
j += 1
print()
i += 1
'''请输入正方形宽度: 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
'''
width = int(input("请输入正方形宽度: "))
for i in range(width):
for j in range(1, width + 1):
print(j, end=' ')
print()
'''
* * **** ****
** ** *** ***
*** *** ** **
**** **** * *
'''
width = int(input("请输入三角形的宽度: "))
i = 1
while i <= width:
print("*" * i)
i += 1
width = int(input("请输入三角形的宽度: "))
i = 1
while i <= width:
print(" " * (width - i) + "*" * i)
i += 1
width = int(input("请输入三角形的宽度: "))
i = width
while i >= 0:
print("*" * i)
i -= 1
width = int(input("请输入三角形的宽度: "))
i = width
while i >= 0:
print(" " * (width - i) + "*" * i)
i -= 1
begin = int(input("请输入开始值: "))
end = int(input("请输入结束值: "))
count = 0
while begin < end:
print("%s\t" % begin, end=' ')
count += 1
if count % 5 == 0:
print()
begin += 1
i = ord('A')
while i <= ord('Z'):
print(chr(i) + chr(i + 32), end= '')
i += 1
s = input("请输入字符串: ")
count = 0
i = 0
while i < len(s):
if s[i] == ' ':
count += 1
i += 1
else:
print(count)
s = input("请输入字符串: ")
count = 0
for i in s:
if i == ' ':
count += 1
print(count)
'''请输入一个10以内的整数: 5
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
'''
num = int(input('请输入一个10以内的整数: '))
for i in range(1, num + 1):
for j in range(i, num + i):
print(j, end=' ')
print()
count = 0
for i in range(1, 101):
for j in [5, 7, 11]:
if i % j == 0:
break
else:
count += i
print(count)
begin = int(input("begin: "))
end = int(input("end: "))
temp_list = list(chr(i) for i in range(begin, end + 1))
for i in temp_list:
print(i, end=" ")
'''
*
***
*****
*
*
*
'''
height = int(input("请输入树干的高度: "))
for i in range(1, height + 1):
print("%s" % ("*" * (i * 2 - 1)).center(2 * height - 1))
for i in range(height):
print("*".center(2 * height - 1))
num = int(input("正整数: ") or "")
if num >= 2:
print(2, end= ' ')
for i in range(2, num + 1):
for j in range(2, i):
if i % j == 0:
break
else:
if i - j == 1:
print(i, end= ' ')
'''
1)输出这些数的和
2)输出这些数的最大的数和第二大的数
3)删除最小的一个数
4)按原来输入的顺序打印出乘余的这些数
'''
temp_list = list()
while True:
num = int(input("小于0退出: ") or "-1")
if num < 0:
break
else:
temp_list.append(num)
print("求和: %d最大数: %d" % (sum(temp_list), max(temp_list)))
temp_list.remove(max(temp_list))
print("第二大数: %d" % max(temp_list))
temp_list.remove(min(temp_list))
for i in temp_list:
print(i)
temp_list = [x for x in range(100) if (x + 1) * x % 11 == 8]
print(temp_list)
temp_list = [1, 1]
a = 1
b = 1
for i in range(18):
a, b = b, a + b
temp_list.append(b)
print(temp_list, sum(temp_list))
nums = ([x for x in range(100, 1000)
if int(str(x)[0])**3 + int(str(x)[1])**3 + int(str(x)[2])**3 == x])
print(nums)
'''
L = [1, 3, 5, 3, 7, 9, 3, 7, 6, 5]
1)打印出这些数
2)打印这些数的和
3)去掉列表L中重复第二次或之后出现的数再次存到另一个列表L2中,L2 = [1, 3, 5, 7, 9, 6]
4)打印L2列表中的数据的和
5)将L列表中出现两次的数存到另一个列表L3中L3 = [5, 7]
'''
L = list()
while True:
num = int(input("请输入大于0的整数: ") or "")
if num == 0:
continue
if num == -1:
break
L.append(num)
for i in L:
print(i, end= ' ')
count = 0
for i in L:
count += i
print("求和: ", count, sep= ':')
L2 = list()
for i in L:
if i not in L2:
L2.append(i)
for i in L2:
print(i, end= ' ')
print("L2求和: %s" % sum(L2))
L3 = list()
for i in L:
if L.count(i) == 2 and i not in L3:
L3.append(i)
print(L3)
data = {
'': '星期天',
'': '星期一',
'': '星期二',
'': '星期三',
'': '星期四',
'': '星期五',
'': '星期六'
}
while True:
num = input("请输入0-6查询: ") or "-1"
if num in data:
print(data[num])
else:
print("字典内没有相应的数据")
L = ['coco', 'angels', 'cat', 'mouse']
dic = {x:len(x) for x in L}
print(dic)
L1 = [1001, 1002, 1003, 1004]
L2 = ['Coco', 'Angels', 'Cat', 'Mouse']
dic = {L1[x]:L2[x] for x in range(4)}
print(dic)
se = {x for x in input("请输入字符串: ")}
print(se)
s = input("请输入字符串: ") or "-1"
dic = dict()
for i in s:
dic[i] = s.count(i)
print(dic)
# 规律1 4 10 (n+1)*2
num = 1
for i in range(9):
num = (num + 1) * 2
print(num)
num = 0
for i in range(1, 10000):
for j in range(1, i):
if i % j == 0:
num += j
if num == i:
print(i, end=' ')
num = 0
'''
1
121
12321
1234321
12321
121
1
'''
height = int(input("请输入高度: ") or '输入有误')
temp_str = str()
for i in range(1, height + 1):
for j in range(1, i):
temp_str += str(j)
for x in range(i, 0, -1):
temp_str += str(x)
print(temp_str.center(height * 2 - 1))
temp_str = ''
for i in range(height - 1, 0, -1):
for j in range(1, i):
temp_str += str(j)
for x in range(i, 0, -1):
temp_str += str(x)
print(temp_str.center(height * 2 -1))
temp_str = ''
for i in range(1, 10):
for j in range(1, i + 1):
print("%d*%d=%d" % (i, j, i*j), end= '\t')
print()
i = 1
while i <= 9:
j = 1
while j <= i:
print("%d*%d=%d" % (i, j, i*j), end= '\t')
j += 1
print()
i += 1
'''t1.txt
1,coco,22,13550505050,IT
2,Rubicon,23,13304320533,Tearcher
3,master,18,1333235322,IT
利用文件操作,将其构造成如下数据类型
[{'id': '1','name': 'coco','age': '22','phone': '13550505050', 'job': 'IT'},……]
'''
result = []
with open("t1.txt", encoding="UTF-8") as f:
# 读取每一行数据
for line in f:
dic = {}
lst = line.split(",")
dic["id"] = lst[0].strip()
dic['name'] = lst[1].strip()
dic['age'] = lst[2].strip()
dic['phone'] = lst[3].strip()
dic['job'] = lst[4].strip()
result.append(dic)
print(result)
手机扫一扫
移动阅读更方便
你可能感兴趣的文章