美文网首页
python练习一(基本语法)

python练习一(基本语法)

作者: 幺姨母 | 来源:发表于2020-03-28 00:35 被阅读0次

第一部分:基础练习

1.1 基本数据类型,表达式,简单输入输出

输入三角形的三边长,计算三角形的面积。如果输入不合法,请输出'Whoops!'
例如:输入:2 输出:1.7320508075688772
2
2

#海伦公式sqrt(p*(p-a)(p-b)(p-c))
#乘方运算**

a = float(input())
b = float(input())
c = float(input())
if a+b<=c or a+c<=b or b+c<=a:
    print("Whoops!")
ssx4 = (a+b+c)*(a+b-c)*(a+c-b)*(b+c-a)
s = ssx4**0.5/4
print(s)
2
2
2
1.7320508075688772

1.2 字符串基本操作

找出字符串中小写字母a第二次出现的位置
例如:输入:'An apple a day keeps the doctor away'
输出:9

'''
s = input()
flag = False
l = int(0)
for a in s:
    if a == 'a' and flag:
        print(l)
        break
    elif a == 'a' and (not flag):
        l = l + 1;
        flag = True;
    else:
        l = l + 1;
'''
s = 'An apple a day keeps the doctor away'
i = s.find('a')
j = s.find('a', i+1)
print(j)  
9

1.3 条件表达式、分支流程

输出和成绩对应的等级:85分以上为A,60-84为B,60以下为C
例如:输入:84 输出:B

score = int(input())
'''
if score > 84:
    print("A")
elif score <60:
    print("C")
else:
    print("B")
'''
print('A' if score>=85 else ('B' if score>=60 else 'C'))
85
A

1.4 List、Tuple、Dict类型基本操作

新学期的选课抽签已经结束,可怜的小明只选上了这些课:['编译', '毛概', '操统']
(1)请在小明的课表中增加一门课程 'Python',并删除课程 '毛概'
这是小明想补选的通选课清单:{'A': ['音数'], 'C': ['三宝'], 'E': ['西音', '西美']}
(2)请修改清单中不正确的通选课分类

course = set(['编译', '毛概', '操统'])
print(course)
course.add('Python')
print(course)
course.discard('毛概')
print(course)

optionalcourse = dict({'A': ['音数'], 'C': ['三宝'], 'E': ['西音', '西美']})
print(optionalcourse)
optionalcourse['A'].append('三宝')
optionalcourse['D'] = ['西美']
optionalcourse['E'].append('音数')
optionalcourse.pop('C')
print(optionalcourse)
{'毛概', '操统', '编译'}
{'毛概', '操统', 'Python', '编译'}
{'操统', 'Python', '编译'}
{'A': ['音数'], 'C': ['三宝'], 'E': ['西音', '西美']}
{'A': ['音数', '三宝'], 'E': ['西音', '西美', '音数'], 'D': ['西美']}

第二部分:进阶练习

2.1 循环语句、表达式

计算数列前10项和
a_n=a_{n-1}+\frac{1}{a_{n-1}}, a_1=1

#range的用法

sum = float(0)
temp = 1
for i in range(10):
    sum += temp
    temp = temp + 1/temp
print(sum)
32.03687831484261

2.2 循环表达式与列表

找出列表中只出现过一次的数字,并保持原次序输出
例如: 输入:2,6,1,3,7,2,7 输出:6,1,3

#不换行输出end=''

s = input()
li = list(s)
li.remove(',')
flag = False
for a in li:
    if s.count(a) == 1 and a != ',':
        if flag:
            print(","+a, end = '')
        else:
            flag = True
            print(a, end = '')
2,6,1,3,7,2,7
6,1,3

2. 将以 ':' 和 '|' 为分隔符的字符串处理成 python 字典

例如: 输入:'k:1|k1:2|k2:3|k3:4' 输出:{'k': '1', 'k1': '2', 'k2': '3', 'k3': '4'}

slist = input().split('|')
sdict = {}
for element in slist:
    sdict[element.split(':')[0]] = element.split(':')[1]
print(sdict)
k:1|k1:2|k2:3|k3:4
{'k': '1', 'k1': '2', 'k2': '3', 'k3': '4'}

3. 将一个正整数分解质因数

例如: 输入:210 输出:235*7

flag = False
temp = int(2)
num = int(input())
if num == 1:
    print(1)
else:
    while num >= temp:
        while num % temp == 0:
            num = num / temp
            if flag == False:
                flag = True
                print(temp, end='')
            else:
                print('*' + str(temp), end='')
        temp = temp + 1
18
2*3*3

4. 利用filter函数,找出1~100之间的所有素数

#filter留下对的

def isprime(num):
    temp = int(2)
    if num == 1:
        return False
    while temp < num:
        if num%temp == 0:
            return False
        temp = temp + 1
    return True

print(list(filter(isprime, range(1, 101))))
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

5. 英文词频统计

输入为以下一段英文,不区分大小写,不计标点符号。
'There is no doubt that happiness is the most precious thing in the world. Without it, life will be empty and meaningless. If you wish to know how to get happiness, you must pay attention to the following two points.'

#多个分隔符
#大小写转换
#remove只删一个
#sorted用法

import re

s = 'There is no doubt that happiness is the most precious thing in the world. Without it, life will be empty and meaningless. If you wish to know how to get happiness, you must pay attention to the following two points.'
s = s.lower()
slist = re.split('[ .,]', s)
sset = sorted(set(slist))
sdict = {}
for element in sset:
    if element != '':
        sdict[element] = slist.count(element)
print(sdict)
{'and': 1, 'attention': 1, 'be': 1, 'doubt': 1, 'empty': 1, 'following': 1, 'get': 1, 'happiness': 2, 'how': 1, 'if': 1, 'in': 1, 'is': 2, 'it': 1, 'know': 1, 'life': 1, 'meaningless': 1, 'most': 1, 'must': 1, 'no': 1, 'pay': 1, 'points': 1, 'precious': 1, 'that': 1, 'the': 3, 'there': 1, 'thing': 1, 'to': 3, 'two': 1, 'will': 1, 'wish': 1, 'without': 1, 'world': 1, 'you': 2}

相关文章

网友评论

      本文标题:python练习一(基本语法)

      本文链接:https://www.haomeiwen.com/subject/gsmsuhtx.html