Python Practicals

 

# Printing Hello World

print("Hello World.")


# Write a program to display ASCII code of a character and vice versa.

Theory :

The ASCII (American Standard Code for Information Interchange) code of a character is a numerical representation of a character such as a letter, number, or symbol. Each character has a unique ASCII code. Here are a few examples of ASCII codes:

  • The ASCII code for the letter "A" is 65.
  • The ASCII code for the digit "0" is 48.
  • The ASCII code for the exclamation mark "!" is 33.


Code :

var=True
while var:
  choice=int(input("Press-1 to find the ordinal value of a character.\nPress-2 to find the character of a value.\nEnter you choice : "))
  if choice==1:
    ch=input("Enter the character :")
    o=ord(ch)
    print("The ordinal value of ",ch,"is",o)
  elif choice==2:
    val=int(input("Enter the ordinal value : "))
    v=chr(val)
    print("The character with ordinal value ",val,"is",v)
  else:
    print("Invalid input.")
  option=input("Do you want to continue? (Y/N) :")
  if option=='Y' or option=='y':
    var=True
  else:
    var=false


# Palindrome Number

Theory :

A palindrome number is a number that remains the same when its digits are reversed. In other words, it reads the same backward as forward. Here are some examples of palindrome numbers:

  • 121
  • 1331
  • 12321
  • 45654


Operators used :

The % operator is the modulo operator, also known as the remainder operator. It returns the remainder of the division of the left operand by the right. For example, 5 % 2 would return 1 because 5 divided by 2 is 2 with a remainder of 1.

The // operator is the floor division operator. It performs a division and rounds down to the nearest whole number. For example, 5 // 2 would return 2 because the result of 5 divided by 2 is 2.5, and floor division rounds down to the nearest integer.


Code :

num=int(input("Enter the number : "))
decoy = num
constant = 0
while num>0:
  (remainder) = (num) % 10
  (constant) = (remainder) + (constant) * 10
  (num) = (num) // 10
if (constant) == (decoy):
    print("The number is a palindrome.")
else:
   print("The number is not a palindrome.")


# Finding factorial of an integer

Code using function :

def factorial(n):
  if n==1:
    return n
  else:
    return n*factorial(n-1)
num=int(input("Enter the integer : "))
if num<0:
  print("Factorial does not exist for negative numbers.")
elif num==0:
  print("The factorial of 0 is 1.")
else:
  print("The factorial of ",num,"is",factorial(num))

Code using recursion :

number=int(input("Enter the number : "))
factorial=1
while number>1:
    (factorial) = (factorial) * (number)
    (number) = (number) - 1
print(factorial)

# Sum of Natural Numbers

Code :

number=int(input("Enter the number : "))
sum=0
while number>0:
  (sum) = (sum) + (number)
  (number) = (number) - 1
print("The sum is : ",sum)

# Fibonacci Sequence

Code :

t1 = 1
t2 = 1
a = int(input("Enter the number : "))
print(t1)
print(t2)
n = 1
while (n <= a):
  (t3) = (t1) + (t2)
  print(t3)
  (t1) = (t2)
  (t2) = (t3)
  n = n + 2


# To identify whether a number is prime or not

Code :

number = int(input("Enter number : "))
flag = 0
for i in range(2,number):
  if (number) % i == 0:
    flag = 1
    break
if flag==1:
  print("Not a prime.")
else:
  print("Number is a prime.")


# Even or Odd

Code :

n=int(input("Enter the number :"))
d=n%2
if d==0:
   print("The number",n,"is even.")
else:
   print("The number",n,"is odd.")


# To check whether given character is alphabet, digit, or any character

Code :

c=input("Enter a single character : ")
if (c>='A') and (c<='Z') or (c>='a') and (c<='z'):
  print("You entered an alphabet.")
elif (c>='0') and (c<='9'):
  print("You entered a digit.")
else:
  print("You entered a character other than alphabets and digits.")


# Volume of a Cuboid

Code :

l=float(input("Enter the length : "))
b=float(input("Enter the breadth : "))
h=float(input("Enter the height : "))
v=l*b*h
print("The volume of the cuboid is",v,"cubic units")


# Swap values of two variables

Code :

x=float(input("Enter the first number : "))
y=float(input("Enter the second number : "))
x,y=y,x
print("After swapping,")
print("The fist number has become : ",x)
print("The second number has become : ",y)


# Finding Factors of a number

Code :

x=int(input("Enter the number : "))
for i in range(1,x+1):
    if x%i==0:
        print(i)


# Prime numbers upto the given number

Code :

x=int(input("Enter the number : "))
for i in range(2, x+1):
    prime = True
    for j in range(2, i):
        if i % j == 0:
            prime = False
            break
    if prime:
        print(i)


# Armstrong number

Code :

num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
   digit = temp % 10
   sum += digit ** 3
   temp //= 10
if num == sum:
   print(num,"is an Armstrong number")
else:
   print(num,"is not an Armstrong number")


#Nested Loops


Variety 1 :

number = int(input("Enter number : "))
for n in range(1,number):
  for i in range(1 , n+1):
    print(i , end=" ")
  print(" ")

Output :

1
1 2
1 2 3
1 2 3 4


Variety 2 :

number = int(input("Enter number : "))
for n in range(1,number):
  for i in range(1 , n+1):
    print(n , end=" ")
  print(" ")

Output :

1  
2 2  
3 3 3  
4 4 4 4


Variety 3 :

number = int(input("Enter number : "))
for n in range(1,number):
  for i in range(1 , n+1):
    print(n , end=" ")
  print("\n ")

Output :

1 

2 2 
 
3 3 3 
 
4 4 4 4 


Variety 4 :

number = int(input("Enter number : "))
for n in range(number,0,-1):
  for i in range(0 , n):
    print(n , end=" ")
  print("\n ")

Output :

5 5 5 5 5 
 
4 4 4 4 
 
3 3 3 
 
2 2 
 
1 

Popular posts from this blog