码迷,mamicode.com
首页 > 编程语言 > 详细

Python 3.4.4 学习笔记(004)python manuals/the python tutorial -- 3. An Informal Introduction to Python

时间:2016-03-04 01:53:24      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:

可以看到python版本号变了,找到一本学习python的书,Mark Lutz所著的Python学习手册,建议我直接学习最新版本的Python,到网站上看了一下,是3.5.1,但是不支持XP,能支持XP的是3.4.4,都是2015年12月发布的,果断更新。等漫游指南看完了,就转向那本学习手册。

学习运算符

/    除法,17/3 = 5.66...

//           17 // 3 = 5  整数除法

%           17 % 3 = 2  取余数

**           5 ** 2 = 25 乘方 这个和fortran 一样

交互模式下,最后一个表达式的值储存在变量"_"(下划线)里面

如:

>>> a=5
>>> b=4
>>> a*b
20
>>> _
20
>>> b+_
24
>>> _
24
>>>

数值方面,除了int float还支持 Decimal 、Fraction,(小数分数) 复数

支持字符串,Strings, 单双引号都行

\‘ \" 分别表示 ’ “  转义字符的作用  \n 交互模式下原样输出,print函数作用下成为换行符

>>> print(C:\some\name)  # here \n means newline!
C:\some
ame
>>> print(rC:\some\name)  # note the r before the quote
C:\some\name

三个双引号可以原样输出多行文本:

print("""Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
""")

作用于字符串,n*"..." 或者 "。。。"*n表示将字符串重复n次,+则连接字符串。并排放置的字串自动连在一起,先连后乘(所乘数字置于最后,否则不合语法),先乘后加

>>> a= "abc" "def" "kk"*3
>>> a
abcdefkkabcdefkkabcdefkk
>>> a="abc"+"def"+"kk"*3
>>> a
abcdefkkkkkk
>>> 
>>> word = Python
>>> word[0]  # character in position 0
P
>>> word[5]  # character in position 5
n
>>> word[-1]  # last character
n
>>> word[-2]  # second-last character
o
>>> word[-6]
P
>>> word[0:2]  # characters from position 0 (included) to 2 (excluded)
Py
>>> word[2:5]  # characters from position 2 (included) to 5 (excluded)
tho
>>> word[:2] + word[2:]
Python
>>> word[:4] + word[4:]
Python
>>> word[:2]  # character from the beginning to position 2 (excluded)
Py
>>> word[4:]  # characters from position 4 (included) to the end
on
>>> word[-2:] # characters from the second-last (included) to the end
on
 +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
-6  -5  -4  -3  -2  -1

看到以上这些,我只想说,太强大了

然后是列表,列表好像是数组,可是太灵活了

>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
>>> squares[0]  # indexing returns the item
1
>>> squares[-1]
25
>>> squares[-3:]  # slicing returns a new list
[9, 16, 25]
>>> squares[:]
[1, 4, 9, 16, 25]
>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> cubes = [1, 8, 27, 65, 125]  # something‘s wrong here
>>> 4 ** 3  # the cube of 4 is 64, not 65!
64
>>> cubes[3] = 64  # replace the wrong value
>>> cubes
[1, 8, 27, 64, 125]
>>> cubes.append(216)  # add the cube of 6
>>> cubes.append(7 ** 3)  # and the cube of 7
>>> cubes
[1, 8, 27, 64, 125, 216, 343]
>>> letters = [a, b, c, d, e, f, g]
>>> letters
[a, b, c, d, e, f, g]
>>> # replace some values
>>> letters[2:5] = [C, D, E]
>>> letters
[a, b, C, D, E, f, g]
>>> # now remove them
>>> letters[2:5] = []
>>> letters
[a, b, f, g]
>>> # clear the list by replacing all the elements with an empty list
>>> letters[:] = []
>>> letters
[]
>>> letters = [a, b, c, d]
>>> len(letters)
4
>>> a = [a, b, c]
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[[a, b, c], [1, 2, 3]]
>>> x[0]
[a, b, c]
>>> x[0][1]
b
>>> a="abcde"
>>> a=[1,2,3,a,"b"]
>>> a
[1, 2, 3, abcde, b]
>>> 

列表可以取元素,可以使用”+“,可以使用函数len(),可以追加元素,可以多维列表,可以不同变量类型放在一组

甚至这样也可以:

>>> a, b = 0, 1
>>> while b < 1000:
...     print(b, end=,)
...     a, b = b, a+b
...
1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,

感觉python就是一门外语有木有?

Python 3.4.4 学习笔记(004)python manuals/the python tutorial -- 3. An Informal Introduction to Python

标签:

原文地址:http://www.cnblogs.com/argent/p/5240698.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!