Explain this Dict comprehension python [duplicate] - python

This question already has answers here:
Understanding .get() method in Python [duplicate]
(5 answers)
Closed 11 months ago.
dict1={"a":4,"b":2,"A":6}
print({k.lower():dict1.get(k.lower(),0) + dict1.get(k.upper(),0) for k in dict1.keys()})
I copied this code from a youtube video. I couldn't understand the code properly. Please help me to figure it out.
I couldn't understand the purpose of 0 in second line.
k.lower():dict1.get(k.lower(),0)
I'm a beginner in python. kindly help me

The second parameter is the value parameter, that is , if the key does not exist this will be the value.
In your example, dict1["B"] does not exist. Normally that would cause an error. But because the value parameter is 0, instead of causing an error it pretends that dict1["B"] is 0. Note that this does not change the original dictionary.\

Related

New to Python, trying to do print statements. College textbook is super short not being helpful [duplicate]

This question already has answers here:
How can I print variable and string on same line in Python? [duplicate]
(18 answers)
Closed 2 years ago.
Ok so I'm working on a problem in my python class. I've gotten most of it figured out aside from print statements. I assigned the arguements correctly (I think) and am just trying to get the text to print correctly on the terminal side. What am I doing wrong here?
here is what I currently have
is the example that mine is supposed to look similar to
you could use f string print statements too like this:
print(f"distance in knots is: {distance_in_knots}")

train['Gender'].fillna(train['Gender'].mode()[0], inplace=True) [duplicate]

This question already has answers here:
Uncomfortable output of mode() in pandas Dataframe
(4 answers)
Closed 4 years ago.
train['Gender'].fillna(train['Gender'].mode()[0], inplace=True)
I got this code in one of my basic data science course. I wanted to understand, what is the significance of "[0]" after mode() in this. I would really appreciate the answer.
Thanks!
Mode documentaion
The mode() return 2 value, first is mode value second is count. So train['Gender'].mode()[0] means get the mode value of train['Gender'].
The notation [0] means that the thing before it (mode() in this case) is a collection, a list, an array, ..., and you are taking the first element.
In case you need more information, you need to include the rest of the source code (preferably by editing your question), explaining the exact meaning of the mentioned objects.

C command similar to "if x in list" of Python [duplicate]

This question already has answers here:
Check if a value exists in an array in Cython
(2 answers)
Closed 6 years ago.
In Python we could use if x in list: so I was wondering if there's a similar command in C, so that we don't have to go through the whole thing using a for.
How can you know whether a value is contained in an array without cycling through it? This is exactly what Python does under the hood. No, there's no magical way to instantly know this.

what's the meaning of "[ ]" in python references? [duplicate]

This question already has answers here:
What do square brackets, "[]", mean in function/class documentation?
(4 answers)
Closed 7 years ago.
I'm using python for a long time. but it was always strange to me why In python references, commands are written like this:
del var1[,var2[,var3[....,varN]]]]
I know that if I want to use the above command I should write it in this way:
del var1, var1
I can't understand the meaning of [] is it related to the lists? any help will be appreciated.
Its just showing that these parameters are optional. This is normal style for references.
There is nothing to do with this in python. Just a tutorial explanation.
I bealive, this is taken origin from Usage message

Showing Syntax Error while try to output the dictionary or a list value [duplicate]

This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 9 years ago.
I am a amature in Python,using the python33 version.The problem i am facing while i want to get output of a list of dictionary just like that.
dict = {'name':'Tanvir','Position':'Programmer'};
print dict['name'];
if i run the code then there showing a syntax error.same thing is happening for the list also.
Please help me to fix the problem.
Thanks in advance.
In Python 3, print is a function, so you need print(dict[name]). You also don't need the semicolons. You also need to read the Python tutorial to learn the basics first.

Categories

Resources