python中常见内建类型

1. Number类型

2. String类型 

3. List类型

4. 第一个python控制结构 

5. 参考资料 

 上面两篇文章中主要还是熟悉python的开发环境:第一篇主要是介绍python开发的ide环境,这主要是为了开发比较大型的工程。第二篇主要是来介绍python解释器的使用。这里将简单介绍一下python的几个常见类型numbers,strings,lists。

<1>. Numbers;

 

>>> 2 + 2 # 将python解释器作为计算器使用
4
>>> # this is a comment
... 2 + 2
4
>>> (50 - 5 * 6) / 4
5
>>> 7 // 3 # 这里将结果截断floor
2
>>> 7 / 3
2
>>> width = 20 # 可以给变量赋值
>>> height = 5 * 9
>>> width * height
900
>>> x = y = z = 0 # 连续赋值
>>> x = y = z = 0
>>> x
0
>>> y
0
>>> z
0
>>> undefine # 变量必须定义,这里将出现error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'undefine' is not defined

 >>> a = 1.5 + 0.5j # 定义复数

>>> a.real # 复数实部
1.5
>>> a.imag # 虛部
0.5
>>> abs(a) # 复数的模
1.5811388300841898
>>> _ # _表示的是前一个表达是的结果,这里是1.5811388300841898

1.5811388300841898 

<2>. Strings;

 

>>> 'spam eggs' # 使用''定义字符串
'spam eggs'
>>> "spam eggs" # 使用""定义字符串
'spam eggs' # 下面字符串使用/在多行定义

 >>> hello = "this is a rather long string /

...     in several lines"
>>> hello
'this is a rather long string /tin several lines' # """ """中的字符串不需要转移 
>>> print ("""/
...     usage : thingy [OPTIONS]
...     -h
...     -H hostname
... """)
        usage : thingy [OPTIONS]
        -h
        -H hostname
>>> word = 'Help''A' # 字符串拼接,或者使用+来实现
>>> word[0] # 取出word中的第一个字符
'H'
>>> word[0:2] # 得到从0开始,长度为2的子字符串
'He'
>>> word[:2] # 省略开始位置,得到从开始位置0,长度为2的子字符串
'He'
>>> word[2:] # 省略结束位置,得到从2开始到结束的所有字符
'lpA'
>>> word[0] = 'x' # 无法修改某这出现错误
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> word[-1] # -1表示最后一个字符
'A'
>>> word[-2:] # 省略结束位置,得到从-2位置开始,到结束之间的字符串
'pA'
>>> s = 'supercalifragilisticexpialidocious'
>>> len(s) # 得到字符串长度len()

 

<3>. Lists;

 

>>> a = ['spam', 'eggs', 100, 1234] # 定义一个list a,可以看出python中list可以容纳任何类型
>>> a
['spam', 'eggs', 100, 1234]
>>> len(a) # list a的长度
4
>>> q = [2, 3]
>>> p = [1, q, 4] # 嵌套list类型
>>> len(q)
2
>>> len(p)
3
>>> p[1][0]
2
>>> p[1].append('xtra') # 向q中增加元素'xtra'
>>> p
[1, [2, 3, 'xtra'], 4]
>>> q
[2, 3, 'xtra']
>>> a[0] = 1 # 更改list a中的第一个元素
>>> a
[1, 'eggs', 123, 1234]

 

<4>. 第一个python控制结构;

Fibonacci.py

#  Fibonacci 
#  the sum of the two elements defines the next
a, b  =  0,  1 ;
while  b  <   10  :
     print (b);
    a, b = b, a + b; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值