python beginner simple exercises (a)

  1. Obtain any string of English from the keyboard.
    Implementation: (1) repeated characters removed
    (2) to convert uppercase to lowercase and lowercase to uppercase. Such as: abC treatment ABc
print("从键盘上输入的英文:")
st = input()
s = set(st)
print("去掉重复字符后:", s)
print("大小写字母转换后:")
for num in s:
    if 97<=ord(num)<=122:  #小写字母
        print(num.upper(),end="") #end=""表示换行
    if 65<=ord(num)<=90:
        print(num.lower(),end="")

result:

F:\pythonTest\venv\Scripts\python.exe F:/pythonTest/Test/test.py
从键盘上输入的英文:
AAAdddWWEWEWEsfsfsdf
去掉重复字符后: {'s', 'E', 'A', 'd', 'W', 'f'}
大小写字母转换后:
SeaDwF
Process finished with exit code 0

2.List1 = [2,4,6] list2 = [1,3,5] and the List1 List2 combined and ascending

list1 = [2, 4, 6]
list2 = [1, 3, 5]
list3 = list1 + list2
print(sorted(list3))
list3.sort()
print(list3)

result:

F:\pythonTest\venv\Scripts\python.exe F:/pythonTest/Test/test.py
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]

Process finished with exit code 0

Knowledge summary
升序函数:sort()或sorted()用法看例题 降序函数: [::-1] 或reverse()(反转),,用之前先升序

3. a positive integer less than 100, which is coupled with the 100 is a perfect square, plus 168 is a perfect square. Will the number is how much?

from math import sqrt

for i in range(0,100):
    a = sqrt(i+100)
    b = sqrt(i+268)
    if (a == int(a)) and (b == int(b)):
        print(i)

result:

F:\pythonTest\venv\Scripts\python.exe F:/pythonTest/Test/test.py
21

Process finished with exit code 0

4. Get any string of numbers from the keyboard, which allows them scattered sum and outputs the result. Such as: 12345 scattered adding 1 + 3 + 4 + 5 2 96 final result, scattered 123456 adding 1 + 3 + 52 + 4 + 6 912 final result

print("输入一串数字")
st = input()
sum1 = sum2 = 0
for i in range(0,len(st),2):
    sum1 += int(st[i])
for i in range(1,len(st),2):
    sum2 += int(st[i])
print(str(sum1)+str(sum2))

result:

F:\pythonTest\venv\Scripts\python.exe F:/pythonTest/Test/test.py
输入一串数字
12345
96

Process finished with exit code 0

5. Define List = [ 'a', 'b', 0,1 'c',] the output of the first three elements, the output of the second and third elements, the outputs of all the elements except the first.

List = ['a', 'b', 0, 1, 'c']
print("输出前三个元素", List[0:3])
print("输出第二个和第三个元素", List[1:3])
print("输出除第一个外的所有元素", List[1:])
print(List[1:5:3]) #输出第2个和第5个元素

result:

F:\pythonTest\venv\Scripts\python.exe F:/pythonTest/Test/test.py
输出前三个元素 ['a', 'b', 0]
输出第二个和第三个元素 ['b', 0]
输出除第一个外的所有元素 ['b', 0, 1, 'c']
['b', 'c']

Process finished with exit code 0

Guess you like

Origin www.cnblogs.com/-cheng-/p/12388774.html