python入门(输入输出if else 判断流while循环for循环)
Posted 56321pia
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python入门(输入输出if else 判断流while循环for循环)相关的知识,希望对你有一定的参考价值。
# -*- 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!")
5、 while 循环
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循环)的主要内容,如果未能解决你的问题,请参考以下文章
Python 学习星球5.0if else , while和for控制流
爆肝六万字整理的python基础,快速入门python的首先