Basic user input and module introduction to python

  • 2020-05-17 05:44:28
  • OfStack

1. The comments

Current line comment: # annotated content

Multi-line comment: """ "annotated content """ "

2. User input


#!/usr/bin/env python
#_*_coding:utf-8_*_
#name = raw_input("What is your name?") #only on python 2.x
name = input("What is your name?")
print("Hello " + name )

When entering the password, if you want to be invisible, you need to make use of the getpass method in the getpass module, that is:


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import getpass
#  Assign a value to the user's input  name  variable 
pwd = getpass.getpass(" Please enter your password: ")
#  Print the input 
print(pwd)

3. Format a string

String formatted output


name = "alex"
print "i am %s " % name 
#  The output : i am alex
# PS:  The string is  %s; The integer  %d; Floating point Numbers %f

4. Preliminary understanding of the module

The great thing about Python is that it has a very rich and powerful standard library and third side library, and almost anything you want to do is supported by the Python library.


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
os.system("df -h") # Call system command 

If you write the tab module yourself, you will find that the tab.py module you wrote above can only be imported from the current directory. What if you want to use it anywhere in the system?

At this point you need to put tab.py in the python global environment variables directory, basically 1 in a directory called Python/2.7/ site-packages, this directory in different OS location is not the same, print(sys.path) can see the python environment variables list.


Related articles: