Day1_Python学习
内容目录
1、变量和常量
2、用户输入
3、getpass模块
4、表达式if...else
5、表达式while
6、表达式for
一、变量和常量
声明变量:
name = "Jeffery" ----------声明一个变量,变量名:name,变量name的值为:“Jeffery”
变量定义的规则:
- 变量名只能是 字母、数字或下划线的任意组合
- 变量名的第一个字符不能是数字
- 以下关键字不能声明为变量名
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
变量的赋值:
name="Jeffery"
name2=name
print("My name is",name)
print("My name is",name2)
二、用户输入
name=input("name")
age=input("age")
job=input("job")
字符串拼接:
第一种方式
info='''
--------info of '''+name+'''-------'''+'''
Name:'''+name+'''
Age:'''+age+'''
Job:'''+job
print(info)
第二种方式
info='''
----------info of %s-------
Nmae:%s
Age:%s
Job:%s
'''%(name,name,age,job)
print(info)
第三种方式
info='''
--------info of _name---
Name:{_name}
Age:{_age}
Job:{_job}
'''.format(_name=name,
_age=age,
_job=job)
print(info)
三、getpass模块
import getpass
_username='jeffery'
_password='123'
username=input("username:")
password=getpass.getpass("password:")
if _username==username and _password==password:
print("Welcome user {name} login...".format(name=username))
else:
print("Invalid username or password")
四、表达式if...else
age_of_oldboy=56
guess_age = int(input("guessage:"))
if guess_age==age_of_oldboy:
print("you are right..")
break
elif guess_age<age_of_oldboy:
print("think bigger..")
else:
print("think smaller..")
简单的if...else语句,做简单判断
外层变量,可以被内层代码使用
内层变量,不应被外层代码使用
五、表达式while
count=0
while True:
print("count:",count)
count=count+1
if count==1000:
break
简单的while语句
age_of_oldboy=56
count=0
while count<3:
guess_age = int(input("guessage:"))
if guess_age==age_of_oldboy:
print("you are right..")
break
elif guess_age<age_of_oldboy:
print("think bigger..")
else:
print("think smaller..")
count+=1
else:
print("you have tried too many times...out")
做年龄判断
age_of_oldboy=56
count=0
while count<3:
guess_age = int(input("guessage:"))
if guess_age==age_of_oldboy:
print("you are right...")
break
elif guess_age<age_of_oldboy:
print("think bigger...")
else:
print("think smaller...")
count+=1
if count==3:
countinue_confirm=input("do you want keep countinue?")
if countinue_confirm != 'n':
count=0
年龄三次猜完之后,询问是否继续
六、表达式for
for i in range(0,10,2):
if i<4:
print("loop:",i)
else:
continue
简单的for循环语句
age_of_oldboy=56
for i in range(3):
guess_age = int(input("guessage:"))
if guess_age==age_of_oldboy:
print("you are right..")
break
elif guess_age<age_of_oldboy:
print("think bigger..")
else:
print("think smaller..")
else:
print("you have tried too many times...out")
做年龄猜测,总共猜三次

浙公网安备 33010602011771号