Skip to main content

Accessing elements of a python List - python list element slicing

 #Accessing elements of a List:

we can access any item from a list using its index number

forward index stat with 0 end with n-1

negative indexing stat with -1

# accessing 2nd element of the list

Ex:

mylist = [10, 20, 'ram', 12.50, 'ravi']

print(mylist[1])  

output: 20

#Negative Indexing:

# accessing last element of the list

mylist = [10, 30, 'ram', 12.50, 'praveen']

print(mylist[-1])


python list element slicing:

syntax for list slicing.

listname[startindex : endindex : step]

Ex:

mylist = [10, 20, 'ram', 12.50, 'prasad', 25, 50]

print(mylist[1:4])

print(mylist[:3])     

print(mylist[::2])    

print(mylist[::-2])  

print(mylist[2:])   

Note :

# slice 1 index from before 4th index element

 # slice first three elements

# with a skip count 2

# reversing the list

 # Stating from 2nd item to last item

#Iterating a List : The objects in the list can be iterated over one by one, by using a for loop

Ex:

mylist = [15, 22, 'mohan', 7.50, 'rani']

for x in mylist:

    print(x)

output:

15

22

mohan

7.50

rani





    


Comments

Popular posts from this blog

Hiring Now: Software Developer-Home Thali Private Limited

Hiring Now: Software Developer-Home Thali Private Limited  Location: Kamatham Complex, 2nd Floor, Near Vijayasree Hospital, Mahatma Gandhi Inner Ring Road, Guntur 💼 Position: Software Developer 💰 Salary: Based on interview performance 🕒 Interview Timings: 11:00 AM – 2:00 PM 3:00 PM – 5:00 PM 📞 Contact: 9494075426 🔧 Key Responsibilities Design, develop, and maintain software applications Analyze user and business requirements to build effective solutions Test, debug, and troubleshoot applications for quality and reliability Update and optimize existing systems Collaborate with designers, testers & project managers Write clean, efficient, and well-documented code Use Git or similar tools for version control Implement security and data-protection practices Stay updated with new tools, frameworks & technologies 🎓 Desired Skills Strong programming knowledge (Java, Python, JavaScript, etc.) Understanding of web technologies ...

Software Jobs - Web Developer jobs in Andhra Pradesh, Rajahmundry,

Software Jobs - Web Developer jobs in Andhra Pradesh, Rajahmundry,  Openings: Web Developer  Qualification: BE,Btech,MCA,MSC Experience: 1-5 years Requirements: Should have good experience as a PHP Application developer Hands on Experience in developing the application using MVC architecture. Good to have knowledge in MySQL, Codeignitor, Zend, CakePHP, Joomla, Magneto and Laravel is a plus. Have experience with REST API, JSON, and SOAP. Expertise in HTML5, CSS, Bootstrap, jQuery, Ajax. Experience in taking ownership of product delivery, code quality and architecture. Should be proactive, goal-oriented, reliable and a have a self-structured way of working. Responsibilities: Creating website layout/user interfaces by using standard HTML/CSS practices. Designing well organized databases. Integrating data from various back-end services and databases Be involved in al...

Adding and changing elements in a tuple using insert() and append methods

  Adding and changing elements in a tuple using insert and append: A list is a mutable type, which means we can add or modify values in it, but tuples are immutable, so they cannot be changed. #how to add new element in tuple object using index based tup=(6,75,66,34,23) print(tup) #print all tuple elements newlist=list(tup) #tuple to list convert newlist.insert(2,40) #add new element in list tup=tuple(newlist) #convert list to tuple print(tup) #print all elements in tuple output: (6, 75, 66, 34, 23) (6, 75, 40, 66, 34, 23) #how to add new element in tuple object using apped() method tup=(6,75,66,34,23) newlist=list(tup) newlist.append(22) tup=tuple(newlist) print(tup) output: (6, 75, 66, 34, 23, 22) #Modify nested items of a tuple: then we can change its values in the case of a nested tuple. Ex: tuple1 = (10, 20, [25, 75, 85]) print(tuple1) tuple1[2][0] = 250 print(tuple1) output: (10,20,[250,75,85])      #python tuples, #python training in guntur, #python jobs in gu...