Python Lists Part 2, A Deeper Dive in English & Hindi
- Get link
- X
- Other Apps
Advanced Python Lists
Looping, Comprehensions, and More
एडवांस्ड पाइथन लिस्ट्स
लूपिंग, कॉम्प्रिहेंशन, और भी बहुत कुछ
1. Looping Through a List
To perform an action on every item in a list, you use a `for` loop. This is one of the most common things you'll do with lists.
fruits = ["apple", "banana", "cherry"]
# Loop through the list and print each fruit
for fruit in fruits:
print(fruit)
# Output:
# apple
# banana
# cherry
2. Checking if an Item Exists
You can easily check if a specific item is in a list using the `in` keyword. It returns `True` or `False`.
fruits = ["apple", "banana", "cherry"]
# Check if "banana" is in the list
if "banana" in fruits:
print("Yes, banana is in the list.") # This will be printed
# Check if "mango" is in the list
if "mango" not in fruits:
print("No, mango is not in the list.") # This will be printed
3. List Comprehensions (A Powerful Shortcut)
List comprehensions provide a short and elegant way to create a new list based on the values of an existing list.
The Old Way (using a loop):numbers = [1, 2, 3, 4, 5]
squares = []
for n in numbers:
squares.append(n * n)
# squares is now [1, 4, 9, 16, 25]
The New Way (using a list comprehension):
numbers = [1, 2, 3, 4, 5] squares = [n * n for n in numbers] # squares is now [1, 4, 9, 16, 25]
It's cleaner and often faster than a standard loop!
4. Combining and Repeating Lists
You can use the `+` operator to combine two lists and the `*` operator to repeat a list's content.
# Combining (Concatenation) list1 = [1, 2, 3] list2 = ['a', 'b', 'c'] combined = list1 + list2 print(combined) # Output: [1, 2, 3, 'a', 'b', 'c'] # Repeating repeated = [0] * 4 print(repeated) # Output: [0, 0, 0, 0]
1. लिस्ट में लूपिंग
लिस्ट के प्रत्येक आइटम पर कोई क्रिया करने के लिए, आप `for` लूप का उपयोग करते हैं। यह लिस्ट के साथ किए जाने वाले सबसे आम कामों में से एक है।
fruits = ["apple", "banana", "cherry"]
# लिस्ट में लूप करें और प्रत्येक फल को प्रिंट करें
for fruit in fruits:
print(fruit)
# आउटपुट:
# apple
# banana
# cherry
2. जांचना कि कोई आइटम मौजूद है या नहीं
आप `in` कीवर्ड का उपयोग करके आसानी से जांच सकते हैं कि कोई विशिष्ट आइटम लिस्ट में है या नहीं। यह `True` या `False` लौटाता है।
fruits = ["apple", "banana", "cherry"]
# जांचें कि क्या "banana" लिस्ट में है
if "banana" in fruits:
print("हाँ, केला लिस्ट में है।") # यह प्रिंट होगा
# जांचें कि क्या "mango" लिस्ट में नहीं है
if "mango" not in fruits:
print("नहीं, आम लिस्ट में नहीं है।") # यह प्रिंट होगा
3. लिस्ट कॉम्प्रिहेंशन (एक शक्तिशाली शॉर्टकट)
लिस्ट कॉम्प्रिहेंशन मौजूदा लिस्ट के मानों के आधार पर एक नई लिस्ट बनाने का एक छोटा और सुंदर तरीका प्रदान करते हैं।
पुराना तरीका (लूप का उपयोग करके):numbers = [1, 2, 3, 4, 5]
squares = []
for n in numbers:
squares.append(n * n)
# squares अब [1, 4, 9, 16, 25] है
नया तरीका (लिस्ट कॉम्प्रिहेंशन का उपयोग करके):
numbers = [1, 2, 3, 4, 5] squares = [n * n for n in numbers] # squares अब [1, 4, 9, 16, 25] है
यह एक मानक लूप की तुलना में अधिक स्वच्छ और अक्सर तेज़ होता है!
4. लिस्ट्स को जोड़ना और दोहराना
आप दो लिस्ट्स को जोड़ने के लिए `+` ऑपरेटर और एक लिस्ट की सामग्री को दोहराने के लिए `*` ऑपरेटर का उपयोग कर सकते हैं।
# जोड़ना (Concatenation) list1 = [1, 2, 3] list2 = ['a', 'b', 'c'] combined = list1 + list2 print(combined) # आउटपुट: [1, 2, 3, 'a', 'b', 'c'] # दोहराना repeated = [0] * 4 print(repeated) # आउटपुट: [0, 0, 0, 0]
- Get link
- X
- Other Apps
Comments
Post a Comment