Learn Python

Ohd Damh
1 min readOct 20, 2021
  • check python version
    python --ver or python3 --ver
  • to enter python
    python or python3 or python3.<version> or python2.<version>
  • after enter to python, type help() to get help
    >>>help() or help('modules') or help('<modules_name>') e.g. help('paramiko') or help('<modules_name>.<class_name>') e.g. help('getpass.getpass')
    also without entering python, can use:
    pydoc <moudule> or pydoc <module>.<class>
  • to list the modules
    help>modules
  • Create virtual environment: install using pip
    pip install virtualenv or pip3 install virtualenv
    Create virtualenv directory: virtualenv <directory_name>
    Activate virtualenv: source <directory_name>/bin/activate
    The prompt now will begin with <directory_name>.
  • Virtual environment to use python 2.7
    virtualenv -p /usr/bin/python2.7 <new_venv>
  • input
    var_as_st = raw_input('some words')
    var_python_will_chose_best_type = input('some words')
    but in python 3.x, we need to add int before the input:
    var = int(input("something"))
    raw_input
    also not available anymore in python3.x
  • Output
    python2.x : print var
    python3.x : print(var)
    if need to decode the ascii: print(var.decode(‘ascii’)
  • Write
    python2.x: var.write(“something”)
    python3.x: var.write(b”something”)
    where b means byte
    to read from input:
    python3.x:
    var.write(var_input.encode(‘ascii’))
  • Comment: use #
  • Variable type: >>>type(var)
  • List of operation to a variable: >>>dir(var)
  • PEP: Python Enhancement Proposals
    - single quote(‘) and double quote(“) are same
    - indentation: use 4 spaces. e.g. all lines after IF with indentation are clause if IF true (or false)
    - be consistent: tab, then all tab. Space then all spaces

Source: Youtube

Error

  • ImportError: cannot import name
    This is if the filename is same as library/module name.
$ python2.7 python/telnetlib.py
Traceback (most recent call last):
File "python/telnetlib.py", line 2, in <module>
from telnetlib import Telnet
File "/home/damha/python/telnetlib.py", line 2, in <module>
from telnetlib import Telnet

--

--