Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
118 views

Python Programming

The document discusses Python programming concepts like input/output, comparison operators, loops, patterns, series, factorials, and determining if a number is perfect, Armstrong, or a palindrome. Code examples and outputs are provided for each concept.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

Python Programming

The document discusses Python programming concepts like input/output, comparison operators, loops, patterns, series, factorials, and determining if a number is perfect, Armstrong, or a palindrome. Code examples and outputs are provided for each concept.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Python Programming

 Input a welcome message and display it .


Code
message=input("Enter welcome message : ")
print("Hello, ",message)

Output

 Input 2 numbers and display the larger/smaller number.


Code
#input first number
num1=int(input("Enter First Number"))
#input Second number
num2=int(input("Enter Second Number"))
#Check if first number is greater than second
if (num1>num2):
print("The Larger number is", num1)
else:
print ("The Larger number is", num2)
Output

 Input 3 no. and display the largest/smallest number.


Code
#input first,Second and third number
num1=int(input("Enter First Number"))
num2=int(input("Enter Second Number"))
num3=int(input("Enter Third Number"))
#Check if first number is greater than rest of the two
numbers.
if (num1> num2 and num1> num3):
print("The Largest number is", num1)
#Check if Second number is greater than rest of the two
numbers.
elif (num2 > num1 and num2> num3):
print ("The Largest number is", num2)
else:
print ("The Largest number is", num3)

Output
 Generate the following patterns using nested loop.
Pattern-1 Pattern-2 Pattern-3
* 12345 A
** 1234 AB
*** 123 ABC
**** 12 ABCD
***** 1 ABCDE

Code
Pattern 1
for i in range(1,6):
for j in range(i):
print('*',end='')
print('')

Pattern 2
for i in range(5,0,-1):
for j in range(1,i+1):
print(j,end=' ')
print()
Pattern 3-i
n=6
for i in range(n) :
t = 65
for j in range(i + 1) :
print(chr(t), end = ' ')
t += 1
print()

 Write a program to input the value of x and n and print


the sum of the following series:
 1+x+x^2+x^3+x^4+…x^n
Code
x=float(input("Enter value of x:"))
n=int(input("Enter value of n:"))
s=0
for a in range(n+1):
s+=x**i
print("The sum of the series is:",s)

Output

 1-x+x^2-x^3+x^4-…x^n
Code
print("Following series: 1-x+x^2-x^3+x^4-…x^n")
x=int(input("Enter value of x: "))
n=int(input("Enter value of n: "))
s=0
j=1
for i in range (n+1):
j=(-1)**i
k=j*(x**i)
s=s+k
print("Sum of the series: 1-x+x^2-x^3+x^4-…x^n")
print("with x:",x,"and n:",n,"is=",s)

Output
 x+x^2/2-x^3/3+x^4/4-…x^n/n
Code
n=int(input("Enter the number of terms:"))
x=int(input("Enter the value of x:"))
sum1=1
for i in range(2,n+1):
sum1=sum1+((x**i)/i)
print("The sum of series is",round(sum1,2))

Output

 x+x^2/2!-x^3/3!+x^4/4!-…x^n/n!
Code
x = int(input("Enter the value of x: "))
sum = 0
m=1
for i in range(1, 7) :
fact = 1
for j in range(1, i+1) :
fact *= j
term = x ** i / fact
sum += term * m
m = m * -1
print("Sum =", sum)

Output

 Determine whether a number is a perfect number, an


Armstrong number or a palindrome.
Code
number=int(input("Enter any number:"))
num=number
num1= number
rev=0
while num>0:
digit=num%10
rev=rev*10+digit
num=int(num/10)
if number==rev:
print(number ,'is a Palindrome')
else:
print(number , 'Not a Palindrome')
# Armstrong number is equal to sum of cubes of each digit
sum = 0
temp = num1
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num1 == sum:
print(num1," is an Armstrong number")
else:
print(num1," is not an Armstrong number")
# Perfect number, a positive integer that is equal to the sum of its proper
divisors.
sum1 = 0
for i in range(1, number):
if(number % i == 0):
sum1 = sum1 + i
if (sum1 == number):
print(number, " The number is a Perfect number")
else:
print(number, " The number is not a Perfect number")

Output

You might also like