#!/usr/bin/env python : 指定解释器
*******************************
只要变成可执行的文件必须加上这句话。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: 许海明
前面的格式
*******************************
在Python中没有定义常量的方法,规定用大写来定义常量
*******************************
单行注释用#.多行注释用“”“ “””或‘’‘ ’‘’,打印多行也用
‘’‘ ’‘’,多引号和单引号是等价的,要灵活使用见下图
******************************
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: 许海明
#交互模式 input输入 print格式输出
name=input("your name :")
age=input("your age :")
job=input("your job :")
salary=input("your salary :")
info1=''' 格式1 --------infor of %s ----
Name:%s
Age:%s
Job:%s
Salary:%s
'''%(name,name,age,job,salary)
print(info1)
info2=''' 格式2 --------infor of {_name} ----
Name:{_name}
Age:{_age}
Job:{_job}
Salary:{_salary}
'''.format(_name=name,
_age=age,
_job=job,
_salary=salary)
print(info2)
info3=''' 格式3 --------infor of {0} ----
Name:{0}
Age:{1}
Job:{2}
Salary:{3}
'''.format(name,age,job,salary)
print(info3)
msg ="------打印一个变量的格式和强制类型转换--------- "
msg2 ="例如变量age的类型:"+str(type(age))
msg3="用强制类型转换后age的类型:"+str(type(int(age)))
msg4="使用强制类型转换后age的类型: "+str(type(str(age)))
print(msg)··
print(msg2)
print(msg3)
print(msg4)
上述程序输出:
C:\python36\python.exe E:/python学习/s14/day1/interaction.py
your name :许海明
your age :22
your job :IT
your salary :30000
格式1 --------infor of 许海明 ----
Name:许海明
Age:22
Job:IT
Salary:30000
格式2 --------infor of 许海明 ----
Name:许海明
Age:22
Job:IT
Salary:30000
格式3 --------infor of 许海明 ----
Name:许海明
Age:22
Job:IT
Salary:30000
------打印一个变量的格式和强制类型转换---------
例如变量age的类型:<class 'str'>
用强制类型转换后age的类型:<class 'int'>
使用强制类型转换后age的类型: <class 'str'>
密文输入
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: 许海明
import getpass
name = input("你的姓名:")
password = getpass.getpass("你的密码: ")
print(name,password)
******************************
只要看到indentationError 就是缩进错误
IF else elif 的用法
_username = "admin"
_password = "123456"
username = input("你的姓名:")
password = input("你的密码: ")
print(username,password)
if _username==username and _password==password:
print("Welcome {name} to login..".format(name=username))
else:
print("Invalid username or password")
Elif的用法
age_of_china=88
age_guess = int(input("输入你猜的中国的年龄: "))
if age_guess==age_of_china:
print("you got it !")
elif age_guess>age_of_china:
print("Think smaller...")
elif age_guess<age_of_china:
print("Think Large...")
*******************************
while的用法,注意此处和else的灵活使用!
age_of_china=88
count=0
while count<3:
age_guess = int(input("输入你猜的中国的年龄: "))
if age_guess ==
age_of_china:
print("you got it !")
break
elif age_guess >
age_of_china:
print("Think smaller...")
elif age_guess < age_of_china:
print("Think Large...")
count += 1
if count==3:
continue_confirm=input("Do you keep guessing...")
if continue_confirm != 'N':
count=0
else:
print("you have tried too many times....fuck off")
*******************************
for的用法,同时注意continue和break的用法
'''
for 循环中的range中的三个参数
第一个 开始
第二个 结束
第三个 步长
注意continue的特点:跳出本次循环 继续下一次循环。break则是跳出当前的循环,结束循环'''
for i in range(0,10):
if i < 3:
print("i in loop",i)
else :
continue
print("测试continue 的用法")
上述代码的输出是:
C:\python36\python.exe E:/python学习/s14/day1/for.py
i in loop 0
测试continue 的用法
i in loop 1
测试continue 的用法
i in loop 2
测试continue 的用法
Process finished with exit code 0