当前位置:Gxlcms > Python > python有哪些需要学习的知识?

python有哪些需要学习的知识?

时间:2021-07-01 10:21:17 帮助过:7人阅读

一、python第一行代码:

1 name = "你好,世界"2 print(name)

二、变量:

1 name = "SunDM12"2 name2 = name3 print("my name : ",name, name2)4 5 name = "wangba"6 print(name, name2)

name前后变化,而name2 = name已经将“SunDM12”赋值给了name2,name变化后,name2不再变化

三、交互:

1 username = input("username : ")2 print(username)

input函数:用户可以在界面上显示输入字符,并赋值给了username

 1 name = input("name :") 2 age = input("age :") 3 job = input("job :") 4 salary = input("salary :") 5  6 info = ''' 7 ------ info of %s ------ 8 name : %s 9 age : %s10 job : %s11 salary : %s12 '''%(name, name, age, job, salary)

在屏幕打印的第一种格式。

%s是字符串;%d是双精度;%f是浮点型

1 info2 = '''2 ------- info of {_name} -------3 name : {_name}4 age : {_age}5 job : {_job}6 salary : {_salary}7 '''.format(_name=name,8                _name=name,_age=age,_job=job,_salary=salary)

在屏幕打印的第二种格式。

1 info3 = '''2 ------ info of {0} ------3 name : {0}4 age : {1}5 job : {2}6 salary : {3}7 '''.format(name,age,job,salary)

在屏幕打印的第三种格式。

四、登陆密码:

 1 import getpass 2  3 _username = 'SunDM12' 4 _password = '123456' 5  6 username = input('username :') 7 password = input('password :') 8 print(username,password) 9 10 if _username == username and _password == password11     print("welcome user {name} login...".format(name = username))12 else:13     print("Invalid username or password!")

其中getpass是一个package,提供可移植的面膜输入

1. getpass.getpass()

2. getpass.getuser()

五、猜密码游戏

1 correct_number = 122 guess_number = int(input("guess number:"))3 4 if correct_number == guess_number:5     print("yes,you got it...")6 elif guess_number >correct_number:7     print("think smaller...")8 else:9     print("think bigeer...")

其中input函数输入是一个字符,此时要将其强制转换为整型

5.1 while循环

1 count = 02 while True:3     print("count :",count)4     count = count +15     if count == 1000:6         break

其中break表示跳出整个循环

correct_number = 12count = 0while count<3:
    guess_number = int(input("guess number:"))if correct_number == guess_number:print("yes,you got it...")elif guess_number >correct_number:print("think smaller...")else:print("think bigeer...")
    count + = 1else:print("you have tried too many times.")

利用while循环进行设计猜数字游戏

5.2 for循环

1 for i in range(10):2     print("loop ",i)

显示1到10

1 for i in range(0,10,2):2     print("loop :",i)

2是步长

correct_number = 12for i in range(3):
    guess_number = int(input("guess number:"))if correct_number == guess_number:print("yes,you got it...")elif guess_number >correct_number:print("think smaller...")else:print("think bigeer...")else:print("you have tried too many times.")

利用for循环进行猜数字游戏

5.3 continue和break的区别

 1 for i in range(10): 2     if i<5: 3         print("loop",i) 4     else: 5         continue 6     print("....") 7  8 for i in range(10): 9     print('-------',i)10     for j in range(10):11         print(j)12         if j>5:13             break

continue忽略当前语句,继续执行下一行

break跳出当前执行的整个循环

以上就是python有哪些需要学习的知识?的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行