Skip to main content

Index in Python

 



Generally, we add elements in lists, tuples and we delete items, insert elements etc but how can we do that, how can we access a particular element, this is done by using index number.

PDF FOR INDEX IN PYTHON 

Ø  Index numbers are nothing but numbers which are given to elements in a list or tuple or array etc to access them.

Ø  Index number starts with zero and ends with one less than the given number but

For example

 List = [1,5,8,13,24,31]

0  1  2  3   4   5

Ø  Each index refers to one element, as we know here the elements in the list are 6 but the maximum index will be 5 this is because index starts with zero so maximum index will always be one less than the length of list or tuple or array etc.

ADVANTAGES in Python:

Ø  It makes our work easier like we can easily delete the element we want to delete by just using its index number.

Ø  There are lot of ways to print elements using index values.

Ø  By directly using that element in square brackets

For example: Python_closet = [7,27,56,65,48]   

0   1    2   3    4

If we want to print only one element of the list then we can print using its index number in square brackets like this

 Print(python_closet[3])   // here index 3 refers to the 4th element in the list//

 65  

Syntax = print(list name or tuple name [index number])

 Here we printed pythoncloset[3]for the 4th element in list because as we all know that index value starts with zero so for 4th element in list its index will be one less than it 

EX: if we want to print second element then print(python_closet[1])

 If we want to print third element then print(python_closet[2]) like this we can use index number .

If in case we have a nested list or a nested tuple then we can access elements like this:

Python_closet=[2,36,56,[1,4,6,7]]

Here how can we take indexes for this nested list that will be your question, well it’s so simple.

Here the complete nested list will be considered as an index like

                                0, 1  2 , 3

     [2, 36, 54, [1 ,4, 6, 7]]

       0,  1 ,   2 ,    --- 3 ---

 If we want to print 6 from the above list

 Then print(python_closet[3][2])

6

Same for number 7  print(python_closet[3][3])

 7

This is because whole second list is a list and, in that list, again we take index values from zero, so if we want a number then bracket of index number of that list (to access elements inside that list), index of the element in that list. This might be somewhat confusing but when u try this on your IDE you will understand.     this is the method for nested ones

*** Accessing elements using negative indexes

We can also access elements using negative indexes like -1 and -2

For example:mypython = [31,65,69,72,84]

 -5 , -4 , -3,-2 ,-1

Here in negative indexes the last number of the list or the index of the last element of list will be -1, as we know indexes start from zero but if we want to use negative index then last element will always have an index of -1 and then it comes in reverse -2 ,-3 and so on

Example:  print(mypython[-1])

                    84

                  Print(mypython[-3])

                  69

*** Accessing elements using slicing

              Pythontuple=(25, 36 ,42 ,79 ,51)

 0 , 1 ,   2 ,  3 ,   4

Ø   using ":" this operator we can print from one particular element to another element.

     

print(pythontuple[0:3])

 

so from the first index to the second index we give it will print all the elements in between that two indexes including first index but it will exclude last index that means in above case it starts printing from index 0 to 2 but it will not print the element in 3rd index which means here the last element

 

 example:  print(pythontuple[0:3])  3rd index will be excluded and till 2nd index elements will be printed

                     25 , 36, 42

                     print(pythontuple[2:4])

                      42 ,

 4th index will be excluded because it is the element given after colon or the last index, it prints from 2nd index and 3rd index and it stops.

 

Ø  If we don’t define any index value before operator then it automatically takes the first

           Element

Example:  print(pythontuple[:3])

 25 , 36 , 42

Here we didn’t mention an index before colon which means I didn’t give any  index number from which index the elements should be printed ,  when we don’t mention any index before colon then computer will automatically consider 0 which means here 0 index so from there it starts printing elements , if we want to print elements from first index then we need not mention zero it will automatically consider zero if we don’t write any number.

What happens if we don’t give last index, I mean what happens when we don’t mention any number after colon

 Example:  print(pythontuple[2:])

Here we didn’t give the last index or index after colon so what happens is it will print till the last element in that tuple

 Print(pythontuple[2:])

42,79,51

So here it prints till last element

What happens if we don’t give both the numbers, which means what happens when we don’t mention the index before colon and after the colon also

Then it prints all the values from first element to last element in that tuple Including first and last element also. so, if you want to print that complete tuple just put: this symbol and print

       Print(pythontuple[:])

       25 ,35 ,42 ,79 ,51

Ø  This is what happens because if we don’t define any index value before the symbol it automatically takes 0 as first index and if we don’t mention any index after symbol it prints till the last element. so, if we don’t give both then all elements in that list or tuple will be printed.

   

**** programming is not about reading some books or remembering some functions ,it is about practicing the more you do the more you will get so just don’t read and stop ,read and try to do code so that u can remember stuff and can understand still better when you try TO DO  them on your  IDE.

YOU CAN DOWNLOAD PDF HERE INDEX IN PYTHON  

My instagram page to Learn Python from basics.

Comments

Popular posts from this blog

Types of Operating System

 Types of Operating System Batch Operating system Time sharing Operating system Distributed Operating system Network Operating system Real Time Operating system Multi programming/Processing/Tasking Operating system Mobile Operating System Batch Operating System               In a Batch operating system the similar jobs are grouped together into batches with the help of some operators and these batches are executed one by one. For example, suppose we have ten programs, some are written in c++ programming language, Some are written in c programming language and the rest are written java programming language. Now, every time we run these programs one by one, we have to load the compiler of that particular language and then run the code. But if we do a batch of these ten programs, The advantage with this approach is that for the c++ batch, you only need to load the compiler once. Similarly, for Java and C , the compiler only needs to load once and the entire batch is executed. The followin

Python Variables

variables,the name itself implies that a variable is something which can change.  Variables are like containers for storing  our data values in memory. we can change the data in variables, in python everything is an object and variables are like names given to the object, by labeling them we can easily access them. In other high level languages like C programming language, c++ programming language, and java programming language. etc.,   we need to declare the datatype of the variable , but in python it will automatically recognize the type of data you are storing in a variable. ** Remember that a variable is a name which is given to a value not to the memory. Declaring a variable is very easy you need to assign a value or a string using '=' operator. >>> p=9 >>> print(p) 9 Assigning a single value to multiple variables We can assign a single value to multiple variables.  >>> a=b=c=56 >>> a 56 >>> b 56 >>> c 56 Assigning diff

DATA TYPES IN PYTHON | (Numeric,Boolean, Dictionary)

Previous Variables in Python  In python programming language everything is an object and data type tells us which types of value the object is. Data types are classified into five types in Python programming language, they are:- Best Laptop for Web Development  in 2021. NUMERIC DATATYPES BOOLEAN DATATYPES DICTIONARY SEQUENCE DATATYPE SET type() method  Type method is used to know what type of data we are using. Numeric Data Types Numeric type consists of numerical value data types for example whenever we assign a decimal value or numbers to variables then it comes under numeric data types. It contains : int float complex type Int or Integer It is a data type where the value given by the user can be positive or negative and can be of any length that means it can be a big integer or small integer but it should not be a fraction or a decimal value. #Python Program to # demonstrate int value p= 5556677 print ( type (p)) q=- 45 print ( type (q)) #output < class 'int' > <

Even or Odd Number and Factorial of a Number in Python Programming

1. To Check if the given number is even or odd If a number is completely divided by 2 then it is even number. When the number is divided by 2, we use the remainder operator %  to calculate the remainder. If the remainder is not zero, the number is odd. SOURCE CODE:- n= int ( input ( "enter any number:- " )) if (n% 2 == 0 ): print (n , 'is even number' ) else : print (n , 'is odd number' ) OUTPUT 1st Run enter any number:- 2 2 is even number 2nd Run enter any number:- 3 3 is odd number In the above code we use "Simple if" statement.  In the above program, we ask the user for input and check if the number is odd or even. 2.Factorial number Factorial of a number defined as the product of all numbers from 1 to given number. For example  5!(5 factorial)=5*4*3*2*1 is 120. Note = factorial of 0 is 1 SOURCE CODE n= int ( input ( "Enter any number:- " )) fact= 1 for i in range ( 1 , n+ 1 ): fact=fact*i print ( 'factorial of' ,

Functions of Operating System

 Operating System is an interface between the user and the hardware and enables the interaction of a computer's hardware and software.          Also, an operating system is software that performs all the basic functions of file management, storage management, process management, managing input and output, and controlling peripherals such as disk, drivers and printers. Functions of Operating System Device Management  File Management Memory Management Process Management Mastermind Storage Management Handling input/output operations Security Functions of Operating System Device Management          Operating system manages device communication via their respective drivers. It does the following activities for device management: Keeps tracks of all devices, input/output controller is responsible for this task. Decides which process gets the device when and for how much time Allocates the device in an efficient way. De-allocates devices. File Management           The operating system al

Computer System Architecture

 Based on number of general purpose processors computer systems are classified into three types.They are: Single Processor Systems. Multiprocessor Systems. Clustered Systems. Single Processor Systems From the name it is very clear that it has only single processor. A major central processing unit that can execute a set of general utility instructions, including user process instructions. There are some other special purpose processors present that performs device specific task. In the single processor system apart from the main central processing unit(CPU) there are also other processors which are present which does not do the general purpose task but it performs some device specific task. It means that we have a certain devices in our computers like keyboard, disk, etc. for all this they may be some microprocessor  which is specified to do a specific task related to that device like for example keyboard, when we press a key on our keyboard the keystroke has to be converted to some kin

Contact form

Name

Email *

Message *