Found 10805 Articles for Python

Basic Slicing and Advanced Indexing in NumPy Python

Samual Sam
Updated on 30-Jul-2019 22:30:24

378 Views

Indexing of ndarrays can be done using the standard python x[obj] syntax, where x is the array and obj the selection.There are three kinds of indexing available −field accessbasic slicingadvanced indexingWhat kind of indexing will be there depends on obj. In this section, we are going to mainly concentrate on basic slicing & advanced indexing.We can divide advanced indexing into two parts −Integer array indexingBoolean indexingBasic SlicingPython basic concept of slicing is extended in basic slicing to n dimensions. As with python slice object which is constructed by giving a start, stop & step parameters to slice function. To get ... Read More

Differences between Flatten() and Ravel() in Numpy

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

198 Views

There are numerous ways to create a numpy array. Numpy provides two different kinds of ways to convert a ndarray to 1Darray: that is using the flatten() method and the other using the ravel() method.Example#Import required library, numpy import numpy as np #create an array from a list arr = np.array( [ (2, 7, 3, 4), (5, 6, 9, 1)]) #flatten_output print(arr.flatten()) #ravel_output print(arr.ravel())Output[2 7 3 4 5 6 9 1] [2 7 3 4 5 6 9 1]Now above we can see that both functions return the same list, so the question arises, why two methods for the same ... Read More

Replacing strings with numbers in Python for Data Analysis

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

696 Views

Sometimes there is a requirement to convert a string to a number (int/float) in data analysis. For each string, we can assign a unique integer value to differentiate string values.For this, we use the data in Comma Separated Values(CSV) files. Say we have an excel file containing CSV data as follow −CompanyIndustryRecommendationHDFC BankFinanceHoldApolloHealthcareBuyHeroAutomobileUnderperformYes BankFinanceHoldM&MAutomobileUnderperformFortisHealthcareBuyMarutiAutomobileUnderperformAbove is just a few lines from a large dataset, we need to give different recommendation .i.e. Buy, Hold, Underperform etc. integer values, which will link to our metadata. So for the above input, our expected output will be something like −CompanyIndustryRecommendationHDFC BankFinance2ApolloHealthcare1HeroAutomobile3Yes BankFinance2M&MAutomobile3FortisHealthcare1MarutiAutomobile3Here is a way ... Read More

Garbage Collection in Python

Samual Sam
Updated on 30-Jul-2019 22:30:24

752 Views

Python memory management is straight forward. You don’t need to worry about memory management, as memory allocation and deallocation is automatic. one of the mechanisms of memory management is garbage collection. Let’s understand different aspects of garbage collection, Garbage collectionIt is the process by which shared computer memory is cleaned which is currently being put to use by a running program when that program no longer needs that memory. With garbage collection, that freed memory can be used by another program.There are two methods used by python for memory management −Reference countingGarbage collectionPython’s garbage collection is automatic but in some ... Read More

Pattern matching in Python with Regex

karthikeya Boyini
Updated on 30-Jun-2020 09:16:45

6K+ Views

What is Regular Expression?In the real world, string parsing in most programming languages is handled by regular expression. Regular expression in a python programming language is a method used for matching text pattern.The “re” module which comes with every python installation provides regular expression support.In python, a regular expression search is typically written as:match = re.search(pattern, string)The re.search() method takes two arguments, a regular expression pattern and a string and searches for that pattern within the string. If the pattern is found within the string, search() returns a match object or None otherwise. So in a regular expression, given a ... Read More

Python Tricks for Competitive Coding

Samual Sam
Updated on 30-Jul-2019 22:30:24

2K+ Views

Python is one of the preferred languages among coders for most of the competitive programming challenges. Most of the problems are easily computed in a reasonable time frame using python.For some of the complex problem, writing fast-enough python code is often a challenge. Below are some of the pythonic code constructs that help to improve the performance of your code in competitive coding −1. Strings concatenation: Do not use the below construct.str1 = "" some_list = ["Welcome ", "To ", "Tutorialspoint "] for x in some_list:    str1 += x print(str1)Above method gives huge time overhead.Instead, try to use this ... Read More

Why is python best suited for Competitive Coding

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

178 Views

Competitive programming is generally referred to coding to make use of efficient algorithms using an appropriate data structure. They test the skills of programmers on many levels.With the help of algorithms and data structures, you have to solve a hypothetical programming problem posed to you by applying different logics. You not only have to solve the problem but you have to come up with a very efficient solution, which is having a good time and space complexity.Example of a problem statement for what is called competitive programming might be −You are given a string s of length n consisting only ... Read More

Python object serialization

Samual Sam
Updated on 30-Jun-2020 09:11:53

562 Views

Serialization is a process in which an object is transformed into a format that can be stored/save (in a file or memory buffer), so we are able to deserialize it later and recover the original content/object from the serialized format. We are going to use a python pickle module to do all these operations.What is pickling?Python pickle module is used for serializing and de-serializing python object structures. The process to converts any kind of python objects (list, dict, etc.) into byte streams (0s and 1s) is called pickling or serialization or flattening or marshaling. We can convert the byte stream ... Read More

Performing Google Search using Python code?

karthikeya Boyini
Updated on 30-Jun-2020 09:13:14

1K+ Views

In this article, we will try to do google search using python code, this comes handy in case you are working on a python project and you need to access some data from the web and the search result(from the web) is going to be used inside your project.Prerequisite –You must have python installed on your system.Install google module. You can use pip to install google module like below −C:\Users\rajesh>python -m pip install google Collecting google Downloading https://files.pythonhosted.org/packages/c8/b1/887e715b39ea7d413a06565713c5ea0e3132156bd6fc2d8b165cee3e559c/google-2.0.1.tar.gz Requirement already satisfied: beautifulsoup4 in c:\python\python361\lib\site-packages (from google) (4.6.0) Installing collected packages: google Running setup.py install for google ... done Successfully installed ... Read More

How to generate byte code file in python

Samual Sam
Updated on 30-Jul-2019 22:30:24

1K+ Views

All python program automatically compiles your source code to compile code also called as byte code, before executing it.Whenever we import a module for the first time or when your source file is a new file or we have an updated file then the recently compiled file, a .pyc file will be created on compiling the file in the same directory as the .py file (from python 3- you might see .pyc file is in a subdirectory called __pycache__ instead in the same directory as your .py file). This is a time-saver mechanism as it prevents python to skip the ... Read More

Advertisements