Python day01 - 变量

变量和循环

-- coding:utf-8 --

Author:Tinky

变量,编码,

input,默认输出类型为字符串

格式化输出%s /

秘文输出 import getpass

if 和 while, for(break,continue)

print("hello world")
name = "afi"
print("My name is", name)
gf_name = "Chen rong"
age = int(input("age")) # 默认所有的输入都是字符串 ,可以进行数字转换
print(type(str(age)))
sex = "女"
print("My infomation :", "年龄:", age, "性别:", sex)

ASCII 只能存储255个bytes python2

1980 年出现了GB2312 只能支持7445 个汉字

1995 年GBK1.0 支出21886

2000 年GB18030 27484

Unicode 2 bytes

utf - 8 英文存储1bytes 中文 3bytes---- python3

注释 单行# 多行"""

打印多行 """"""

单引号和双引号 只有在嵌套使用的时候才需要注意 例如:print("i'm qing")外面和里面不一样,里面单(双)外面双(单)

print("""哈哈哈,
哈哈哈哈
哈哈哈""")

username = input("username:")
password = '1213'
print(username, password)

变量占位符 %s ,,注意是小写的s,,%s 便是字符占位符,%d表示数字占位符, %f 表示浮点占位符

info = '''-----info of %s---
Name:%s
Age:%d
''' % (name, name, age)

info1 = '''------info of {_name}-----
Name: {_name}
Age: {_age}'''.format(_name=name, _age=age)

print(info, info1)

password1 = input("password:")

if password1 == password:
print("Welcome user{name} login".format(username))
else:
print("invalid password")

count = 0
while count < 4:
count += 1
guess_age = int(input("guess my age:"))
if guess_age == age:
print("yes, you got it.")
print("你猜了%s次" % count)
break
elif guess_age > age:
print(" think smaller")
else:
print("think bigeer")
if count == 4:
continue_play = input("Do you want to continue:")
if continue_play != "n":
count = 0
else:
print("you have tried too many times ....") # 如果 不满足循环条件,就会执行else中的内容

for count in range(4):
guess_age1 = int(input("guess my age:"))
if guess_age1 ==age:
print("yes you got it")
break
elif guess_age1 >age:
print("think smaller")
else:
print("think bigger")
else:
print("you have tried %s times" % (count+1)) # 循环不满足条件后结束循环才会执行这行代码,如果循环满足条件break后,不会执行

跳跃打印

for i in range(0, 10, 2):
print("heihei")
if i < 3:
print("loop", i)
else:
continue # 跳出本次循环,进入下一次循环,当i>3时,还会进行循环,所有"heihei"还会打印
print("hehe")

for i in range(0, 10, 2):
if i < 3:
print("loop", i)
else:
break # 结束当前循环,当i>3时,"heihei"不会再次打印
print("hehe")

循环嵌套

for i in range(4):
for j in range(10):
print("%s%s=%s" % (i, j, ij))
if j > 5:
break # 结束的是当前循环,大循环并不结束

posted @ 2024-03-25 14:33  TingKi  阅读(32)  评论(0)    收藏  举报