首页 > 代码库 > python学习笔记第一节

python学习笔记第一节

一、HelloWorld

#!/usr/bin/env python
#-*- coding:utf-8 -*-

print("HelloWorld!")


二、用户交互
#!/usr/bin/env python
#_*_coding:utf-8_*_
 
name = input("What is your name?")
print("Hello " name )

 

输入密码时,如果想要不可见,需要利用getpass 模块中的 getpass方法,即:import getpass

# 将用户输入的内容赋值给 name 变量
pwd = getpass.getpass("请输入密码:")
  
# 打印输入的内容
print(pwd)
 
#!/usr/bin/env python
# -*- coding: utf-8 -*-
username = input("username:")
password =  getpass.getpass("password:")
print("username,password")
 
#在pycharm中会执行不了,但可在Linux环境下执行
 
三、字符串格式化输出
name = "emily"
print "i am %s " % name
  
#输出: i am emily
 
PS: 字符串是 %s;整数 %d;浮点数%f
 
 
四、表达式 if......else
#!/usr/bin/env python  
# -*- coding: utf-8 -*-
_username = ‘emily‘
_password = ‘abc123‘
username = input("username:")
password = input("password:")

if _username == username and _password == password:
  print("welcome user {name} login...")
else:
  print("invalid username or password!")
 
五、while语句
a = 30
count = 0
while count <3:
b = int(input("a:"))
if a == b:
print("yes")
break
elif a>b:
print("small")
else:
print("big")
count +=1
else:
print ("you have tried too many times..fuck off")
 
 
 
 
六、for语句
for i in range(10):
print(‘------‘,i)
for j in range(10):
print(j)
if j>5:
break
 
 

python学习笔记第一节