首页 > 编程语言 > 详细

python零基础学习-基础知识2-代码初见

时间:2017-09-14 15:58:45      阅读:226      评论:0      收藏:0      [点我收藏+]

注释及引号的使用

#我是一行注释
‘‘‘
那么巧, 我也是一行注释
‘‘‘
print(‘‘‘打印多行字符串-第一行
打印多行字符串-第二行
‘‘‘)
print("我在尝试引号嵌套‘我在尝试引号嵌套")
print(那么巧, 我也在尝试引号嵌套"我也在尝试引号嵌套)

要求用户输入字符, 及字符串拼接

注意: 用户输入的都为字符串, 如需当做数字使用需要进行转换: int(age)

#要求用户输入
name=input("what‘s your name?")
job=input("what‘s your job?")
#打印输出内容
print(name,job)
#字符串拼接方法1, 尽量不要使用, 要占多块内存
print(‘‘‘---info of ‘‘‘+name+‘‘‘---
name:‘‘‘+name+‘‘‘
job:‘‘‘+job)
#字符串拼接方法2, %s代表string
print(‘‘‘---info of %s---
name:%s
job:%s‘‘‘%(name, name, job))
#字符串拼接方法3
print(‘‘‘---info of {_name}---
name:{_name}
job:{_job}‘‘‘.format(_name=name,_job=job))
#字符串拼接方法4
print(‘‘‘---info of {0}---
name:{0}
job:{1}‘‘‘.format(name,job))

模拟linux登录, 密码密文展示

注意: 该写法在pycharm中不不好用

import getpass #引入标准库
password=getpass.getpass("password:")

 if...else流程判断

注意: 由于没有{ }等结束符, 缩进必须正确

if _username==username and _password==password:
    print("Welcome {name}".format(name=username))
else:
    print("Invalid username or password")

while循环

count=0
num=15
while count<3:
    guess=int(input("please a number:"))
    if guess==num:
        print("correct!")
        break
    elif guess>num:
        print("Bigger!")
        count+=1
        continue
    else:
        print("Smaller!")
        count+=1
        continue
else:
    print("Guess incorrectly for 3 times!")

无限循环

while True:
    name=input("name:")

for循环

for i in range(10):
    name=input("name:")
    if name=="bell":
        break
else:
    print("Not correct")

 

python零基础学习-基础知识2-代码初见

原文:http://www.cnblogs.com/bellwang/p/7520512.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!