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

Publicado em: 25 Março 2025
no canal de: 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)


Nesta página do site você pode assistir ao vídeo on-line Python sorted() Function Tutorial | Sort Lists & Strings with Examples for Beginners duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário JR: Educational Channel 25 Março 2025, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 23 vezes e gostou 0 espectadores. Boa visualização!