Python snippets
- Basics
- Make script directly executable with a Python interpreter
- Get all properties and methods of an object
- Swap values of two variables (with multiple assignment)
- Swap values of two variables (with a third variable)
- Check a variable is not None
- Ternary conditional operator
- for statement
- for statement example
- Iterate over a sequence of numbers (range function)
- Iterate over a sequence of numbers (range function)
- if statements
- while loop
- while loop example
- Fibonacci series
- break and continue statements on loops
- Command line arguments (sys.argv)
- Strings
- Lists
- Accessing and slicing a list
- Looping over lists
- Adding elements
- Removing elements
- Sorting and reversing lists
- Simple statistics on numerical list
- Shuffle a list
- Randomly select an item from the list
- Combine lists with zip iterator
- Unpacking list of lists with zip and * operator
- List comprehension
- List comprehension with condition
- List comprehension (nested loops)
- List comprehension with if..else
- Transpose a matrix with list comprehension
- Dictionaries
- Convert two lists into a dictionary (with zip)
- Convert two lists into a dictionary (with dict comprehension)
- Looping through all key-value pairs
- Looping through all the keys
- Looping through all the values
- Removing key-value pair
- Merging two dictionaries
- Dictionary comprehension
- Update a dictionary’s keys with comprehension
- Lambdas
- Map, Filter, Reduce
- map syntax
- map - Compute squares values of a list
- map - Convert a list with strings to uppercase
- map - Element-wise addition of 2 lists
- filter syntax
- filter - Get list of even numbers from a range
- filter - Get only strings with a length of 3 characters from a list
- reduce syntax
- reduce - Sum up elements of a list
- reduce - Sum up elements of a list (with an initial value)