Introduction to Python.

Installation of Python:

For windows version available.

  •  X 64 version for 64 bit OS
  • X 86 version for 32 bit OS

    Document comes with CHM (compiled HTML)

-----------------------------------------------------------------------------------------------------------
  • Setup System Variable. 

Once Python is installed setup system variable.

It helps to Run python from any prompt, no need to run python code from installed directory.

 

Method 1

%path%;C:\Python-path  

(Python-path = location where python is installed)

SYStem Device Manager Control PaneL applet:

 

Or

 

Method 2:  (System Device Manager Control Panel applet)

Run and type sysdm.cpl

Click on "Advanced" tab of system properties 

Environment Variables

Set path variable: c:\Program Files\Python310


---------------------------------------------------------------------------------------------------------------

To Verify Version of Python:  

python -V


---------------------------------------------------------------------------------------------------------------

Running python for first time:

There are 4 ways to execute python program.

  1. Command line: Run Python scripts from the command line or terminal of your operating system.
  2. Interactive mode: Use the standard REPL to execute Python code and scripts in interactive mode. E.g. IDLE (Integrated Development and Learning Environment)
  3. Text editor: Use a text editor like Atom, Notepad++, Sublime Text, Visual Studio Code, or Notepad to write and run Python code.
  4. IDE: Use an IDE or code editor to run Python scripts. For example, you can use PyCharm




Execute Python scrip Linux CLI.

Open notepad, type following command and save it with .py extension.

print("Hello World")

python test.py

 

$ chmod +x test.py # This is to make file executable.

$./test.py

-------------------------------------------------------------------------------------------------------------------

 To know python installation path.

>>> import sys

>>> print(sys.path)


Getting details of Current Working Directory (CWD)

>>>import os.

>>>os.getcwd()






Change current working directory

>>> os.chdir('c:\\Users\\User1\\desktop')


Moving back to parent directory from current directory.

os.chdir('../')


Interactive help:

1st function is dir()    dir is shortcut for “directory”.

With dir() we get list of available objects



Help function in Python

help("modules")

















Understanding Script, module, packagelibrary

  • Script: python file that run directly to perform certain task.
  • Module: Python file that is imported in script or another program. It defines member like classes, functions and variable intended to be used in other files that import it.
  • Python.org definition: An object that serves as an organizational unit of Python code. Modules have a namespace containing arbitrary Python objects. Modules are loaded into Python by the process of importing.
  • Package:  collection of related modules that work together to provide certain functionality. This are folder containing modules. It has special file with name __init__.py
  • Python.org definition : A Python module which can contain submodules or recursively, subpackages. Technically, a package is a Python module with an __path__ attribute.

 

 To create Python package yourself, we have to create directory and a file named __init__.py inside it. This file cab be empty.

Note: Directories without an __init__.py file are still treated as packages by Python. However, these won’t be regular packages, but something called namespace packages.

In general, submodules and subpackages aren’t imported when you import a package. However, you can use __init__.py to include any or all submodules and subpackages if you want.

 Library: a bundle of code / collection of packages. E.g. Matplotlib (plotting libaray)

  • Standard library (available by default)
  • 3rd-Party Modules


Question:
What does '__init__.py' in folder do in Python?
  • Folder with __init__.py file is treated like Python package, this folder will have Python module which is imported in Python script. Each module will have it own function called as method. 
  • We can keep __init__.py this file is run every time we import package, so we can keep it empty, or we can add some setup code like (message)

---------------------------------------------------------------------
Command to import module:

SYNTAX 1: Import required module and call methods.

import <MODULE-NAME>
MODULE-NAME.METHOD()

Example: Get value of pi from math module.
Note: Calling pi directly gives error.
---------------------------------------------------------------------

SYNTAX 2: import with Alias name.

import <module-name> as ALIAS
ALIAS.METHOD()




---------------------------------------------------------------------

SYNTAX 3: from <MODULE-NAME> import <METHOD>








SYNTAX 3: import with . (dot) relative import, help to call method directly.

from PACKAGE.MODULE import METHOD

METHOD()


--------------------------------------------------------------------- 

SYNTAX 5: import *

from package.module import *

METHOD()


---------------------------------------------------------------------


SYNTAX 6: import * without specifying module name. To get everything form package.


from  PACKAGE import *

MODULE.METHOD()

This will give name error.

To make above code work we must add following line in __init__.py

__all__ =[ ] 

Empty [ ] allow to access all module, we can also specify specific module without extension inside square bracket. 
Or
Do relative import of module and specify it method in square bracket 






Comments

Popular posts from this blog

Computer Programming Brief History.

Loop: for & range

How to install Deepseek R1 local machine.