Advertisements
Advertisements
Question
Prove with the help of an example that the variable is rebuilt in case of immutable data types.
Solution
When a variable is assigned to the immutable data type, the value of the variable cannot be changed in place. Therefore, if we assign any other value to the variable, the interpreter creates a new memory location for that value and then points the variable to the new memory location. This is the same process in which we create a new variable. Thus, it can be said that the variable is rebuilt in case of immutable data types on every assignment.
Program to represent the same:
#Define a variable with immutable datatypes as value
var = 50
#Checking the memory location of the variable
print("Before: ",id(var))
#Assign any other value
var = 51
#Checking the memory location of the variable
print("After: ",id(var))
OUTPUT:
Before: 10916064
After: 10916096
It can be seen that the memory location a variable is pointing at after the assignment is different. The variable is entirely new and it can be said that the variable is rebuilt.
APPEARS IN
RELATED QUESTIONS
What is the use of the type() function in python?
Write the syntax of creating a Tuple with n number of elements.
What are the advantages of Tuples over a list?
What is a nested tuple? Explain with an example.
“Lists and Tuples are ordered”. Explain.
What advantages do tuples have over lists?
When to use a tuple or dictionary in Python. Give some examples of programming situations mentioning their usefulness.
Write a program to read the email IDs of n number of students and store them in a tuple. Create two new tuples, one to store only the usernames from the email IDs and the second to store domain names from the email IDs. Print all three tuples at the end of the program. [Hint: You may use the function split()]
A bank is a financial institution that is involved in borrowing and lending money. With advancements in technology, online banking, also known as internet banking allows customers of a bank to conduct a range of financial transactions through the bank’s website anytime, anywhere. As part of the initial investigation, you are suggested to
- collect a bank application form. After careful analysis of the form, identify the information required for opening a savings account. Also, enquire about the rate of interest offered for a saving account.
- The basic two operations performed on an account are Deposit and Withdrawal. Write a menu-driven program that accepts either of the two choices of Deposit and Withdrawal, then accepts an amount, performs the transaction, and accordingly displays the balance. Remember, every bank has a requirement for a minimum balance which needs to be taken care of during withdrawal operations. Enquire about the minimum balance required in your bank.
- Collect the interest rates for opening a fixed deposit in various slabs in a savings bank account. Remember, rates may be different for senior citizens.
Finally, write a menu-driven program having the following options (use functions and appropriate data types):
- Open a savings bank account
- Deposit money
- Withdraw money
- Take details, such as the amount and period for a Fixed Deposit, and display its maturity amount for a particular customer.
Our heritage monuments are our assets. They are a reflection of our rich and glorious past and an inspiration for our future. UNESCO has identified some Indian heritage sites as World heritage sites. Collect the following information about these sites:
- What is the name of the site?
- Where is it located?
District
State - When was it built?
- Who built it?
- Why was it built?
- The website link (if any).
Write a Python program to
- create an administrative user ID and password to add, modify or delete an entered heritage site in the list of sites.
- display the list of world heritage sites in India.
- search and display information about a world heritage site entered by the user.
- display the name(s) of world heritage site(s) on the basis of the state input by the user.