Python basics-02 output input operator data type conversion

1. Output

  • Output the data in the specified format to the standard console or the specified file object

1.1print()

print('Hello World')

1.2 Formatted output

  • Role: to simplify the procedure
a = 'python'
print('I love %s' %a)
Format symbol Conversion
%c character
%s String
%d Signed decimal integer
% u Unsigned decimal integer
%O Octal integer
%x Hexadecimal integer (lowercase letter 0x)
%X Hexadecimal integer (uppercase letter 0X)
%f Floating point
%e Scientific notation (lowercase'e')
%E Scientific notation (uppercase "E")
%g Shorthand for %f and %e
%G Shorthand for %f and %E

1.3 Newline output:\n

If there is \n in the output, the following content will be displayed on the next line

print('123\nABC')
123
ABC

2. Enter

  • input()
  • Receive user keyboard input
a = input('请输入姓名:')
print(a)
  • input() The input data are all string types

3. Operator

3.1 Arithmetic operators

Operator description Instance
+ plus 1+1=2
- Less 1-2=-1
* Multiply 2*3=6
/ except 2/2=1
// Divide 7//2=3
% Take the remainder 7%2=1
** index 2**3, which is 2 to the 3rd power
  • In mixed operation, the order of precedence is: ** higher than * /% // higher than + -, in order to avoid ambiguity, it is recommended to use () to handle operator precedence.
  • When performing mixed operations on different types of numbers, integers will be converted into floating-point numbers for operations.

3.2 Assignment operator

Operator description Instance
= Assignment operator Assign the result on the right side of the = to the variable on the left a = 1+1, the value of a is 2

3.3 compound assignment operator

Operator description Instance
+= Addition assignment operator c+=a is equivalent to c = c+a
-= Subtraction assignment operator c-=a is equivalent to c=ca
*= Multiplication assignment operator c*=a is equivalent to c=c*a
/= Division assignment operator c/=a is equivalent to c=c/a
%= Modulus assignment operator c%=a is equivalent to c=c%a
**= Power assignment operator c**=a is equivalent to c=c**a
//= Divide and Divide Assignment Operator c//=a is equivalent to c=c//a

4. Data type conversion

Common data type conversion

function Description
int(x [,base]) Convert x to an integer
float(x) Convert x to a floating point number
complex(real [,imag]) Create a complex number, real is the real part and imag is the imaginary part
str(x) Convert object x to string
repr(x) Convert object x to expression string
eval(str) Used to calculate valid Python expressions in a string and return an object
tuple(s) 将序列 s 转换为一个元组
list(s) 将序列 s 转换为一个列表
chr(x) 将一个整数转换为一个Unicode字符
ord(x) 将一个字符转换为它的ASCII整数值
hex(x) 将一个整数转换为一个十六进制字符串
oct(x) 将一个整数转换为一个八进制字符串
bin(x) 将一个整数转换为一个二进制字符串

5.布尔类型

类型 描述
True 空字符为False,其余都是True(空格视为一个字符)
False 空字符、None、0

6.字符串加减

类型 描述
字符串&数字 不能相加
字符串 数字 相乘 把字符串复制数字的倍数
字符串 字符串 相加 字符串拼接

Guess you like

Origin blog.csdn.net/weixin_47761086/article/details/108517001