Python _ basic syntax,
I. Variables
Variables are used to store intermediate results during program operation, and to facilitate future calls, operations, or changes to programs. It also provides a way to mark descriptive data for a clearer understanding. It is easier to understand variables as a container for saving information. Variables are stored in the memory and will apply to the entire program.
Declare Variables
#_*_coding:utf-8_*_name = "Imomy Mu"
Naming rules for variables:
- To be descriptive
- Variable names can contain only _, numbers, and letters. They cannot contain spaces or special characters (#? <., ¥ $ *!~)
- Cannot use Chinese as the variable name
- Cannot start with a number
- Reserved characters cannot be used
Variable assignment
name = "Imomy Mu" name2 = nameprint(name,name2) name = "Jack"print("What is the value of name2 now?")
Ii. user input
Use the input command.
name = input("What is your name?")print("Hello " + name )
All the data accepted by input is a string. Even if you input a number, it is still processed as a string. However, sometimes we need to input the data information, or an integer. In this case, we need to use data type conversion to convert the input data.
Int integer = integer converts a string to int and uses int (converted data)
Str string = convert data to a string using str (converted data)
Assume that a person can live for 80 years and determine how many years the user can live.
death_age = 80 name = input("your name:")age = input("your age:") print("Your name:",name)#print("You can still live for ", death_age - int(age)," years ....")print("You can still live for " + str(death_age - int(age)) +" years ....")