Python学习笔记-初级(一):基本语法及简单程序

一、问题背景

“Python语言导论“课程作业:

1. 从键盘输入一个整数,判断该数字能否被2和3同时整除,能否被2整除,能否被3整除,不能被2和3整除。输出相应信息。

2. 打印出所有的“水仙花数”,所谓“水仙花数”是指一个3位数,其各位数字立方和等于该数本身。例如,153是一个水仙花数,因为153=13+53+33。


二、实现环境

Window 8,32位系统,Python 3.5 IDLE。


三、代码

1. 整除

'''
Author: WJT
Date: 10/1/2016

Iuput: an integer
decide whether the integer can be diveded by 2 or 3

'''

a = input("Please input an integer\n");
a = int(a);

if(a % 2 == 0):
	if(a % 3 == 0):
		print(a, " can be diveded by 2 and 3");
	else:
		print(a, " only can be diveded by 2");
else:
	if(a % 3 == 0):
		print(a, " only can be diveded by 3");
	else:
		print(a, " can't be diveded by 2 or 3");
 

2.水仙花数

'''
Author: WJT
Date: 10/1/2016

find out all narcissistic numbers
'''

from math import *;
print("all the narcissistic numbers:");
for i in range(100, 1000):
	hundred = floor(i / 100);
	ten = floor(i % 100 / 10)
	one = floor(i % 10);
	if(i == pow(hundred, 3) + pow(ten, 3) + pow(one, 3)):
		print(i);






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值