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
- 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
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.
- 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.
- 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
- .
- output:
OOOOOGeeks for Geeks
(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
A nested loop is a loop inside a loop.
The "inner loop" will be executed one time for each iteration of the "outer loop":
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry
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
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?