Found 10805 Articles for Python

Print first m multiples of n without using any loop in Python

Pradeep Elance
Updated on 23-Oct-2019 07:48:16

505 Views

In this tutorial, we are going to write a program to find out m multiples of a number n without using loops. For example, we have a number n = 4 and m = 3, the output should be 4, 8, 12. Three multiples of four. Here, the main constraint is not to use loops.We can use the range() function to get the desired output without loops. What is the work of the range() function? range() function returns a range object which we can convert into an iterator.Let's see the syntax of range().Syntaxrange(start, end, step)Algorithmstart - starting number to the ... Read More

Python program to count occurrences of a word in a string

Pradeep Elance
Updated on 23-Oct-2019 07:36:17

3K+ Views

In this tutorial, we are going to write a program that counts the number of times a word occurs in the string. You are given the word and a string, we have to calculate the frequency of the word in the string.Suppose we have a string I am a programmer. I am a student. And the word is. The program that we are going to write will return a number 2 as the word occurs two times in the string.Let's follow the below steps to achieve our goal.Algorithm1. Initialize the string and the word as two variables. 2. Split the string ... Read More

Python program to find the IP Address of the client

Pradeep Elance
Updated on 25-Aug-2023 00:22:01

46K+ Views

In this tutorial, we are going to find the IP address of the client using the socket module in Python. Every laptop, mobile, tablet, etc.., have their unique IP address. We will find it by using the socket module. Let's see the steps to find out the IP address of a device.Algorithm Import the socket module. Get the hostname using the socket.gethostname() method and store it in a variable. Find the IP address by passing the hostname as an argument to the socket.gethostbyname() method and store it in a variable. Print the IP address. Let's write code for the above ... Read More

Sort a list according to the Length of the Elements in Python program

Pradeep Elance
Updated on 23-Oct-2019 06:54:17

5K+ Views

We have a list of strings and our goal is to sort the list based on the length of strings in the list. We have to arrange the strings in ascending order according to their lengths. We can do this using our algorithms or Python built-in method sort() or function sorted() along with a key.Let's take an example to see the output.Input: strings = ["hafeez", "aslan", "honey", "appi"] Output: ["appi", "aslan", "honey", "hafeez"]Let's write our program using sort(key) and sorted(key). Follow the below steps to achieve the desired output using a sorted(key) function.Algorithm1. Initialize the list of strings. 2. Sort ... Read More

Sort Tuples in Increasing Order by any key in Python program

Pradeep Elance
Updated on 23-Oct-2019 06:53:14

95 Views

In this tutorial, we are going to sort a list of tuples in increasing order by nth index key. For example, we have a list of tuples [(2, 2), (1, 2), (3, 1)] then, we have to sort it using 0th index element. The output for that list would be [(1, 2), (2, 2), (3, 1)].We can achieve this by using the sorted method. We have to pass a key while giving the list to the sorted function. Here, the key is the index on which the sorting is based.sorted takes a list and returns that list in ascending order ... Read More

Reverse words in a given String in Python

Pradeep Elance
Updated on 23-Oct-2019 06:48:45

15K+ Views

We are given a string, and our goal is to reverse all the words which are present in the string. We can use the split method and reversed function to achieve the output. Let's see some sample test cases.Input: string = "I am a python programmer" Output: programmer python a am IInput: string = "tutorialspoint is a educational website" Output: website educational a is tutorialspointLet's follow the below steps to achieve our goal.Algorithm1. Initialize the string. 2. Split the string on space and store the resultant list in a variable called words. 3. Reverse the list words using reversed function. ... Read More

How to run Python code on Google Colaboratory?

Pradeep Elance
Updated on 17-Oct-2019 13:25:07

353 Views

Google Colaboratory is a free Jupyter notebook environment that requires no setup and runs entirely in the cloud. It is hosted in google cloud and maintained by Google for the benefit of python coders who want to run and test python scripts using a cloud environment. In this article, we will see how to set up and run a google collaboratory cloud environment.Opening the Python NotebookWe navigate to this google link (https://colab.research.google.com/). It opens up the below screen. We choose the option NEW PYTHON3 NOTEBOOK from the bottom right corner as shown in the diagram below.Python CodeOptionsOn clicking the NEW ... Read More

How to implement Dictionary with Python3

Pradeep Elance
Updated on 17-Oct-2019 13:20:17

183 Views

Dictionaries in python are a type of data structure that map keys to values as a key-value pair. They are one of the frequently used data structures and have many interesting properties. They are presented by enclosing them in a pair of curly brace like below.dict = {'day1':'Mon' ,'day2':'Tue', 'day3':'Wed'}The elements or key-value pairs in the dictionary are represented within single quotes and separated by a colon.Creating a DictionaryWe create a dictionary by assigning values written in the form of a key.ExampleDict1 = {'day1':'Mon' ,'day2':'Tue', 'day3':'Wed'} print(type(dict1)) print(dict1) # Using the dict() method dict2 =dict({('day1', 'Mon'), ('day2', 'Tue'), ('day3', ... Read More

What is Heap queue (or heapq) in Python?

Pradeep Elance
Updated on 17-Oct-2019 13:12:58

160 Views

Heap queue is a special tree structure in which each parent node is less than or equal to its child node.In pythin it is implemented using the heapq module. It is very useful is implementing priority queues where the queue item with higher weightage is given more priority in processing. TheCreate a HeapA heap queue is created by using python’s inbuilt library named heapq. This library has the relevant functions to carry out various operations on heap data structure. Below is a list of these functions.heapify − This function converts a regular list to a heap. In the resulting heap ... Read More

Guide to Install MongoDB with Python in Windows

Pradeep Elance
Updated on 17-Oct-2019 13:08:55

434 Views

Get MongoDBTo install MongoDB on Windows, first, download the latest release of MongoDB from https://www.mongodb.org/downloads. Below is an example of selecting a 64-bit version for windows as a msi installer.Install MongoDBNext, we follow the below steps to install MongoDB. As it is a windows installation using the msi installer, the steps are very straight forward. We choose a complete installation instead of a custom installation.Running the installerHere we run the installer which has been downloaded to our system. The installation starts asking for various steps of confirmation.Choosing a Service ConfigurationWe choose to run the service as a network service user. Clicking ... Read More

Advertisements