Hello World
Print HelloWorld using the print () method
Name = "Jenkin Li"
Print ("My name is", name)
Coding issues in Python 2.x
Because Python 2.x uses ASCII encoding, the default does not support Chinese, the file header must be used to declare what encoding
#--Coding:utf-8--
Comments for Python
Divided into single-line comments and multiline comments
# single Comment
'''
Multi-line comments
'''
Python Text Formatted output
1. Use placeholders such as%s,%d, etc.
Name = input ("Name:") Age = Input ("Age:") job = input ("job:") salary = input ("Salary:") info = ""----------info of%s-- -------Name:%sage:%sjob:%ssalary:%s '% (name, name, age, job, salary) print (info)
PS: If you use%d, you must convert to a numeric type using int (), and the type of input defaults to a string. In contrast to int (), str () converts a numeric type to a string.
It is not possible to concatenate numbers and strings through the + sign in Python, you must first convert
2. Format the output with parameters
info = "'----------info of {_name}---------Name: {_name}age: {_age}job: {_job}salary: {_salary} '. Format (_name = name, _age = age, _job = job, _salary = salary)
3. Format the output with subscript
info = "'----------info of {0}---------Name: {0}age: {1}job: {2}salary: {3} '". Format (Name, age, job, salary)
Hide user-entered passwords using the Getpass module
Import getpassusername = input ("username:") password = getpass.getpass ("Password:") print (username) print (password)
It is important to note that the above code cannot be run in the IDE, such as Pycharm, and must be run in the terminal
Get the variable type using the type () function
Type (variable)
While ... else statement
Count = 0while Count < 3: guess_age = int (input ("Guess Age:")) if guess_age = = Age_of_oldboy: print ("Yes, You got It ") elif guess_age > Age_of_oldboy: print (" Ooops, think smaller ... ") else: Print ("Ooops, Think bigger!") Count + = 1else: print ("Ooops, you dont got It")
The Else statement block must be executed again while it exits normally, and the Else statement block will not be executed if the while statement is break
For ... else ... Statement
For-I in range: print ("I value =", i) # does not run the else block after break: print ("Success ended")
With while ... else ... Similarly, it will run when the for statement ends normally, and will not run after a break