开发工具:PyCharm
1、第一个python程序:
print("Hello World!")
2、变量:只能是 数字,字母,下划线的任意组合
定义变量:
name = "deyi liu"#name:变量名,deyi liu:变量值 print("My name is",name)#调用变量
3、打印多行:
msg = ‘‘‘ name = "deyi liu" age = "24" PIE = "常量" ‘‘‘ print(msg)
4、用户输入:
name = input("name:")
wu加密(getpass)
import getpass #导入getpass模块,密码加密 passworld = getpass.getpass("passworld:")
5、字符串拼接:
‘‘‘ #字符串拼接:(%s:占位符,s代表string %d:占位符,d代表date %f:占位符,f代表float) ‘‘‘ name = input("name:")#raw_input:python2.x这样写,效果等同于Python3.x的input age = int(input("age:"))#int:integer 整型 print(type(age) , type(str(age)))#打印一个变量的数据类型 job = input("job:") salary = input("salary:") info = ‘‘‘ ---------info of %s----------- name = %s age = %d job = %s salary =%s ‘‘‘ % (name,name,age,job,salary) info2 = ‘‘‘ ---------info of {_name}----------- name = {_name} age = {_age} job = {_job} salary = {_salary} ‘‘‘ .format(_name=name, _age=age, _job=job, _salary=salary) info3 = ‘‘‘ ---------info of {0}----------- name : {0} age : {1} job : {2} salary : {3} ‘‘‘ .format(name,age,job,salary) print(info) print(info2) print(info3)
6、if else 判断:
import getpass real_username = "liudeyi" real_passworld = "Aa111111" username = input("username:") passworld = getpass.getpass("passworld:")#在pycharm不能执行,需在cmd交互器里执行 if real_username == username and real_passworld == passworld : print("Wlecome user {name} login...".format(name=username)) else: print("Out!")
7、while 循环:
count = 0 while True: print("count:",count) count = count+1 #count +=1
8、猜数游戏-while:
age_of_oldboy = 56 count = 0 while count<3: age = int(input("guess age:")) if age == age_of_oldboy: print("yes,you got it") break #退出本次循环 elif age > age_of_oldboy: print("think smaller") else: print("think bigger") count = count+1 else: print("you have tried too many times..fuck off")
9、for 循环:
for i in range(10):#内建函数range() ,它产生等差级数序列 print("loop:",i) for i in range(0,10,2):#打印从0 开始打印到10,跳一个打一个;guess for.py2代表步长,默认是1 print("loop:",i) for i in range(0,10,): if i<5: print("loop:",i) else: continue#跳出本次循环,进入下一循环 print("hehe") for i in range(10): print("-----------",i) for j in range(10): print(j) if j>5: break
10、猜数-for:
age_of_oldboy = 56 count = 0 for i in range(3): age = int(input("guess age:")) if age == age_of_oldboy: print("yes,you got it") break #退出本次循环 elif age > age_of_oldboy: print("think smaller") else: print("think bigger") else: print("Your account has been locked")
原文:http://12594527.blog.51cto.com/12584527/1901864