编译器是把源程序的每一条语句都编译成机器语言,并保存成二进制文件,这样运行时计算机可以直接以机器语言来运行此程序,速度很快;
解释器则是只在执行程序时,才一条一条的解释成机器语言给计算机来执行,所以运行速度是不如编译后的程序运行的快的.
#!usr/bin/env python
# -*- coding:utf-8 -*-
# Author:W
#格式化拼接的三种方法info和info2、info3
username = input("username:")
age = int(input("age:"))
print(type(age))
job = input("job:")
salary= input("salary:")
info='''
---- info of %s ----
username:%s
age: %d
job: %s
salary: %s
''' % (username,username,age,job,salary)
print(info)
info2='''
---- info of {_username} ----
username:{_username}
age: {_age}
job: {_job}
salary: {_salary}
''' .format(_username=username,
_age=age,
_job=job,
_salary=salary)
print(info2)
info3='''
---- info of {0}----
username:{0}
age: {1}
job: {2}
salary: {3}
''' .format(username,age,job,salary)
print(info3)
运行结果截图如下:
#!usr/bin/env python
# -*- coding:utf-8 -*-
# Author:W
import getpass
_username ="alex"
_password ='123123'
username = input("username:")
password = input("password:")
#print(username,password)
if _username == username and _password == password:
print("welcome user {name} login..".format(name=username))
else:
print("invalid username or password! ")
#!usr/bin/env python
# -*- coding:utf-8 -*-
# Author:W
age_of_boy =56
count = 0
while count<3:
if count ==3:
break
guess_age = int(input("guess age:")) #iuput的默认数据类型是字符串str,但是给定的年龄是整型所以需要加int转义
if guess_age == age_of_boy:
print("yes,you are right!")
break
elif guess_age > age_of_boy: #elif 是python特有的流程控制结构,等同于else if 但是只能写成elif
print("think smaller!")
else:
print('think bigger!')
count += 1
if count==3: #如果猜对了就直接跳出循环
print("you have tried many times") #对计数的次数做一个判断,如果超过三次都没有猜出来,再提示尝试次数太多
elif 是python特有的流程控制结构,等同于else if 但是只能写成elif
Python range() 函数用法
python range() 函数可创建一个整数列表,一般用在 for 循环中。
函数语法
range(start, stop[, step])
参数说明:
start: 计数从 start 开始,默认是从 0 开始,例如range(5)等价于range(0, 5);
stop: 计数到 stop 结束,但不包括 stop,例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5
step:步长,默认为1,例如:range(0, 5) 等价于 range(0, 5, 1)
下面是一个具体的例子
for i in range (0,10,2): #表示打印0-10之间的数,每隔两个数
print(i)
#!usr/bin/env python
# -*- coding:utf-8 -*-
# Author:W
age_of_boy =56
count = 0
while count<3:
if count ==3:
break
guess_age = int(input("guess age:"))
if guess_age == age_of_boy:
print("yes,you are right!")
break
elif guess_age > age_of_boy:
print("think smaller!")
else:
print('think bigger!')
count += 1
if count==3:
contine_confirm = input("do you want to trying ..? ")
if contine_confirm!='no':
count = 0
#当count计数到3的时候,提问是否还要再尝试,使用input让用户确认,如果继续,则重新开始计数;
for i in range(0,10):
if i<3:
print("loop",i)
else :
continue
print("hehe..")
continue 是跳出本次循环,执行下一次循环
break是直接跳出本次循环