Python 小目标3

Python Day 9

Today we will try the basic Arithmetic Operators including , + , - ,* ,/ and squares in Python.

Arithmetic Operators:

  1. The first line contains the sum of the two numbers.
  2. The second line contains the difference of the two numbers (first - second).
  3. The third line contains the product of the two numbers.
  4. The fourth line contains the division of the two numbers.

Solution:

def arith_op(a,b):
    n1=a+b
    n2=a-b
    n3=a*b
    n4=a/b
    print (n1,n2,n3,n4, sep="\n")

arith_op(2,3)

#Alternative Solution
print("%d\n%d\n%d"%(a+b,a-b,a*b))

Output

arith_op(2,3)
5
-1
6
0.6666666666666666

[caption id="attachment_1787" align="alignnone" width="600"]
image

BrooklynJohn / Pixabay[/caption]

Division:

Read two integers and print two lines. The first line should contain integer division, a//b. The second line should contain float division, a /b.

a = int(input())
b = int(input())
print(a//b, a/b,  sep="\n")

Output:

1
1.33333333333

Square List:

Read an integer N. For all non-negative integers i< N , print i^2.

Solution:

[print(i*i) for i in range(n)]

Output:

0
1
4
9
16

Happy Practicing! 💤

Reference:
https://www.hackerrank.com

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,168评论 0 10
  • 不少人应该都看过日剧《被嫌弃的松子的一生》,它讲述了一个平凡普通的女主角松子丧、糟糕且潦草的人生。 父亲因患有先天...
    城迹荒野阅读 1,436评论 0 0
  • 秋天的一场雨,让天气骤然降了下来。雨不大,却细细碎碎地一直下着,天空阴着个脸,沉甸甸地罩着大地,这分量给人厚实的安...
    木梓玲阅读 1,738评论 0 3
  • 很多人都说美丽人生不像个悲剧,是的,确实不像。但这部电影所用的是喜剧的手法传递悲剧的意义。 影片前半部分,讲的都是...
    瑞纳尼阅读 1,697评论 0 3

友情链接更多精彩内容