任务:1
#Print Numbers #Write a program to print numbers from 1 to 10 using a while loop. def print_numbers(num): no=1 while no <p>输出:<br> </p> <pre class="brush:php;toolbar:false">1 2 3 4 5 6 7 8 9 10
任务:2
#Sum of N Numbers #Write a program to calculate the sum of the first NN natural numbers using a while loop. def nat_num(N): sum = 0 natural_no = 1 while natural_no <p>输出:<br> </p> <pre class="brush:php;toolbar:false">Enter the no: 10 Sum of 10 natural numbers are 55
任务:3
#Find even numbers between 1 to 50 def even_number(num): no=2 while no <p>输出:<br> </p> <pre class="brush:php;toolbar:false">2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
任务:4
#Find odd numbers between 1 and NN def odd_number(num): no=1 while no <p>输出:<br> </p> <pre class="brush:php;toolbar:false">Enter the number:15 1 3 5 7 9 11 13 15
任务:5
#Multiplication table of number NN #NN-2 no=1 while no <p>输出:<br> </p> <pre class="brush:php;toolbar:false">1*2=2 2*2=4 3*2=6 4*2=8 5*2=10 6*2=12 7*2=14 8*2=16 9*2=18 10*2=20
阶乘:
编写一个程序,使用 while 循环计算给定数字的阶乘。
案例1
no = 1 factorial = 1 factorial = factorial * no #1 no = no+1 #2 factorial = factorial * no #2 no = no+1 #3 factorial = factorial * no #6 no = no+1 #4 factorial = factorial * no #24 no = no+1 #5 factorial = factorial * no #120 print(factorial)
案例2
def find_factorial(num): no=1 factorial=1 while no <p>对于场景输出将是相同的<br> 输出:<br> </p> <pre class="brush:php;toolbar:false">Enter the number:5 120
数字总和:
求给定数字的数字之和(例如,123 → 1 2 3=61 2 3=6)。
def sum_of_digits(num): sum=0 while num>0: sum=sum+num%10 num=num//10 return sum num=int(input("Enter the number:")) print(sum_of_digits(num))
输出:
Enter the number:123 6
数数字
计算给定数字的位数(例如,12345 → 5 位数字)。
def count_of_digits(no): count=0 while no>0: no=no//10 count+=1 return count no=int(input("Enter the number:")) print(count_of_digits(no))
输出:
Enter the number:12345 5
反转数字
反转数字
def reverse_no(num): reverse = 0 while num>0: reverse = (reverse*10) + num%10 num = num//10 return reverse print(reverse_no(1234))
输出:
Enter the number:12345 54321
检查回文
检查给定的数字是否是回文(例如,121→回文,123→不是回文)。
def palindrome(num): count=0 while num>0: count=count*10+num%10 num=num//10 return count num=int(input("Enter the number:")) result=palindrome(num) if result==num: print("Palindrome") else: print("Not palindrome")
输出:
Enter the number:121 Palindrome Enter the number:123 Not palindrome
寻找力量
def find_power(base,power): result=1 while power>=1: result=result*base power-=1 return result base=int(input("Enter the base number:")) power=int(input("Enter the power number:")) result=find_power(base,power) print(result)
输出:
Enter the base number:5 Enter the power number:2 25
阿姆斯特朗数
示例:
def count_of_digits(num): count=0 while num>0: num=num//10 count+=1 return count def find_power(base,power): result=1 while power>=1: result=result*base power-=1 return result def find_armstrong(num): armstrong=0 while num>0: rem=num%10 result= find_power(rem,count) armstrong=armstrong+result num=num//10 return armstrong num=int(input("Enter the number:")) count=count_of_digits(num) armstrong_result=find_armstrong(num) if armstrong_result==num: print("Armstrong") else: print("Not armstrong")
输出:
Enter the number:135 Not armstrong Enter the number:153 Armstrong
以上是Python Day - 循环示例和任务的详细内容。更多信息请关注PHP中文网其他相关文章!

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

本文解释了如何使用美丽的汤库来解析html。 它详细介绍了常见方法,例如find(),find_all(),select()和get_text(),以用于数据提取,处理不同的HTML结构和错误以及替代方案(SEL)

Python 对象的序列化和反序列化是任何非平凡程序的关键方面。如果您将某些内容保存到 Python 文件中,如果您读取配置文件,或者如果您响应 HTTP 请求,您都会进行对象序列化和反序列化。 从某种意义上说,序列化和反序列化是世界上最无聊的事情。谁会在乎所有这些格式和协议?您想持久化或流式传输一些 Python 对象,并在以后完整地取回它们。 这是一种在概念层面上看待世界的好方法。但是,在实际层面上,您选择的序列化方案、格式或协议可能会决定程序运行的速度、安全性、维护状态的自由度以及与其他系

本文比较了Tensorflow和Pytorch的深度学习。 它详细介绍了所涉及的步骤:数据准备,模型构建,培训,评估和部署。 框架之间的关键差异,特别是关于计算刻度的

Python的statistics模块提供强大的数据统计分析功能,帮助我们快速理解数据整体特征,例如生物统计学和商业分析等领域。无需逐个查看数据点,只需查看均值或方差等统计量,即可发现原始数据中可能被忽略的趋势和特征,并更轻松、有效地比较大型数据集。 本教程将介绍如何计算平均值和衡量数据集的离散程度。除非另有说明,本模块中的所有函数都支持使用mean()函数计算平均值,而非简单的求和平均。 也可使用浮点数。 import random import statistics from fracti

该教程建立在先前对美丽汤的介绍基础上,重点是简单的树导航之外的DOM操纵。 我们将探索有效的搜索方法和技术,以修改HTML结构。 一种常见的DOM搜索方法是EX

本文讨论了诸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和请求等流行的Python库,并详细介绍了它们在科学计算,数据分析,可视化,机器学习,网络开发和H中的用途

本文指导Python开发人员构建命令行界面(CLIS)。 它使用Typer,Click和ArgParse等库详细介绍,强调输入/输出处理,并促进用户友好的设计模式,以提高CLI可用性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),