Python slicing...the number scaling way!!!

Python slicing...the number scaling way!!!

Here is a more professional rephrasing of the given sentences:

?There are two methods of slicing:

  • ?Forward Slicing: It involves moving from left to right through the data elements.
  • Backward Slicing: It involves moving from left to right through the data elements.

In Python, the numbering scale starts from 0 when counting from the left(Forward slicing), whereas it starts from -1 when counting from the right(Backward Slicing). Therefore, in the string "MUKESH KUMAR," the letter "M" is at the 0th position, and "R" is at the 11th position when counted from the left. However, when counted from the right, "M" is at the -12th position and "R" is at the -1st position.

?When performing slicing operations, it is important to set the upper bound as the desired index plus one.

?For example, consider a variable named "name" with the value "MUKESH KUMAR":

  • ?To slice the letters "UM," you can use the code "name[7:9]". Here, the upper bound is 8+1=9.
  • If you want to retrieve alternate elements, you can use either "name[::2]" or "name[0:12:2]"; both will yield the same output.
  • ?To reverse the letters, you can use the code "name[::-1]".

?Let's examine how reverse indexing works:

  • ?If you want to extract "KU," you can use the code "name[-5:-3]". The upper bound+1 is -4+1=-3.
  • If you want to reverse the elements, you can use the code "name[-1:-13:-1]".

?Now, let's delve deeper into these concepts.

?The third number in the slicing notation "[-1:-13:-1]," which is -1 in this case, is called the "jump."

?You can put any number to jump as per your requirement.

?For instance, if you use the code "name[2:5:1]," the output will be 'KES.' However, if you use the code "name[2:5:-1]," the output will be none.

?To gain a better understanding, consider the number system.

No alt text provided for this image


?In the scenario where you are trying to retrieve data from the 2nd to the 5th index (positive side), but the jump is in the negative direction (i.e., -1), an ambiguity arises, resulting in no output.

?Similarly, in the case of "name[-3:-5:1]," you are trying to move in the negative direction but jumping in the positive direction. Consequently, the output is none.

?Try these concepts for yourself; it is fun…!!!

#pythonprogramming #python #pythondeveloper #pythonfordatascience #pythoncoding #pythonlearning #pythonprojects #datascience #dataanalytics #dataanalysis #dataanalyst #datascientists #slicing #indexing

?

?

?

要查看或添加评论,请登录

Mukesh Kumar的更多文章

  • Are You Cost Avoiding or Cost Saving?

    Are You Cost Avoiding or Cost Saving?

    Generally, there are two types of expenditure in Procurement: Soft Cost and Hard Cost - Soft Cost are basically…

  • Invoicing and Payment Terms

    Invoicing and Payment Terms

    Cash flow is very important for any organization and hence planning Invoicing and Payment terms helps in a regulation…

  • BATNA: Negotiate on your way

    BATNA: Negotiate on your way

  • Blockchain approachs to Supplychain

    Blockchain approachs to Supplychain

    Many companies are exploring the benefits of leveraging the block chain technology in the area of contract and purchase…

社区洞察

其他会员也浏览了