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

Python 学习日记第一篇

时间:2017-02-08 23:25:06      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:python

一、Python数字类型

  1、数字类型有整数型,浮点型以及一些较为少见的类型,数字类型支持数学运算

加减乘除取余
In [1]: 23 + 45
Out[1]: 68

In [2]: 1.7 + 2
Out[2]: 3.7

In [3]: 2 * 10
Out[3]: 20

In [4]:  10 / 2
Out[4]: 5

In [5]: 23 - 45
Out[5]: -22

In [6]: 100 & 7
Out[6]: 4

  2、python的数学模块math

In [7]: import math
math的属性及方法
In [8]: math.
math.acos       math.cos        math.factorial  math.ldexp      math.sin
math.acosh      math.cosh       math.floor      math.lgamma     math.sinh
math.asin       math.degrees    math.fmod       math.log        math.sqrt
math.asinh      math.e          math.frexp      math.log10      math.tan
math.atan       math.erf        math.fsum       math.log1p      math.tanh
math.atan2      math.erfc       math.gamma      math.modf       math.trunc
math.atanh      math.exp        math.hypot      math.pi         
math.ceil       math.expm1      math.isinf      math.pow        
math.copysign   math.fabs       math.isnan      math.radians  

常用函数
阶乘:
In [13]: math.factorial(54)
Out[13]: 230843697339241380472092742683027581083278564571807941132288000000000000L
开平方
In [14]: math.sqrt(54)
Out[14]: 7.3484692283495345

常量pi
In [15]: math.pi
Out[15]: 3.141592653589793


 3、随机数模块random

In [16]: import random
1.生成随机数
In [17]: random.random()
Out[17]: 0.10143517223564436
2.给定随机数生成范围
In [18]: random.uniform(2,3)
Out[18]: 2.8343477251262748

In [19]: random.uniform(100,3)
Out[19]: 84.88334412939696
3.给定范围生成随机整数
In [20]: random.randint(3,4)
Out[20]: 3
4.随机选择器
In [23]: random.choice([1,‘a‘,3.4])
Out[23]: 1

In [24]: random.choice([1,‘a‘,3.4])
Out[24]: 3.4

In [25]: random.choice([1,‘a‘,3.4])
Out[25]: ‘a‘

5.随机重新排序,原地修改
In [26]: a = [1,2,3,4,5]

In [27]: random.shuffle(a)

In [28]: a
Out[28]: [3, 1, 5, 2, 4]

6.随机切片,不影响原有序列
In [34]: random.sample(a,2)
Out[34]: [1, 2]

In [35]: a
Out[35]: [1, 2, 3, 4, 5]


二、Python字符串

  字符串是单个字符的字符串的序列,是不可变的

  1、字符串序列操作

1.定义字符串
In [36]: a = ‘apache‘

2.字符串索引(左起从0开始,右起从-1开始)
In [37]: a[0]
Out[37]: ‘a‘

In [38]: a[1]
Out[38]: ‘p‘

In [39]: a[-1]
Out[39]: ‘e‘

3.字符串切片
In [40]: a[1:3]
Out[40]: ‘pa‘

In [41]: a[2:]
Out[41]: ‘ache‘

In [42]: a[:2]
Out[42]: ‘ap‘

In [43]: a[:-1]
Out[43]: ‘apach‘

In [44]: a[-3:-1]
Out[44]: ‘ch‘

4.字符串复制
In [45]:  b = a[:]

In [46]: b
Out[46]: ‘apache‘

5.字符串的不可变性
In [47]: a + ‘ web server‘
Out[47]: ‘apache web server‘

In [48]: a
Out[48]: ‘apache‘

In [49]: a * 3
Out[49]: ‘apacheapacheapache‘

In [50]: a
Out[50]: ‘apache‘

 

  2、字符串方法

常用的字符串方法如下
1.统计字符串中字符出现的次数
In [54]: a.count(‘a‘)
Out[54]: 2

In [55]: a.count(‘p‘)
Out[55]: 1

2.查找字符位于字符串中的偏移量
In [57]: a.find(‘a‘)
Out[57]: 0

In [58]: a.find(‘p‘)
Out[58]: 1

3.字符替换,不修改元数据
In [59]: a.replace(‘a‘,‘b‘)
Out[59]: ‘bpbche‘

In [60]: a
Out[60]: ‘apache‘

4.大写转换
In [61]: a.upper()
Out[61]: ‘APACHE‘

5.测试字符串
In [62]: a.isalpha()
Out[62]: True

6.字符串拆分
In [63]: b = "11:22:33:44"

In [64]: b.split(‘:‘)
Out[64]: [‘11‘, ‘22‘, ‘33‘, ‘44‘]

7.去字符串尾部空格
In [68]: c = ‘apache\n‘

In [69]: c = c.rstrip()

In [70]: c
Out[70]: ‘apache‘

8.python3.0新特性:替代
In [71]: ‘{0},2,{1},4‘.format(‘1‘,‘3‘)
Out[71]: ‘1,2,3,4‘

本文出自 “linux启航” 博客,请务必保留此出处http://jiayimeng.blog.51cto.com/10604001/1896157

Python 学习日记第一篇

标签:python

原文地址:http://jiayimeng.blog.51cto.com/10604001/1896157

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