TITLE:-WE CAN UPDATE DICTIONARY BY USING LOOPING WITHOUT USING UPDATE() FUNCTION.
EXAMPLE:
with open('currencyData.txt') as f:
lines = f.readlines()
currencyDict = {}
for line in lines:
parsed = line.split("\t")
currencyDict[parsed[0]] = parsed[1]
amount = int(input("Enter amount:\n"))
print("Enter the name of the currency you want to convert this amount to? Available Options:\n")
[print(item) for item in currencyDict.keys()]
currency = input("Please enter one of these values: \n")
print(f"{amount} INR is equal to {amount *float(currencyDict[currency])} {currency}")'
ANOTHER EXAMPLE:
In Python, the `update()` method is used to update a dictionary with elements from another dictionary or from an iterable of key-value pairs. This method modifies the dictionary in place, adding key-value pairs from the specified source.
The basic syntax of the `update()` method is as follows:
```python
dictionary.update([iterable])
```
- `dictionary`: The dictionary to be updated.
- `iterable`: An iterable (e.g., another dictionary, a list of key-value pairs, or any other iterable) containing key-value pairs to be added to the dictionary.
Here's an example to illustrate the use of the `update()` method:
```python
# Initial dictionary
my_dict = {'a': 1, 'b': 2}
# Update the dictionary with a new key-value pair
my_dict.update({'c': 3})
print(my_dict)
```
Output:
```
{'a': 1, 'b': 2, 'c': 3}
```
In this example, the `update()` method adds the key-value pair `'c': 3` to the existing dictionary.
You can also use the `update()` method with an iterable of key-value pairs. Here's another example:
```python
# Initial dictionary
my_dict = {'a': 1, 'b': 2}
# Update the dictionary with key-value pairs from an iterable
iterable = [('c', 3), ('d', 4)]
my_dict.update(iterable)
print(my_dict)
```
Output:
```
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
```
In this case, the `update()` method adds the key-value pairs from the iterable (`('c', 3)` and `('d', 4)`) to the dictionary.
Using the `update()` method is a convenient way to merge or add key-value pairs to a dictionary, and it allows you to do so in an efficient and concise manner.
TITLE:THERE IS A DIFFERENCE BETWEEN READLINE() AND READLINES() FUNCTION
In Python, `readline()` and `readlines()` are methods used for reading data from a file, but they operate in slightly different ways.
1. **`readline()` Method:**
- The `readline()` method reads a single line from the file.
- If you call `readline()` multiple times, it will read consecutive lines in the file.
- It returns an empty string (`''`) when the end of the file is reached.
Example:
```python
with open('example.txt', 'r') as file:
line1 = file.readline()
line2 = file.readline()
print(line1)
print(line2)
```
2. **`readlines()` Method:**
- The `readlines()` method reads all lines from the file and returns them as a list of strings.
- Each element in the list corresponds to a line in the file.
- It reads the entire content of the file into memory at once, so it may not be suitable for very large files.
Example:
```python
with open('example.txt', 'r') as file:
lines = file.readlines()
print(lines)
```
It's worth noting that both methods include the newline character (`'\n'`) at the end of each line when reading from a text file. If you want to remove the newline characters, you can use the `strip()` method:
```python
with open('example.txt', 'r') as file:
line = file.readline().strip()
lines = [line.strip() for line in file.readlines()]
print(line)
print(lines)
```
In summary, `readline()` reads one line at a time, while `readlines()` reads all lines and returns them as a list. Choose the method that best fits your needs based on how you want to process the file content.
TITLE: BY DEFAULT PYTHON CHECKS TRUENESS AT FIRST
Example:-
# function which return reverse of a string
def isPalindrome(s):
return s == s[::-1]
# Driver code
s = "malayalam"
ans = isPalindrome(s)
if ans:
print("Yes")
else:
print("No")
TITLE: IN LIST FORM, remove() function takes only one argument which needs to be value inside the list /sets otherwise throw error.
Example:
Error example