First, the Python line of code:
1 " Hello, World " 2 Print (name)
Second, variable:
1 " SunDM12 " 2 name2 = name3print("", Name, name2) 4 5 " Wangba " 6 Print (Name, name2)
Name changes, and name2 = name has assigned "SunDM12" to name2,name changes, name2 no longer changes
Third, Interaction:
1 username = input ("")2print(username)
Input function: The user can display characters on the interface and assign values to the username
1Name = input ("Name:")2Age = Input ("Age :")3Job = input ("Job:")4Salary = input ("Salary:")5 6info =" "7 ------Info of%s------8 Name:%s9 Age :%sTen Job:%s One Salary:%s A " "% (name, name, age, job, salary)
The first format that is printed on the screen.
%s is a string;%d is double;%f is a floating-point type
1 " " 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)
The second format is printed on the screen.
1 " " 2 ------Info of {0}------ 3 name: {0} 4 Age : {1} 5 job: {2} 6 Salary: {3} 7 ". Format (name,age,job,salary)
The third format is printed on the screen.
Four, login password:
1 ImportGetpass2 3_username ='SunDM12'4_password ='123456'5 6Username = input ('Username:')7Password = input ('Password:')8 Print(Username,password)9 Ten if_username = = Username and_password = =Password One Print("Welcome User {Name} login ...". Format (name =username)) A Else: - Print("Invalid username or password!")
Where Getpass is a package with portable mask input
1. Getpass.getpass ()
2. Getpass.getuser ()
Five, guess the password game
1Correct_number = 122guess_number = Int (input ("Guess number:"))3 4 ifCorrect_number = =Guess_number:5 Print("yes,you got it ...")6 elifGuess_number >Correct_number:7 Print("think smaller ...")8 Else:9 Print("Think Bigeer ...")
Where the input function is entered as a character, at which point it is cast to an integral type
5.1 While loop
1 count = 02 while True:3 print(" Count:", Count)4 count = Count +15 if count = = +:6 break
Where break means jumping out of the loop
Correct_number = 12Count=0 whileCount<3: Guess_number= Int (Input ("Guess number:")) ifCorrect_number = =Guess_number:Print("yes,you got it ...") elifGuess_number >Correct_number:Print("think smaller ...") Else: Print("Think Bigeer ...") Count+ = 1Else: Print("You have tried too many times.")
Using while loop to design guessing number games
5.2 For Loop
1 for in range:2 print("", i)
Displaying 1 to 10
1 for in range (0,10,2):2 print("loop:" , I)
2 is the step size
Correct_number = 12 forIinchRange (3): Guess_number= Int (Input ("Guess number:")) ifCorrect_number = =Guess_number:Print("yes,you got it ...") elifGuess_number >Correct_number:Print("think smaller ...") Else: Print("Think Bigeer ...")Else: Print("You have tried too many times.")
Guessing a number game with a for loop
The difference between 5.3 continue and break
1 forIinchRange (10):2 ifI<5:3 Print("Loop", i)4 Else:5 Continue6 Print("....")7 8 forIinchRange (10):9 Print('-------', i)Ten forJinchRange (10): One Print(j) A ifJ>5: - Break
Continue ignores the current statement and proceeds to the next line
Break jumps out of the current execution of the entire loop
The first day of Python learning