Posts

Showing posts with the label Python

Dictionaries in Python in 5 mins

Image
What is a Dictionary or Dict? A dictionary is a data type used to store key-value pairs as we have in our English dictionary. It is useful when we want to save the data related to an entity at the same place. It is like saving an excel row in python in a column_header:column_value format E.g If we want to store the details(name, class, marks, address) of a student we can create different variables for this, or we can create only one variable of type dict and save the values in it. Syntax: s1={ ‘Name’:’Tarun’, ‘Class’: 12, ‘Address’:’house number 11 xyz street ‘,           ‘marks’:[90,89,67,84]       } As in the above code, we can see that a dictionary can save multiple data types. Variable “Name” is of type string, “Class” is of type int, “Address” is of type string, “Marks” is of type list.  A dict can also store a dictionary in it and is called Nested Dictionary. Syntax : <variable name>=dict({key:value,key:value}) <vari...

Variable and Data Types in Python

Image
Variables in Python A variable is a named storage location where you can save some value when you execute your code. It takes some space in your RAM and its value persists till the time your code is executing. E.g # Without variables  # Add two numbers print(10+20) #With variables a=20 b=20 print(a+b) The benefit of saving your values to a variable is that the values and calculation (addition in our case addition) can be reused in some further parts of the code. Comments A comment is a line in your code that you don’t want to run but write them just for some reference in your code like explaining what your code is doing. E.g #this line do addition c=a+b In python, comments can be done using a ‘ # ’ symbol in the start of the line. It will inform the interpreter not to execute that line. Data Types in Python A data type in python refers to different formats of data that we can use in python. We are not always working with the same type of data. For example, to store the details of a...

Python 2 Vs Python 3

Image
There has been a huge debate on this topic, " Whether you should learn Python 2 or Python 3". If you are also in this dilemma this post is for you. The first thing you should understand is that if an upgrade is done in anything in the world it is mostly for improving that thing in terms of experience, speed, efficiency, etc. So, this upgrade of Python language is also done to improve the features and functionality of the language but than, Why is this debate all around about Python 2 and Python 3, why can't people just accept this new update rather than debating. To understand this you will have to think deeply and understand that Python has been there for nearly 29 years now(created in 1991), there are large number of legacy systems which are built on Python 2 and there are some feature of Python 3 which are not backward compatible with Python 2. For Example:  The scenario of a simple print statement. In Python 2 print was considered as a statement and in Python 3 it is ...

Why should you learn PYTHON in 2020

Image
What is Coding/Programming? For all those who are very new to coding/programming and are about to start their journey by reading this blog post.  We give instructions to our system using the keyboard or mouse informing the computer to perform some set of tasks like: Printing a doc Writing an email Playing music Dimming the monitor backlight and many more tasks The computer is a hardware device which needs some instructions to run and when we are performing some tasks on the computer, in the background there is some code which is running and telling the computer machine what it has to do. And this happens with the help of some programming language, which acts as an interface between the computer user and hardware. So, coding/programming is an act of writing these instructions using some programming language which when runs on a computer performs some tasks. Pheww...... Programming Languages There is a large variety of programming languages available in the market right now and every...

Writing an Instagram Bot with Python

Image
Growing on Instagram is a difficult and time-consuming job if you are already not a famous personality. I started my food blogging page @_.interwined_dodos._ (do check it out and follow me there) and faced these challenges in growing. Some of the very common challenges I have faced were: Interacting with all community throughout the day is a difficult job Instagram blocks the like, comment, follow and unfollow actions if you are over interactive in a short span of time Following and engaging with new people of same or different community(again a time-consuming job) Being a lazy yet ambitious person, I wanted to grow on Instagram but didn't want to spend much time on it. Here is when my developer mind kicked in and I thought of automating this stuff and my journey of this automation lead me to blogs related to selenium and then to my destination Instapy . InstaPy is a tool developed to automate Instagram tasks using Selenium and the brain. It has a large variety of actions that ca...

Autocomplete Using Redis and Python

Autocomplete using Redis and Python Reading about the use cases of the Redis I came across a use case in which we can implement an autocomplete functionality using it. I am going to show you the code to implement the Autocomplete in under 40 lines of code. Here we go: #redis client for python import redis #flask to expose api's to outside world from flask import Flask,request,jsonify app = Flask("autocomplete") #creating a redis connection r = redis.StrictRedis(host='localhost', port=7001, db=0) #route to add a value to autocomplete list ''' FORMAT: localhost:5000/add?name=<name> ''' @app.route('/add') def add_to_dict(): try: name = request.args.get('name') n = name.strip() for l in range(1,len(n)): prefix = n[0:l] r.zadd('compl',{prefix:0}) r.zadd('compl',{n+"*":0}) return "Added" ...

Extracting text from PDF for NLP tasks

Image
Introduction Natural Language Processing is a task that involves data collection from various sources and not every time one is lucky to get the baked data. Many times you have to extract data from various sources, one of them is Files. In this post, I will be talking specifically about the PDF files. Getting the Guns ready After some exploration on the internet, I came across a python package PyPDF  which sounded a good contender to achieve what we want (text extraction), although it can do more than what we need. This package can also be used to generate, decrypting and merging PDF files, although its usage details are not that clear that's why I thought of writing a post to explain it. Installation pip install PyPDF2 Reading the File and extracting Text import PyPDF2 filename = 'complete path of your pdf file'  #opening the file  pdfFileObj = open(filename,'rb') #creating a pdf reader object pdfReader = PyPDF2.PdfFileReader(pdf...

Information Extraction using GROKS in Python

Image
Groks in Python In my previous blog , I wrote about information extraction using GROKS and REGEX. If you have not read that I will encourage you to go through this blog first. One of the important aspects of any tool is the ability to use it in a different environment and automate the tasks. In this post, we will be looking at the implementation of GROKs in python using pygrok library. By now we know that GROKs are a form of regular expressions that are more readable. Installation Pygrok is an implementation of GROK patterns in python which is available through pip distribution pip install pygrok Usage The library is extremely useful for using the pre-built groks as well as our own custom-built GROKS. Let's start with a very basic example: Parsing Text  #import the package from pygrok import Grok #text to be processed text = ' gary is male, 25 years old and weighs 68.5 kilograms ' #pattern which you want to match pattern = ' % {WORD : ...

Python Interview Questions

Hi Readers, Writing the post after a long time, was looking out for job opportunities. Facing around 10-15 interviews, I have experienced some of the common interview questions asked from a Python Developer and thought of creating a list of these questions. Will create a  separate post for answers as well. Here is the list of interview questions for python interviews: Which version of python have you worked with? What are MAGIC methods? What are DECORATORS? Write a decorator to add a '$' sign to a number. What are the different data types in python? What are mutable and immutable data types? Difference between list and tuples? Which one is faster to access list or a tuple and why? What is list comprehension? What is the use of negative indexing in the list? Why do we need list comprehension? How is it different from creating a list using a loop What are SETS in python? How are they different from LISTS? How SETS are internally stored in python? Which dat...

Pandas - DataFrame

Image
Data Frames In my previous blog , I talked about using the Series Data Structure in Pandas. Feel free to have a  look at that blog. This blog post will be a brief walkthrough of Data Frames. Data Frame is one of the most widely used tool in python by Data Scientists/Analyst. A DataFrame can be considered as a data table. Data Frame provides a wide range of functionality like filtering grouping sorting joins merging Let's start with creating a data frame In [7]: #importing pandas and DataFrame import pandas as pd from pandas import DataFrame #constructor to create a data frame #df=DataFrame( data, index, columns, dtype, copy) #Lists, dict, Series, Numpy ndarrays, Another DataFrame #creating an empty data frame df = DataFrame () #creating a data frame form an array df = DataFrame ([ 1 , 2 , 3 , 4 , 5 , 5 ]) print df 0 0 1 1 2 2 3 3 4 4 5 5 5 In ...