3.1. 将Python用作计算器

3.1.1. Numbers 数

  • 作为一个计算器,python支持简单的操作,
  • '+','-','*','/'地球人都知道的加减乘除.
  • ()可以用来改变优先级,同数学里面的四则运算优先级一样.
  • '='用来建立起表达式和变量间的联系,通俗点讲就是赋值. Afterwards, no result is displayed before the next interactive prompt (没看明白...)
  • 变量在使用之前必须被定义.
  • 浮点型的支持:用python进行数学计算的时候,python会帮你处理小数(float)和整数(integer)间的关系(存在小数的时候,那么得到的结果必然是一个浮点型的).
  • 复数的支持:复数虚步在python里面需要带上j或者J的后缀,实部非0的复数表示为 (real + imagj),或者也可以用complex(real, imag)函数来生成.

复数z的实部与虚部可以通过z.real和z.imag获取

类型转换函数float(), int() and long()对复数无效

abs(z)获取它的magnitude (as a float) 或者通过 z.real 的方式获取它的实部

  • 交互模式下(命令行),最后输出的一个表达式会被赋给变量 '_',所以有时候可以通过使用 '_' 简化操作

This variable should be treated as read-only by the user. Don’t explicitly assign a value to it — you would create an independent local variable with the same name masking the built-in variable with its magic behavior.

3.1.2. Strings 字符串

除了数之外,Python同样的有多种处理字符串的方法.

字符串的三种表达方式:

  • 'This is a string'
  • "This is another string"
  • """This is also a string"""

扩展一下

'Hello I'm zhangsan' Wrong

'Hello I\'m zhangsan' Right

"Hello I'm zhangsan" Right

此外,还有一种字符串的表示需要提一下,那就是

r'this is a string and something like \n will not work it you wanna to converted it to newline'

r '' 也就是raw的意思,被其包围的字符串都会原样的输出,像\n \t等等的这时候就不会被转换成换行和回车,但是直接的回车换行就会生效,比如说这样

print r'Hello, I am \

zhangsan'

输出会是这样的

Hello, I am

zhangsan

字符串是可以使用 + 和 * 操作的

比如 'this is a string'和 'this is ' + 'a string' 得到的结果是一样的

'aaa' 同样的也可以表示为 'a' * 3

同list一样的,string也可以切片,比如说

print 'abcde'[:3] # abc

print 'abcdefg'[0:4:2] #ac

for w in 'words':

print w,

#output:

w o r d s (注意我在print后面用了一个逗号,这样不会换行)

len('abc') 得到的结果是字符串 'abc' 的长度(3)

所以上面的一个for循环也可以这样:

for i in range(len('words')):

print 'words'[i],

这会得到同样的结果

3.1.3. Unicode Strings

定义一个Unicode字符串简单的同定义普通字符串一样

Creating Unicode strings in Python is just as simple as creating normal strings:

由于太简单了,所以我就不写了(哈哈,其实是我不知道怎么去说....)

3.1.4. Lists

序列作为Python的基本格式之一,简直是妙极了.这里简单的用几个小例子来介绍一下list的使用方法.

定义一个序列,看起好像有点复杂,其实不复杂.

lst = [0, 1, 2, 3, 4, 5, 'a', 'b', [8, 888], '9', {'10': 10, 10: 100}]

lst[1] # 1 一个整数

lst[8]  # [8, 888] 一个序列

lst[9] # '9' 一个字符串

lst[10] # {'10': 10, 10: 100} 一个字典

看起来好像很灵活的样子,就是这么任性.

list的切片

lst[2:6] #[2, 3, 4, 5, 'a']

lst[2:6:2] #[2, 4, 'a']

lst[-1:0:-1] #[{'10': 10, 10: 100}, '9', [8, 888], 'b', 'a', 5, 4, 3, 2, 1] 其实就是一个逆序

lst[-1:0:-2] #[{'10': 10, 10: 100}, [8, 888], 'a', 4, 2]

len(lst) # 10

3.2. First Steps Towards Programming

Python可不仅仅是用来做加减乘除的,比如,这里我们可以用它来实现一个斐波那契数列(一对兔子,三个月生小兔子.........)

>>> # Fibonacci series:
... # the sum of two elements defines the next
... a, b = 0, 1
>>> while b < 10:
... print b,
... a, b = b, a+b

#output
1 1 2 3 5 8
关于这个函数,后面会有更为详细的介绍(直接定义了一个函数出来了)

Python Tutorial 学习(三)--An Informal Introduction to Python的更多相关文章

  1. [译]The Python Tutorial#3. An Informal Introduction to Python

    3. An Informal Introduction to Python 在以下示例中,输入和输出以提示符(>>>和...)的出现和消失来标注:如果想要重现示例,提示符出现时,必须 ...

  2. Python Tutorial 学习(八)--Errors and Exceptions

    Python Tutorial 学习(八)--Errors and Exceptions恢复 Errors and Exceptions 错误与异常 此前,我们还没有开始着眼于错误信息.不过如果你是一 ...

  3. Python基础学习三

    Python基础学习三 1.列表与元组 len()函数:可以获取列表的元素个数. append()函数:用于在列表的最后添加元素. sort()函数:用于排序元素 insert()函数:用于在指定位置 ...

  4. Python Tutorial 学习(四)--More Control Flow Tools

    4.1 if 表达式 作为最为人熟知的if.你肯定对这样的一些表达式不感到陌生: >>> x = int(raw_input("Please enter an intege ...

  5. Python Tutorial 学习(六)--Modules

    6. Modules 当你退出Python的shell模式然后又重新进入的时候,之前定义的变量,函数等都会没有了. 因此, 推荐的做法是将这些东西写入文件,并在适当的时候调用获取他们. 这就是为人所知 ...

  6. Python Tutorial 学习(二)--Using the Python Interpreter

    Using the Python Interpreter 2.1. Invoking the Interpreter The Python interpreter is usually install ...

  7. Python Tutorial 学习(一)--Whetting Your Appetite

    Whetting Your Appetite [吊你的胃口]... 这里就直接原文奉上了... If you do much work on computers, eventually you fin ...

  8. Python Tutorial 学习(七)--Input and Output

    7. Input and Output Python里面有多种方式展示程序的输出.或是用便于人阅读的方式打印出来,或是存储到文件中以便将来使用.... 本章将对这些方法予以讨论. 两种将其他类型的值转 ...

  9. Python基础学习参考(一):python初体验

    一.前期准备 对于python的学习,首先的有一个硬件电脑,软件python的运行环境.说了一句废话,对于很多初学者而言,安装运行环境配置环境变量的什么的各种头疼,常常在第一步就被卡死了,对于pyth ...

随机推荐

  1. Linux 下挂载硬盘的 方法

    1. 添加磁盘,查看磁盘状况 [root@db1 /]# fdisk -l Disk /dev/sda: 10.7 GB, 10737418240 bytes 255 heads, 63 sector ...

  2. winform datetimepacker 开始日期 结束日期 分类: WinForm 2014-07-15 19:14 124人阅读 评论(0) 收藏

    dtpStart;//开始日期 dtpEnd;//结束日期 1:开始日期小于结束日期 加载dtpEnd的ValueChanged事件即可. //开始日期小于结束日期         private v ...

  3. iOS开发总结-搜索功能实现--使用SKTag

    TagsTableViewController.h 文件 #import <UIKit/UIKit.h> #import "personSearch.h" @inter ...

  4. ORACLE EXP命令

    本文对Oracle数据的导入导出 imp ,exp 两个命令进行了介绍, 并对其对应的參数进行了说明,然后通过一些演示样例进行演练,加深理解.文章最后对运用这两个命令可能出现的问题(如权限不够,不同o ...

  5. zTree实现地市县三级级联报错(一)

    zTree实现地市县三级级联 1.详细报错例如以下: java.lang.IllegalStateException: Failed to load ApplicationContext at org ...

  6. Imatest 崩溃

    在使用Imatest时候,发现选取chart图的区域时候.Imatest停止工作,例如以下图.百度半天没有找到类似的问题,事实上这个问题在之前的公司也碰到过一回,卸载重N次,无效.如今又碰到了,问之前 ...

  7. 颜色渐变的RGB计算

    均匀渐变 渐变(Gradient)是美学中一条重要的形式美法则,与其相对应的是突变.形状.大小.位置.方向.色彩等视觉因素都可以进行渐变.在色彩中,色相.明度.纯度也都可以产生渐变效果,并会表现出具有 ...

  8. [转] 使用git自动部署简单网站

    要做什么 假设你有一个博客,有一台网站服务器(或者很多台作负载均衡的服务器),当你的博客要升级时,你可能要在你自己的电脑上写好代码(可能包括本地调试好),然后提交到git(或svn),然后在每个服务器 ...

  9. nodejs 按行读取 readline

                fs.mkdirSync('./yotmp');      }                  log(out);               input: file,    ...

  10. try{}catch(){}//根据异常信息使用不同的方法要怎么实现

    try{ }catch(Exception e){ if(e.getMessage().contains("123456798")) //使用e.getMessage().cont ...