Take input number from the user and print its reverse.

从用户处获取输入号码并打印其反面。

Example:

例:

    Input:
12345
Output:
54321

Here, we are implementing program to reversing a given number using 2 different ways.

在这里,我们正在实施程序,以使用2种不同的方式来反转给定的数字

1) Famous approach for reversing the number: Take input from the user and typecast into an integer, then iterate in the loop till num is not become zero, inside the loop:

1) 逆转数字的著名方法 :从用户输入并将其类型转换为整数,然后在循环中循环直到num在循环内不为零:

  • Find out the remainder.

    找出其余的。

  • Using this: rev_num = rev_num * 10 + remainder.

    使用这个:rev_num = rev_num * 10 +余数。

  • Update that number by diving by 10.

    通过跳水10来更新该数字。

  • After coming out of the loop printing the reverse number.

    退出循环后,打印反向编号。

if __name__ == "__main__" :
# take string input from user
num = int(input('Enter a number: '))
rev_num = 0
# iterate the loop till num is not equal to zero
while(num) :
rem = num % 10
rev_num = rev_num* 10 + rem
num //= 10
print('Reverse number is: ', rev_num)

Output

输出量

Enter a number: 12345
Reverse number is:  54321

2) Make a user-defined function for reversing the Number: Take input from the user and typecast into integer, thenreverseNum() function call.

2) 制作一个用于反转Number的用户定义函数 :从用户那里输入输入并将其类型转换为整数,然后调用verseNum()函数。

Inside the function:

函数内部:

  • Iterate in the loop till num does not become zero:

    在循环中迭代,直到num不为零:

  • Find out the remainder.

    找出其余的。

  • Using this: rev_num = rev_num * 10 + remainder.

    使用这个:rev_num = rev_num * 10 +余数。

  • Update that number by diving by 10.

    通过跳水10来更新该数字。

  • After coming out of the loop returning the reverse number to the main.

    退出循环后,将反向编号返回到主编号。

# define a function for finding
# reverse of the number
def reverseNum(num) :
rev_num = 0
# iterate the loop till num is not equal to zero
while(num) :
rem = num % 10
rev_num = rev_num* 10 + rem
num //= 10
return rev_num
# Main() method
if __name__ == "__main__" :
# take string input from user
num = int(input('Enter a number: '))
print('Reverse number is: ', reverseNum(num))

Output

输出量

Enter a number: 12345
Reverse number is:  54321

翻译自: https://www.includehelp.com/python/program-to-reverse-a-given-number-2-different-ways.aspx

Python程序反转给定数字(2种不同方式)相关推荐

  1. python定义字符串变量有两种常用方式_Python 1基础语法二(标识符、关键字、变量和字符串)...

    一.标识符 标识符就是程序员自己命名的变量名.名字需要有见名知义的效果,不要随意起名 :比如 a=1 a是个变量,a这个变量名属于标识符 1 company = '小米 2 employeeNum = ...

  2. (201)数字6种表示方式

    (201)数字6种表示方式 1 文章目录 1)文章目录 2)FPGA入门与提升课程介绍 3)FPGA简介 4)数字6种表示方式 5)技术交流 6)参考资料 2 FPGA入门与提升课程介绍 1)FPGA ...

  3. python 读写数据文件的6种常用方式

    本文主要介绍python读写数据文件的6种常用方式. 1. python内置方法 with open(r'test.xlsx') as f:a = f.read() 一般,在应用上述上下文管理器后,可 ...

  4. python程序结构有哪几种_Python数据结构与算法(几种排序)小结

    Python数据结构与算法(几种排序) 数据结构与算法(Python) 冒泡排序 冒泡排序(英语:Bubble Sort)是一种简单的排序算法.它重复地遍历要排序的数列,一次比较两个元素,如果他们的顺 ...

  5. python语言只采用解释一种翻译方式对吗_python-guide翻译

    python看了也有一段时间了,但是由于不常用到,所以经常看了忘忘了又看,这次决定做个记录顺便梳理一下学过的内容. 查资料时恰好在github上看到这个项目https://github.com/jia ...

  6. 吐血整理!程序员常见的几种变现方式!

    今天聊一个特俗但是大家都想的事情, 那就是「赚钱」这件事. 先说为什么这个事情「特俗」,因为其实我发现我身边大部分程序员不爱谈钱,或者羞于谈钱. 加上程序员工资普遍比较高,所以早期都没啥压力,但是随着 ...

  7. python实现p图软件_这才是Python程序员P图的正确打开方式!

    [IT168 评论]现代社会,不P图的人简直就像是恐龙一样稀奇,大到瘦脸瘦腿瘦全身,小到滤镜大眼高鼻梁,五花八门的手段令人应接不暇.那么程序员作为这个星球的特殊物种,P图才不会用毁图秀秀这种软件,下面 ...

  8. python中字符串输出的三种简单方式

    python字符串输出的三种简单方式 第一种: 用'+'号进行字符串的拼接,但只限于字符串,不能进行其他类型的拼接.但是可以进行强制类型转换(字符串的强制转换为str(名字)),再用加号进行拼接. 比 ...

  9. Python程序从给定的N个数字中找到最大倍数

    Here, we will be framing code for finding the maximum multiple of a number x from a given set of a n ...

最新文章

  1. 【数据库优化专题】MySQL视图优化(一)
  2. 【整理】Smartforms的导出和导入
  3. Date、DateFormat、Calendar日期类
  4. wxWidgets:编写应用程序的快速指南
  5. thinkphp日志泄漏漏洞_【Windows高危漏洞预警】CVE20200601,影响关键加密功能
  6. 无刷电调_新款好盈XR10 PRO G2电调搭载XRAY T4车架简评
  7. Redis缓存安装Version5.0.7
  8. 网络-IP/子网掩码/端口
  9. 超大ISO文件的分卷刻录方法
  10. 图像匹配得到精确的旋转角度
  11. 简单人物画像_简易人物画像作品
  12. vim菜鸟学习-杂篇(windows篇)
  13. 中国石油大学(北京)-《安全检测与监测》第一阶段在线作业
  14. 《C Primer Plus》—第九章:函数(指针间接,函数及其定义方式,ANSI C原型,递归,函数调用的底层原理)
  15. hihocoder01串
  16. Nginx学习(1)—— 下载和安装
  17. 紫外线传感器 韩国GENICOM进口 GUVA-T11GD-L 全波段检测,原厂渠道
  18. TU-ctf-2016 pwn woO 分析记录
  19. sqrt函数模拟实现的两种方法
  20. 攻防演练-组织沙盘推演的4个阶段.

热门文章

  1. html 英文文字纵向排列,CSS几种简单方法实现文字竖向排版
  2. php怎么把字符转成大写,php怎么把字符串转换为大写
  3. JQuery Ajax 使用FormData上传文件对象
  4. 如何用纯 CSS 创作一个文本淡入淡出的 loader 动画
  5. 移动优先的响应式布局
  6. python之pyqt5-第一个pyqt5程序-图像压缩工具(2.0版本)-小记
  7. 面试中关于多线程同步,你必须要思考的问题
  8. 埃森哲杯第十六届上海大学程序设计联赛春季赛暨上海高校金马五校赛 C序列变换...
  9. 在物欲横流,心浮气躁的今天,毕业生靠什么来维持自己的梦想?
  10. 零基础自学编程前需要知道的知识