标签:Python学习
1.字符编码2.python中的注释语句
(1)单行注释 # ----------注释/解释语句---------------------
(2)多行注释 ‘‘‘ ----------注释/解释语句---------------------‘‘‘ 或者
""" ----------注释/解释语句--------------------- """
3.格式化输出的三种常用方法
(1)
name=input ("name:")
age=input ("age:")
job=input ("job:")
salary=input("salary:")
info=‘‘‘
------------ info of %s ---------------
name:%s
age:%s
jod:%s
salary:%s
‘‘‘%(name,name ,age,job,salary)
print(info)
name=input ("name:")
age=int(input ("age:")) # 用来将age 默认的string类型强制转化为integer型
job=input ("job:")
salary=input("salary:")
info=‘‘‘
------------ info of %s ---------------
name:%s
age:%d # 区别所在的地方
jod:%s
salary:%s
‘‘‘%(name,name ,age,job,salary)
print(info)
(2)
name=input ("name:")
age=input ("age:")
job=input ("job:")
salary=input("salary:")
info = ‘‘‘
----------info of {_name} -------------
name:{_name}
age:{_age}
job:{_job}
salary{_salary
‘‘‘.format(_name=name, # 区别在于不需要一一对应了,只选用把设置的函数名对应好就可以了;
_age=age,
_job=job,
_salary=salary)
print(info)
4.用户名模块
import getpass (标准库里中存在的模块)
password=getpass.getpass("password:")
在pycharm 中是不可以运行的;要在?
5.循环语句的类型
(1)if-else 与 if-elif
_passname=‘penny‘
_password=‘abc123‘ # 这里已经把用户名和用户密码写死了,可以在后面的学习中优化
passname=input("pass name:")
password=input("pass word:")
**if _passname==passname and _password==password:
print("welcome {name} loding ...".format(name=passname)) # .format(a=b)的格式是为了在输出引入变量
else:
print("invarild pass name or pass word")
age_of_penny=23
age=int(input("age")) # 强制转化类型,input的默认类型是string字符类型,强制转化为int型;
if age_of_penny==age: # 注意=与==的区别要特别注意
print("you thinking is right")
elif age_of_penny < age:
print("thinking smaller...")
else:
print("thinking bigger...")
(2)while 及while-else
age_of_penny=23
count=0
while count<3: # 优化了输入的过程,
age = int(input("age"))
if age_of_penny==age:
print("you thinking is right")
break
elif age_of_penny < age:
print("thinking smaller...")
else:
print("thinking bigger...")
count+=1
if count==3:
continue_input=input("do you want do many guessing?")
if continue_input !=‘no‘:
count=0 # 将count继续初始化为0,可以继续执行上面的内容;这里要注意=与==的区别
age_of_penny=23
count=0
while count<3: # 优化了输入的过程,
age = int(input("age"))
if age_of_penny==age:
print("you thinking is right")
break
elif age_of_penny < age:
print("thinking smaller...")
else:
print("thinking bigger...")
count+=1
else:
print("you have tried too many times")
(3)for循环
for j in range (10)
print ("loop",i)
for-else 他是执行完了for的主体循环之后,在执行else
for j in range(0,10,2) 其中2是字长,
6python中一些要注意的小地方
(1)%s %d
(2)
print(type(----))
print(type(---),type(str(---)) # 打印转化后的类型
(3)_name=penny #变量
_name=‘penny‘ #赋值
(4)
break 结束当前循环
continue 跳出本次循环,进入下一次循环
标签:Python学习
原文地址:http://blog.51cto.com/13747953/2116048