Python Snippets 3
Snippets | Descriptions (from 2018, new one is at top)
There are string/list/tuple/set/dict datatype snippet | E.g. Type Out |
---|---|
To get __main__ type def or function or main- | main- |
To see built-in method example type apply- scroll down | apply- |
Tips: to see specific method type example name= | count= |
To see datatype method example type — | str- |
To see datatype method only type — scroll down | -string |
To see python datatype | dtype |
To initialise type initialise snippet | bool init |
To get fileIO type file- then scroll down | file- |
To get block comment type documentation | doc |
To get algorithm snippet like is_prime | algo- |
Built-in methods code snippets | Description |
---|---|
abs | Returns the absolute value of a number |
all | Returns True if all items in an iterable object are true |
any | Returns True if any item in an iterable object is true |
ascii | Returns a readable version of an object. Replaces none-ascii characters with escape character |
bin | Returns the binary version of a number |
bool | Returns the boolean value of the specified object |
bytearray | Returns an array of bytes |
bytes | Returns a bytes object |
callable | Returns True if the specified object is callable, otherwise False |
chr | Returns a character from the specified Unicode code. |
delattr | Deletes the specified attribute (property or method) from the specified object |
dict | Returns a dictionary (Array) |
dir | Returns a list of the specified object’s properties and methods |
divmod | Returns the quotient and the remainder when argument1 is divided by argument2 |
enumerate | Takes a collection (e.g. a tuple) and returns it as an enumerate object |
eval | Evaluates and executes an expression |
exec | Executes the specified code (or object) |
filter | Use a filter function to exclude items in an iterable object |
float | Returns a floating point number |
frozenset | Returns a frozenset object |
getattr | Returns the value of the specified attribute (property or method) |
globals | Returns the current global symbol table as a dictionary |
hasattr | Returns True if the specified object has the specified attribute (property/method) |
hash | Returns the hash value of a specified object |
help | Executes the built-in help system |
hex | Converts a number into a hexadecimal value |
id | Returns the id of an object |
input | Allowing user input |
int | Returns an integer number |
isinstance | Returns True if a specified object is an instance of a specified object |
issubclass | Returns True if a specified class is a subclass of a specified object |
iter | Returns an iterator object |
len | Returns the length of an object |
locals | Returns an updated dictionary of the current local symbol table |
map | Returns the specified iterator with the specified function applied to each item |
max | Returns the largest item in an iterable |
memoryview | Returns a memory view object |
min | Returns the smallest item in an iterable |
next | Returns the next item in an iterable |
object | Returns a new object |
oct | Converts a number into an octal |
open | Opens a file and returns a file object |
ord | Given a string of length one, return an integer representing the Unicode code point of the character when the argument is a unicode object, or the value of the byte when the argument is an 8-bit str. |
pow | Return x to the power y |
Prints to the standard output device | |
property | Gets, sets, deletes a property |
range | Returns a sequence of numbers, starting from 0 and increments by 1 (by default) |
repr | Returns a readable version of an object |
reversed | Returns a reversed iterator |
round | Rounds a numbers |
slice | Returns a slice object |
sorted | Returns a sorted list |
staticmethod | Converts a method into a static method |
str | Returns a string object |
sum | Sums the items of an iterator |
super | Return a proxy object that delegates method calls to a parent or sibling class of type. |
type | Returns the type of an object |
unichr | Return the Unicode string of one character whose Unicode code is the integer i. |
vars | Returns the dict property of an object |
zip | Returns an iterator, from two or more iterators |
import code snippets | Description |
---|---|
import | import module |
function code snippets | Description |
---|---|
def or function | Defining Function |
def=>with_default_value | Defining Function with default values |
file code examples file- | Description |
---|---|
withFile-listFile | List files in that path |
withFile-listFileWithPath | List files with path |
withFile-openFile | Open a file |
withFile-openFileReadLine | Read file line by line |
withFile-openFileReadSingleLine | Read one line of the file |
withFile-appendFile | Write to an Existing File |
withFile-overwriteFile | Open a file and overwrite the content |
withFile-deleteFile | Delete a file |
if/else statement | code snippets | Description |
---|---|---|
if- | if | |
if-elif | or ifelif | if:else if |
if-elif-else | or ifelifelse | if:else if:else |
if-else | or ifelse | if:else |
if-short | or ifshort | ternary if |
else | else |
match aka switch code snippets | Description |
---|---|
switch or match | match Statement |
try catch | code snippets | Description |
---|---|---|
try- | try:except | |
try-f | or tryf | try:except:finally |
try-e | or trye | try:except:else |
try-e-f | or tryef | try:except:else:finally |
for loop code snippets | Description |
---|---|
for- | for Statement |
for loop code examples | |
---|---|
for=> | for example |
for=>through_a_string | for example |
for=>break_statement | for example |
for=>continue_statement | for example |
for=>range_function_1 | for example |
for=>range_function_2 | for example |
for=>range_function_3 | for example |
for=>for_else | for example |
while loop code snippets | Description |
---|---|
while- | while Statement |
while loop code examples | Description |
---|---|
while=> | while example |
while=>break_statement | while example |
while=>continue_statement | while example |
List Comprehensions code snippets | Description |
---|---|
comp=> | List Comprehensions |
lambda code examples | Description |
---|---|
lambda | A lambda function can take any number of arguments, but can only have one expression. |
class code snippets | Description |
---|---|
class=> | python class |
__init__ => | class init method |
__iter__ => | class iter method |
__next__ => | class next method |
class code examples | Description |
---|---|
class=>_1 | oop inheritance example |
class=>inheritance_1 | oop inheritance example |
class=>inheritance_2 | oop inheritance example |
class=>with_attribute_1 | class with attribute example |
class=>with_attribute_2 | class with attribute example |
class=>with_attribute_3 | class with attribute example |
class=>with_method_1 | class with method example |
class=>with_method_2 | class with method example |
class=>encapsulation | class encapsulation example |
class=>polymorphism | class polymorphism example |
For example
Class Template — thanks for snippsat mention f-string
class Parrot: # class attribute species = 'bird' # instance attribute def __init__(self, name, age): self.name = name self.age = age # instantiate the Parrot class blu = Parrot('Blu', 10) woo = Parrot('woo', 15) # access the class attributes print(f'Blu is a ') print(f'Woo is also a ') # access the instance attributes print(f' is years old') print(f' is years old')
Release Notes
This extension aim to cover as many new Python3 method make it as complete as possible, please help to contribute from feedback link above.
For more information — Thanks to
- python documentation
- w3schools
- www.programiz
- python.swaroopch
- pythonforbeginners
- Christian Clauss’s Python Algorithms — open source community for providing algorithms for our snippets
Enjoy! Type less do more
1.0.2
Initial release and update README.md
2.0.2
Updated and maintain in year 2022
3.0.2
Change .format into f-string and remove unnecessary files
3.3.5
change . become — due to vscode clash ex apply. become apply-
3.3.11
added PyMySQL, Algo, Random snippet by Lakshmikanth
3.3.12
remove dot in built-in methods for good, to see type — then scroll down. Tips, tick in setting: Python › Analysis: Complete Function Parens
python snippets
| dictionary methods code snippets | Description | | — | — | | .clear | Removes all the elements from the dictionary | | .copy | Returns a copy of the dictionary | | .fromkeys | Returns a dictionary with the specified keys and values | | .get | Returns the value of the specified key | | .items | Returns a list containing the a tuple for each key value pair | | .keys | Returns a list containing the dictionary’s keys | | .pop | Removes the element with the specified key | | .popitem | Removes the last inserted key-value pai | | .setdefault | Returns the value of the specified key. If the key does not exist: insert the key, with the specified value | | .update | Updates the dictionary with the specified key-value pairs | | .values | Returns a list of all the values in the dictionary |
dictionary methods code examples | Description |
---|---|
dictionary.clear=> | An example for using clear |
dictionary.copy=> | An example for using copy |
dictionary.fromkeys=> | An example for using fromkeys |
dictionary.get=> | An example for using get |
dictionary.items=> | An example for using items |
dictionary.keys=> | An example for using keys |
dictionary.pop=> | An example for using pop |
dictionary.popitem=> | An example for using popitem |
dictionary.setdefault=> | An example for using setdefault |
dictionary.update=> | An example for using update |
dictionary.values=> | An example for using values |
| tuple methods code snippets | Description | | — | — | | .count | Returns the number of times a specified value occurs in a tuple | | .index | Searches the tuple for a specified value and returns the position of where it was found |
tuple methods code examples | Description |
---|---|
tuple.count=> | An example for using count |
tuple.index=> | An example for using index |
| for loop code snippets | Description | | — | — | | for | for Statements |
for loop code examples | |
---|---|
for=> | An example for using for |
for=>through_a_string | An example for using for |
for=>break_statement | An example for using for |
for=>continue_statement | An example for using for |
for=>range_function_1 | An example for using for |
for=>range_function_2 | An example for using for |
for=>range_function_3 | An example for using for |
for=>for_else | An example for using for |
for=>for_else | An example for using for |
| while loop code snippets | Description | | — | — | | while | while Statements | | while_else | while Statements |
while loop code examples | Description |
---|---|
while=> | while Statements |
while=>break_statement | while Statements |
while=>continue_statement | while Statements |
if/else statement code snippets | Description |
---|---|
if | if Statements |
ifelif | if/else if Statements |
ifelifelse | if/else if/else Statements |
ifelse | if/else Statements |
ifshort | ifshort Statements |
else | else Statements |
| class code snippets | Description | | — | — | | | python class | | init=> | class init method | | iter=> | class iter method | | next=> | class next method |
class code examples | Description |
---|---|
class=>_1 | oop inheritance example |
class=>inheritance_1 | oop inheritance example |
class=>inheritance_2 | oop inheritance example |
class=>with_attribute_1 | class with attribute example |
class=>with_attribute_2 | class with attribute example |
class=>with_attribute_3 | class with attribute example |
class=>with_method_1 | class with method example |
class=>with_method_2 | class with method example |
class=>encapsulation | class encapsulation example |
class=>polymorphism | class polymorphism example |
import code snippets | Description |
---|---|
import=> | import module |
List Comprehensions code snippets | Description |
---|---|
comp=> | List Comprehensions |
List Comprehensions code examples | Description |
---|---|
list.comp=>_1 | An example for using list comprehension |
list.comp=>_2 | An example for using list comprehension |
list.comp=>_3 | An example for using list comprehension |
list.comp=>_4 | An example for using list comprehension |
list.comp=>_5 | An example for using list comprehension |
lambda code examples | Description |
---|---|
lambda | A lambda function can take any number of arguments, but can only have one expression. |
function code snippets | Description |
---|---|
def=> | Defining Function |
def=>with_default_value | Defining Function wqith default values |
function=> | Defining Function |
file code examples | Description |
---|---|
file=>openFile | open a file |
file=>openFileReadLine | Read one line of the file |
file=>writeExistFile | Write to an Existing File |
file=>writeOwerWrite | Open a file and overwrite the content |
file=>createFileIfDoesNotExist | Create a new file if it does not exist |
file=>createFile | Create a new file |
file=>deleteFile | delete a file |
For example
class Parrot: # class attribute species = 'bird' # instance attribute def __init__(self, name, age): self.name = name self.age = age # instantiate the Parrot class blu = Parrot('Blu', 10) woo = Parrot('woo', 15) # access the class attributes print('Blu is a <>'.format(blu.__class__.species)) print('Woo is also a <>'.format(woo.__class__.species)) # access the instance attributes print('<> is <> years old'.format( blu.name, blu.age)) print('<> is <> years old'.format( woo.name, woo.age))
Release Notes
Users appreciate release notes as you update your extension.
For more information
1.0.0
Initial release of python code snippets