Run the first program hello_world.py
#_ *_coding:utf-8_*_print ("Hello world!")
Output Result:
Hello world!
Variable
#_ *_coding:utf-8_*_name = "Beyoungt"
#_ *_coding:utf-8_*_name = "Beyoungt" Print (name) name = "Abby" Print (name)
Output Result:
Beyoungtabby
You can modify the value of a variable at any time in your program, and Python will always record the latest value of the variable.
String:
In Python, a string is enclosed in quotation marks. It can be single or double quotes.
"This is a string." This is also a string. '
Use the method to modify the case of the string:
Name = "Beyoungt" Print (Name.title ())
Output:
Beyoungt
Title (): Change the first letter of each word to uppercase.
Upper (): Change all strings to uppercase.
Lower (): Change all strings to lowercase.
Concatenation of strings:
first_name = "Tian" last_name = "beyoungt" full_name = first_name + "" + last_nameprint ("Hello," + full_name.title+ "!")
Output:
Hello,tian beyoungt!
Formatted output of the string:
Name = "Beyoungt" Print ("My name is%s"%name) #输出: My name is Beyoungt
%s: string%d: integer%f: floating-point number
Name = input ("Name:") age = Int (input ("Age:")) #integerjob = input ("job:") Salary = input ("Salary:") info = "'-------- Info of %s -----name:%sage:%djob:%ssalary:%s "% (name,name,age,job,salary) Info2 =" '--------info of {_name } -----Name:{_name}age:{_age}job:{_job}salary:{_salary} ". Format (_name=name, _age=age, _job=job, _salary=salary) Info3 = "--------info of {0}-----name:{0}age:{1}job:{2}salary:{3}". Format (Name,age, job,salary) print (INFO3)
Add tab: \ t
Add line break: \ n
Print ("Languages:\n\tpython\nc\n\tjavascript") Output: Languages: pythonc JavaScript
Delete blank:
Favorite_language = " python " Favorite_language.rstrip () #删除末尾空白favorite_language. Lstrip () # Delete the beginning Blank Favorite_language.strip () #删除两端空白
Module:
#! Usr/bin/env python#-*-coding:utf-8-*-# Author burnywenimport getpass_username = ' Burnywen ' _password = ' 123456 ' username = Input ("username:") #password = Getpass.getpass ("Password:") #fails to work well in Pycharm;password = input (" Password: ") if _username = = Username and _password = = Password:print (" Welcome user {name} login ... ". Format (Name=username) ) Else:print ("Invalid username or password!")
The Getpass module hides the password.
Cycle:
#! Usr/bin/env python#-*-coding:utf-8-*-# Author burnywencount = 0while true:print ("Count:", count) count +=1if count = = 100:break
#! Usr/bin/env python#-*-coding:utf-8-*-# Author burnywenfor i in Range (0,10): If I <3:print ("loop", i) Else:continueprin T ("Oh ...")
#! Usr/bin/env python#-*-coding:utf-8-*-# Author burnywenfor i in range:p rint ('----------', I) to J in Range (Ten):p rint ( j) If J >5:break
Guess mini games and improvements:
#! Usr/bin/env python
#-*-coding:utf-8-*-
# Author Burnywen
Age_of_burnywen = 23for i in range (3): guess_age = int (input ("Guess Age:")) if guess_age = = Age_of_burnywen:p rint ("Co Ngratulations, you is right. ") Breakelif guess_age > Age_of_burnywen:print (" Think smaller ... ") Else:print (" Think bigger ... ") Else:print (" You Have tried too many times. ")
#! Usr/bin/env python#-*-coding:utf-8-*-# Author Burnywen
Age_of_burnywen = 23count = 0while count <3: guess_age = int (input ("Guess Age:")) if Guess_age = = Age_of_burnywen: Print ("Congratulations, you are ringht.") Breakelif guess_age > Age_of_burnywen:print ("Think smaller ...") Else:print ("Think bigger!") Count +=1else:print ("You have tried too many times.")
#! Usr/bin/env python#-*-coding:utf-8-*-# Author Burnywenage_of_buruywen =23count = 0while count <3:guess_age = Int (i Nput ("Guess Age:")) if guess_age = = Age_of_burnywen:p rint ("Congratulations, you is right.") Breakelif guess_age > Age_of_burnywen:print ("Think smaller ...") Else:print ("Think bigger!") Count +=1if Count = = 3:countine_confirm = Input ("Does want to keep guessing ...") If countine_confirm! = ' n ': Count =0else:print ("You have tried too many times.")