What knowledge does Python need to learn?

零下一度
Release: 2017-07-23 13:37:44
Original
1399 people have browsed it

1. The first line of python code:

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

2. Variables:

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

name changes before and after, and name2 = name has changed Assign "SunDM12" to name2. After name changes, name2 will no longer change

3. Interaction:

1 username = input("username : ")2 print(username)
Copy after login

Input function: The user can display input on the interface characters and assigned to 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)
Copy after login

in the first format printed on the screen.

%s is a string; %d is a double precision; %f is a floating point type

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)
Copy after login

The second format for printing on the screen.

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

The third format for screen printing.

4. Login password:

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!")
Copy after login

getpass is a package that provides portable mask input

1. getpass.getpass()

2. getpass.getuser()

5. Password guessing game

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...")
Copy after login

where input The function input is a character, which needs to be forced to an integer type

5.1 while loop

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

break means to jump out of the entire loop

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.")
Copy after login

Use while loop to design number guessing game

5.2 for loop

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

Display 1 to 10

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

2 is the step length

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.")
Copy after login

Use for loop to guess the number game

5.3 The difference between continue and 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
Copy after login

continue ignores the current statement and continues to execute the next line

break jumps out of the entire currently executed loop

The above is the detailed content of What knowledge does Python need to learn?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!