Skip to main content

Posts

Showing posts from 2022

How To Schedule A Python Script On Windows

Whenever we think about automating something, there are many questions which come to our mind. Like, How will we schedule it? How many times we want to execute it? Is it possible to automate this scheduling part? Well, in this article I’m going to walk you through all those various steps which are required to schedule a Python script on Windows. Step 1: Prepare The Python script Automation begins with the piece of code which will automate something. So, the first step here is to get ready with a Python script which must be in working condition. There is no constraint on how big or small a script has to be, but we need to make sure that the script is doing what it is intended to do. Step 2: Create An Executable Once the script is verified, we need to create an executable or EXE file as this executable file we are going to schedule in our next step. In order to create an executable in Python, we need to install a package named pyinstaller using pip: pip install pyinstaller Once the packa

How To Print Calendar Using Python

In this article, I’ll show you those two lines of Python code, using which you can print calendar of any year. Required Package The pre-requisite to generate calendar is to import the required package named calendar as shown below: from calendar import * Generate Calendar Now to generate a calendar, we need to call constructor and pass in four parameters as shown below: print(calendar(2018,2,1,4)) Here, 2018 is the year for which calendar will be printed 2 signifies width of each character 1 signifies number of lines per week 4 signifies column separation value You can adjust the last three parameters for better spacing in between the text. Output This is how the calendar looks like: I hope you enjoyed generating a calendar of your choice. Make sure to check out my recording explaining each and every parameter discussed in this article:

Sort A Python Dictionary By Value

It is quite common to sort a Python dictionary based on key. But what if you want to perform sorting on the value? In this article, I’m going to show you multiple ways to sort dictionary value and then return the resultant dictionary. Method 1: Using operator The very first method to sort dictionary value is by using sorted(…) function along with operator. So, let’s go ahead and do it as shown below: import operator students = {‘Shweta’:25,’Andy’:30,’Maddy’:3} students = sorted(students.items(), key=operator.itemgetter(1)) print (students) Method 2: Using lambda The second method to sort dictionary value is by using sorted(…) function along with lambda and the code looks as shown below: : import operator students = {‘Shweta’:25,’Andy’:30,’Maddy’:3} students = sorted(students.items(), key=lambda stud:stud[1]) print (students) Output On execution of above two methods, you will get exactly same output. I hope you enjoyed sorting your dictionary values in Python. If you have reached t

What Is REDUCE In Python

Before jumping on to what is reduce, let’s have a quick look at the lines  below :  import operator sum = 0 for n in [1,2,3]: sum = sum + n print ( sum ) You got it right. Here we are taking a collection having three numbers and summing them up. As such, there is nothing wrong with this code but, of course there is a big room for optimization with respect to the number of lines of code we have written, just go get summation. Now the question is, how can we optimize? How can we reduce the number of lines and achieve the same result? Well, the answer is reduce function. What is reduce? Reduce is a function in Python provided by functools . This function takes a collection of values, performs some operation by calling a function and then returns a single value as an output. For example, you can give multiple values as input and perform mathematical calculations on them, you can perform operations on multiple strings, etc. Ways to use reduce There are two ways you can use reduce: Way 1

Get Key Having Maximum Value In Python Dictionary

In this article, I’m going to share a scenario wherein we need to get a key from a Python dictionary, which is holding maximum value. Usually we are required to get a key having maximum key from a dictionary, which, of course, most of us can do very easily. But when it comes to the other way round, it is not that straight forward. I also found that this is one of the hottest questions these days for interviewers :) To achieve the scenario of a dictionary key having maximum value, we can go with two different ways as mentioned below: Method 1: Using itemgetter()  import operator students = {'Shweta':25,'Andy':30,'Maddy':3} v = max(students.items(),key=operator.itemgetter(1))[0] print ( v ) Method 2: Using lambda CODE import operator students = {'Shweta':25,'Andy':30,'Maddy':3} v = max(students.items(),key=lambda x:x[1])[0] print (v) You can use any of these methods and you will get the same output. I hope you enjoyed learning this co

How To Give Name To A Size Column In Python

It is quite common to use size() in Python. size() function gives you a total number of elements. Now, if it is that easy and straight forward, then why am I writing about it? Isn't it? Well, calculating the size or getting the output of the size() function is very straight forward, but when it comes to labeling this value, things become more complicated. Let's understand this with the help of an example. Input Data Here is how our sample data looks like. It is in the form of CSV: Scenario Explained The idea is to group data based on 2 columns named 'type_school' and 'interest' and then show their item count in a separate column. Here is the sample code to achieve this: import pandas as pd df = pd . read_csv ( 'data.csv' ) data = df . groupby ( [ 'type_school' , 'interest' ] ) data [ 'size' ] = data . size ( ) print ( data ) Python Copy The above code looks all good but you will end up seeing an error in it's execu

Getting The Terminal Size In Python

Many times our output doesn’t get fit into the terminal of default size. In that case we have to pull it accordingly and make the size as per our need. Image: Akshay Chauhan on Unsplash In this article, I’ll show you those few lines of Python code using which you can get the size of your terminal programmatically.  Now, before resizing our output we need to know, how to read the terminal size. There are possibly many ways to get the terminal size, i.e. reading environment variables, making low level system calls, etc. In this article, I’ll show you one of the easiest and simplest way to get the terminal size. import os s = os . get_terminal_size ( ) print ( s . columns , s . lines ) Python Copy You can also watch the video recording of this article on my YouTube channel  named Shweta Lodha .

Get Your Horoscope Using Python

If you are a person who believes in horoscopes and also know Python, then this article is for you. Horoscope is a way to forecast future. In this article, I’ll show you how to get your horoscope based on your zodiac sign.  Required Packages Beautiful soup: We will use this to extract data out of HTML and can be installed as shown below: : pip install bs4 Python Copy Requests: We will use this to make HTTP call and can be installed as shown below: : pip install requests Python Copy Website For Reading Horoscope For this article, I’m using a website named https://www.horoscope.com for extracting horoscope information and it looks like this: From above page, you can select your zodiac sign and select the day. Doing this will generate an URL similar to this: https://www.horoscope.com/us/horoscopes/general/horoscope-general-daily-tomorrow.aspx?sign=9 In this URL, tomorrow represents the day for which I’m looking for horoscope and 9 represents the zodiac sign.  Yes, you guessed it correct. I

Get User Name And Password At Runtime Using Python

It is quite common that you need to grab a logged in user name and ask for the password from a user on an application launch. The biggest question that comes here is — Can we grab password from user in plain text? Can password input be in plain text which can be seen while entering? Of course not. As a password is considered as one of the most sensitive data, it can not be exposed so lightly. Due to its sensitivity, either user input has to be masked or it had taken in invisible form. Let’s learn about it in more detail. Like every other Python application, here also we need to import the required module/package and that is getpass . Required Package getpass allows us to prompt for password without having the password displayed on screen or on user’s terminal. Below is the command to install getpass : pip install getpass Python Copy Read User Name getpass has a function named getuser() , which gives us the login name of a currently logged-in user. userName = getpass . getuser ( ) Pyth