python例题(基本数据类型常见的例题):
2018-11-06 16:51
232 查看
一、判断数字的位数和取出数字各个权位的数字:
1、利用str将数字转化为字符串利用字符串索引、切片的方式:
[code]num1 = input('输入数字:') # str类型 print(len(num1)) # 获取数字的位数 print(num1[index]) # 知道索引后利用num1的索引进行获取,计算式转化为int类型。
2、利用int数字取商(//)和取模的方式(%):
[code] a = 1234 print(a//1000) # 获取千位数字 print(a%10) #获取个位数字 print(a//10%10) # 获取十位数字 print(a//100%10) # 获取百位数字
二、计算从2到100奇数的和:
[code]sum = 0 for i in range(3,101,2) # 遍历奇数 sum += i print(sum)
相关文章推荐
- python基本数据类型(三)-字符串拼接-格式化输出-深浅复制-python3笔记
- Python基础(自学)基本数据类型
- python的基本数据类型
- Python基本数据类型
- python 基本数据类型占用内存空间大小
- Python学习之基本数据类型(一)
- 002---Python基本数据类型--字符串
- Python3 基本数据类型
- python3的六种基本数据类型
- Python基本数据类型
- python3手册-基本数据类型-字符串
- python基本数据类型
- python基本数据类型
- Python3基本数据类型
- Python的基本数据类型
- python基本数据类型——tuple
- 从零学Python之入门(二)基本数据类型
- python基本数据类型——int
- 跟我一起学python(二),python基本数据类型
- python基本数据类型——str