Found 10805 Articles for Python

Python program to Count words in a given string?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

2K+ Views

Lets suppose we have a ‘string’ and the ‘word’ and we need to find the count of occurence of this word in our string using python. This is what we are going to do in this section, count the number of word in a given string and print it.Count the number of words in a given stringMethod 1: Using for loop#Method 1: Using for looptest_stirng = input("String to search is : ") total = 1 for i in range(len(test_stirng)):    if(test_stirng[i] == ' ' or test_stirng == '' or test_stirng == '\t'):       total = total + ... Read More

Increment and Decrement Operators in Python?

Arjun Thakur
Updated on 23-Aug-2023 14:01:38

58K+ Views

Python does not have unary increment/decrement operator (++/--). Instead to increment a value, usea += 1to decrement a value, use −a -= 1Example>>> a = 0 >>> >>> #Increment >>> a +=1 >>> >>> #Decrement >>> a -= 1 >>> >>> #value of a >>> a 0Python does not provide multiple ways to do the same thing .However, be careful if you are coming from a language like C, Python doesn’t have "variables" in the sense that C does, instead python uses names and objects and in python integers (int’s) are immutable.Let’s understand it with an example−>>> a =1 >>> ... Read More

Print Single and Multiple variable in Python?

AmitDiwan
Updated on 26-Oct-2023 03:30:56

23K+ Views

To easily display single and multiple variables in Python, use the print() statement. For multiple variables, use the comma operators. Variables are reserved memory locations to store values. It means that when you create a variable, you reserve some space in the memory. Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to the variables, you can store integers, decimals or characters in these variables. Print Single Variable in Python To display a single variable, simply use the print() method − ... Read More

Swap two variables in one line in using Python?

AmitDiwan
Updated on 11-Aug-2022 11:50:04

2K+ Views

We will learn how to swap two variables in one line. Let’s say the following is our input − a = 10 b = 5 The following is our output after swap − a = 5 b = 10 Swap two variables in one line using comma operator Using the comma operator, you can create multiple variables in a single line. The same concept is considered here and the variable values are swapped − Example a = 5; b = 10; print("Variable1 = ", a); print("Variable2 = ", b); # Swap two variables in one line ... Read More

When to use yield instead of return in Python?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

844 Views

In short, whenever control reach the return statement in your program, the execution of the program is terminated and the remaining statements will not executed.However, in case of yield, whenever control reach the yield statement in your program, the execution of your program is paused and later we can continue other statements in function.Let’s understand both the statements in detail.YieldUsing yield statement in a function makes the function a generator function which can be used in a loop. When the function is running and the yield statement exeucutes, the value after the yield is passed back to the loop that ... Read More

Returning Multiple Values in Python?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

6K+ Views

Python functions can return multiple values. These values can be stored in variables directly. A function is not restricted to return a variable, it can return zero, one, two or more values.This is the default property of python to return multiple values/variables which is not available in many other programming languages like C++ or Java.For returning multiple values from a function, we can return tuple, list or dictionary object as per our requirement.Method 1: Using tupledef func(x):    y0 = x+ 1    y1 = x * 3    y2 = y0 ** 3    return (y0, y1, y2)However, above ... Read More

What is the maximum possible value of an integer in Python?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

2K+ Views

Unlike C/C++ Long in Python 3 have unlimited precision and there is no explicitly defined limit. The amount of available address space considered to be the practical limit.In python 2, integers will automatically switch to longs when they grown beyond their limit −Python 2>>> import sys >>> type(sys.maxint) >>> type(sys.maxint + 1) Python 3Maxint was removed in python 3, but sys.maxsize can often be used instead.>>> import sys >>> type (sys.maxsize) >>> type (sys.maxsize + 1) From above output we can see, in python 3 int and long are actually merged and the value has no such importance.#Python ... Read More

Implementing Web Scraping in Python with BeautifulSoup?

George John
Updated on 30-Jul-2019 22:30:26

186 Views

BeautifulSoup is a class in the bs4 module of python. Basic purpose of building beautifulsoup is to parse HTML or XML documents.Installing bs4 (in-short beautifulsoup)It is easy to install beautifulsoup on using pip module. Just run the below command on your command shell.pip install bs4Running above command on your terminal, will see your screen something like -C:\Users\rajesh>pip install bs4 Collecting bs4 Downloading https://files.pythonhosted.org/packages/10/ed/7e8b97591f6f456174139ec089c769f89a94a1a4025fe967691de971f314/bs4-0.0.1.tar.gz Requirement already satisfied: beautifulsoup4 in c:\python\python361\lib\site-packages (from bs4) (4.6.0) Building wheels for collected packages: bs4 Building wheel for bs4 (setup.py) ... done Stored in directory: C:\Users\rajesh\AppData\Local\pip\Cache\wheels\a0\b0\b2\4f80b9456b87abedbc0bf2d52235414c3467d8889be38dd472 Successfully built bs4 Installing collected packages: bs4 Successfully installed bs4-0.0.1To verify, ... Read More

Whatsapp using Python?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

529 Views

In this section we are going to create a Whatsapp chatbots, but unlike few other chatbots for twitter or facebook, whatsapp chatbots don’t run on the platform directly because of whatsapp’s policies.But there is a way to get is done, using selenium, a very smart package in python with which developer’s can automate the browser’s activity. With this we can make use of whatsapp-web through the browser.RequirementsWe need three basic things to get things done: Selenium.We can install selenium very easily using pip, just run below command on your terminal −$pip install seleniumChrome/firefox or any other webdriver.As I using chrome ... Read More

Downloading files from web using Python?

Arjun Thakur
Updated on 22-Aug-2023 00:38:16

140K+ Views

Python provides different modules like urllib, requests etc to download files from the web. I am going to use the request library of python to efficiently download files from the URLs.Let’s start a look at step by step procedure to download files using URLs using request library−1. Import moduleimport requests2. Get the link or urlurl = 'https://www.facebook.com/favicon.ico' r = requests.get(url, allow_redirects=True)3. Save the content with name.open('facebook.ico', 'wb').write(r.content)save the file as facebook.ico.Exampleimport requests url = 'https://www.facebook.com/favicon.ico' r = requests.get(url, allow_redirects=True) open('facebook.ico', 'wb').write(r.content)ResultWe can see the file is downloaded(icon) in our current working directory.But we may need to download ... Read More

Advertisements