【C010】Python - 基础教程学习(一)
第一章:基础知识
>>> from __future__ import division #实现斜杠/为除,而不是整除
>>> 1 // 2 #实现整除
>>> 2 ** 3 #实现指数
>>> pow(2,3) #实现指数函数
>>> 0xAF #十六进制
>>> 010 #八进制
>>> x = input("x:") #输入的时候要带着格式,字符串要加引号
>>> x = raw_input("y:") #输入的时候不用加格式,直接转换为字符串>>> import math #导入模块
>>> math.floor(342.34)
342.0 >>> from math import sqrt #下面不用再加math
>>> sqrt(9)
3.0>>> import cmath #复数
>>> cmath.sqrt(-1)
1j>>> print "Hello" #单引号和双引号可以交叉使用,都可以用
Hello
>>> print 'Hello'
Hello
>>> print "Let's go!" #字符串有单,就用双
Let's go!
>>> print '"Hello!"' #字符串有双,就用单
"Hello!"
>>> print 'Let\'s go' #转义字符
Let's go>>> 1+2+3\ #反斜杠可以作为换行符+4+5
15>>> print 'Hello,\nworld' #\n作为换行符
Hello,
world>>> print r'C:\nowhere' #前面加个r就实现原始字符,不再考虑转义字符了
C:\nowhere
>>> print r'C:\Program Files\fnord'
C:\Program Files\fnord
本章的新函数
第二章:列表和元组
参考学习内容:http://blog.sina.com.cn/s/blog_4b5039210100e9yd.html
>>> name = 'Alex'
>>> name[0] #第一个
'A'
>>> name[-1] #从最后一个倒回去
'x'>>> 'Hello'[1] #直接用
'e'>>> fourth = raw_input('Year:')[3]
Year:2005
>>> fourth
'5'
2-1
months = ['January','Feburuary','March','April','May','June','July','August','September','October','November','December']endings=['st','nd','rd'] + 17*['th']\+['st','nd','rd'] + 7 * ['th']\+['st']year = raw_input('Year:')
month = raw_input('Month(1-12):')
day = raw_input('Day(1-31):')month_number = int(month)
day_number = int(day)month_name = months[month_number-1]
ordinal = day + endings[day_number-1]print month_name + ' ' + ordinal + ', ' + year
运行结果如下(不得不说今天是11.11.11)
Year:2011
Month(1-12):11
Day(1-31):11
November 11th, 2011
分片(格式关系很大)
>>> tag = '<a href="http://www.python.org">Python web site</a>'
>>> tag[9:30]
'http://www.python.org'
>>> tag[32:-4]
'Python web site'
>>> numbers = [1,2,3,4,5,6,7,8,9,10]
>>> numbers[3:6]
[4, 5, 6]
>>> numbers[0:1]
[1]
>>> numbers[8:]
[9, 10]
>>> numbers[-3:]
[8, 9, 10]
>>> numbers[:]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers[:3]
[1, 2, 3]
2-2
# Split up a URL of the form http://www.something.comurl = raw_input('Please enter the URL: ')
domain = url[11:-4]print "Domain name: " + domain
Please enter the URL: http://www.baidu.com
Domain name: baidu
更大的步长
>>> numbers = [1,2,3,4,5,6,7,8,9,10]
>>> numbers[0:10:1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers[0:10:2]
[1, 3, 5, 7, 9]
>>> numbers[0:10:3]
[1, 4, 7, 10]
>>> numbers[0:10:-2]
[]
>>> numbers[::-2]
[10, 8, 6, 4, 2]
>>> numbers[5::-2]
[6, 4, 2]
>>> numbers[:5:-2]
[10, 8]
序列相加
>>> [1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
>>> a = [1,3,5]
>>> b = [2,4,6 ]
>>> c = a + b
>>> c
[1, 3, 5, 2, 4, 6]
乘法
>>> 'python'*5
'pythonpythonpythonpythonpython'
>>> [42]*10
[42, 42, 42, 42, 42, 42, 42, 42, 42, 42]
初始化一个长度为10的列表
>>> sequence = [None] * 10
>>> sequence
[None, None, None, None, None, None, None, None, None, None]
2-3 序列(字符串)乘法示例
sentence = raw_input("Sentence: ")screen_width = 80
text_width = len(sentence)
box_width = text_width + 6
left_margin = (screen_width - box_width) // 2print
print ' ' * left_margin + '+' + '-' * (box_width-2) + '+'
print ' ' * left_margin + '| ' + ' ' * text_width + ' |' #注意空格
print ' ' * left_margin + '| ' + sentence + ' |'
print ' ' * left_margin + '| ' + ' ' * text_width + ' |'
print ' ' * left_margin + '+' + '-' * (box_width-2) + '+'
print
成员资格(in)
>>> permissions = 'rw'
>>> 'w' in permissions
True
>>> 'x' in permissions
False
>>> users = ['mlh','foo','bar']
>>> raw_input('Enter your user name:') in users
Enter your user name:mlh
True
>>> subject = '$$Get rich now!!!$$$'
>>> '$$$' in subject
True
2-4 序列成员资格示例
database = [['albert', '1234'],['dilbert', '4242'],['smith', '7524'],['jones', '9843']
]username = raw_input('User name: ')
pin = raw_input('PIN code: ')if [username, pin] in database: print 'Access granted'
User name: smith
PIN code: 7524
Access granted
内建函数
>>> numbers = [100,34,679]
>>> len(numbers)
3
>>> max(numbers)
679
>>> min(numbers)
34
>>> max(2,3)
3
>>> min(8,3,4,2)
2
list函数
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
>>> lists = list('hello')
>>> lists
['h', 'e', 'l', 'l', 'o']
>>> x = [1,1,1]
>>> x[1]
1
>>> x[2] = 2
>>> x
[1, 1, 2]
删除元素
>>> names = ['Alice','Beth','Cecil','Dee-Dee','Earl']
>>> del names[2]
>>> names
['Alice', 'Beth', 'Dee-Dee', 'Earl']
分片赋值、插入新的元素
>>> name = list('Perl')
>>> name
['P', 'e', 'r', 'l']
>>> name[2:] = list('ar')
>>> name
['P', 'e', 'a', 'r']
>>> name[1:] = list('ython')
>>> name
['P', 'y', 't', 'h', 'o', 'n']
>>> numbers = [1,5]
>>> numbers[1:1] = [2,3,4]
>>> numbers
[1, 2, 3, 4, 5]
>>> numbers[1:4] = []
>>> numbers
[1, 5]
列表方法
append
>>> lst = [1,2,3]
>>> lst.append(4)
>>> lst
[1, 2, 3, 4]
count
>>> [2,4,3,2,4,5,6,2,6].count(2)
3
>>> x = [[2,3],[3,4],[2,5]]
>>> x.count(1)
0
>>> x.count([2,3])
1
extend
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6]
index
>>> knights = ['We','are','the','knight','who','say','in']
>>> knights.index('who')
4
insert
>>> numbers = [1,2,3,4,5,6]
>>> numbers.insert(3,'four')
>>> numbers
[1, 2, 3, 'four', 4, 5, 6]
>>> numbers[4:4] = ['four']
>>> numbers
[1, 2, 3, 'four', 'four', 4, 5, 6]
>>>
pop(栈操作)
>>> x = [1,2,3]
>>> x.append(x.pop())
>>> x
[1, 2, 3]
remove
>>> x = ['to','be','or','not','to','be']
>>> x.remove('be')
>>> x
['to', 'or', 'not', 'to', 'be']
reversed和list函数
>>> x = [1,2,3]
>>> list(reversed(x))
[3, 2, 1]
sort
>>> x = [4,6,2,3,8,1]
>>> x.sort()
>>> x
[1, 2, 3, 4, 6, 8]
存入副本
>>> x = [4,6,2,1,7,9]
>>> y = x[:]
>>> y.sort()
>>> x
[4, 6, 2, 1, 7, 9]
>>> y
[1, 2, 4, 6, 7, 9]
比较
>>> x = [3,2,5,4,6]
>>> y = x
>>> y.sort()
>>> x
[2, 3, 4, 5, 6]
>>> y
[2, 3, 4, 5, 6]
使用sorted函数
>>> x = [4,6,2,1,7,9]
>>> y = sorted(x)
>>> x
[4, 6, 2, 1, 7, 9]
>>> y
[1, 2, 4, 6, 7, 9]
高级排序
>>> cmp(42,32)
1
>>> cmp(99,100)
-1
>>> numbers = [5,3,9,7]
>>> numbers.sort(cmp)
>>> numbers
[3, 5, 7, 9]>>> x = ['aadkfjl','dkfjl','eori','dkf']
>>> x.sort(key = len)
>>> x
['dkf', 'eori', 'dkfjl', 'aadkfjl']>>> x = [3,4,6,2,5]
>>> x.sort(reverse = True) #大小写敏感的!!!
>>> x
[6, 5, 4, 3, 2]
元组:不可变序列
>>> 1,2,4
(1, 2, 4)
>>> 43
43
>>> 34,
(34,)
>>> (1,2,5)
(1, 2, 5)
>>> (43,)
(43,)
tuple函数
>>> tuple([1,2,4])
(1, 2, 4)
>>> tuple('abc')
('a', 'b', 'c')
>>> tuple((1,2,3))
(1, 2, 3)
基本元组操作
>>> x = 1,2,4
>>> x
(1, 2, 4)
>>> x[1]
2
>>> x[0:2]
(1, 2)
【C010】Python - 基础教程学习(一)相关推荐
- Python基础教程学习笔记:第一章 基础知识
Python基础教程 第二版 学习笔记 1.python的每一个语句的后面可以添加分号也可以不添加分号:在一行有多条语句的时候,必须使用分号加以区分 2.查看Python版本号,在Dos窗口中输入&q ...
- python基础教程-学习python有什么好的视频教程?
干货来袭,以下均为python好的学习视频,我们先从python的入门教程开始分享起! python入门教程(600集)https://www.bilibili.com/video/BV1ex411x ...
- 【莫烦Python】Python 基础教程——学习笔记
文章目录 本笔记基于p1-p29[莫烦Python]Python 基础教程 大家可以根据代码内容和注释进行学习. 安装 我的:python3.8+anaconda+VS code print() pr ...
- Python基础教程学习目录 - Python入门教程
Python 基础入门 2021 年 1 月 23 日 下午 12:48 目录 一.Python 基础篇 二.Python 线程/进程篇 一.Python 基础篇 Python 简介 Python P ...
- python基础教程 学习前的准备
学习python之前的准备工作 一:windows下python的安装 1.下载python安装包 2.安装python 3.安装完成 二.安装完成之后的检查 1.打开IDLE 2.文本文件编程 三. ...
- python笔记基础-Python基础教程学习笔记-1
今天学习了第9章的八皇后问题,Python简洁的语法令我叹服.现总结如下: Python实现程序如下: def conflict(state,nextX): nextY=len(state) for ...
- python基础课程第12章,Python基础教程学习笔记 | 第12章 | 图形用户界面
Python支持的工具包很多,但没有一个被认为标准的工具包,用户选择的自由度大些.本章主要介绍最成熟的跨平台工具包wxPython.官方文档: ------ 丰富的平台: Tkinter实际上类似于标 ...
- python基础教程学习笔记十二
图形用户界面 Tkinter Wxpython Pythonwin Java swing PyGTK pyQt 第五章 数据库支持 一python数据库api 1 全局变量 Apilevel 版本 ...
- 【C012】Python - 基础教程学习(三)
第五章 条件.循环和其他语句 print和import的更多信息 >>> print 'Age:',42 Age: 42 >>> print 'Age;';42 ...
最新文章
- WSAGetLastError()部分常见返回值
- 构建之法读书笔记03
- nyoj66分数拆分
- php字符串反转abcdefg_php中实现字符串翻转的方法
- 深度学习笔记(43) Siamese网络
- SpringBoot应用中JSP的角色及整合
- 服务器虚拟化 远程,服务器虚拟化 远程
- VC下的人人对弈五子棋(dos)
- 数字图像处理—美图秀秀:磨皮算法
- linux 批量更改三四级目录 扩展名,Linux批量更改文件后缀名
- 游戏外挂基本原理及实现
- 计算机研究生申请 MIT,麻省理工计算机专业研究生申请条件有什么?
- 《指弹:Like a star》
- react.createContext
- 揭秘:全球第一张云安全国际认证金牌得主
- 嵌入式系统移植课笔记1(学通)
- 蓝桥杯 单片机 决赛 第7届 电压、频率采集设备
- vmware虚拟机连接usb,显示:无法识别的usb设备,跟这台计算机连接的前一个usb设备工作不正常
- textpad设置Java版本_如何在textpad中保存设置以显示行号?
- 华为面试题库c语言,华为校园招聘c语言面试题集.doc