This article provides a detailed introduction to python, basic syntax, and process control. For more information, see section 1. user input.
#!/usr/bin/env python#_*_coding:utf-8_*_#name = raw_input("What is your name?") #only on python 2.xname = input("What is your name?")print("Hello " + name )
2. when entering the password, if you want to be invisible, you need to use the getpass method in the getpass module, that is:
#! /Usr/bin/env python #-*-coding: UTF-8-*-import getpass # assign the user input content to the name variable pwd = getpass. getpass ("Enter password:") # print the entered content print (pwd)
3. sys module
#!/usr/bin/env python# -*- coding: utf-8 -*- import sysprint(sys.argv)
# Output
$ Python test. py helo world ['test. py', 'helo', 'World'] # obtain the parameters passed during script execution.
4. OS module
#! /Usr/bin/env python #-*-coding: UTF-8-*-import osos. system ("df-h") # Call system commands
Import OS, sysos. system (''. join (sys. argv [1:]) # run the user input parameters as a command to OS. system for execution.
5. if statement
Scenario 1: User login verification # prompt for entering the user name and password # verify the user name and password # if an error occurs, the output user name or password is incorrect # if successful, the output is welcome, XXX! #! /Usr/bin/env python #-*-coding: UTF-8-*-import getpassname = raw_input ('Enter your username: ') pwd = getpass. getpass ('Enter password: ') if name = "alex" and pwd = "cmd": print ("Welcome, alex! ") Else: print (" incorrect user name and password ")
6. expression for loop
The simplest loop is 10 times
#!/usr/bin/env python#_*_coding:utf-8_*___author__ = 'Alex Li'for i in range(10): print("loop:", i )
7. while loop
Count = 0 while True: print ("You are wind, I am sand, entangled in the world...", count) count + = 1
The above is a detailed introduction to python, basic syntax, and process control. For more information, see other related articles in the first PHP community!