Python Dictionary Clear Method

Asked 2 years ago, Updated 2 years ago, 98 views

fruits_dic={'apple':6000,'melon':3000,'banana':5000,'orange':7000}
fruits_dic.clear()
print(fruits_dic)

Result value
{}
-------------------------------------------------

fruits_dic={'apple':6000,'melon':3000,'banana':5000,'orange':7000}

print(fruits_dic.clear())

Result value
None

I wonder why the two results are different!

python dictionary method

2022-09-20 10:18

1 Answers

This is because the clear command is not a command to spit out the result value.

fruits_dic={'apple':6000,'melon':3000,'banana':5000,'orange':7000}
a = fruits_dic.clear()
print(a)
print(fruits_dic)
>> None
{}
fruits_dic={'apple':6000,'melon':3000,'banana':5000,'orange':7000}
print(fruits_dic.clear())
print(fruits_dic)
>> None
{}


2022-09-20 10:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.