Home > Article > Backend Development > In-depth understanding of Python introduction, basic syntax, and process control
1. 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 )
2. When entering the password, if you want it 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 # 将用户输入的内容赋值给 name 变量 pwd = getpass.getpass("请输入密码:") # 打印输入的内容 print(pwd)
3. sys module
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys print(sys.argv)
#Output
$ python test.py helo world ['test.py', 'helo', 'world'] #把执行脚本时传递的参数获取到了
4, os module
#!/usr/bin/env python # -*- coding: utf-8 -*- import os os.system("df -h") #调用系统命令
import os,sys os.system(''.join(sys.argv[1:])) #把用户的输入的参数当作一条命令交给os.system来执行
5, if statement
场景一、用户登陆验证 # 提示输入用户名和密码 # 验证用户名和密码 # 如果错误,则输出用户名或密码错误 # 如果成功,则输出 欢迎,XXX! #!/usr/bin/env python # -*- coding: utf-8 -*- import getpass name = raw_input('请输入用户名:') pwd = getpass.getpass('请输入密码:') if name == "alex" and pwd == "cmd": print("欢迎,alex!") else: print("用户名和密码错误")
6, expression for loop
is the simplest Loop 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("你是风儿我是沙,缠缠绵绵到天涯...",count) count +=1
The above is the detailed content of In-depth understanding of Python introduction, basic syntax, and process control. For more information, please follow other related articles on the PHP Chinese website!