Snippets

About

← Back

Control Flow

One-line For Loop & Conditional

Python snippet depicting a one-line loop iterating through a list and processing a conditional statement.

Output

Even: [(0, 'item0'), (2, 'item2'), (4, 'item4')]

Data Operations

List Enumeration & Dictionary

Python snippet for converting a list to enumerated form and to a dictionary.

Output

Enumeration: [(0, 'item0'), (1, 'item1'), (2, 'item2'), (3, 'item3'), (4, 'item4')]
Dictionary: {0: 'item0', 1: 'item1', 2: 'item2', 3: 'item3', 4: 'item4'}

General Operations

__name__ Check

Python snippet to run function within current file while also allowing use of the function to be imported into other files for use outside of this file.

True: Runs the function within the context of this file.

False: Function is being run via another file and therefore out of the context of this file.

Resource: https://www.freecodecamp.org/news/whats-in-a-python-s-name-506262fe61e8/

Formatting

Leading Zeros

Output

010

Output

['000', '001', '002', '003', '004', '005', '006', '007', '008', '009', '010']

Replace Specific Characters

Output

This_is_sample_text

Functions

Variable Number of Arguments

Output

first arg: 1
second arg: Second
third arg: [3, 'Third', [3.0, 33.0, 0.3]]

Environment Variables

Acquisition