Everyday Python Programming——2022.2.27

1. Some different ways to delete elements from a string:

First, we can use 'strip()':

'''Actually, we can use 'strip()' which means delete ' ' from two sides of the code 
to the middle'''

>>> s = '  bee'
>>> s.strip()
'bee'

>>> s = 'b e e'
>>> s.strip()
'b e e'

——————————————————————————————————————————————————————————————————————————

'''On the other side, not only can we delete ' ', but also other codes.
   BE CAREFUL : JUST LIKE strip(), from two sides to middle!! '''

s = 'bed'
>>> s.strip('e')
'bed'

>>> s.strip('b')
'ed'

>>> s = 'bee'
>>> s.strip('e')
'b'

2. 

Q : 要求统计给定整数M和N区间内素数的个数并对它们求和。

输入在一行中给出两个正整数M和N(1≤M≤N≤500)。

MAIN PART:

So the first time, I try to use these codes :

M,N = map(int,input().split())
count = 0
for i in range(M, N + 1):
    for j in range(2, i):
        if i % j == 0:
            break

''' However, after each loop, we actually want to execute a judge which is 
used to count the number of primer number.
So we can use:    '''

M,N = map(int,input().split())
count = 0
for i in range(M, N + 1):
    for j in range(2, i):
        if i % j == 0:
            break
        if j == i-1:
            count = count + 1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值