Found 10784 Articles for Python

Why is indentation important in Python?

Malhar Lathkar
Updated on 30-Sep-2019 09:00:06

729 Views

Many a times it is required to treat more than one statements in a program as a block. Different programming languages use different techniques to define scope and extent of block of statements in constructs like class, function, conditional and loop. In C and C++ for example, statements inside curly brackets are treated as a block. Python uses uniform indentation to mark block of statements.Before beginning of block symbol : is used. First and subsequent statements in block are written by leaving additional (but uniform) whitespace (called indent) . In order to signal end of block, the whitespace is dedented. ... Read More

What are Reserved Keywords in Python?

Malhar Lathkar
Updated on 14-Feb-2020 11:03:21

19K+ Views

Reserved words (also called keywords) are defined with predefined meaning and syntax in the language. These keywords have to be used to develop programming instructions. Reserved words can’t be used as identifiers for other programming elements like name of variable, function etc.Following is the list of reserved keywords in Python 3andexceptlambdawithasfinallynonlocalwhileassertfalseNoneyieldbreakfornotclassfromorcontinueglobalpassdefifraisedelimportreturnelifinTrueelseistryPython 3 has 33 keywords while Python 2 has 30. The print has been removed from Python 2 as keyword and included as built-in function.To check the keyword list, type following commands in interpreter −>>> import keyword >>> keyword.kwlist

What are valid python identifiers?

Pythonista
Updated on 30-Sep-2019 09:00:47

706 Views

An identifier in a Python program is name given to various elements in it, such as keyword, variable, function, class, module, package etc. An identifier should start with either an alphabet (lower or upper case) or underscore (_). More than one alpha-numeric characters or underscore may follow.Keywords are predefined. They are in lowercase. They can not be used for any other purpose.By convention, name of class starts with uppercase alphabet. Whereas others start with lowercase alphabet.Single underscore in the beginning of a variable name is used to indicate a private variable.Two underscores in beginning indicate that the variable is strongly ... Read More

How do I find the location of Python module sources?

Manogna
Updated on 30-Sep-2019 09:02:50

10K+ Views

For a pure python module you can find the location of the source files by looking at the module.__file__. For example,  >>> import mymodule >>> mymodule.__file__ C:/Users/Ayush/mymodule.py  Many built in modules, however, are written in C, and therefore module.__file__ points to a .so file (there is no module.__file__ on Windows), and therefore, you can't see the source. You can manually go and check the PYTHONPATH variable contents to find the directories from where these built in modules are being imported.  Running "python -v"from the command line tells you what is being imported and from where. This is useful if you want to ... Read More

How do I unload (reload) a Python module?

Manogna
Updated on 30-Sep-2019 08:50:27

477 Views

The function reload(moduleName) reloads a previously loaded module (assuming you loaded it with the syntax "importmoduleName" without exiting the script. It is intended for conversational use, where you have edited the source file for a module and want to test it without leaving Python and starting it again. For example, >>> import mymodule >>> # Edited mymoduleand want to reload it in this script >>> reload(mymodule)Note that the moduleName is the actual name of the module, not a string containing its name. The python docs state following about reload function: Python modules’ code is recompiled and the module-level code re-executed, defining ... Read More

How to retrieve Python module path?

Manogna
Updated on 30-Sep-2019 08:51:02

1K+ Views

For a pure python module you can find the location of the source files by looking at the module.__file__. For example, >>> import mymodule >>> mymodule.__file__ C:/Users/Ayush/mymodule.py  Many built-in modules, however,are written in C, and therefore module.__file__ points to a .so file (there is no module.__file__ on Windows), and therefore, you can't see the source. Running "python -v"from the command line tells you what is being imported and from where. This is useful if you want to know the location of built-in modules.

What are the modules available in Python for converting PDF to text?

Manogna
Updated on 11-Dec-2019 06:12:51

199 Views

You can use the PDFMiner package to convert PDF to text.ExampleYou can use it in the following way:  import sys from cStringIO import StringIO  from pdfminer.pdfpage importPDFPage from pdfminer.pdfinterp importPDFResourceManager, PDFPageInterpreter from pdfminer.layout importLAParams from pdfminer.converter importXMLConverter, HTMLConverter, TextConverter  def pdfparser(data):     fp = file(data, 'rb')     resource_manager = PDFResourceManager()     retstr = StringIO()     codec = 'utf-8'     laparams = LAParams()     device = TextConverter(resource_manager, retstr, codec=codec, laparams=laparams)     interpreter =PDFPageInterpreter(resource_manager, device)       # Process each page contained in thedocument.     for page in PDFPage.get_pages(fp):         interpreter.process_page(page) ... Read More

How to check version of python modules?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:53:00

5K+ Views

When you install Python, you also get the Python package manager, pip. You can use pip to get the versions of python modules. If you want to list all installed Python modules with their version numbers, use the following command:$ pip freezeYou will get the output:asn1crypto==0.22.0 astroid==1.5.2 attrs==16.3.0 Automat==0.5.0 backports.functools-lru-cache==1.3 cffi==1.10.0 ...To individually find the version number you can grep on this output on *NIX machines. For example:$ pip freeze | grep PyMySQL PyMySQL==0.7.11On windows, you can use findstr instead of grep. For example:PS C:\> pip freeze | findstr PyMySql PyMySQL==0.7.11If you want to know the version of a module ... Read More

How can I get a list of locally installed Python modules?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:54:02

3K+ Views

There are multiple ways to get a list of locally installed Python modules. Easiest way is using the Python shell, for example, >>> help('modules') Please wait a moment while I gather a list of all available modules... BaseHTTPServer      brain_nose          markupbase          stat Bastion             brain_numpy         marshal             statvfs CGIHTTPServer       brain_pkg_resources math                string Canvas              brain_pytest        matplotlib       ... Read More

What does reload() function do in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:55:31

1K+ Views

The function reload(moduleName) reloads a previously loaded module (assuming you loaded it with the syntax "import moduleName". It is intended for conversational use, where you have edited the source file for a module and want to test it without leaving Python and starting it again. For example, >>> import mymodule >>> # Edited mymodule and want to reload it in this script >>> reload(mymodule)Note that the moduleName is the actual name of the module, not a string containing its name. In Python 3, reload was moved from builtins to imp. So to use reload in Python 3, you'd have to ... Read More

Advertisements