首页 技术 正文
技术 2022年11月19日
0 收藏 445 点赞 3,825 浏览 1121 个字

基本语法

>>> width = 20
>>> height = 2 * 3
>>> width * height
120
>>> x = y = z = 0
>>> x
0
>>> y
0
>>> z
0

变量在使用前,必须定义

>>> n
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined

对浮点数支持很好

>>> 7.0 / 2
3.5
>>> 7 / 2
3
>>> hello = "This is a string\n Hello Python."
>>> print hello
This is a string
Hello Python.
>>> hello
'This is a string\n Hello Python.'

print 可以输出内容。

字符串可以由 + 操作符连接(粘到一起),可以由 * 重复。

>>> word = 'Help' + 'A'
>>> word
'HelpA'
>>> word * 5
'HelpAHelpAHelpAHelpAHelpA'

这个厉害了。

>>> word[4]
'A'
>>> word[0:2]
'He'
>>> word[2:4]
'lp'
>>> word[:2]
'He'
>>> word[2:]
'lpA'
>>> word[1:100]
'elpA'
>>> word[10:0]
''
>>> word[2:1]
''
>>> word[-1]
'A'
>>> word[-2]
'p'
>>> word[-2:]
'pA'
>>> word[:-2]
'Hel'
 +---+---+---+---+---+
| H | e | l | p | A |
+---+---+---+---+---+
0 1 2 3 4
-5 -4 -3 -2 -1
>>> s = "hello world"
>>> len(s)
11
>>> u'Hello\u0020World !'
u'Hello World !'

转码序列 \u0020 表示在指定位置插入编码为 0x0020 的 Unicode 字符(空格)。

>>> h = u'Hello\u0020World !'
>>> print h
Hello World !
>>> h
u'Hello World !'

列表

>>> a = ['hello','world',100,20]
>>> a
['hello', 'world', 100, 20]
>>> len(a)
4
>>> a[0]
'hello'
>>> a[3]
20
>>> a[0:2] + ['python',2*2]
['hello', 'world', 'python', 4]

编程

>>> a,b = 0,1
>>> a
0
>>> b
1
>>> while b < 10:
... print b
... a,b = b,a+b
...
1
1
2
3
5
8
>>> i = 2 * 2
>>> print 'The value of i is',i
The value of i is 4

学习给我快乐,没有别的目的。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893