python笔记2

Python的对象类型

需要首先引用一句话 Names have no type, but objects do

再看一下核心的数据类型

Built-in objects preview
Object type                                   Example literals/creation
Numbers                                      1234, 3.1415, 3+4j, Decimal, Fraction
Strings                                         'spam', "guido's", b'a/x01c'
Lists                                               [1, [2, 'three'], 4]
Dictionaries                                   {'food': 'spam', 'taste': 'yum'}
Tuples                                           (1, 'spam', 4, 'U')
Files                                              myfile = open('eggs', 'r')
Sets                                               set('abc'), {'a', 'b', 'c'}
Other core types                          Booleans, types, None
Program unit types                        Functions, modules, classes (Part IV, Part V, Part VI)
Implementation-related types      Compiled code, stack tracebacks (Part IV, Part VII)

1.整数 浮点

A.整数

(例子) 123 + 222

(应用)  怎么计算一个大整数,有多少位?

len(str(2 ** 1000000))

 

B.浮点

(例子) 1.5 * 4

 

C.math库

import math
>>> math.pi
3.1415926535897931
>>> math.sqrt(85)
9.2195444572928871

 

D.random库

>>> import random
>>> random.random()
0.59268735266273953
>>> random.choice([1, 2, 3, 4])   #从1,2,3,4这几个数中随机选一个。
1

 

2.字符串

字符串是很容易与数组,字符进行转换的。但需要注意,字符串对象本身是不能被修改的。

定义字符串   S = 'Spam'

求字符串长度   len(S)

字符串是可以进行索引和分片操作的  

>>>S = 'Spam'

>>> S[0]                 # The first item in S, indexing by zero-based position
'S'

>>> S[1:3]               # Slice of S from offsets 1 through 2 (not 3)
'pa'

相加和相乘操作

>>> S + 'xyz'            # Concatenation
'Spamxyz'

>>>S * 8                # Repetition
'SpamSpamSpamSpamSpamSpamSpamSpam'

查找特定的内容

>>> S.find('pa')            # Find the offset of a substring
1                           #找出来的是索引值

替换操作

>>> S.replace('pa', 'XYZ')  # Replace occurrences of a substring with another
'SXYZm'                     # replace的返回值是新的字符串,原有字符串不改变。

分割字符串

>>> line = 'aaa,bbb,ccccc,dd'
>>> line.split(',')            # Split on a delimiter into a list of substrings
['aaa', 'bbb', 'ccccc', 'dd']   #split(',')会返回一个分割后的数组。

判断字符串内容

>>>S="123k"

>>> S.isalpha()                # Content tests: isalpha, isdigit, etc.
True                                  # 判断字符串是否全为字母。'1','we,are'都不是字母。

去除空白符

>>> line = 'aaa,bbb,ccccc,dd/n'
>>> line = line.rstrip()       # Remove whitespace characters on the right side
>>> line
'aaa,bbb,ccccc,dd'

字符串的格式化

>>> '%s, eggs, and %s' % ('spam', 'SPAM!')        # Formatting expression (all)
'spam, eggs, and SPAM!'
这种方法接近C语言
>>> '{0}, eggs, and {1}'.format('spam', 'SPAM!')  # Formatting method (2.6, 3.0)
'spam, eggs, and SPAM!'

这种方法更灵活

求ASCII值

>>> ord('/n')                # /n is a byte with the binary value 10 in ASCII
10

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值