Category Archives: Python

List methods in Python

This article is extension of below articles : List Methods in Python | Set 1 (in, not in, len(), min(), max()…) List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()…) Adding and Appending append(): Used for appending and adding elements to List.It is used to add elements to the last position… Read More »

OS Module in Python with Examples

The OS module in python provides functions for interacting with the operating system. OS, comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. The *os* and *os.path* modules include many functions to interact with the file system. Following are some functions in OS module: 1.… Read More »

Implementing Web Scraping in Python with BeautifulSoup

There are mainly two ways to extract data from a website: Use the API of the website (if it exists). For example, Facebook has the Facebook Graph API which allows retrieval of data posted on Facebook. Access the HTML of the webpage and extract useful information/data from it. This technique is called web scraping or… Read More »

range() vs xrange() in Python

range() and xrange() are two functions that could be used to iterate a certain number of times in for loops in Python. In Python 3, there is no xrange , but the range function behaves like xrange in Python 2.If you want to write code that will run on both Python 2 and Python 3,… Read More »

Output of Python Program | Set 4

Difficulty level : Intermediate Predict the output of following Python Programs. Program 1: Output: k Explanation: The index position -1 represents either the last element in a list or the last character in a String. In the above given list of names “nameList”, the index 1 represents the second element i.e, the second string “Pratik”… Read More »

Output of Python Program | Set 3

Difficulty level : Intermediate Predict the output of following Python Programs. Program 1: Output: 51 Explanation : In the above program we are creating a member variable having name ‘life’ by adding it directly to the dictionary of the object ‘manager’ of class ‘Geeks’. Total numbers of items in the dictionary is 2, the variables… Read More »

copy in Python (Deep Copy and Shallow Copy)

Python defines a module which allows to deep copy or shallow copy mutable object using the inbuilt functions present in the module “copy“. Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so… Read More »

Import module in Python

Import in python is similar to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is the most common way of invoking the import machinery, but it is not the only way. import module_name When import is used, it searches for… Read More »

Output of python program | Set 2

Difficulty level : Intermediate Predict the output of following Python Programs. Program 1: Output: 111 Explanation: Instantiation of the class “Acc” automatically calls the method __init__ and passes the object as the self parameter. 111 is assigned to data attribute of the object called id. The value “555” is not retained in the object as… Read More »

Output of Python Program | Set 1

Predict the output of following python programs: Program 1: Output: 24 Explanation : In the above program r and s are lambda functions or anonymous functions and q is the argument to both of the functions. In first step we have initialized x to 2. In second step we have passed x as argument to… Read More »

Reloading modules in Python

reload() reloads a previously imported module. This is useful if you have edited the module source file using an external editor and want to try out the new version without leaving the Python interpreter. The return value is the module object. Note: The argument should be a module which has been successfully imported. Usage: For… Read More »