初识数据类型、for循环示例

数据类型初识

数字
整数型 int
长整型 long
在Python3 里已经不区分整型和长整型,同一种都叫整型

float (浮点型)
浮点数用来处理实数,即带有小数的数字。
complex(复数)
复数由实数和虚数 部分组成 eg.4+6j

布尔 只有两种状态 ,分别是
真 true
假 false

字符串
salary.isdigit()
计算机中,一切皆为对象 一切对象皆可分类

"."的含义:通过这个“.”取到(调用)对象的属性,

用“+”号拼接,相当于开辟多个内存,费时。一般还是使用% 占位符

循环 loop
有限循环
无限循环=死循环

有限循环,循环三次,示例如下:


for i in range(3):
  print(i)

#    i:从0开始  每次加1

可以指定初始值:

示例:打印出1到100的奇数

for i in range(1,101):
if i % 2==1:
print(i)

简化版:
for i in range(1,101,2): #“2”表示步长
print(i)

for循环示例:

让用户输入用户名、密码,正确则结束,否则重新输入,可重复3次。

_user="elaine"
_passwd="abc123"

for i in range(3):
user=input("user:")
password=input("password:")
if user==_user and password==_passwd:
print("welcome %s to logging"%_user )
break
else:
print("wrong user or wrong password")


输错3次后,添加一句话:“你已经错误太多次了”

法一:
_user="elaine"
_passwd="abc123"
pass_authentication= False #判断是否通过验证,默认为假
for i in range(3):
user=input("user:")
password=input("password:")
if user==_user and password==_passwd:
print("welcome %s to logging"%_user )
pass_authentication=True #真,成立
break
else:
print("wrong user or wrong password")
if not pass_authentication :
print("你已经错误太多次了")

法二:

猜你喜欢

转载自www.cnblogs.com/elaine-blog1/p/9643627.html