Saturday, 16 December 2023

NEW FINDINGS IN PYTHON

 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

a=[47,8,4]
a.remove()
print(a)

Output:
Python312/python.exe "c:/Users/dell/Desktop/python_project/Random coding/main.py" Traceback (most recent call last): File "c:\Users\dell\Desktop\python_project\Random coding\main.py", line 2, in <module> a.remove() TypeError: list.remove() takes exactly one argument (0 given)

#Not error example
a=[47,8,4]
a.remove(4)
print(a)


Output:
[47, 8]

TITLE:- append() IN LIST IS EQUIVALENT TO add() in set ,whereas extend() in list is equivalent to 
update() in set and dictionary.

append()  takes one argument that should be the no to be added in list and similar for add() in set.
extend() takes list form one argument in list data type and similar for update() in set.
 
Example for append() in list.

# Creating an empty list
my_list = []

# Adding elements to the list using append()
my_list.append(10)
my_list.append(20)
my_list.append(30)

# Displaying the updated list
print("Updated List:", my_list)

OUTPUT:-
Updated List: [10, 20, 30]

Example for add() in set

# Creating an empty set
my_set = set()

# Adding elements to the set using add()
my_set.add(10)
my_set.add(20)
my_set.add(30)

# Displaying the updated set
print("Updated Set:", my_set)

Output:
Updated Set: {10, 20, 30}
 
Example for extend() in list

# Creating a list
my_list = [1, 2, 3]

# Creating another iterable (in this case, another list)
another_list = [4, 5, 6]

# Using extend() to add elements from another iterable to the original list
my_list.extend(another_list)

# Displaying the updated list
print("Updated List:", my_list)

Output:
Updated List: [1, 2, 3, 4, 5, 6]

Example for update() in set

# Creating two sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}

# Using update() for set union
set1.update(set2)

# Displaying the updated set
print("Updated Set:", set1)

OUTPUT:
Updated Set: {1, 2, 3, 4, 5}








No comments:

Post a Comment

NEW FINDINGS IN PYTHON

 TITLE:-WE CAN UPDATE DICTIONARY BY USING LOOPING WITHOUT USING UPDATE() FUNCTION. EXAMPLE: with open('currencyData.txt') as f: li...