Mastering Python Lists: A Ready Reckoner

🚀 Day 6 of my 30 Days Python Challenge: Mastering Python starts with mastering lists! Here’s your ready reckoner with real code + outputs and use cases—just for you! A Python list is an ordered, dynamic, and mutable collection that can hold any type of data—like numbers, strings, or even other lists. 💡 Why use lists? Flexible for all data types Store, access, edit, or remove elements easily Used everywhere—from automation to data science! 🎯 Let’s see real list magic in Python: # 1️⃣ Create & check type my_list = [1, 2, 3, 4, 5] print(type(my_list)) # Output: <class 'list'> ###################################### # 2️⃣ List with multiple data types mixed = [10, "Krishna", 2.5, True] print(mixed) # Output: [10, 'Krishna', 2.5, True] ###################################### # 3️⃣ Access by index fruits = ['Apple', 'Banana', 'Mango'] print(fruits[0]) # Output: Apple print(fruits[2]) # Output: Mango ###################################### # 4️⃣ Slicing numbers = [1, 2, 3, 4, 5] print(numbers[1:4]) # Output: [2, 3, 4] ###################################### # 5️⃣ Change value numbers = [10, 20, 30, 40] numbers[2] = 99 print(numbers) # Output: [10, 20, 99, 40] ###################################### # 6️⃣ Add elements (append, insert, extend) nums = [1, 2, 3] nums.append(4) print(nums) # Output: [1, 2, 3, 4] nums.insert(1, 99) print(nums) # Output: [1, 99, 2, 3, 4] nums.extend([7, 8]) print(nums) # Output: [1, 99, 2, 3, 4, 7, 8] ###################################### # 7️⃣ Remove element (pop) colors = ['red', 'green', 'blue'] removed = colors.pop() print(removed) # Output: blue print(colors) # Output: ['red', 'green'] ###################################### # 8️⃣ Nested lists matrix = [ [1, 2, 3], [4, 5, 6] ] print(matrix[1][2]) # Output: 6 ###################################### # 9️⃣ List functions (len, max, min) data = [7, 12, 4, 9, 21] print(len(data)) # Output: 5 print(max(data)) # Output: 21 print(min(data)) # Output: 4 ###################################### # 🔟 Methods (reverse, copy, count) nums = [5, 2, 5, 7] nums.reverse() print(nums) # Output: [7, 5, 2, 5] copy_nums = nums.copy() print(copy_nums) # Output: [7, 5, 2, 5] print(nums.count(5)) # Output: 2 ###################################### 🔥 Real-world uses: Data science: holding survey results, experiment values Web: lists of products, users ML/AI: feature sets, predictions Automation: batch rename, organize files and folders ❓ Your turn: What’s the most creative way you used a Python list? Share your favorite tip, question, or challenge below 👇 Want more Python details? Comment "YES" & save this post! #Python #DevOps #LearnPython #CodingTips #Automation #DataScience #AIBasics #Programming #LinkedInLearning Follow for more actionable DevOps and Python tips with real-world examples!

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories