python

#注:在python中需要注意代码之间的缩进,通常以一个tab的距离表示隶属关系

import os #1、利用import语句进行导入模块,用逗号分隔可以导入多个包

import math,copy,random,time

from collections import Counter  #2、利用from...import ....进行导入

import numpy as np  #3、利用as关键字重命名包名,以后再使用就可以直接用np了

def hello_world():  #4、利用def关键字创建函数,简单来说:函数就是将具有独立功能的代码块组织成一个模块,需要的时候调用

#创建函数格式:def name(参数1,参数2....):

yourname = input('你好,请输入你的名字:')  #5、输入函数,input(),若input中有字符串可以输出

print('欢迎来到Python的世界,',yourname)  #6、输出函数,print(),若要输出多个对象,利用逗号分隔

print('让我们开始学习吧~')

def hello_twice():

global yourname,yourheight,yourweight  #7、利用global关键字定义全局变量,使之在整个程序运行周期能够被调用

yourname = input('请输入你的名字:')

yourheight = input('请输入你的身高:')

yourweight = input('请输入你的体重:')

#python中字符串的部分操作

def deviding_line():

word1 = 'i am line'  #8、字符串的创建,利用单引号' 或者双引号" 进行创建

word2 = word1.upper()  #9、字符串的函数,利用运算符.进行调用,该语句中的upper()可以将字符串转换为全大写字母

word3 = word1.lower()  #10、lower()函数,将字符串转换为全小写

word4 = word1.title()  #11、title()函数,可以将字符串标题化

#以上三个函数仅为字符串的部分函数

words = [word1,word2,word3,word4]  #12、[]可以创建一个列表,列表可以存放很多的对象

line = '-' * 40  #13、利用*运算符创建串,这里就是有40个-符号

endReturn = line+words[random.randint(0,3)]+line #14、字符串可以利用+号直接相连

#15、上面出现的random.randint()可以创建随机整数,0和3为随机数的上下限

return endReturn  #16、函数返回值,可以在被调用时将这个值返回

#学习python中的数字模型

def study_number():

num1 = input('请输入一个数字:')

print('你输入的是数字%s'%num1,'可它的类型为:',type(num1)) #17、输出函数格式控制

#18、type()函数可以返回该值的类型

num2 = int(input('再输入一个数字:'))  #19、利用int()函数进行数值类型转换,将数字转换为int整型

print('你输入的是数字%s' % num2, '它的类型为:', type(num2))

num3 = float(input('再输入一个数字:'))  #20、float()函数可以转换为浮点数类型

print('你输入的是数字%s' % num3, '它的类型为:', type(num3))

print('num1+num2={}'.format(int(num1)+num2)) #21、数字加法

# 22、format()函数格式化输出,在字符串中的{}符号将被替换为format()的参数

print('num1-num2={}'.format(int(num1)-num2))  #23、数字减法

print('num1*num2={}'.format(num1*num2)) #24、num1*num2并不会是你想要的数据,因为input()函数,默认输入为字符类型

print('num1*num2={}'.format(int(num1) * num2))  #25、数字乘法

print('num2//num3={:.3f}'.format(num2//num3)) #26、数字整除,同时{:.3f}表示输出格式小数点后面保留三位

print('num2/num3={:.4f}'.format(num2/num3)) #27、数字除法,保留小数点后四位

print('num2%num3={:.4f}'.format(num2 % num3)) #28、求余数

print('num2%num3={:.4%}'.format(num2%num3)) #29、求余数,{:.4%}输出格式为百分比格式

print('num1**num2={}'.format(int(num1)**num2))  #30、幂运算

print('This is the {a},and {b}'.format(a='numbers',b='some operations')) #31、format多参数,标记位置对应输出

one,two,three = True,True,False  #32、bool值

print(one,two,three)

print('and运算符:',one and two,one and three) #33、and运算,当两个值同时为真时才为真

print('or运算符:',one or two,one or three) #34、or运算符,当两个值同假时为假

print('not运算符:',not one,not two,not three)  #35、not运算符,得到相反的值

#学习python中的列表模型

def study_list(length): #36、带有参数的函数

l1 = [1,2,3,4,5,9.0]  #37、创建列表,利用符号[]

l2 = list(range(10,10+length))  #38、创建列表,也可以用list()

#39、range()函数,可以创建一个整数列表,格式为range(start,end,step),start为开始位置,end为结束位置,前闭后开,step为步长

print('l1的类型为:',type(l1))

print(l1[1],l2[1])  #40、访问列表值,可以直接用list[num]的方式进行访问

l3 = l2  #41、将l2的引用赋给l3

print(id(l1),id(l2),id(l3)) #42、id()函数可以获取对象的内存地址,在这里可以看到l3的的地址和l2是一样的

l3[0]=99  #43、更新列表值

print('l2==l3么?',l2==l3)  #44、更新l3后依旧等于l2,因为l3和l2本来就是一个对象,不过换了个名字

l4 = l2.copy()  #45、复制一个l2给l4,copy()创建一个一模一样的列表

l4[0]=999

print('l4==l2么?',l4==l2)  #46、此时l4不等于l2

print('删除前',l4)

del l4[0]  #47、del语句进行删除列表值,在python中del可以删除所有的变量

print('删除后',l4)

l4.append(30)  #48、给列表添加值

l4.extend(l1)  #49、给列表追加一个序列多个值

print('添加l1后:',l4)

l4.reverse()  #50、列表反转

print('反转后:',l4)

l4.sort()  #51、sort()函数,将列表进行排序

print('排序后:',l4)

#学习python中的元组模型

def study_tuple(length:int)->bool:  #52、解释参数类型的函数创建,->为返回值类型

tuple1 = (1,2,3,4)  #53、创建元组,利用()符号,元组的特性是不可以改变

tuple2 = tuple(range(10,10+length))  #54、利用tuple创建元组

print(tuple1.count(1))  #55、元组函数count(),用于输出某个值的数量

print(tuple1.index(1)) #56、元组函数index(),可以按照索引得到值

try:    #57、python中的异常处理,try:语句内部如果出现错误则会转入到except中

tuple1[0] = 9  #58、因为元组的不可改变性,所以该语句会出错

except TypeError:

print('元组插入失败')

finally:  #59、finally内语句不管是否出现错误都会执行

print('不管插入成不成功我都会执行')

try:

print(id(tuple1),id(tuple2))

except:

return False

else:

tuple3 = tuple1+tuple2  #60、元组虽然不可改变,但是可以通过+号进行合并为另一个元组

print(tuple3,id(tuple3))

return True

def study_dict():  #学习python中的字典模型,字典是  键->值 的映射

dict1 = {1:'一',2:'二',3:'三',4:'四'}  #61、以下为创建字典的5种方法

dict2 = dict(one=1,two=2,three=3)

dict3 = dict(zip([6,7,8,9],['Six','Seven','Eight','Nine']))

dict4 = dict([('One',1),('Two',2),('Three',3)])

dict5 = dict({1:'一',2:'二',3:'三',4:'四'})

print(type(dict1),dict1==dict5)  #62、可以看到,dict1和dict5是等价的

print(dict1[1],dict2['one'],dict3[6],dict4['One'],dict5[1])  #63、通过字典的键访问

print(dict1.get(4)) #64、通过get函数访问内容

dict1[1] = '壹' #65、修改字典内容

dict1[5] = '五' #66、添加字典

print(dict1)

print(1 in dict1, 6 in dict1, 7 not in dict1) #67、in和not in关键字,可以判断值是否在序列中

dict6 = dict1.copy()  #68、字典的复制

dict6[1] = 'One'

print(dict1,'<dict1------------dict6>',dict6)

dict1.clear() #69、字典的清空

print(dict1)

del dict1,dict2,dict3,dict4,dict5,dict6 #70、删除字典,也可以用del dict[key]的方式删除某个键

def study_set(): #python中集合的学习,集合中不存在相等的值

set1 = set(['You','Are','Not','Beautiful']) #71、利用set()函数进行创建集合

set2 = {'You','Are','So','Beautiful'}  #72、利用{}创建集合,创建空集合的时候不能用{},因为{}表示字典

set3 = set2.copy() #73、集合的复制

print(type(set1))

print(set1,set2)

print(set1|set2)  #74、集合或运算符,得到两个集合中所有元素

print(set1&set2)  #75、集合与运算符,得到两个集合共同元素

print(set1^set2)  #76、不同时包含于set1和set2的元素

print(set1-set2)  #77、集合差运算,得到set1有,set2没有的元素

print(set1<=set2,set3<=set2,set3<set2)  #78、<=符号,判断是否为子集,<符号,判断是否为真子集

set1.add('Me too') #79、集合添加元素

print('is语句用法',set3==set2,set3 is set2,set1 is not set2) #80、is和is not语句,is语句用于判断对象是否一样,==判断值是否一样

set3.clear()  #81、清空集合,集合变为空

print(set3)

del set3

def study_Some_functions(): #python中一些函数

list1 = [1,2,3,4,5,6]  #同学们,眼熟不,这就是之前的列表,下面的这些大家都认认是啥

tuple1 = (11,12,13,14,15,16)  #元组

set1 = set(list1)  #集合

dict1 = dict(zip([1,2,3,4,5],['one','Two','Three','Four','Five']))  #字典

print(max(list1),max(tuple1),max(set1),max(dict1))  #82、max()函数,得到序列中最大值

print(min(list1),min(tuple1),min(set1),min(dict1))  #83、min()函数,得到最小值

print(sum(list1),sum(tuple1),sum(set1),sum(dict1))  #84、sum()函数,得到序列和

print(len(list1),len(tuple1),len(set1),len(dict1))  #85、len()函数,得到序列长度

print(divmod(list1[0],tuple1[0]))  #86、divmod()函数,计算两个数的商和余数,结果两个格式为(商,余数)

print(list(enumerate(tuple1)))  #87、enumerate(),给元组添加一个索引

list2 = list(tuple1)  #88、利用list()将元组,字典等等转换为列表

list3 = list(set1)

list4 = list(dict1)

tuple2 = tuple(list1)  #89、利用tuple()将列表,字典等转换为元组

print(list2,list3,list4)

for i in range(len(list1)):  #90、for循环语句

print(list1[i],end=' ')  #91、print的属性end,可以使输出格式为end的内容,而不是默认换行

print()

for i in dict1:  #92、for循环遍历

print(i,dict1[i],end=' ')

list5 = list(reversed(list1))  #93、reversed()函数,可以反转序列

print('\n',list5)  #94、\n,换行符

testStr = "The mountains and rivers are different, the wind and the moon are the same"

words = testStr.split(' ')  #95、split()函数,以split()内参数分割字符串,返回一个列表

print(words)

words.sort(key=len)  #96、sort()函数,进行排序,参数key=len时,以字符串长度为标准排序

print('以长度排序:',words)

words.sort(key=len, reverse=True)  #97、reverse参数,结果反转

print('以长度排序并且反转:', words)

words.sort(key=str)  #98、以字典序进行排序

print('以字典序排序:',words)

ct = Counter(testStr)  #99、collections模块中的Counter,可以得到字符串中每个数字出现次数

print(ct)

ct.update('eeeexxxxxlllll')  #100、更新

print(ct)

print(ct.most_common(5))  #101、得到字符数最多的前五位

def study_Slice():  #python的切片操作,得到序列的部分内容

str1 = 'I hope one day, I can find you, my sweet dream'

list1 = list(range(10))

tuple1 = tuple(list1)

print(str1[:])  #102、切片格式为str[start:end:step],前闭后开,step可为正负,默认步长为1

print(str1[::-1])  #103、当步长为负数的时候,反转

print(str1[:15])  #104、只有end时,截取最开始到end

print(str1[15:])  #105、只有start时,截取从start到末尾的所有字符

print(str1[::2])  #106、步长为2

print(str1[1::2])

print(list1[:])  #107、和str一样

print(list1[2:])

print(list1[:2])

print(list1[::-1])

list1[1:5] = [10] #切片赋值,右边必须为一个可以遍历的序列

#list1[1:5] = 10  这样就会报错

print(list1)

def study_loop_select():  #python中的循环和选择语句

list1 = [1,2,3,4,5]

num = int(input('while循环,输入你想要循环的次数:'))

i = 1

while i<=num:  #108、while expression:当expression为真的时候进行循环

if i<5:  #109、if...elif...else选择语句,如果判断结果只有两个,可以使用if...else

print('我打印了',i,'次')

elif i<10:

print('打印了',i,'次,真累啊')

elif i<15:

print('打印太多啦,再打印我就要停止了...')

elif i<20:

print('continue...')

i+=1

continue  #110、continue语句,用在循环中,continue后的所有语句都不允许,直接进入下次循环

print('我想我可能输出不了了')

else:

print('累死我了,休息。都',i,'次了~_~')

break  #111、break语句,运用在循环中,直接退出循环,所以,在本例子中,这个循环最多循环20次

i+=1

time.sleep(0.5)  #112、time库为python中的时间库,time.sleep(second)可以使程序暂停运行second秒

else:  #113、while循环后接一个else语句,当执行完所有循环后执行一次,可以省略(个人感觉用处不大)

print('while结束了')

for i in list1:  #113、for循环,上面代码有用到过

print(i,end=' ')

print()

for i in range(5):

print(i)

def study_expression_deduction(): #114、python表达式推导

list1 = [i for i in range(10)]  #115、利用该语句推导出列表

list2 = [x for x in range(20) if x%2==0]  #116、语句中增加if,满足if后表达式的才会是列表

print(list1,'<list1--------------list2>',list2)

print(deviding_line())  #117、函数可以在任何地方被调用,如果是自己调用自己就可以称为递归调用

list3 = [['_'] * 3 for i in range(3)]

print(list3)

fruits = ['Apple','Banana','Pear']

colors = ['Red','Yellow','Green']

suitcolor = [(color,fruit) for color,fruit in zip(colors,fruits)] #118、两个列表合并

print(suitcolor)

cartesian = [(color,fruit) for color in colors for fruit in fruits] #119、两个列表的笛卡尔积

print(cartesian)

dict1 = {fruit:color for fruit,color in suitcolor}  #120、字典的推导,只要是带有键值对的任何序列,都可以推导出字典

print(dict1)

def study_files():

filepath = input('请输入你的文件路径(输入quit退出):')

if filepath=='quit':

return True

try:

file = open(filepath,'w') #121、打开文件,'w'为写格式打开

file.write('哈哈,现在开始写文件')  #122、向文件写入字符串

file.close()  #123、关闭文件

file = open(filepath,'r')  #122、以'r'读格式打开

print('从文件中读出的内容:\n',file.read())  #123、read()函数可以得到文件内容

except FileNotFoundError:

print('文件未找见请重新输入')

study_files()  #124、这就是上面所说的递归调用

except:

print('出现错误,请重新输入路径')

study_files()

class Users():  #125、面向对象编程,python中创建类class,类包含有属性与方法,包括有私有变量,共有变量等等

def __init__(self,name,height,weight):  #126、类的构造方法,创建实例时自动调用

self.name = name

self.height = height

self.weight = weight

self.yanzhi = 100

def display(self): #127、类方法

print('大家好,我是{},身高{},体重{},颜值超高{}'.format(self.name,self.height,self.weight,self.yanzhi))

if __name__=="__main__":  #128、无论之前有什么,程序都会从这里开始运行

hello_world()  #129、所以这是运行的第一句,调用该函数

deviding_line()

try:

print(yourname)  #130、调用完hello_world()函数后,因为在hello_world()函数内部有一个yourname变量,所以我们进行输出,看在这里能不能找见这个变量

except:

print('  未能找见该变量  ')#131、不可能找见这个变量的,因为yourname是局部变量,只存在于hello_world()函数内部

deviding_line()

hello_twice()  #132、因为在该函数中定义了global语句,所以该函数中的变量在以下程序中都可以使用

user = Users(yourname,yourheight,yourweight) #133、实例化对象,创建Users类的实例

user.display()  #134、对象调用方法

#135、在python中,可以用三引号进行多行注释,但是如果用变量接收注释的话也可以是一个有格式的字符串,如下

chooseinformation = '''Input the number of the function you want to Run(quit is exit):

1、study_number 2、study_list

3、study_tuple 4、study_dict

5、study_set 6、study_Some_functions

7、study_Slice 8、study_loop_select

9、study_expression_deduction

10、study_files

'''

deviding_line()

while True: #136、while循环进行运行程序,只有当输入quit时才会退出循环(不过你强制退出当然也可以退出)

input('按键继续') #137、为了让输出不那么快,等待按键后才输出以下内容

print(chooseinformation)

num = input('输入序号:')

#138、在以下if...elif...else选择中,我们来选择运行不同的函数

if num=='quit':

break

elif num=='1':

study_number()

elif num=='2':

study_list(10)

elif num=='3':

study_tuple(10)

elif num=='4':

study_dict()

elif num=='5':

study_set()

elif num=='6':

study_Some_functions()

elif num=='7':

study_Slice()

elif num=='8':

study_loop_select()

elif num=='9':

study_expression_deduction()

elif num=='10':

study_files()

deviding_line()

print('哈哈,恭喜你,这个程序结束咯~')

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

这个程序我写了很久,里面包含了python的一些基本语法,比如说有几种常见数据结构:列表,元组,字典,集合,字符串,也有他们的基本操作,有面向对象的类,循环语句,选择语句,函数的创建,包的导入,文件的读取,切片,表达式推导。

但是这依旧是python中最基础的部分,如果想要精通一门语言,没有自己的亲自实践,不一行行的去敲打键盘,不一个bug一个bug的去寻找,永远也难以掌握它的精髓。

当然,目前的我也有太多太多的知识没有掌握,这也是为什么我只写了这么一个基础的入门例子。并且可能还有一些经不起推敲。

最后,其实我也不是很清楚这个代码是不是能带你入门,如果看完觉得有用的话,还请在评论区给出你的意见或者建议,嘻嘻~

这里有十个python小程序例子:python入门的十个小程序

————————————————

版权声明:本文为CSDN博主「桑梓南」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/the_sangzi_home/article/details/105823432

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335

推荐阅读更多精彩内容