0% found this document useful (0 votes)
66 views18 pages

Computer Python

The document discusses various Python programming concepts like input/output, conditional statements, loops, functions, strings, lists, tuples, dictionaries and their applications. These include programs to find largest/smallest of numbers, palindrome, Armstrong number, perfect number, prime number, Fibonacci series, GCD, LCM, counting characters in string and more.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
66 views18 pages

Computer Python

The document discusses various Python programming concepts like input/output, conditional statements, loops, functions, strings, lists, tuples, dictionaries and their applications. These include programs to find largest/smallest of numbers, palindrome, Armstrong number, perfect number, prime number, Fibonacci series, GCD, LCM, counting characters in string and more.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 18

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
 Input a number and check if the number is a prime or
composite number.
Code
num = int(input("Enter any number : "))
if num > 1:
for i in range(2, num):
if (num % i) == 0:
print(num, "is NOT a PRIME number, it is a COMPOSITE
number")
break
else:
print(num, "is a PRIME number")
elif num == 0 or 1:
print(num, "is a neither Prime NOR Composite number")
else:
print()

Output

 Display the terms of a Fibonacci series.


Code
# Program to display the Fibonacci sequence up to n-th term
nterms = int(input("How many terms? "))
# first two terms
n1, n2 = 0, 1
count = 0
# check if the number of terms is valid
if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1

Output
 Compute the greatest common divisor and the least
common multiple of 2 integers.
Code
num1=float(input("Enter the value of num1: "))
num2=float(input("Enter the value of num2: "))
a=num1
b=num2
lcm=0
while(num2 !=0):
temp=num2
num2=num1%num2
num1=temp
gcd=num1
lcm=((a*b)/gcd)
print("HCF of",a,"and",b,"=",gcd)
print("LCM of",a,"and",b,"=",lcm)
Output

 Count and display the number of vowel, consonants,


uppercase, lowercase character in a string.
Code
string = input("Enter Your String : ")
vowels = consonants = uppercase = lowercase = 0
vowels_list = ['a','e','i','o','u','A','E','I','O','U']
for i in string:
if i in vowels_list:
vowels += 1
if i not in vowels_list:
consonants += 1
if i.isupper():
uppercase += 1
if i.islower():
lowercase += 1
print("Number of Vowels in this String = ", vowels)
print("Number of Consonants in this String = ", consonants)
print("Number of Uppercase characters in this String = ",
uppercase)
print("Number of Lowercase char`acters in this String = ",
lowercase)
Output

 Input a string and determine whether it is a palindrome


or not; convert the case of character in a string.
Code
str=input("Enter a string:")
w=""
for element in str[::-1]:
w = w+element
if (str==w):
print(str, 'is a Palindrome string')
else:
print(str,' is NOT a Palindrome string')

Output
 Find the largest/smallest number in a list.
Code
lst = []
num = int(input('How many numbers: '))
for n in range(num):
numbers = int(input('Enter number '))
lst.append(numbers)
print("Maximum element in the list is :", max(lst), "\nMinimum element
in the list is :", min(lst))

Output

 Input a list of number and swap elements in the even


location with the elements at the odd location.
Code
# Entering 5 element Lsit
mylist = []
print("Enter 5 elements for the list: ")
for i in range(5):
value = int(input())
mylist.append(value)
# printing original list
print("The original list : " + str(mylist))
# Separating odd and even index elements
odd_i = []
even_i = []
for i in range(0, len(mylist)):
if i % 2:
even_i.append(mylist[i])
else :
odd_i.append(mylist[i])
result = odd_i + even_i
# print result
print("Separated odd and even index list: " + str(result))

Output

 Input a list/tuple of elements, search for a given element


in the list/tuple.
Code
mylist = []
print("Enter 5 elements for the list: ")
for i in range(5):
value = int(input())
mylist.append(value)
print("Enter an element to be search: ")
element = int(input())
for i in range(5):
if element == mylist[i]:
print("\nElement found at Index:", i)
print("Element found at Position:", i+1)

Output

 Input a list of number and find the smallest and largest


number from the list.
Code
#create empty list
mylist = []
number = int(input('How many elements to put in List: '))
for n in range(number):
element = int(input('Enter element '))
mylist.append(element)
print("Maximum element in the list is :", max(mylist))
print("Minimum element in the list is :", min(mylist))

Output
 Create a dictionary with the roll number, name and
marks of n students in a class and display the name of
students who have marks above 75.
Code
no_of_std = int(input("Enter number of students: "))
result = {}
for i in range(no_of_std):
print("Enter Details of student No.", i+1)
roll_no = int(input("Roll No: "))
std_name = input("Student Name: ")
marks = int(input("Marks: "))
result[roll_no] = [std_name, marks]
print(result)
# Display names of students who have got marks more than 75
for student in result:
if result[student][1] > 75:
print("Student's name who get more than 75 marks is/are",
(result[student][0]))

Output

You might also like