Arithmetic operator
2, 1 variable
A variable defines an equal sign (=) to assign a value to the variable
= The left side is the variable name
= The right-hand side is the value of the variable name = value
Money = weight * price print (money)Copy the code
Note: A variable name defines a variable only when it first appears
2, 2 Variable type
Integer, floating point, Boolean, complex, non-numeric, string, list, primitive, dictionary
Type ## You can view the type of a function.Copy the code
2, 3 Calculation between different types of variables
1) Numerical variables can be calculated directly
In: I = 10 f = 10.5b = True print(I *f) print(I *b) print(f*b) out: 105.0 10 10.5Copy the code
Note: In Boolean type True corresponds to 1, False corresponds to 0
2)”+” concatenation string
In: first_name = 'three' last_name = 'liu' print(first_name + last_name) out: threeCopy the code
3) String variables and integers repeatedly concatenate the same string using *
In :print(' 3 '* 10) print(' 3' * 10Copy the code
2, 4 variable input (using codes to get user input through the keyboard)
1) Get from the input function
String variable = input(" prompt ") ## Anything the user enters is considered a string by PythonCopy the code
Note: function, a pre-prepared function, can be used directly without worrying about internal code details
2) Type conversion function
Int (x) ## Converts x to an integer float (x) ## converts x to a floating point numberCopy the code
3) Buy apples
# input apple price = input(' please input apple price: \n') ## convert to float: price = input(' please input apple price: \n') ## Input apple weight = input(' please input apple price: \n') ## input apple weight = input(' please input apple price: \n') ## input apple weight = input(' please input apple price: \n') ## \n') ## convert directly to float: weight = float(input(' please input apple weight: Price = float(price) weight_f = float(weight) price_f = float(weight) + str(money))Copy the code
2, 5 formatted output of variables
%s — string
%d — a signed decimal integer. %06d represents a 6-digit integer preceded by 0
%f – floating point number
%% — prints %
Format:
Print (' format string '% 1) print(' format string' % 2.....)Copy the code
case
Price = float(input(' please input apple price: \n')) weight = float(input(' please input apple weight: \n') \n') ## print(' %(price,weight,money) ') ## print(' %(price,weight,money) ') ## print(' %(price,weight,money)Copy the code