码迷,mamicode.com
首页 > 编程语言 > 详细

python入门(输入、输出、if else 判断流、while循环、for循环)

时间:2018-12-17 11:45:15      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:nbsp   get   user   login   getpass   utf-8   lan   引号   格式   

# -*- coding:utf-8 -*-        ———————— 申明字符编码集

注释:当行 —    #

           多行 —    “”” “”” 或者 ‘’’ ‘’’  (在python中单引号双引号没有区别)

1、 输出

   print ("hello word !")

2、 变量

    定义编码:name = "hui"

    使用编码:print ("name is",name)  (变量之间逗号隔开)

    打印变量数据类型:print(type(name))

3、 用户输入

    输入格式:username = input(“username:”)

    转换输入数据类型:age=int( input(“password:”) )

    加密显示输入:

          import  getpass #引用密码包

          password = getpass.getpass("password:")

 4、格式化输出

    列:name = input("name:")

           age = input("age:")
           sex = input("sex:")
           height = input("height:")

          # 方式1:(字符串拼接用 +)--建议不使用

          msg = ‘‘‘------- msg of ‘‘‘ + name + ‘‘‘ -------
          name:‘‘‘ + name +‘‘‘
          age:‘‘‘ + age + ‘‘‘
          sex:‘‘‘ + sex + ‘‘‘
          height:‘‘‘ + height

          print (msg)

         方式2:占位符 %s (s——string简写)--不推荐

         msg = ‘‘‘------- msg of %s -------
         name:%s
         age:%s
         sex:%s
         height:%s
          ‘‘‘%(name,name,age,sex,height)

        print (msg)

       方式3:--不推荐

  msg = ‘‘‘------- msg of {0} ------- 
  name:{0}
age:{1}
sex:{2}
height:{3}
‘‘‘.format(name,age,sex,height)

      print (msg)

  方式4:推荐使用
  msg = ‘‘‘------- msg of {name_1} ------- 
name:{name_1}
age:{age_1}
sex:{sex_1}
height:{height_1}
‘‘‘.format(name_1=name,
          age_1=age,
           sex_1=sex,
           height_1=height)
print (msg)

4、 if else 流程判    username_a = ‘qaz‘ 

    password_a = ‘123‘
username = input("username:")
password = input("password:")
if username == username_a and password == password_a:
    print("Welcome user {name} login...".format(name=username))
else:
    print("Invalid username or password!")

 5while 循环

username_a = ‘qaz‘
password_a = ‘123‘
entry_count=0
while entry_count<3:
    username = input("username:")
    password = input("password:")
    if username == username_a and password == password_a:
        print("Welcome user {name} login...".format(name=username))
        break
    else
:
        print("Invalid username or password! Please Try Again")
    entry_count +=1
else:
    print("you have tried too many times..fuck off")

 

6、 for 循环

for i in range (0,10,1):
    print(‘----------‘, i)
    for j in range(10):
        if j > 5:
            break
        else
:
            print(j)

 

 

 

python入门(输入、输出、if else 判断流、while循环、for循环)

标签:nbsp   get   user   login   getpass   utf-8   lan   引号   格式   

原文地址:https://www.cnblogs.com/56321pia/p/10129776.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!