python死循环例子_Python | 一些循环的例子

python死循环例子

1. Print all the no. between 1 to n

1.打印所有编号。 在1到n之间

n=int(input("Enter N: "))

for i in range(1,n+1):
    print(i)

Output

输出量

Enter N: 5 
1
2
3
4
5 

2. Print table of number

2.打印号码表

n=int(input("Enter N: "))

for i in range(1,11):
    print(n,"x",i,"=",i*n)

Output

输出量

Enter N: 2 
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10 
2 x 6 = 12 
2 x 7 = 14 
2 x 8 = 16 
2 x 9 = 18 
2 x 10 = 20	

3. Print sum of n number

3.打印n个数字的总和

n=int(input("Enter N: "))
s=0
for i in range(1,n+1):
    s=s+i
print("Sum = ",s)

Output

输出量

Enter N: 10
Sum = 55 

4. Print factorial of n

4.打印n的阶乘

n=int(input("Enter N: "))
f=1
for i in range(n,0,-1):
    f=f*i
print("Factorial = ",f)

Output

输出量

Enter N: 4
Factorial =  24 

5. Check prime number

5.检查素数

n=int(input("Enter N: "))
c=0
for i in range(1,n+1):
    if n%i==0:
        c=c+1
if c==2:
    print(n,"is Prime")
else:
    print(n,"is Not Prime")

Output

输出量

Enter N: 131
131 is Prime

6. Check palindrome number

6.检查回文数

n=int(input("Enter Number: "))
m=n
rev=0
while(n>0):
    dig=n%10
    rev=rev*10+dig
    n=n//10
if rev==m:
    print(m,"is Palindrome")
else:
    print(m,"is not Palindrome")

Output

输出量

Enter Number: 12321
12321 is Palindrome


翻译自: https://www.includehelp.com/python/loops-examples.aspx

python死循环例子

你可能感兴趣的:(python,算法,机器学习,深度学习,tensorflow)