#数据类型
string1 = "hello"
print("字符型:",string1,'\n')
int1 = 123
print("整型:",int1)
print("字符型:",str(int1))
print("16进制型:",hex(int1))
print("8进制型:",oct(int1))
print("2进制型:",bin(int1),'\n')
float1 = 15.79
print("浮点型:",float1)
print("字符型:",str(float1))
print("整型:",int(float1),'\n')
#数据运算
num1,num2 = 5,3
print("数字1:",num1,";数字2:",num2)
num3 = num1 + num2
print("加:",num3)
num3 = num1 - num2
print("减:",num3)
num3 = num1 * num2
print("乘:",num3)
num3 = num1 / num2
print("除:",num3)
num3 = num1 // num2
print("商:",num3)
num3 = num1 % num2
print("余:",num3)
num3 = num1 ** num2
print("幂:",num3,'\n')
hex1,hex2 = 0x1,0x2
print("16进制数据1:",bin(hex1),";16进制数据2:",bin(hex2))
hex3 = hex1 & hex2
print("位与:",bin(hex3))
hex3 = hex1 | hex2
print("位或:",bin(hex3))
hex3 = ~hex1
print("数据2取反:",bin(hex3),'\n')
hex1 = 0x31
print("16进制数据:",bin(hex1))
hex3 = hex1 << 2
print("左移2位:",bin(hex3))
hex3 = hex1 >> 2
print("右移2位:",bin(hex3),'\n')
#条件语句
num1,num2 = 1,2
if num1 < num2:
print("if判断:a小于b\n")
elif num1 == num2:
print("if判断:a等于b\n")
else:
print("if判断:a大于b\n")
#循环语句
for i in range(1,5,2):
print("for循环:",i)
print("\n")
num1 = 7
while num1 < 10:
print("while循环:",num1)
num1 += 1
else:
print("跳出while循环:",num1,"\n")
#分割字符串并删除空列表
string1 = 'this,,,is,a,,,boy'
list1 = string1.split(',')
print("列表长度:",len(list1),"列表:",list1)
print("列表中查找空个数:",list1.count(''))
while list1.count(''):
list1.remove('')
print("删除后的列表:",list1)
newString = ' ' .join(list1)
print("添加空连接符的字符串:",newString,"\n")
#字符串转换列表排序函数
def stringToListSort(string,separator):
newList = string.split(separator)
for i in range(0,len(newList)):
for j in range(i,len(newList)):
if int(newList[j]) < int(newList[i]):
temp = newList[i]
newList[i] = newList[j]
newList[j] = temp
return newList
string1 = input("字符串排序:请输入一串数字,以逗号分隔:")
list1 = stringToListSort(string1,',')
print("列表长度:",len(list1),"列表:",list1,"\n")
#字符串转换列表,后移一位函数
def stringToListShiftRightOne(string,separator):
newList = string.split(separator)
temp = newList[0]
for i in range(1,len(newList)):
newList[i-1] = newList[i]
newList[len(newList)-1] = temp
return newList
string1 = input("字符串后移一位:请输入一串数字,以逗号分隔:")
list1 = stringToListShiftRightOne(string1,',')
print("列表长度:",len(list1),"列表:",list1,"\n")
#整型列表
newList = list(map(int,input("整型列表:请输入一串数字,以逗号分隔:").split(',')))
print("整型列表:",newList)
|