B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Python List Tutorial with Examples and Functions | LiveCodeProgramming
Python Installation First Python Program Control Flow if...else nested if else match-case logical-operators for loops while loops break / continue Functions Defining Functions Function Arguments Function return value Lambda Function Recursion Exception Handling Exception Handling Try Except Else and Finally Raise Exception Custom Exceptions File Handling File in python Read File Write File Append File Delete File Exception Handling

Python Lists

A list in Python is a collection of items that are ordered, changeable (mutable), and allow duplicate values. Lists are written with square brackets [].

Python में list एक ऐसा collection है जिसमें items ordered होते हैं, उन्हें बदला जा सकता है (mutable होते हैं), और duplicate values भी हो सकती हैं। इसे [] में लिखा जाता है।

Why and When to Use Lists?
  • Store multiple values in a single variable
  • Good for collections like student names, marks, etc.
  • Can easily loop through and modify elements
  • एक ही variable में कई values रखने के लिए
  • जैसे student के नाम या marks को list में store कर सकते हैं
  • Elements को loop और modify करना आसान होता है
Basic Example
fruits = ["apple", "banana", "cherry"]
print(fruits)
Output:
["apple", "banana", "cherry"]
Useful List Functions in Python
  1. append(): Add item to end
    fruits = ["apple", "banana", "cherry"]
    fruits.append("orange")
    print(fruits)
    Output:
    ["apple", "banana", "cherry", "orange"]
  2. insert(): Add at specific index
    fruits = ["apple", "banana", "cherry"]
    fruits.insert(1, "grape")
    print(fruits)
    Output:
    ["apple", "grape", "banana", "cherry"]
  3. remove(): Remove specific item
    fruits = ["apple", "banana", "cherry"]
    fruits.remove("banana")
    print(fruits)
    Output:
    ["apple", "cherry"]
  4. pop(): Remove last item (or by index)
    fruits = ["apple", "banana", "cherry"]
    fruits.pop()
    print(fruits)
    Output:
    ["apple", "banana"]
  5. sort(): Sort list alphabetically or numerically
    fruits = ["banana", "apple", "cherry"]
    fruits.sort()
    print(fruits)
    Output:
    ["apple", "banana", "cherry"]
  6. reverse(): Reverse list order
    fruits = ["apple", "banana", "cherry"]
    fruits.reverse()
    print(fruits)
    Output:
    ["cherry", "banana", "apple"]
  7. len(): Get number of items
    fruits = ["apple", "banana", "cherry"]
    print(len(fruits))
    Output:
    3