Parsing Command Line arguments

Parsing Command Line arguments allow both the user and other programs to run the Python file passing it data as it starts. Learn more here. Most command line tools rely on arguments passed to the program upon its execution. Instead of prompting for input, these programs expect data or specific flags (which become booleans) to … Read more

PyPI Package Manager

pip: PyPI Package Manager: pip is the most widely-used package manager for the Python Package Index, installed by default with recent versions of Python. PyPI Package Manager: Install Packages To install the latest version of a package named SomePackage: $ pip install SomePackage To install a specific version of a package: $ pip install SomePackage==1.0.4 … Read more

“pip” module Usage: PyPI Package Manager

python

Usage of “pip” module: PyPI Package Manager is helpful when you may need to use pip package manager inside python eg. when some imports may raise. ImportError and you want to handle the exception. If you unpack on Windows Python_root/Scripts/pip.exeinside is stored main.py file, where main class from pip package is imported. This means pip … Read more

Creating Python packages

python

Creating Python packages Introduction Every package requires a setup.py file which describes the package. Consider the following directory structure for a simple package: +– package_name| |+– init.py+– setup.py The init.py contains only the line def foo(): return 100. The following setup.py will define the package: from setuptools import setupsetup(name=’package_name’, # package nameversion=’0.1′, # versiondescription=’Package Description’, … Read more

Checking Path Existence and Permissions

Checking Path Existence and Permissions Parameter Details os.F_OK Value to pass as the mode parameter of access() to test the existence of path. os.R_OK Value to include in the mode parameter of access() to test the readability of path. os.W_OK Value to include in the mode parameter of access() to test the writability of path. … Read more

The name special variable

The name special variable is used to check whether a file has been imported as a module or not, and to identify a function, class, module object by their name attribute. name == ‘main‘ The special variable name is not set by the user. It is mostly used to check whether or not the module … Read more

Context Managers (“with” Statement)

While Python’s context managers are widely used, few understand the purpose behind their use. These statements, commonly used with reading and writing files, assist the application in conserving system memory and improve resource management by ensuring specific resources are only in use for certain processes. This topic explains and demonstrates the use of Python’s context … Read more

Copying data in Python

Copying data CoSpy a dictionary A dictionary object has the method copy. It performs a shallow copy of the dictionary. d1 = {1:[]}d2 = d1.copy()d1 is d2 Falsed1[1] is d2[1]True Performing a shallow copy A shallow copy is a copy of a collection without performing a copy of its elements. import copyc = [[1,2]]d = … Read more

Python Regular Expressions (Regex)

Regular Expressions (Regex): Python makes regular expressions available through the re module. Regular expressions are combinations of characters that are interpreted as rules for matching substrings. For instance, the expression ‘amount\D+\d+’ will match any string composed by the word amount plus an integral number, separated by one or more non-digits, such as:amount=100, amount is 3, … Read more

The Print Function

python

The Print Function Print basics In Python 3 and higher, print is a function rather than a keyword. print(‘hello world!’) out: hello world! foo = 1bar = ‘bar’baz = 3.14print(foo)out: 1 print(bar)out: bar print(baz)out: 3.14 You can also pass a number of parameters to print: print(foo, bar, baz) out: 1 bar 3.14 Another way to … Read more

Python Counting Tutorial | Counting In Python Tutorial

python

Counting: Counting all occurrence of all items in aniterable: collections.Counter from collections import Counterc = Counter([“a”, “b”, “c”, “d”, “a”, “b”, “a”, “c”, “d”])cOut: Counter({‘a’: 3, ‘b’: 2, ‘c’: 2, ‘d’: 2}) c[“a”]Out: 3c[7] # not in the list (7 occurred 0 times!) Out: 0 The collections.Counter can be used for any iterable and counts … Read more

Python Map Function Tutorial | Map Function In Python

Map Function- Parameter Details function function for mapping (must take as many parameters as there are iterables) (positional-only) iterable the function is applied to each element of the iterable (positional-only) *additional_iterables see iterable, but as many as you like (optional, positional-only) Basic use of Map Function, itertools.imap and future_builtins.map The map function is the simplest … Read more

Python Reduce Tutorial | Reduce Tutorial In Python

Reduce function that is used for reducing the iterable (must take two arguments). (positional-only). Learn More about it here. Parameter Details function function that is used for reducing the iterable (must take two arguments). (positional-only) iterable iterable that’s going to be reduced. (positional-only) initializer start-value of the reduction. (optional, positional-only) Overview No import neededNo import … Read more

Python Generators Tutorial | Generators In Python Tutorial

python

Generators Generators are lazy iterators created by generator functions (using yield) or generator expressions (using (an_expression for x in an_iterator)). Generators: Introduction Generator expressions are similar to list, dictionary and set comprehensions, but are enclosed with parentheses. The parentheses do not have to be present when they are used as the sole argument for a … Read more

Plotting with Matplotlib

Plotting with Matplotlib Matplotlib (https://matplotlib.org/) is a library for 2D plotting based on NumPy. Here are some basic examples. More examples can be found in the official documentation (https://matplotlib.org/2.0.2/gallery.html and https://matplotlib.org/2.0.2/examples/index.html) Plotting with Matplotlib: Plots with Common X-axis but di erent Y-axis :Using twinx() In this example, we will plot a sine curve and a … Read more

Indexing and Slicing

Indexing and Slicing is another important part of Python programming as it helps in managing different objects involved in functions. Learn more here. Paramer DescriptionThe object that you want to extract a “sub-object” fromobjThe index of that you want the sub-object to start from (keep in mind that Python is zero-indexed,objstartmeaning that the first item … Read more

pyautogui module

pyautogui module pyautogui is a module used to control mouse and keyboard. This module is basically used to automate mouse click and keyboard press tasks. For the mouse, the coordinates of the screen (0,0) start from the top-left corner. If you are out of control, then quickly move the mouse cursor to top-left, it will … Read more