Python sorted() Function Tutorial | Sort Lists & Strings with Examples for Beginners

Опубликовано: 25 Март 2025
на канале: JR: Educational Channel
23
0

Learn Python’s `sorted()` function in this beginner-friendly tutorial! The `sorted()` function creates a new sorted list from any iterable—like lists, tuples, or strings—without changing the original. We’ll sort numbers `[5, 2, 9, 1, 5, 6]` in descending order and sort words `["ad", "bingo", "cake"]` by length in reverse order using `key=len`. Perfect for Python beginners, coding newbies, or anyone wanting to master sorting in Python with practical examples!

🔍 *What You'll Learn:*
How `sorted()` works with iterables (lists, strings)
Sorting in ascending and descending order (`reverse=True`)
Using `key` for custom sorting (e.g., by length)
Why `sorted()` doesn’t modify the original data

💻 *Code Used in This Video:*
Sort numbers in descending order
numbers = [5, 2, 9, 1, 5, 6]
print(sorted(numbers, reverse=True)) # Output: [9, 6, 5, 5, 2, 1]
print(numbers) # Output: [5, 2, 9, 1, 5, 6] (original unchanged)

Sort strings by length in descending order
words = ["ad", "bingo", "cake"]
print(sorted(words, key=len, reverse=True)) # Output: ['bingo', 'cake', 'ad']

🌟 *Why Learn sorted()?*
Sorting is a core skill in coding—used in data analysis, algorithms, and interviews! We’ll show how `sorted()` keeps your original data safe, explain the `key=len` trick for custom sorting, and compare it to `.sort()` (which modifies lists directly). Master this, and you’ll handle lists, strings, and more like a pro—great for projects, coding challenges, or just organizing data efficiently!

📚 *Who’s This For?*
Python beginners learning sorting
Coders prepping for interviews or challenges
Anyone curious about Python’s built-in functions

👍 Like, subscribe, and comment: What Python function should we explore next? Next up: More list tricks—stay tuned!

#PythonTutorial #SortedFunction #PythonSorting #LearnPython #PythonBeginner

sorted() - Returns a new sorted list from an iterable without modifying the original

Sort numbers in descending order
numbers = [5, 2, 9, 1, 5, 6]
print(sorted(numbers, reverse=True)) # Output: [9, 6, 5, 5, 2, 1]
print(numbers) # Output: [5, 2, 9, 1, 5, 6] (original unchanged)

Sort strings by length in descending order
words = ["ad", "bingo", "cake"]
print(sorted(words, key=len, reverse=True)) # Output: ['bingo', 'cake', 'ad']
'bingo' (5 letters), 'cake' (4 letters), 'ad' (2 letters)


На этой странице сайта вы можете посмотреть видео онлайн Python sorted() Function Tutorial | Sort Lists & Strings with Examples for Beginners длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь JR: Educational Channel 25 Март 2025, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 23 раз и оно понравилось 0 зрителям. Приятного просмотра!