Python基礎練習100題(31~40)
4109 ワード
問題を続けていく
昨日みんなと21-30題を分かち合って、今日引き続き31~40題を塗ります
Question 31:
Define a function which can print a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys.
解法一
解法二
Question 32:
Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the keys only.
解法一
解法二
Question 33:
Define a function which can generate and print a list where the values are square of numbers between 1 and 20 (both included).
解法一
Question 34:
Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the first 5 elements in the list.
解法一
Question 35:
Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the last 5 elements in the list.
解法一
Question 36:
Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print all values except the first 5 elements in the list.
解法一
Question 37:
Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included).
解法一
Question 38:
With a given tuple (1,2,3,4,5,6,7,8,9,10), write a program to print the first half values in one line and the last half values in one line.
解法一
解法二
Question 39:
Write a program to generate and print another tuple whose values are even numbers in the given tuple (1,2,3,4,5,6,7,8,9,10).
解法一
解法二
Question 40:
Write a program which accepts a string as input to print "Yes"if the string is "yes"or "YES"or "Yes", otherwise print "No".
解法一
ソースのダウンロード
この10問のコードは私のgithubにあります.もし皆さんが各問の出力結果を見たいなら、以下のリンクをクリックしてダウンロードすることができます. Python 31-40題 私の運用環境Python 3.6+は、Python 2.7バージョンを使用している場合、ほとんどの違いは以下の3点に現れます. raw_Input()Python 3ではinput() printは括弧 を付ける必要がある fstringは交換可能である.format()、または%s,%d 皆さん、ありがとうございます.各位の友达がけちけちしないでくださいを望んで、すべての问题のもっと効率的な解法を评论の中で书いて、私达はいっしょに进歩します!!!
昨日みんなと21-30題を分かち合って、今日引き続き31~40題を塗ります
Question 31:
Define a function which can print a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys.
解法一
def printDict():
d=dict()
for i in range(1,21):
d[i]=i**2
print(d)
printDict()
解法二
def printDict():
dict={i:i**2 for i in range(1,21)}
print(dict)
printDict()
Question 32:
Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the keys only.
解法一
def printDict():
dict = {i: i**2 for i in range(1, 21)}
print(dict.keys())
printDict()
解法二
def printDict():
d=dict()
for i in range(1,21):
d[i]=i**2
for k in d.keys():
print(k)
printDict()
Question 33:
Define a function which can generate and print a list where the values are square of numbers between 1 and 20 (both included).
解法一
def printList():
lst = [i ** 2 for i in range(1, 21)]
print(lst)
printList()
Question 34:
Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the first 5 elements in the list.
解法一
def printList():
lst = [i ** 2 for i in range(1, 21)]
print(lst[:5])
printList()
Question 35:
Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the last 5 elements in the list.
解法一
def printList():
lst = [i ** 2 for i in range(1, 21)]
print(lst[-5:])
printList()
Question 36:
Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print all values except the first 5 elements in the list.
解法一
def printList():
lst = [i ** 2 for i in range(1, 21)]
print(lst[5:])
printList()
Question 37:
Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included).
解法一
def printTuple():
lst = [i ** 2 for i in range(1, 21)]
print(tuple(lst))
printTuple()
Question 38:
With a given tuple (1,2,3,4,5,6,7,8,9,10), write a program to print the first half values in one line and the last half values in one line.
解法一
tpl = (1,2,3,4,5,6,7,8,9,10)
for i in range(0,5):
print(tpl[i],end = ' ')
print()
for i in range(5,10):
print(tpl[i],end = ' ')
解法二
tp = tuple(i for i in range(1,11))
lst1,lst2 = list(tp[:5]),list(tp[5:])
print(lst1)
print(lst2)
Question 39:
Write a program to generate and print another tuple whose values are even numbers in the given tuple (1,2,3,4,5,6,7,8,9,10).
解法一
tpl = (1,2,3,4,5,6,7,8,9,10)
tpl_even = tuple(i for i in tpl if i%2 == 0)
print(tpl_even)
解法二
tpl = (1,2,3,4,5,6,7,8,9,10)
tpl_even= tuple(filter(lambda x : x%2==0,tpl))
print(tpl_even)
Question 40:
Write a program which accepts a string as input to print "Yes"if the string is "yes"or "YES"or "Yes", otherwise print "No".
解法一
s = input()
if s.lower() == 'yes':
print('Yes')
else:
print("No")
ソースのダウンロード
この10問のコードは私のgithubにあります.もし皆さんが各問の出力結果を見たいなら、以下のリンクをクリックしてダウンロードすることができます.