Afternerd Python Part(1)

This article is about python programming skills from afternerd

Python Lambdas

Python Lambdas Explained (With Examples)

Why we need lambdas (difference between traditional functions)

lambdas are only useful when you want to define a one-off function.
In other words, a function that will be used only once in your program. These functions are called anonymous functions.


Python Enumerate

Python Enumerate Explained (With Examples)

How do I get the index of an element while I am iterating over a list?

The pythonic way is to use built-in enumerate function

enumerate list

1
2
3
L = ['apples', 'bananas', 'oranges']
for idx, val in enumerate(L):
print("index is %d and value is %s" % (idx, val))

other enumerate

  • enumerate tuple
  • enumerate list of tuples
  • enumerate a string
  • Enumerate with a Different Starting Index

Python: How to Sort a List?


Python: How to Sort a List? (The Right Way)

Two ways:

  • sort: object method
  • sorted: built-in function

Contents:

  • Sorting a list of numbers
  • Sorting a list of strings
  • Sorting a list of strings in a case insensitive manner
  • Sorting a list of tuples
  • Sorting a list of tuples by the second element
  • Sorting a list of objects

Useful parameters:

  • reverse
  • key

Python Lists: Append vs Extend


Python Lists: Append vs Extend (With Examples)

append

The append method is used to add an object to a list.

This object can be of any data type, a string, an integer, a boolean, or even another list.

extend

extend is another very common list method.

Unlike append that can take an object of any type as an argument, extend can only take an iterable object as an argument.

0%