python mid -1 answer's

  

1.Discuss about mutable and immutable Data types in Python with example

answer: 1 

Mutable data types are the objects that can be modified and altered (i.e. adding new elements, removing an element, replacing an element) even after creating an object. The mutable objects in Python are:

  • List(example):
  • thislist = ["apple""banana""cherry"]
    print(thislist)

  • Dictionary
  • Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
    print("\nDictionary with the use of Integer Keys: ")
    print(Dict)
  • Set
  • thisset = {"apple""banana""cherry"}
    print(thisset)

Immutable data types are the objects that cannot be modified and altered (i.e. adding new elements, removing an element, replacing an element) after creating an object. The immutable data types in Python are:

  • Tuple
  • thistuple = ("apple""banana""cherry")
    print(thistuple)
  • Float
  • int 
2 )Write a Python program to calculate the amount payable if money has been lent on simple interest. Principal or money lent = P, Rate of interest = R% per annum, and Time = T years. Then Simple Interest (SI) = (P x R x T)/ 100. Amount payable = Principal + SI. P, R and T are given as input to the program

4.What are Python string padding functions?

Different Types of Padding

The three most commonly used String Padding techniques are :

  • Inserting a character to the end of the String to make it of a specified length is known as Left Padding. It is mostly helpful when naming files which mostly start with numeric characters which are generated in sequence.
  • example:
    str = "Geeks for Geeks"
      
    # Printing the output of ljust() method
    print(str.ljust(20, '0'))
  • output:
  • Geeks for Geeks00000
  • In the case of Center Padding, the required character is inserted at both the ends of the strings with equal number of times until the newly formed string is of the specific length. This centers the string of the specific or required length. This returns a string centered of length width. Here, the default character is space.
  • # Creating a string variable
    str = "Geeks for Geeks"
      
    # Using center() method
    print("Center String: ", str.center(20))
  • output:
  • center String: Geeks for Geeks
  • In Right Padding, the given character is inserted to the right side of the String which needs to be padded. The specified character is added to the end of the string until it attains the required length
  • # Creating a string variable
    str = "Geeks for Geeks"
      
    # Using rjust() method
    print(str.rjust(20, "O")).
  • output: 
  • OOOOOGeeks for Geeks
5.Write the corresponding Python assignment statements: i) Assign 10 to variable length and 20 to variable breadth. ii) Assign the average of values of variables length and breadth to a variable sum. iii) Assign a list containing strings ‘Paper’, ‘Gel Pen’, and ‘Eraser’ to a variable stationery. iv) Assign the strings ‘Mohandas’, ‘Karamchand’, and ‘Gandhi’ to variables first, middle and last. v) Assign the concatenated value of string variables first, middle and last to variable full name. Make sure to incorporate blank spaces appropriately between different parts of names.
(a)
length = 10
breadth = 20

(b)
sum = (length + breadth)/2

(c)
stationery = [‘Paper’, ‘Gel Pen’, ‘Eraser’]

(d)
first = ‘Mohandas’
middle = ‘Karamchand’
last = ‘Gandhi’

We can also assign variable in that way:-

first, middle, last = ‘Mohandas’, ‘Karamchand’, ‘Gandhi’

(e)

fullname = first + ' ' + middle + ' ' + last
6.Explain about input validation loops and nested loops with examples.
nested loops :

A nested loop is a loop inside a loop.

The "inner loop" will be executed one time for each iteration of the "outer loop":

adj = ["red""big""tasty"]
fruits = ["apple""banana""cherry"]

for x in adj:
  for y in fruits:
    print(x, y)
ouput:
red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry

7.Write a program that reads a string from the user and uses a loop to determines whether or not it is a palindrome. Display the result, including a meaningful output message

def isPalindrome(s):

    return s == s[::-1]

 

 

# Driver code

s = input("enter the string dude!!")

ans = isPalindrome(s)

 

if ans:

    print("it’s a palindrome ")

else:

    print("No")

output:

mom

it’s a palindrome 


8.Write a program to repeat the string ‘‘GOOD MORNING” n times. Here ‘n’ is an integer entered by the user.
n= int(input("enter a number ")
for i in range (n):
     print(‘‘GOOD MORNING”)
ouput:
       
   3 
GOOD MORNING
GOOD MORNING
GOOD MORNING

  
3.Explain about explicit conversion

Explicit Type Conversion

In Explicit Type Conversion, users convert the data type of an object to required data type. We use the predefined functions like int()float()str(), etc to perform explicit type conversion.

This type of conversion is also called typecasting because the user casts (changes) the data type of the objects.

num_int = 123
num_str = "456"



num_str = int(num_str)


num_sum = num_int + num_str

print("Sum of num_int and num_str:",num_sum)

4.What are Python string padding functions?
Previous Post Next Post