Link to home
Create AccountLog in
Python

Python

--

Questions

--

Followers

Top Experts

Avatar of excel learner
excel learner🇬🇧

Understanding the relationship between square brackets and paranthesis in python

Dear experts,

 

While trying to understand the below code, I got these further questions:

 

series={'index':[0,1,2,3],'data':[145,142,38,13],'name':'Songs'}

def get(series,idx):
   value_idx=series['index'].index(idx)
   return series['data'][value_idx]

get(series,1)

Open in new window

 

 

 

User generated image

 

This is a follow-up question from 

https://www.experts-exchange.com/dashboard/#/questions/29278044

 

Kindly guide/help me.

 

Thank you

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


SOLUTION
Avatar of skullnobrainsskullnobrains

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of excel learnerexcel learner🇬🇧

ASKER

Skull,

It looks like I need to do a another read of Panda, from scratch (most likely I would have covered it and forgotten)

Please allow me to breakdown the code into small bits and start

 

I take the below code:

series={'index':[0,1,2,3],'data':[145,142,38,13],'name':'Songs'}

In the above code:

Q1) What does series represent (a variable???)

Q2) What does index represent?

Q3) what does data represent?

Q4) purpose of including 'name':'Songs'

 

Understood your below explanation:

index() is a builtin function of the series['index'] list that returns the index of the passed element

I have gone through the below page:

https://www.w3schools.com/python/trypython.asp?filename=demo_ref_list_index2

Thank you for the above explanation

Q5) can you please help me understand what does “series['index']” in value_idx=series['index'].index(idx) represent?

 

Q6) In the below picture, the argument and parameter are same. My question is if only one Series is provided, do we need to include this in the function defition    “ def get(series, idx):”?

User generated image

I can understand if a lot of similar such data is provided , then I need to tell the function which data (here ‘series’) it should take.

 

Kindly help/guide me.

Thank you


SOLUTION
Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.

SOLUTION
Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.

Avatar of excel learnerexcel learner🇬🇧

ASKER

pepr,

 

Thank you for the detailed explanation.

 

I have gone through __getitem__ dunder method. I have a few questions on the code I have chosen. I will raise another questionn.

 

The word is appearing in many places and I am not clear. So to get a better understanding I have replaced ‘index’ with ‘Botox’ and below are the results (something need fix in the code):

 

series1={'Botox':[0,1,2,3],'data':[145,142,38,13],'name':'Songs'}

def get(series1,idx):
   value_idx=series1['Botox'].Botox(idx)
   return series1['data'][value_idx]

get(series1,1)

Open in new window

 

Output:

 

User generated image

 

 

I then changed Botox(idx) to index(idx) which is a built-in python method

 

series1={'Botox':[0,1,2,3],'data':[145,142,38,13],'name':'Songs'}

def get(series1,idx):
   value_idx=series1['Botox'].index(idx)
   return series1['data'][value_idx]

get(series1,1)

Open in new window

 

Output:

 

User generated image

 

Hope, my understanding is correct now.

 

Kindly add any further info/reading required in this context.

 

Thank you


Avatar of excel learnerexcel learner🇬🇧

ASKER

pepr, 

any thoughts on the reason/purpose of including: 'name':'Songs'

series={'index':[0,1,2,3],'data':[145,142,38,13],'name':'Songs'}

 

This property or similar type of code is appearing in all examples in Serialisation topic in Pandas 2 book (Harrison, Matt. Effective Pandas 2: Opinionated Patterns for Data Manipulation:)

 

I am not sure, what I will miss if I do not understand this aspect.

 

 

Here is the text given above the code

 

The dictionary, series, has a list of the data points stored under the 'data' key. In addition to an entry in the dictionary for the actual data, there is an explicit entry for the corresponding index values for the data (in the 'index' key), as well as an entry for the name of the data (in the 'name' key):


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


SOLUTION
Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.

Avatar of excel learnerexcel learner🇬🇧

ASKER

pepr, 

Understood the recent comment.

 

This bit is bothering me to proceed. How this field is useful either from logic or from programmng perspective?

 

User generated image

ASKER CERTIFIED SOLUTION
Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Python

Python

--

Questions

--

Followers

Top Experts

Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in other languages. Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive set of standard libraries, including NumPy, SciPy, Django, PyQuery, and PyLibrary.