欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python 基础(一)

程序员文章站 2023-11-19 16:26:04
1.branch: example 1 if else 语句 example 2 if else、if elif、while 语句 example 3 for 语句 和 range() 函数 example 4 不明文显示输入的密码,getpass标准库 2.格式化输出: 打印一个简单的自我介绍情况 ......

1.branch:

example 1

if else 语句

1 #if else
2 username = "joker"
3 password = "123456"
4 in_user = input("name:")
5 in_passwd = input("password:")
6 if username == in_user and password == in_passwd :
7     print("welcome to login in...")
8 else:
9     print("username or password not correct!")

example 2

if else、if elif、while 语句

 1 #little game for if else elif and while
 2 user = "admin"
 3 psswd = "weak1234"
 4 count = 0
 5 while count < 3:
 6     user_name = input("name:")
 7     pass_wd = input("password:")
 8     if(user == user_name and psswd == pass_wd):
 9         print("oh ,your hacked my account!")
10         break
11     elif count == 2:
12         print("you have tried so many times")
13     else:
14          print("i am a strong admin ,you gays fuck off!")
15     count += 1
16 else:
17     print("you don't have any chance any more ,see you!")

example 3

for 语句 和 range() 函数

 1 #for and range(start,end,steps)
 2 #print the numbers from 1 to 10
 3 for i in range(1,11):
 4     print(i,end=" ")
 5 print()
 6 #print the even numbers
 7 for i in range(0,11,2):
 8     print(i,end=" ")
 9 print()
10 #print the odd numbers
11 for i in range(1,12,2):
12     print(i,end=" ")
13 print()

example 4

不明文显示输入的密码,getpass标准库

1 #getpass
2 import getpass
3 username = input("name:")
4 password_1 = input("password:")
5 password_1 = getpass.getpass("password:")    # in pycharm it doesn't work

2.格式化输出:

打印一个简单的自我介绍情况

example 1

占位符:%s、%d、%f

 1 #格式化输出
 2 name=input("name:")
 3 age=int(input("age:"))
 4 job=input("job:")
 5 salary=float(input("salary:"))
 6 msg1='''-----------name:%s------------
 7 name:%s
 8 age:%d
 9 job:%s
10 salary:%f
11 '''%(name,name,age,job,salary)
12 print(msg1)

example 2

format()函数

 1 #格式化输出
 2 name=input("name:")
 3 age=input("age:")
 4 job=input("job:")
 5 salary=input("salary:")
 6 msg2='''-----------name:{_name}------------
 7 name:{_name1}
 8 age:{_age}
 9 job:{_job}
10 salary:{_salary}
11 '''.format(_name=name,_name1=name,_age=age,_job=job,_salary=salary)
12 print(msg2)