Use the print() method to print HelloWorld
name = "Jenkin Li"
print("My name is ", name)
Because Python 2.x uses ASCII encoding and does not support Chinese by default. You must declare in the file header what encoding the file uses.
# -- coding:utf-8 --
are divided into single-line comments and multi-line comments
# Single-line comments
'''
Multi-line comments
'''
name = input("name: ") age = input("age: ") job = input("job: ") salary = input("salary: ") info = ''' ---------- info of %s --------- Name: %s Age: %s job: %s salary: %s ''' % (name, name, age, job, salary) print(info)
PS: If you use %d, you must use int() to convert to numeric type, input Type defaults to string. In contrast to int(), str() converts a numeric type to a string.
In Python, values and strings cannot be connected through the + sign. They must be converted first
info = ''' ---------- info of {_name} --------- Name: {_name} Age: {_age} job: {_job} salary: {_salary} '''.format(_name = name, _age = age, _job = job, _salary = salary)
info = ''' ---------- info of {0} --------- Name: {0} Age: {1} job: {2} salary: {3} '''.format(name, age, job, salary)
import getpass username = input("username: ") password = getpass.getpass("password: ") print(username) print(password)
It should be noted that the above code cannot be run in IDEs such as PyCharm and must be run in the terminal Run in
type(variable)
count = 0 while count < 3: guess_age = int(input("guess age: ")) if guess_age == age_of_oldboy: print("yes, you got it") break elif guess_age > age_of_oldboy: print("Ooops, think smaller...") else: print("Ooops, think bigger! ") count += 1 else : print("Ooops, you dont got it")
else statement block must be executed before the while exits normally. When the while statement is broken, the else statement block will not be executed.
for i in range(10): print("i value = ", i) # break 后不会运行 else 块 else: print("success ended")
is similar to while … else …. It will only run when the for statement ends normally. It will not run after break
The above is the detailed content of What you need to pay attention to when learning Python. For more information, please follow other related articles on the PHP Chinese website!