python基本元素:数字、字符串和变量

一、 变量、名字和对象
python里所有的数据——布尔值、整数、浮点数、字符串,甚至大型数据结构、函数以及程序——都是以对象(object)的形式存在的,使得python具有很强的统一性。
Python是强类型的(strong typed),你无法修改一个已有对象的类型,即使它包含的值是可变的。
#打印一个对象的类型
type(opject)
二 、数字

运算符
描述
+
加法
-
减法
*
乘法
/
浮点数除法
//
整数除法
%
模(求余)
**

1 整数
任何仅含数字的序列在Python中都被认为是整数。
#简单写法
>>> a /= 3
#同时得到余数和商
>>> divmod(9,5)
(1,4)
2 优先级
在实际编程中,可以使用括号来保证运算顺序与我们期望的一致。
3 基数
在Python中,整数默认使用十进制数,除非在数字前添加前缀 ,显式地指定使用其他基数。
  • 0b或0B代表二进制;
  • 0o或0O代表八进制;
  • 0x或0X代表十六进制。
>>> ob10
2
>>> 0o10
8
>>> 0x10
16
16进制用的16个数字:0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f.
4 类型转换
用int()将其他的Python数据类型转换为整型。
5 浮点数 
使用float()函数可以将其他数字类型转换为浮点型 。

三、字符串
  • 多行字符串用三引号创建 。
#print()可以自动添加空格
>>> print(99,'bottles','would be enough')
99 bottles would be enough.
  • 使用str()可以将其他Python数据类型转换为字符串。

  • 使用\转义:
#最常见的转义符\n
>>> palindom = 'A man,\nAplan,\nA canal:\nPanama.'
>>> print(palindom)
A man,
A plan,
A canal:
Panama.

#转义符\t(tab制表符 )常用于对齐文本
>>> print('\tabc')
    abc

#用\'和\"表示单、双引号
>>> testimony = "\"I did nothing!\" he said. \"Not that either! Or the other thing.\""
>>> print(testimony)
"I did nothing!" he said. "Not that either! Or the other thing."

#如果需要输入一个反斜线符,连续输入两个反斜线即可
>>> speech = 'Today we honor our friend,the backslash:\\.'
>>> print(speech)
Today we honor our friend,the backslash:\.

  • 使用+拼接
#这里+号省去效果一样
>>> 'Darren' + 'Chen'
'Darren Chen'
#注意进行字符串拼接时,Python不会自动为你添加空格,需要显示定义。

  • 使用*复制
>>> start = 'Na' * 4 + '\n'
>>> middle = 'Hey' * 3 + '\n'
>>> end = 'Goodbye.'
>>> print(start + middle + end)
NaNaNaNa
HeyHeyHey
Goodbye.

  • 使用[]提取字符
>>> letters = 'djlasjlfjsahglsajglkjslfjlsajf'
>>> letters[4]
's'
>>> letters[5]
'j'
>>> letters[9]
's'
>>> letters[13]
'l'

  • 使用[start:end:step]分片
    • [:]提取从开头到结尾的整个字符串;
    • [start:]从start提取到结尾;
    • [:end]从开头提取到end-1;
    • [start:end]从start提取到end-1;
    • [start:end:step]从start提取到end-1,每step个字符提取一个。
>>> letters = 'djlasjlfjsahglsajglkjslfjlsajf'
>>> letters[:]
'djlasjlfjsahglsajglkjslfjlsajf'
>>> letters[4:]
'sjlfjsahglsajglkjslfjlsajf'
>>> letters[:14]
'djlasjlfjsahgl'
>>> letters[1:14:2]
'jajfshl'
>>> letters[::2]
'dlsljagsjljljsj'
>>> letters[:-1:2]
'dlsljagsjljljsj'

  • 使用len()获取长度 
>>> len(letters)
30
>>> empty = ''
>>> len(empty)
0
  • 使用split()分割
使用内置的字符串函数split()可以基于分隔符将字符串分割成由若干个子串组成的列表。
>>> celebrated_dictum = '眼光长远是理性的,但也是苦闷的,因为美好永远在将来,当下永远有苦难。'
>>> celebrated_dictum.split(',')
['眼光长远是理性的', '但也是苦闷的', '因为美好永远在将来', '当下永远有苦难。']
  • 使用join()合并
>>> ','.join(celebrated_dictum.split(','))
'眼光长远是理性的,但也是苦闷的,因为美好永远在将来,当下永远有苦难。'

熟悉字符串
>>> poem = '''All that doth flow we cannot liquid name Or else would fire and water be the same; But that is liquid which is moist and wet Fire that property can never get. Then 'tis not cold that doth the fire put out But 'tis the wet that makes it die, no doubt.'''

>>> poem[:13]
'All that doth'

#len()计入空格和换行符
>>> len(poem)
250

>>> poem.startswith('All')
True
>>> poem.endswith('no doubt.')
True

#查找第一次出现the的位置
>>> word  = 'the'
>>> poem.find(word)
73

#最后一次出现the的偏移量
>>> poem.rfind(word)
214

#the在这首诗中出现了多少次
>>> poem.count(word)
3

#诗中所有的字符都是字母或数字吗?
>>> poem.isalnum()
False

  • 大小写与对齐方式、使用replace()替换
>>> setup = 'a duck goes into a bar...'

#将字符串收尾的.都删除掉
>>> setup.strip('.')
'a duck goes into a bar'

#将字符串首字母变大写
>>> setup.capitalize()
'A duck goes into a bar...'

#将所有单词的开头变大写
>>> setup.title()
'A Duck Goes Into A Bar...'

#将所有字母变大写
>>> setup.upper()
'A DUCK GOES INTO A BAR...'

#将所有字母变小写
>>> setup.lower()
'a duck goes into a bar...'

#将所有字母大小写转换
>>> setup.swapcase()
'A DUCK GOES INTO A BAR...'

#在30个字符位居中
>>> setup.center(30)
'  a duck goes into a bar...   '

#左对齐和右对齐
>>> setup.ljust(30)
'a duck goes into a bar...     '
>>> setup.rjust(30)
'     a duck goes into a bar...'

#使用replace()替换
>>> setup.replace('duck','marmoset')
'a marmoset goes into a bar...'

#修改最多100处
>>> setup.replace('a ','a famous',100)  #注意这里a后面有空格
'a famousduck goes into a famousbar...'

猜你喜欢

转载自blog.csdn.net/PyDarren/article/details/79782927