python 数字加法运算

Take input of two numbers from the user and print their sum.

从用户那里输入两个数字并打印它们的总和。

Example:

例:

    Input:
A = 2, B = 3
Output:
Sum = 5

Here, we are implementing the program to find the addition of two numbers using 4 different ways.

在这里,我们正在实施程序,以使用4种不同的方法来找到两个数字的加法

1) Simply take input from the user and typecast to an integer at the same time after that performing addition operation on the both number.

1)只需从用户输入并在对两个数字进行加法运算后同时将其类型转换为整数 。

if __name__ == "__main__" :
# take input from user
a = int(input())
b = int(input())
# addition operation perform
sum_num = a + b
print("sum of two number is: ",sum_num)

Output

输出量

10
20
sum of two number is:  30

2) Using a user-defined function for doing the sum of two numbers.

2)使用用户定义的函数进行两个数字的和。

# define a function for performing
# addition of number
def sum_num(a,b) :
return a + b
# Main code
if __name__ == "__main__" :
a = int(input())
b = int(input())
print("sum of two number:",sum_num(a,b))

Output

输出量

10
20
sum of two number: 30

3) We are taking the input from user in one line after that typecast into an integer and stored them in the list then use sum() inbuilt function which returns the sum of elements of the list.

3)在类型转换后,我们将用户输入的内容输入一行以整数形式存储,然后将其存储在列表中,然后使用sum()内置函数返回列表元素的总和。

if __name__ == "__main__" :
# take input from the user in list
a = list(map(int,input().split()))
# sum function return sum of elements
# present in the list
print("sum of two number is:",sum(a))

Output

输出量

10 20
sum of two number is: 30

4) We are taking the input from user in one line and store them in two different variables then typecast both into an integer at the time of addition operation.

4)我们将用户的输入放在一行中,并将它们存储在两个不同的变量中,然后在加法运算时将它们都转换为整数 。

if __name__ == "__main__" :
# take input from the user in a and b variables
a,b = input().split()
# perform addition operation
rslt = int(a) + int(b)
print("sum of two number is:",rslt)

Output

输出量

10 20
sum of two number is: 30

翻译自: https://www.includehelp.com/python/program-to-find-addition-of-two-numbers-4-different-ways.aspx

python 数字加法运算

python 数字加法运算_Python程序查找两个数字的加法(4种不同方式)相关推荐

  1. kotlin 或 运算_Kotlin程序对两个数字执行算术运算

    kotlin 或 运算 Here, we are implementing a Kotlin program to perform various arithmetic operations on t ...

  2. python中二进制整数_Python程序查找表示二进制整数的必要位数

    python中二进制整数 Given an integer number and we have to find necessary bits to represent it in binary in ...

  3. python用递归方式实现最大公约数_Python程序查找最大公因数(HCF)或最大公约数(GCD)...

    Python程序查找最大公因数(HCF)或最大公约数(GCD) 在此示例中,您将学习使用两种不同的方法查找两个数字的GCD:函数和循环以及欧几里得算法 要理解此示例,您应该了解以下Python编程主题 ...

  4. 100以内两个整数的(随机产生)的加法运算练习程序

    C语言实例一 问题描述 编制100以内两个整数的(随机产生)的加法运算练习程序. 算法分析 首先需要考虑定义哪些变量. 因为此题是用来计算100以内的两个整数的和,所以至少要定义三个短整型变量a.b. ...

  5. 【编程题目】输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。...

    第 14 题(数组): 题目:输入一个已经按升序排序过的数组和一个数字, 在数组中查找两个数,使得它们的和正好是输入的那个数字. 要求时间复杂度是 O(n).如果有多对数字的和等于输入的数字,输出任意 ...

  6. 输入一个已经按升序排序过的数组和一个数字, 在数组中查找两个数,使得它们的和正好是输入的那个数字。

    原文转自:http://blog.csdn.net/u013322907/article/details/38300711 题目:输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们 ...

  7. python timer 死掉_Python timer定时器两种常用方法解析 Python中如何在一段时间后停止程序...

    如何调用定时器 python 如何在python里面for循环中放了一个定时函数,当定# 我的构想程序效果 for Img in ImgArray: timer = threading.Timer(1 ...

  8. python的编程模式有哪两种_python程序的两种运行方式是什么

    python程序的两种运行方式是什么 第一种方式:REPL 所谓REPL即read.eva.print.loop(读取.计算.打印.循环),实现REPL运行方式有以下两种: 1.IDLE( 集成开发环 ...

  9. python的运行方式有哪两种 有何区别_python程序的两种运行方式是什么

    python程序的两种运行方式是什么 第一种方式:REPL 所谓REPL即read.eva.print.loop(读取.计算.打印.循环),实现REPL运行方式有以下两种: 1.IDLE( 集成开发环 ...

  10. python数组内运算_Python数组介绍和操作运算详解

    本文概述 数组定义为存储在连续内存位置的项目的集合.这是一个可以容纳固定数量项目的容器, 这些项目应为同一类型.数组在大多数编程语言(例如C / C ++, JavaScript等)中都很流行. 数组 ...

最新文章

  1. Java / Android String.format 的使用
  2. 4.4MSSQLServer常用版本介绍
  3. 你可能不知道的小知识-bug为什么叫bug
  4. 太原市中考计算机考试系统,太原中考报名系统
  5. 招博士生 | 澳门科技大学人工智能课题组
  6. python大纲_python学习大纲
  7. 查询手机号段对应地区编码_Elasticsearch实战 | 如何从数千万手机号中识别出情侣号?...
  8. stylus 迭代+插值实现css同类型不同值样式序列
  9. unity下载和安装
  10. 电脑文件一键实时备份同步至云端(百度云盘)
  11. Object-C 与C/C++的区别
  12. 3.7V锂电池升压到5V1A,FS2114升压转换芯片设计布局
  13. 未将引用设置到对象的实例
  14. RFID 工作频率的分类
  15. oracle清除temp表空间,Temp表空间占用长时间不释放,是谁惹的祸
  16. 坚果云同步linux,备份Linux系统数据到坚果云
  17. oracle 用户名密码找回
  18. 力扣(145.102)补9.5
  19. 如何在Windows 7中将管理工具添加到开始菜单
  20. Axure 高保真 日期选择器实现 可选任意年/月/日

热门文章

  1. CryEngine 渲染流程
  2. easyexcel一个单元格导出多张图片等
  3. mysql ix锁_mysql锁详解
  4. 数值优化学习十八——SQP
  5. 计算机显示器画画的清晰度,如何设置显示器分辨率让画面更清晰
  6. js正则表达式 验证非负数
  7. 哈曼收购混合现实领域领先公司Apostera
  8. 转-Tensorflow之GPU和CPU
  9. 第四章 Sysrepo连接与会话
  10. keystone创建服务实体HTTP500,An unexpected error prevented the server from fulfilling your request. HTTP500