码迷,mamicode.com
首页 > 其他好文 > 详细

基础知识1

时间:2019-10-07 11:33:54      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:ble   分片   def   标签   string   小数点   middle   nbsp   组成   

 

1.       数据类型类型

 

1.1.         原子类型

 

type()函数

>>> type(3.4)

<class ‘float‘>

>>> type(True)

<class ‘bool‘>

>>> type(3<4)

<class ‘bool‘>

1.1.1.           布尔型

True

False

>>> 4>4

False

>>> 3<4

True

1.1.2.           整型

>>> type(2)

<class ‘int‘>

>>> type(2.0)

<class ‘float‘>

1.1.3.           浮点型

1.1.4.           字符串型

1.2.         分子类型

 

2.       对象

 

2.1.         变量

 

2.1.1.           有开口的盒子,可以修改里面的数据,但盒子无法改变

2.2.         常量

 

2.2.1.           透明且封闭的盒子,可以看到,无法修改

2.3.         a=5

 

2.3.1.           a是变量名,a仅仅是个名字,是贴在对象盒子上的标签

2.3.2.           变量名必须 数字、字母和下划线组成,不能以数字开头,不能与关键字冲突!

a_2

NOfM

_3

4ac

2.4.         type(thing)语句

type(4)

type(‘4‘)

type(3<4)

type(4.0)

type(Ture)

3.       整数

 

3.1.         运算

 

3.1.1.           算数运算+、-、*、/、//、%、**

 

>>> a=4

>>> b=3

>>> a/b

1.3333333333333333

>>> a//b

1

>>> a%b

1

>>> a**b

64

>>> a**0.5

2.0

>>> a**-1

0.25

3.1.1.1.            /,结果浮点数

3.1.1.2.            //,结果整数

3.1.1.3.            %,取余

3.1.1.4.            **,幂运算

 

3.1.1.4.1.             可以小数和负数幂次

3.1.2.           divmod(),结果为元祖(tuple)

>>> divmod(a,b)

(1, 1)

>>> divmod(b,a)

(0, 3)

3.2.         进制

 

3.2.1.           0b 或 0B 代表二进制(以 2 为底)
0o 或 0O 代表八进制(以 8 为底)
0x 或 0X 代表十六进制(以 16 为底)

>>> 0b1111

>>> 0o7777

>>> 0xfff

>>>

3.3.         类型转换

 

3.3.1.           int()

 

>>> int(True)

1

>>> int(False)

0

>>> int(2.123)

2

3.3.1.1.            布尔型 0,1

3.3.1.2.            浮点型,截去小数部分

3.3.1.3.            字符串型,仅仅能处理只包含正负号和数字的整数形式

>>> int(‘2‘)

2

>>> int(‘-2‘)

-2

3.4.         取值范围,任意大小!

4.       浮点型

 

4.1.         运算

 

4.1.1.           +、-、*、/、//、%、**、divmod()

>>> -1.2**2

-1.44

>>> (-1.2)**2

1.44

4.2.         科学计数法,前缀e

>>> 4.1234e4

41234.0

4.3.         float()

 

4.3.1.           布尔型 0.0,1.0

>>> float(True)

1.0

>>> float(False)

0.0

4.3.2.           整型,增加小数部分

4.3.3.           字符串型,仅仅能处理只包含正负号、数字、小数点和前缀e的

>>> float(‘2.4‘)

2.4

>>> float(‘1e2‘)

100.0

5.       字符串

 

5.1.         创建字符串

 

5.1.1.           将一系列字符包裹在“ ”或‘ ‘中

>>> ‘Snap‘

‘Snap‘

>>> "Crackle"

‘Crackle‘

>>> "‘Nay,‘ said the naysayer."

"‘Nay,‘ said the naysayer."

>>> ‘The rare double quote in captivity: ".‘

‘The rare double quote in captivity: ".‘

>>> ‘A "two by four" is actually 1 1⁄2" × 3

5.1.2.           使用三个单引号或双引号创建多行字符串

>>> poem = ‘‘‘There was a Young Lady of Norway,

... Who casually sat in a doorway;

... When the door squeezed her flat,

... She exclaimed, "What of that?"

... This courageous Young Lady of Norway.‘‘‘

>>> 

5.2.         空字符串

s=""

5.3.         str()类型转换

>>> str(98.6)

‘98.6‘

>>> str(1.0e4)

‘10000.0‘

>>> str(True)

‘True‘

5.4.         转义字符 \

 

5.4.1.           \n 换行
\t 制表符
\‘ 和 \"
\\

5.5.         使用+号拼接字符串

>>> ‘Release the kraken! ‘ + ‘At once!‘

‘Release the kraken! At once!‘

>>> a = ‘Duck.‘

>>> b = a

>>> c = ‘Grey Duck!‘

>>> a + b + c

‘Duck.Duck.Grey Duck!‘

5.6.         使用*号复制字符串

>>> start = ‘Na ‘ * 4 + ‘\n‘

>>> middle = ‘Hey ‘ * 3 + ‘\n‘

>>> end = ‘Goodbye.‘

>>> print(start + start + middle + end)

5.7.         in, not in判断是否为子串

s = "hello",

print(‘he‘ in s)

print(‘aa‘ in s)

print(‘he‘ not in s)

5.8.         使用 [偏移量] 提取字符

 

>>> letters = ‘abcdefghijklmnopqrstuvwxyz‘

>>> letters[0]

‘a‘

>>> letters[1]

‘b‘

>>> letters[-1]

‘z‘

>>> letters[-2]

‘y‘

>>> letters[100]

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

IndexError: string index out of range

5.8.1.           第一个字符(最左侧)的偏移量为 0

5.8.2.           最后一个字符(最右侧)的偏移量也可以用 -1 表示

5.9.         使用[start:end:step]分片

 

>>> letters = ‘abcdefghijklmnopqrstuvwxyz‘

5.9.1.           [:] 提取从开头到结尾的整个字符串

>>> letters[:]

‘abcdefghijklmnopqrstuvwxyz‘

5.9.2.           [start:] 从 start 提取到结尾

>>> letters[20:]

‘uvwxyz‘

>>> letters[10:]

‘klmnopqrstuvwxyz‘

>>> letters[-3:]

‘xyz‘

5.9.3.           [:end] 从开头提取到 end - 1

5.9.4.           [start:end] 从 start 提取到 end - 1

>>> letters[12:15]

‘mno

>>> letters[18:-3]

‘stuvw‘

>>> letters[-6:-2]

‘uvwx‘

5.9.5.           [start:end:step] 从 start 提取到 end - 1,每 step 个字符提取一个

>>> letters[::7]

‘ahov‘

>>> letters[4:20:3]

‘ehknqt‘

>>> letters[19::4]

‘tx‘

>>> letters[:21:5]

‘afkpu‘

>>> letters[::-1]

‘zyxwvutsrqponmlkjihgfedcba‘

5.9.6.           练习

提取倒数10 个字符:

提取从倒数第11 到倒数第10 个字符:

从开头提取到偏移量为9 的字符:

从偏移量为7的字符提取到偏移量为17的字符:

5.10.     len()长度

>>> len(letters)

26

>>> empty = ""

>>> len(empty)

0

5.11.     split()分割

 

5.11.1.      可以基于分隔符将字符串分割成由若干子串组成的列表

>>> color=‘green,blue,yellow,red,orange,purple,pink‘

>>> color.split(‘,‘)

[‘green‘, ‘blue‘, ‘yellow‘, ‘red‘, ‘orange‘, ‘purple‘, ‘pink‘]

>>> ‘abcdefg‘.split()

 [‘abcdefg‘]

 >>> ‘a,b,c,d,e,f,g‘.split()

 [‘a,b,c,d,e,f,g‘]

>>> ‘a,b,c,d,e,f,g‘.split(‘,‘)

[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘]

>>> ‘a b c d e f g‘.split()

[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘]

5.12.     join()合并

 

5.12.1.      join() 函数与 split() 函数正好相反:它将包含若干子串的列表分解,并将这些子串合成一个完整的大的字符串

>>> n=[‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘]

>>> ‘*‘.join(n)

‘1*2*3*4*5*6*7‘

5.13.     其它字符串函数

 

5.13.1.      startswith()是否以()开始
endswith()是否以()结尾

str8 = "Hello Walt Smith"

print(str8.startswith("Hello"))

5.13.2.      isalnum()判断字符串是否完全由字母或数字组成
isalpha()判断字符串是否完全由字母组成
isdigit()判断字符串是否完全由数字组成

5.13.3.      isupper()判断字符串当中的字母是否完全是大写
islower()判断字符串当中的字母是否完全是小写
isspace()判断字符串是否完全由空格组成

5.13.4.      find()查找位置
rfind()从后向前查找位置
找不到返回 -1

>>>s=‘abcdedjcjdlslk‘

>>>s1=‘abc‘

>>>s.find(s1)                      

0

>>>s.find(‘j‘)                   

6

>>>s.find(‘j‘,2)                 

6

>>>s.find(‘j‘,7)                  

8

>>>s.find(‘j‘,10)                   

-1

5.13.5.      count()出现次数

5.13.6.      capitalize()字符串首字母大写,其余字母全部小写
title()所有单词首字母大写,其余字母全部小写
istitle()判断字符串是否满足title格式

str0 = ‘I aM waLt smith‘

print(str4.capitalize()

str5 = "I am walt smith!"

print(str5.title())

5.13.7.      upper()所有大写
lower()所有小写
swapcase()所有大小写转换

str10 = "Hello Walt Smith"

print(str10.lower())

5.13.8.      strip()去除头尾某个字符

>>> s="  **abc**  "

>>> s.strip()

‘**abc**‘

>>> s.strip(‘*‘)

‘  **abc**  ‘

>>> s=s.strip()

>>> s

‘**abc**‘

>>> s.strip(‘*‘)

‘abc‘

5.13.9.      replace()子串替换,可指定替换多少处

str0 = ‘hello world hello world‘

str1 = ‘world‘

str2 = ‘waltsmith‘

print( str0.replace(str1, str2) )

print( str0.replace(str1, str2, 1) )

5.13.10.  chr()和ord()

chr(u)   x为Unicode编码,返回其对应的字符

ord(x)  x为字符,返回其对应的Unicode编码

5.14.     练习题

 

5.14.1.      输入:1-7的整数,表示星期几
输出:输入整数对应的星期字符串
例如:输入3,输出 星期三

5.14.2.      输入一行字符,统计其中有多少个单词,每两个单词之间以空格隔开。如输入: This is a c++ program. 输出:There are 5 words in the line.

5.14.3.      输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。
例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”

5.14.4.      给定一个句子(只包含字母和空格), 将句子中的单词位置反转,单词用空格分割, 单词之间只有一个空格,前后没有空格。 比如:“hello xiao mi”-> “mi xiao hello”

5.14.5.      字符串 a = "aAsmr3idd4bgs7Dlsf9eAF"。请将 a 字符串的数字取出,并输出成一个新的字符串。

 

基础知识1

标签:ble   分片   def   标签   string   小数点   middle   nbsp   组成   

原文地址:https://www.cnblogs.com/nmucomputer/p/11629681.html

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