簡単なエクササイズ()初心者のpython

  1. 。任意のキーボードから英語の文字列を取得します
    削除(1)繰り返し文字:実装
    大文字を小文字に、小文字を大文字に変換するには(2)。以下のような:ABC治療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="")

結果:

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]とのList1 LIST2を合わせ、昇順

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

結果:

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

サポート技術情報の概要
升序函数:sort()或sorted()用法看例题 降序函数: [::-1] 或reverse()(反转),,用之前先升序

3. 100に連結されて正の整数未満100は、完全な方形であり、プラス168は完全な方形です。数はどのくらいあるのでしょうか?

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)

結果:

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

Process finished with exit code 0

4.彼らは合計を散乱可能にし、結果を出力し、キーボードからの数字の任意の文字列を取得します。例えば:12345は、1 + 3 + 4 + 5 2 96最終結果を加算散乱1 + 3 + 52 + 4 + 6 912最終結果を追加123456散乱

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))

結果:

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

Process finished with exit code 0

5.一覧= [ '、「B」、0,1「C」]最初の3つの要素、第二及び第三の要素の出力、最初以外のすべての要素の出力の出力を定義します。

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

結果:

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

おすすめ

転載: www.cnblogs.com/-cheng-/p/12388774.html