IKH

Immutability

  • All fundamental data types (int, float, complex, bool, str) are immutable. i.e. once we creates an object, we cannot perform any changes in that object. If we are trying to change then with those changes a new object will be created. This non-changeable behaviors’ is called immutability.
  • In python if a new object is required, then PVM wont create object immediately. First it will check is any object available with the required content or not. If available then existing object will be reused. If it is not available then only a new object will be created. The advantage of this approach is memory utilization and performance will be improved.
  • But the problem in this approach is, several references pointing to the same object, by using one reference if we are allowed to change the content in the existing object then the remaining references will be effected. To prevent this immutability concept is required. According to this once creates an object we are not allowed to change content. If we are trying to change with those changes a new object will be created.

Example 1

Python
num1 = 10
num2 = 10

print(num1 is num2)
print(id(num1))
print(id(num2))

Output

PowerShell
True
3228203248208
3228203248208

Example 2

Python
bool1 = True
bool2 = True

print(bool1 is bool2)
print(id(bool1))
print(id(bool2))

Output

PowerShell
True
3228203243456
3228203243456

Example 3

Python
str1 = "ajay"
str2 = "ajay"

print(str1 is str2)
print(id(str1))
print(id(str2))

Output

PowerShell
True
3228203243123
3228203243123
  • Reusability of int value applicable only for 0-256 value.

Example

Python
num1 = 256
num2 = 256

print(num1 is num2)
print(id(num1))
print(id(num2))

Output

PowerShell
False
3228205754736
3228205754768
  • Reusability of object not allowed to float and complex number.

Example

Python
com1 = 10+5j
com2 = 10+5j

print(com1 is com2)
print(id(com1))
print(id(com2))

Output

PowerShell
False
3228205754836
3228205754236

Escape Characters

  • In String literals we can use esacpe characters to associate a special meaning.
  • The following are various important escape characters in python.
    1. \n ==> New Line
    2. \t ==> Horizontal tab
    3. \r ==> Carriage Return
    4. \b ==> Back space
    5. \f ==> Form Feed
    6. \v ==> Vertical tab
    7. \’ ==> Single quote
    8. \” ==> Double quote
    9. \\ ==> back slash symbol

Example

Python
str1 = "ajay\nshukla"
print(str1)
            
str2 = "ajay\tshukla"
print(str2)

str3 = "this is \" symbol"
print(str3)

Output

PowerShell
ajay
shukla
ajay	shukla
this is " symbol

Constants

  • Constants concept is not applicable in python. But it is convention to use only uppercase characters if we don’t want to change value.
Python
MAX_VALUE = 10
print(MAX_VALUE)

Output

PowerShell
10
  • It is just convention but we can change the value.

Ungraded Questions

Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!


Name
Email
Phone

Report an error