Python Codes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

PYTHON

CODES

1) Program to print squares:

i=1

while i<10:

print(i*i)

i=i+1

to print cubes, replace (i*i) to (i*i*i)

2) Program to print a list of things in different lines:

Fruits[“apple”,”mango”,”banana”,”grape”]

For x in fruits:

Print(x)

3) Program to print a table of n:

n = int(input("Enter value of n"))

for i in range(1,11):

result=n*i

print(n,"*",i,"=",result)

4) Program to print odd no. till 10:

n = int(input("Enter value of n"))

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

print(i)

5) Program to print even no. till 10

n = int(input("Enter value of n"))

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

print(i)
6) Program to display number till the number given by user

i=0

n=int(input('enter a number'))

while i<=n:

print(i)

i=i+1

7) Program to write numbers in reverse order:

i=10

while i>0:

print(i)

i=i-1

8) Program to display no. from 1 to 10:

for x in range(1,11):

print(x)

9) Program to increment the sequence with 3:

for x in range(1,20,3):

print(x)

10) Program to decrement the sequence with 3:

for x in range(30,1,-3):

print(x)

11) Write a Program to find the sum of the following series:

S = 1 + 2 + 3 + 4 + ---------n (accept n from user):

n=int(input('enter number'))

x=0

for i in range(1,n+1):
x+=i

print(x)

12) Program to print(sum) Write a Program to find the sum of the following series:

S = 1 + 4 + 9 + 16 + ---------n2:

n=int(input('enter a number:'))

i=1

sum=0

while i<=n:

sum=sum+i*i

i=i+1

print(sum)

13) Write a Program to calculate and print sum of integers of the first n natural numbers. (accept
n from user):
n=int(input('enter a number'))
i=1
sum=0
while i<=n:
sum=sum+i
i=i+1
print(sum)

14) Program to find factores


n=int(input("Enter value of n"))
i=1
while(i <= n):
if ( n % i == 0):
print(i)
i=i+1

15) Program to find the sum of odd numbers from 1 to n:


n=int(input('enter'))
sum=0
for i in range(1,n+1,2):
sum=sum+i
print(sum)

16) To find prime no.


n=int(input("Enter value of n"))
i=1
count = 0
while(i <= n):
if ( n % i == 0):
count = count + 1
i=i+1
if (count == 2):
print("It's a prime number")
else:
print("It's not a prime number")

17) To find perfect no.:


n = int(input("Enter value of n"))
sum = 0
for i in range(1, n + 1):
if (n % i == 0) and (i != n):
sum = sum + i
if (sum == n):
print("It's a perfect number")
else:
print("It's not a perfect number")

You might also like