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.
- Command line: Run Python scripts from the command line or terminal of your operating system.
- Interactive mode: Use the standard REPL to execute Python code and scripts in interactive mode. E.g. IDLE (Integrated Development and Learning Environment)
- Text editor: Use a text editor like Atom, Notepad++, Sublime Text, Visual Studio Code, or Notepad to write and run Python code.
- 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, package, library
- 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
- 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)
Comments
Post a Comment