python基础教学习题_Python基础每天习题练习-第一天

Question 1

Question:Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5, between 2000 and 3200 (both included).The numbers obtained should be printed in a comma-separated sequence on a single line.

Hints:Consider use range(#begin, #end) method.

Main author's Solution: Python 2

l=[]

for i in range(2000, 3201):

if (i%7==0) and (i%5!=0):

l.append(str(i))

print ','.join(l)

My Solution: Python 3 - Using for loops

for i in range(2000,3201):

if i%7 == 0 and i%5!=0:

print(i,end=',')

print("\b")Using generators and list comprehension

print(*(i for i in range(2000, 3201) if i%7 == 0 and i%5 != 0), sep=",")

Question 2

Question:Write a program which can compute the factorial of a given numbers.The results should be printed in a comma-separated sequence on a single line.Suppose the following input is supplied to the program: 8 Then, the output should be:40320

Hints:In case of input data being supplied to the question, it should be assumed to be a console input.

Main author's Solution: Python 2

def fact(x):

if x == 0:

return 1

return x * fact(x - 1)

x = int(raw_input())

print fact(x)

My Solution: Python 3Using While Loop python n = int(input()) #input() function takes input as string type #int() converts it to integer type fact = 1 i = 1 while i <= n: fact = fact * i; i = i + 1 print(fact)

Using For Loop python n = int(input()) #input() function takes input as string type #int() converts it to integer type fact = 1 for i in range(1,n+1): fact = fact * i print(fact)

Using Lambda Function

python # Solution by: harshraj22

n = int(input()) def shortFact(x): return 1 if x <= 1 else x*shortFact(x-1) print(shortFact(n))

'''Solution by: minnielahoti'''

while True:

try:

num = int(input("Enter a number: "))

break

except ValueError as err:

print(err)

org = num

fact = 1

while num:

fact = num * fact

num = num - 1

print(f'the factorial of {org} is {fact}')

'''Soltuion by: KruthikaSR'''

from functools import reduce

def fun(acc, item):

return acc*item

num = int(input())

print(reduce(fun,range(1, num+1), 1))

Question 3

Question:With a given integral number n, write a program to generate a dictionary that contains (i, i x i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary.Suppose the following input is supplied to the program: 8

Then, the output should be:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}

Hints:In case of input data being supplied to the question, it should be assumed to be a console input.Consider use dict()

Main author's Solution: Python 2

n = int(raw_input())

d = dict()

for i in range(1,n+1):

d[i] = i * i

print d

My Solution: Python 3:Using for loop

n = int(input())

ans = {}

for i in range (1,n+1):

ans[i] = i * i

print(ans)Using dictionary comprehension

n = int(input())

ans={i : i*i for i in range(1,n+1)}

print(ans)

'''Solution by: minnielahotiCorrected by: TheNobleKnight'''

try:

num = int(input("Enter a number: "))

except ValueError as err:

print(err)

dictio = dict()

for item in range(num+1):

if item == 0:

continue

else:

dictio[item] = item * item

print(dictio)

'''Solution by: yurbika'''

num = int(input("Number: "))

print(dict(list(enumerate((i * i for i in range(num+1))))))

Conclusion

These was the solved problems of day 1. The above problems are very easy for the basic syntex learners.I have shown some easy ways of coding in my solutions. Lets see how to face and attack new problems in the next day.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值