Showing posts with label software development. Show all posts
Showing posts with label software development. Show all posts

Saturday, September 23, 2023

11 pip Commands For Python Developers

11 pip Commands For Python Developers

 Python has been preferred over all programming languages for technological advancement. It is one of the most lucrative programming languages that is used as the main programming language by more than 80%of developers. It is used for web development, mobile applications, hardware programming, and many more. 

12-pip-Commands-For-Python-Developers

Pip commands cannot be ignored when we talk about Python. When you get your task done within a second, it feels great, Right! Here comes the role of pip commands in Python which allow users to perform certain operations in a second. Pip is a package management system that is used to manage software packages, it also comes pre-installed with Python. There are certain commands which you should be handy and keep in practice whether you build a small or big application. Let’s discuss those.

Here, we present 11 Pip commands for Python Developers:

1. Install and Uninstall Package

To install and uninstall a certain package as per your requirement, this command plays a major role. You can directly install or uninstall using this command in your command prompt by going into the Python folder. 

pip install package_name

pip uninstall package_name

Here, package_name can be any package, whether it is Pandas, NumPy, etc. 

For Example, pip install pandas: installs pandas and 

                      pip  uninstall pandas: uninstalls pandas.

2. Update Pip

You can also update the pip if it’s not in its updated version. You just need to type pip –version to check whether it has an updated version or not, if not, just update it using the below-given command. Do check this command should be written under the Python folder. 

pip install –upgrade pip

For Example, pip install –upgrade pip updates pip with its newest version. 

3. Upgrade Package

Once, you see the installed packages are outdated, upgrade them using the command pip install package_name –upgrade. Also, keep in mind that this might even have compatibility issues with other packages. 

pip install package_name –upgrade

For Example, pip install pandas –upgrade installs pandas with its updated version containing all functionalities, and libraries. 

4. Update All Packages

To update all packages at once, you need to initially generate a requirements.txt file using the command pip freeze > requirements.txt, this file contains all the packages, and to update all packages you just need to update the requirements.txt file. 

In the list of packages in the requirements.txt file, packages with updated versions will be ignored rest will be updated.

pip freeze > requirements.txt

pip install -r requirements.txt –upgrade

For Example, pip freeze requirements.txt creates a temporary file called requirements.txt which could contain all the packages needed to be installed, then the command pip install -r requirements.txt –upgrade updates all the packages contained in requirements.txt at a time. 

5. Info About an Installed Package

To know about the package’s detail like its version, and features, one must type the command pip show package_name, where package_name could be anything like Pandas, matplotlib, etc. 

Note: In case you don’t have a package installed in your system and you try to update it, it shows an error as “Package Not Found”.

pip show package_name

For Example, pip show pandas display all the details about pandas like their version, features, etc. 

6. Install Editable Mode

Once you install a package and you want to develop it locally, you can install it in its editable mode. It links the package to the specified location which allows you to make changes that will be reflected directly.

This command pip install -e gives you the package in develop mode/ editable mode.

pip install -e

For Example, pip install -e this command opens the package(whichever is installed previously) in an editable form to make changes locally if required. 

7. Generate Requirements.txt File

To merge all your packages in a single file, we generate a requirements.txt file. It helps all the developers to use the package at a time. Using the command, pip freeze > requirements.txt, a requirements.txt file is generated which stores all the required packages.

pip freeze > requirements.txt

For Example, pip freeze>requirements.txt creates a temporary file called requirements.txt to store all the packages collectively in a single place. 

8. List All Installed Packages

This command helps you to display all the packages installed in your system. It shows a list of packages along with their version and path.

Note: Only the packages installed in your system will be displayed here, not those which are installed remotely. 

pip list

For Example, pip list, this command lists all the packages listed in your system which could be anything installed like Pandas, Matplotlib, NumPy.

9. Remove All Packages Installed By Pip

Instead of removing packages one by one, you can directly remove all packages installed by pip to save time using the command pip freeze > requirements.txt && pip uninstall -e requirements.txt -y. Here, it creates a requirements.txt file that contains a list of packages and then uninstalls all of them. 

pip freeze > requirements.txt && pip uninstall -e requirements.txt -y

For Example, pip freeze > requirements.txt && pip uninstall -e requirements.txt -y this command checks the requirements.txt file to check if there’s any package installed, if installed removes it by uninstalling it. 

10. Verify That Installed Packages Have Compatible Dependencies

This command checks whether all installed packages in the requirements.txt file have compatible dependencies. It checks all three conditions i.e., whether dependencies are compatible, whether the package has an updated version or not, and whether a package is missing. 

pip check

For Example, the pip checks this command checks whether all the installed packages contain compatible dependencies according to the package.

11. Install All Dependencies From the requirements.txt File

All the dependencies which are required for the project implementation can be installed using this command, pip install -r requirements.txt. Dependencies contain all the information about the package. 

pip install -r requirements.txt

For Example, pip install -r requirements.txt is needed when you have to install all the dependencies from requirements.txt (which contains all the installed packages).

10 Python In-Built Functions You Should Know

 

10 Python In-Built Functions You Should Know

Python is one of the most lucrative programming languages. According to research, there were approximately 10 million Python developers in 2020 worldwide and the count is increasing day by day. It provides ease in building a plethora of applications, web development processes, and a lot more. When it comes to making a program short and clear, we use in-built functions which are a set of statements collectively performing a task. Using in-built functions in a program makes it beneficial in many ways such as:

  • Makes it less complex.
  • Enhances readability.
  • Reduces coding time and debugging time.
  • Allows re-usage of code.

10-Python-In-Built-Functions-You-Should-Know

Hence, it plays a major role in the development of an application. In python 3, we have 68 in-built functions, some of them are listed below:

1) append()

This method adds an item at the end of the existing list, tuple, or any other set. Then, the length of the list gets increased by one. We can append an item to the list and also list to a list. It adds any data type which is to be added at the end of the list. It has time complexity: O(1).

Syntax: 

append(item) 

where item refers to the item needed to be appended with the existing element.

For Example:

a=[“apple”,” banana”,” mango”,” grapes”]

a.append(“orange”)

print(a)

Output:

[“apple”,” banana”,” mango”,” grapes”,” orange”]

2) reduce()

The reduce() function applies a function of two arguments collectively on a list of objects in succession from left to right to reduce it to one value. It is defined in a functools library. This works better than for loop. 

Syntax: reduce(function, iterable) 

where, function refers to the function which will be used in a program, and iterable refers to the value that will be iterated in the program. 

For Example:  

From functools import reduce

Def sum(a, b):

res=return (sum, [1,2,4,5])

print res

Output: 

12

3) slice()

This function returns the sliced object from a given set of elements. It allows you to access any set of sequences whether it is ta tuple, list, or set. Time complexity of slice() is O(n).

Syntax: 

slice(start, stop, step) 

where start refers to the start index from where you have to copy, stop refers to the index till where you want to slice, and step refers to the count by which you want to skip.

For Example:

a=”Hello World”

y=slice(2,4,1)

print(y)

Output:

 lo

4) sorted()

This function sorts the given element in specified (ascending or descending) order. The set of elements could be a list, tuple, and dictionary. The time complexity of the sorted function is O(n.logn).

Syntax: 

sorted(set of elements) 

where a set of elements refers to the elements which need to be sorted.

For Example:

a=[1,7,3,8]

y=sorted(a)

print(y)

Output: 

[1,3,7,8]

5) split()

This method breaks up the string into a list of substrings, based on the specified separator. It returns strings as a list. By default, whitespace is the separator. Time complexity of split() is O(n).

Syntax: 

split(separator)

where separator refers to the value which is to be split from the given sequence.

For Example: 

a=”HelloWorld”

y=a.split(‘l’)

print(y)

Output:

 ['He','oWor','d']

6) eval()

The eval() function evaluates the given expression whether it is a mathematical or logical expression. If a string is passed through it, it parses the function, compiles it to bytecode, and then returns the output. Since operators have no time complexity therefore eval doesn’t have one. 

Syntax:

 eval(expression)

where the expression could be any operation such as mathematical or logical.

For Example: 

x=6

y=eval(‘x*8’)

print(y)

Output:

 48

7) bin()

This function converts an integer to a binary string that has the prefix 0b. Also, the integer passed could be negative or positive. Its time complexity for a number n is O(log(n))

Syntax:

 bin(integer)

where the integer is any value passed to receive its binary form. 

For Example:

print(bin(8))

Output:  

0b1000

8) map()

This function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple, etc.). It applies a function to all objects in a list. The time of complexity of the map() function is O(n).

Syntax: 

map(function, iterable)

where function refers to the function which will be used in a program, iterable refers to the value that will be iterated in the program. 

For Example:

def add(x):

   return x+2

x = map(add, (3, 5, 7, 11, 13))

print (x)

Output:

(2,7,9,13,15)

9) filter()

This function creates a new iterator from an existing one (such as a list, tuple, or dictionary) that filters elements. It checks whether the given condition is available in the sequence or not and then prints the output. The time complexity of the filter function is O(n).

Syntax: 

filter(function, iterable)

where function refers to the function which will be used in a program, iterable refers to the value that will be iterated in the program. 

For Example:

c = [‘Ant’,’Lizard’,’Mosquito’,’Snake’]      

def vowels(x):

return x[0].lower() in ‘aeiou’

items = filter(vowels, c)

print(list(items))

Output:

 ['Ant']

10) exec()

This function executes the given condition and prints the output in python expression. It executes the program dynamically Python exec() function executes the dynamically created program, which is either a string or a code object. If it is a string, then it is parsed as a Python statement and then executed; else, a syntax error occurs.

Syntax: 

exec(object[, globals[, locals]])

where the object can be a string or object code, globals can be a dictionary and the parameter is optional, and locals can be a mapping object and are also optional.

For Example:

exec(print(sum(2,8)))

Output:

10

So till now you must have got the information about 10 Python in-built functions. With these in-built functions, you can make complex applications very easy. Use it whenever you’re working on any Python application to be handy. 


What is RSA encryption and why it matters to you?

  Do you like stories? We've got a good one. At its core, the story is about RSA encryption, but it has government secrets, a breakthrou...