Python 编写确定个位、十位以上方法及各数位的和程序

Python 编写确定数字位方法

  • [Python 编写确定个位、十位](#Python 编写确定个位、十位)
  • [Python 编写确定个位、十位、百位](#Python 编写确定个位、十位、百位)
  • 方法解析:
  • [Python 各数位的和程序](#Python 各数位的和程序)

利用%(取余符号)、//(整除)符号。

Python 编写确定个位、十位

python 复制代码
num = 17
a = num % 10 
b = num // 10 
print(a)
print(b)

输出:

7

1

Python 编写确定个位、十位、百位

python 复制代码
num = 754
a = num % 10 # 个位
b = (num % 100) // 10 # 十位
c = num // 100 # 百位
print(a)
print(b)
print(c)

输出:

4

5

7

方法解析:

由此我们可得求数字各位数之和程序:

Python 各数位的和程序

python 复制代码
num = int(input())
last_digit = num % 10
first_digit = num // 10
print("十位数:", first_digit)
print("个位数:", last_digit)
print("和:", last_digit+first_digit)
相关推荐
踏着七彩祥云的小丑6 小时前
pytest——Mark标记
开发语言·python·pytest
Dream of maid6 小时前
Python12(网络编程)
开发语言·网络·php
W23035765737 小时前
经典算法:最长上升子序列(LIS)深度解析 C++ 实现
开发语言·c++·算法
Y4090017 小时前
【多线程】线程安全(1)
java·开发语言·jvm
不爱吃炸鸡柳7 小时前
Python入门第一课:零基础认识Python + 环境搭建 + 基础语法精讲
开发语言·python
minji...8 小时前
Linux 线程同步与互斥(三) 生产者消费者模型,基于阻塞队列的生产者消费者模型的代码实现
linux·运维·服务器·开发语言·网络·c++·算法
Dxy12393102168 小时前
Python基于BERT的上下文纠错详解
开发语言·python·bert
语戚8 小时前
力扣 968. 监控二叉树 —— 贪心 & 树形 DP 双解法递归 + 非递归全解(Java 实现)
java·算法·leetcode·贪心算法·动态规划·力扣·