Python numeric data, operations and strings (Introduction to Python automated testing 2)

This article is relatively long and introduces some operations on Python numerical types, operations, and strings. The interview questions will also be involved in the step-by-step learning process. I will sort out an interview frequently asked questions later.

One, Python operator

Python operators include arithmetic operators, assignment operators, comparison operators, and logical operators.

1.1 Arithmetic operators

Arithmetic operators are simple addition, subtraction, multiplication, and division

1.1.1 Add

num1 = 100
num2 = 99
sum = num1 + num2
print(sum)

operation result:

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe E:/python程序小项目/day17/one.py
199

1.1.2 Minus

num1 = 100
num2 = 99
dec = num1 - num2
print(dec)

Calculation result:

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe E:/python程序小项目/day17/one.py
1

1.1.3 Multiply

num1 = 100
num2 = 99
mul = num1 * num2
print(mul)

Calculation result:

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe E:/python程序小项目/day17/one.py
9900

1.1.4 Division

num1 = 100
num2 = 99
div = num1 * num2
print(div)

Calculation result:

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe E:/python程序小项目/day17/one.py
1.0101010101010102

1.2 Assignment operator

Assignment operators include +=, -=, *=, /=

1.2.1 +=

sum+=100, Which is equal to sum = sum + 100 and the
rest of the assignment operators are the same

1.3 Comparison operators

Comparison operators include ==,! =,>, <, >=, <=
comparison operators run results are Boolean values ​​(False, True)

num1 = 100
num2 = 99
print(num1 == num2)
print(num1 != num2)

print(num1 >= num2)
print(num1 <= num2)

print(num1 < num2)
print(num1 > num2)

operation result:

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe E:/python程序小项目/day17/one.py
False
True
True
False
False
True

1.4 Logical operators

Logical operators include and (and), or (or), and not (not)
. The operation result of logical operators is also a Boolean value (False, True)

1.4.1 and

All conditions are true, the result is true

user_age = int(input('请输入你的年龄:'))
user_city = input('请输入你的城市:')
res = user_age < 30 and user_city == '上海'
print(res)

operation result:

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe E:/python程序小项目/day17/one.py
请输入你的年龄:31
请输入你的城市:上海
False

1.4.2 或(or)

If any condition is true, the result is true

user_age = int(input('请输入你的年龄:'))
user_city = input('请输入你的城市:')
res = user_age < 30 or user_city == '上海'
print(res)

operation result:

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe E:/python程序小项目/day17/one.py
请输入你的年龄:31
请输入你的城市:上海
True

1.4.3 Not

Contrary to the actual calculation result

user_age = int(input('请输入你的年龄:'))
user_city = input('请输入你的城市:')
res = not(user_age < 30 or user_city == '上海')
print(res)

operation result:

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe E:/python程序小项目/day17/one.py
请输入你的年龄:31
请输入你的城市:上海
False

Two, Python string operations

2.1 Definition

  1. Single quotation marks, double quotation marks: define a single-line string
  2. Triple quotes, triple double quotes: define multi-line strings
  3. Empty string: s=''
  4. Converted to a string: str()

Note: When there are single quotation marks in the string (the outside is distinguished by double quotation marks; the outer double and inner single, the outer single and inner double, the same as inside and outside use escape)
such as:s = "hello,py\"30\"!!"

2.2 Index value

Get the character at the specified position by index

2.2.1 Sequential Index

The subscript of the sequential index starts from 0

s = 'hello,py33'
print(s[0])
print(s[4])

operation result:

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe E:/python程序小项目/day17/one.py
h
o

2.2.2 Reverse Index

The subscript of the reverse index starts from -1

s = 'hello,py33'
s_len = len(s)  # 字符串长度
print(s[s_len-1])  # 字符串长度减1
print(s[-1])  # 倒序,字符串最后一位

operation result:

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe E:/python程序小项目/day17/one.py
3
3

2.3 Index slice

2.3.1 Format

  1. String [start index: end index: step size]
  2. The default starting index is 0, and the default step size is 1
  3. Left closed and right open: including the beginning, not including the end
  4. If the step length is positive, it means positive cut; if the step length is negative, it means reverse cut
  5. String reversal: [::-1] (Note: This is often asked in interviews)
s = "monty,python"

print(s[:3])  # 下标从0开始,结束下标为2,默认步长为1   [0,1,2]   取头不取尾
print(s[:])  # 下标从0开始,一直到字符串最后。步长为1
print(s[2:]) # 下标从2开始,一直到最后。

print(s[::2]) # 下标从0开始,下标+2
print(s[1::2]) # 下标从1开始,下标+2

# 如果步长是正数,正向取。如果步长是负数,倒序取。
print(s[::]) # 全取
print(s[::-1]) # 倒序 - 反转

operation result:

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe E:/python程序小项目/day17/one.py
mon
monty,python
nty,python
mnypto
ot,yhn
monty,python
nohtyp,ytnom

2.4 Common operations

After entering s in pycharm, the drop-down box will come out
Insert picture description here

2.4.1 Find (find)

s = "monty,python"
res = s.find(",") # 参数就是子字符串。查找结果为下标。
print(res)

res = s.find("PY")  # 如果找不着,就返回-1
print(res)

res = s.find("py") 
print(res)

operation result:

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe E:/python程序小项目/day17/one.py
5
-1
6

2.4.2 Return subscript (index)

s = "monty,python"
res = s.index("t")
res2 = s.index("t",4)
res3 = s.index("th")
print(res3)

operation result:

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe E:/python程序小项目/day17/one.py
8

Note: When the searched character does not exist in the string, the operation result will report ValueError: substring not foundan error

2.5 splicing and truncation

2.5.1 split

  1. Delimiter
  2. Specify the number of separations
  3. The default is to use spaces as the separator
s = 'keke 九三 数 水滴 小婵 秋呆呆'
res = s.split(" ")
# res = s.split()  # 默认就是以空格作为分隔符
# res = s.split(" ",2)  # 运行结果:['keke', '九三', '数 水滴 小婵 秋呆呆']
print(res)

operation result:

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe E:/python程序小项目/day17/one.py
['keke', '九三', '数', '水滴', '小婵', '秋呆呆']

2.5.2 join

  1. The strings in the list are spelled into one string. The members in the list must all be strings.
  2. Combine the members of s into a string with the number.
  3. Usage: splicing character.join (list) splicing character is also a string type.
s = ['keke', '九三', '数', '水滴', '小婵', '秋呆呆']
new_str = "2 ".join(s)
print(new_str)

operation result:

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe E:/python程序小项目/day17/one.py
keke2 九三22 水滴2 小婵2 秋呆呆

2.5.3 Irregular splicing

Example: Concatenate two strings at will

s1 = 'hello'
s2 = 'python'
print(s1 + ',' + s2)

operation result:

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe E:/python程序小项目/day17/one.py
hello,python

2.6 Formatted output

2.6.1 format

Format: string. format(): {}

age = input("请输入你的年龄:")
height = input("你的身高:")
city = input("你的城市:")

s = "hello,大家好,我今年{}岁!".format(age)

# 用法一  每一个{}都有一个值替换
s1 = "hello,大家好,我今年{}岁,身高{},我在{}".format(age, height, city)

# 用法二
s2 = "hello,大家好,我今年{}岁,身高{},我的大学在{},我的工作城市在{}".format(age, height, city, city)
s3 = "hello,大家好,我今年{0}岁,身高{1},我的大学在{2},我的工作城市在{2}".format(int(age), height, city)

print(s1)
print(s2)
print(s3)

operation result:

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe E:/python程序小项目/day17/one.py
请输入你的年龄:24
你的身高:160
你的城市:成都
hello,大家好,我今年24岁,身高160,我在成都
hello,大家好,我今年24岁,身高160,我的大学在成都,我的工作城市在成都
hello,大家好,我今年24岁,身高160,我的大学在成都,我的工作城市在成都

If the output data needs to retain two decimal places, then use .2f
as follows:

num1 = 100.236
s = "我今天买了个liulian,价格为{:.2f}".format(num1)
print(s)

operation result:

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe E:/python程序小项目/day17/one.py
我今天买了个liulian,价格为100.24

2.6.2 f expression

The f expression can only be used above
Python 3.6. I personally prefer to use this method to format the output, which is relatively simple, but you must make sure that the Python version is 3.6 or above before using it, otherwise an error will be reported

age = input("请输入你的年龄:")
height = input("你的身高:")
city = input("你的城市:")
s = f"hello,大家好,我今年{age}岁,身高{height},我在{city}"
print(s)

operation result:

请输入你的年龄:24
你的身高:160
你的城市:成都
hello,大家好,我今年24岁,身高160,我在成都

2.6.3% (Just understand)

  1. %s (formatted string)
  2. %d() formatted integer
  3. %f (format floating-point numbers, you can specify the precision after the decimal point)
age = input("请输入你的年龄:")
height = input("你的身高:")
city = input("你的城市:")
s = "hello,大家好,我今年%d岁,身高%s,我在%s" % (int(age),height,city)
print(s)

operation result:

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe E:/python程序小项目/day17/one.py
请输入你的年龄:24
你的身高:160
你的城市:成都
hello,大家好,我今年24岁,身高160,我在成都

2.7 escape characters

  1. Newline (\n)
  2. Output a \, that is, unescaped (\)
print("D:\\Backup\\我的文档")
print(R'D:\Backup\我的文档')  # 大小r都可以

operation result:

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe E:/python程序小项目/day17/one.py
D:\Backup\我的文档
D:\Backup\我的文档

There are many escape characters, here are two commonly used ones. If you need to look at others, you can go to the official website to check the specific usage.

This is the second study note, I hope the big guys have seen a lot of advice.

Guess you like

Origin blog.csdn.net/dhl345_/article/details/109310228