Home Posts Tags Post Search Tag Search

Post 82

Python 01 - Getting Started Again

Published on: 2025-10-23 Tags: Hacker Rank, Python
So a few things that I learned while learning python again.

As with Elixir you don't need to declare what the variable is.

Any statement needs to have the : at the end but not within the code within. IE

if condtion:
     statement
elif condition 2:
    statement 2
else:
    statement 3

Functions can be defined very similarly to Elixir with def and then name but you will need to have a return line at the end

def function_name(param):
    statements

    return value

You can describe a lambda function like this: the x before the : is the param.
f = lambda x: x * 2

We needed to sort by count then alphabet so we used this function
sorted(counts.items(), key=lambda item: (-item[1], item[0]))

counts.items() → get all the key–value pairs from the counts dictionary as tuples (key, value).

sorted() → call Python’s built-in function that returns a new sorted list.

key= → a special parameter that tells sorted() which value to use when comparing each item.

lambda item: (-item[1], item[0]) → a short, inline function (anonymous) that:

Takes a tuple item = (key, value)

Returns a tuple (-value, key) for sorting

Sorting behavior:

-item[1] → sorts by value in descending order (because negative numbers reverse the default ascending sort)

item[0] → if two values are the same, sorts their keys alphabetically as a tiebreaker