Found 10400 Articles for Python

Replacing strings with numbers in Python for Data Analysis

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

878 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

1K+ 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

7K+ 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

246 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

670 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

Python program to check if a number is Prime or not

AmitDiwan
Updated on 22-Aug-2023 00:23:58

213K+ Views

A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. Any whole number which is greater than 1 and has only two factors that is 1 and the number itself, is called a prime number Let’s say the following is our input − 7 The output should be as follows − Prime Number Check if a number is Prime or not Let us check if a number if a Prime number or not using the for loop − Example # Number to be checked for prime n ... Read More

try and except in Python

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

783 Views

To use exception handling in python, we first need to catch the all except clauses.Python provides, “try” and “except” keywords to catch exceptions. The “try” block code will be executed statement by statement. However, if an exception occurs, the remaining “try” code will not be executed and the except clause will be executed.try:    some_statements_here except:    exception_handlingLet’s see above syntax with a very simple example − Live Demotry:    print("Hello, World!") except:    print("This is an error message!")OutputHello, World!Above is a very simple example, let’s understand the above concept with another example − Live Demoimport sys List = ['abc', 0, 2, ... Read More

Advertisements