20180710学习日报

【程序14】
题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。

分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下面去做判断:
1.如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
2.如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,
 重复执行第一步。
3.如果n不能被k整除,则用k+1作为k的值,重复执行第一步

from sys import stdout
n = int(input(“input number:\n”))
print(“n = %d” % n)

for i in range(2,n + 1):
while n != i:
if n % i == 0:
stdout.write(str(i))
stdout.write(“*”)
n = n / i
else:
break
print(“%d” % n)

执行结果:
input number:
15
n = 15
3*5

在这里print中用到了 python中的格式化字符 %s %d %f,下面来说一下这个,下面是常用的一些:
格式 描述
%% 百分号标记 #就是输出一个%
%c 字符及其ASCII码
%s 字符串
%d 有符号整数(十进制)
%u 无符号整数(十进制)
%o 无符号整数(八进制)
%x 无符号整数(十六进制)
%X 无符号整数(十六进制大写字符)
%e 浮点数字(科学计数法)
%E 浮点数字(科学计数法,用E代替e)
%f 浮点数字(用小数点符号)
%g 浮点数字(根据值的大小采用%e或%f)
%G 浮点数字(类似于%g)
%p 指针(用十六进制打印值的内存地址)
%n 存储输出字符的数量放进参数列表的下一个变量中

1.输出%

print (“The percent is %%%d.”%(10))
The percent is %10.

2.打印字符及其ASCII码

print(“The first letter is %c.”%(97))
The first letter is a.

3.打印字符串

print(“His name is %s”%(“Tenni”))
His name is Tenni

4.打印整数
十进制

print (“He is %d years old”%(25))
He is 25 years old

八进制

print (“He is %o years old”%(21))
He is 25 years old

十六进制

print (“He is %X years old”%(37))
He is 25 years old

5.打印浮点数

print (“His height is %f m”%(1.83))
His height is 1.830000 m

6.打印浮点数(指定保留小数点位数)

print (“His height is %.2f m”%(1.83))
His height is 1.83 m

7.指定占位符宽度

print (“Name:%10s Age:%8d Height:%8.2f”%(“Aviad”,25,1.83))
Name: Aviad Age: 25 Height: 1.83

8.指定占位符宽度(左对齐)

print (“Name:%-10s Age:%-8d Height:%-8.2f”%(“Aviad”,25,1.83))
Name:Aviad Age:25 Height:1.83

9.指定占位符(默认用0当占位符)

print (“Name:%-10s Age:%08d Height:%08.2f”%(“Aviad”,25,1.83))
Name:Aviad Age:00000025 Height:00001.83

【程序2】
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数

s = input(‘input a string:\n’)
letters = 0
space = 0
digit = 0
others = 0
for c in s:
if c.isalpha():
letters += 1
elif c.isspace():
space += 1
elif c.isdigit():
digit += 1
else:
others += 1
print(‘char = %d,space = %d,digit = %d,others = %d’ % (letters,space,digit,others))

执行结果:
input a string:
2800999%abckkki iii ii 999
char = 12,space = 6,digit = 10,others = 1

在这个程序中我用到了isalpha(),isspace(),isdigit(),下面说一下这三个的作用
isalpha() 方法检测字符串是否只由字母组成
isalpha()方法语法:str.isalpha()
参数:无
返回值:如果字符串至少有一个字符并且所有字符都是字母则返回 True,否则返回 False

isdigit() 方法检测字符串是否只由数字组成
isdigit()方法语法:str.isdigit()
参数:无
返回值:如果字符串只包含数字则返回 True 否则返回 False

isspace() 方法检测字符串是否只由空格组成
isspace()方法语法:str.isspace()
参数:无
返回值:如果字符串中只包含空格,则返回 True,否则返回 False

以下这些也是常用来做判断用的函数:

istitle() 方法检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写
istitle()方法语法:str.istitle()
参数:无
返回值:如果字符串中所有的单词拼写首字母是否为大写,且其他字母为小写则返回 True,否则返回 False

isupper() 方法检测字符串中所有的字母是否都为大写
isupper()方法语法:str.isupper()
参数:无
返回值:如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False

islower() 方法检测字符串是否由小写字母组成
islower()方法语法:str.islower()
参数:无
返回值:如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False

isnumeric() 方法检测字符串是否只由数字组成。这种方法是只针对unicode对象
isnumeric()方法语法:str.isnumeric()
参数:无
返回值:如果字符串中只包含数字字符,则返回 True,否则返回 False

isalnum() 方法检测字符串是否由字母和数字组成
isalnum()方法语法:str.isalnum()
参数:无
返回值:如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值