double*pd=NULL;
pd=(double*)malloc(10*sizeof(double));
表示将向系统申请10个连续的double类型的存储空间,并用指针pd指向这个连续的空间的首地址。并且用(double)对malloc()的返回类型进行转换,以便把double类型数据的地址赋值
python中使用int(),float(),str()进行类型转换
使用input()输入信息,输入的数据类型为字符串,可以使用类型转换,weight = float (input("shuru"))
print(cost)
7.2
可通过字符串格式化控制小数点位数
print('%.3f' % cost)
7.200
print('%.0f' % cost)
7
x**6 表示x的六次方
翻转123.456:mystr='123.456'
mystr=float(mystr[::-1])//用此法反转字符串
print(mystr)
多路判断使用elif
若try中的代码正常运行,则忽略expect中的代码
若try中的代码出现错误,则运行expect中的代码
使用random 产生随机数 randrange(start,stop,step)
start最小值,包含在范围内 stop最大值,不包含在范围内 step递增的步长
random.randrange(100) 输出0-100
random.randrange(100,200) 输出100-200
random.randrange(0,4,2) 输出0或2
猜数字:import random
x=random.randrange(0,100)
count=0
while True:
count = count + 1
y=int(input("shuru"))
if y
if y>x:
print('caidale')
if y==x:
print('zhengque',count)
break;
for循环:x=[1,2,3]
for y in x:
print(y)
输出1/2/3
import turtle
t=turtle.Turtle()//可画图
输出一百以内质数:
for i in range(2,100):
f = True
for j in range(2,i):
if i%j==0:
f = False
break
if f:
print(i)
abc三个数从小到大输出:
if a>b: a,b=b,a
if a>c: a,c=c,a
if b>c: b,c=c,b
print(a,b,c)
使用Len获取字典,字符串,列表的长度
列表中可以有整数,浮点数,字符串
遍历列表的两种方法:
1.for friend in friends:
print(friend)
2.for i in range(len(friends))
friend = friends[i]
print(friend)
第二种方法可迭代修改列表中的值
列表+列表:连接
列表*整数:重复
列表的分片(slice)
t=[19,41,12,3,74,15]
t[:4]=[9,41,12,3]
t[1:-1]=[41,12,3,74]
通过input()向列表中输入元素
numlist = list()
while True:
inp = input()
if inp = 'done'
break
value = float(inp)
numlist.append(value)
字符串只能查不能添删改,字符串也可用下标查找
s = 'banana'
pos = s.find('b') 返回0
也可查找更短的字符串 pos = s.find('ana') 返回1
若没有找到,则返回-1
字符串的分片与列表完全相同
查找字符串是否存在,若存在则删除:
while True:
pos = s1.find(s2)
if pos ==-1:
break;
s_before = [:pos]
s_after = [pos + len(pos):]
s = s_before + s_after
使用split()分割字符串,结果为列表
使用join()合并列表,结果为字符串
使用list()将字符串转换成列表
列表中每个元素都应当是字符串
s = 'hello world'
s.split(' ')
['hello' 'world']
'*'.join(['you','she','he'])
'you*she*he'
字符串大小写转换:
使用upper()将字母转大写 s.upper()
使用lower()将字母转小写 s.lower()
使用capitalize()将首字母转大写 s.capitalize()
s.title() #把每个单词的第一个字母转化为大写,其余小写
定义一个字典:1.cipher = {}
2.cipher = dict()
bag = dict()
bag['money'] = 32
bag['candy'] = 3
print(bag)
{'money':32,'candy':3}
print(bag['candy']) 返回3
bag['candy'] = bag['candy'] + 2
print(bag)
{'money':32,'candy':5}
列表与字典的对比:
列表用中括号,字典用大括号
列表由一个个变量组成
字典由一对对key-value组成
列表是有序的,字典是无序的
列表用索引访问,字典用key访问
用字典统计一串数字中各个数字出现的次数:
numbers = '1343465789'
count = {}
for i in numbers:
if i in count:
count[i] += 1
else:
count[i] = 1
print(count)
打乱和拷贝:
使用shuffle()打乱一个列表
使用copy()复制一个列表
深拷贝 与 浅拷贝的不同:
浅拷贝:
import random
source = [1,2,3,4]
target = source
random.shuffle(target)
print(source,target) 返回值[2, 1, 4, 3] [2, 1, 4, 3] target和source的顺序都发生变化
深拷贝:
import random
source = [1,2,3,4]
target = source.copy()
random.shuffle(target)
print(source,target) 返回值[1, 2, 3, 4] [3, 1, 2, 4] source顺序不变,target顺序变化



