Python基础(二)数据类型、运算符、语句

基本数据类型

6个标准数据类型

number(数字)、Sttring(字符串)、List(列表)、Tuble(元组)、Set(集合)、Dictionary(字典)

不可变数据:Number、String、Tuble

可变数据:List、Dictionary、Set

.join() 将参数中的每一个元素按指定分隔符拼接

.bit_length()获取变量的最小二进制位数

string(字符串)

用成对的引号标识字符串

字符串一经创建,就会存放于内存中;一旦经过拼接或修改,内存中会重新开辟空间存放

id()函数可以查看变量指向的内存地址

str1 = "hello"
str2 = "world"

#字符串的加法
print(str1 + str2) 

字符串的乘法
print(str*3)  

常用: 

制表符

换行符

upper()

lower()

strip()

find()

join()

split()

replace()

.upper() 英文字符大写

.isupper()

.lower()  英文字符小写

.islower()

.casefold()  字符小写,不常用

.capitalize()  首字母大写

.title() 每个单词首字母大写

.istitle()

.swapcase() 大小写转换

.center() 字符居中,可设置字符长度和空字符的填充

.ljust()  字符居左,可设置字符长度和空字符的填充

.rjust()  字符居右,可设置字符长度和空字符的填充

.counter() 计算字符串中子序列的出现个数

.strip()  去除字符空白符(包括/t  /n), 还可以去除参数中的字符(最长公共子序列优先

.lstrip()

.rstrip()

.encode()

.decode()

.endswith()  判断字符串是否以指定参数结尾

.startswith()  

.find()  获取字符串出现指定字符的索引位置(从零开始),> <=,未找到则返回-1

.format()  将字符串中的占位符替换为指定的值

.format_map() 传入的参数为字典

.index()  获取字符串出现指定字符的索引位置(从零开始),未找到则报错

tempalte = "hello, i am {name}, age:{age}"
# v = tempalte.format(name = 'LiMing', age = 20)
v = tempalte.format(**{"name" : "LiMing", "age" : 20,})
print(v)

   

.isalnum()  判断字符串中是否只包含字母和数字

.isalpha()  判断字符串中是否只包含字母(包括汉字)

.isdecimal()  判断字符串中是否只包含数字

.isdigit()  判断字符串中是否只包含数字,较强大,包括①

.isnumeric()  判断字符串中是否只包含数字,更强大,包括 一、二

.isidentifier()  判断是否满足标识符(只有数字、字母、下划线构成)

.isprintable()  判断是否存在不可显示的字符( )

.isspace()  判断字符串是否全是空格

.join() 将参数中的每一个字符按指定分隔符拼接

.expandtabs()  根据参数大小进行断句,并在 转换为对应的空格

test=’username email password liming lmqq.com 1998 liming lmqq.com 1998 ’

t = test.expandtabs(20)

print(t)

.maktrans()  设置字符转换关系

.translate()  将字符按指定参数指定的关系进行转换

.partition()  按指定参数将字符串分割为三份字符串,返回元组,包含分割元素

.rpartition()

.split() 按指定参数将字符串分割为指定数量的字符串,返回列表,不包含分割元素

.rsplit()

.splitlines() 根据换行符分割字符串,根据参数选择是否保留换行符

.replace()  进行字符替换

索引

“attitude”[0] 的结果是a

切片

“attitude”[0:2] 的结果是at, 左闭右开

字符串格式化

tp1 = 'i am %s' % 'alex'

tp1 = 'i am %s age %d' % ('alex',18)

tp1 = 'i am %(name)s age%(age)d' % {'name':'alex', 'age':18,}

tp1 = 'percent %.2f' % 99.1234567

tpl = "i am %(pp).2f" % {"pp": 123.425556, }

tpl = "i am %.2f %%" % {"pp": 123.425556, }

 

tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')
  
tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])
  
tpl = "i am {0}, age {1}, really {0}".format("seven", 18)
  
tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])
  
tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
  
tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
  
tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
  
tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
  
tpl = "i am {:s}, age {:d}".format(*["seven", 18])
  
tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18)
  
tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
 
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
 
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
 
tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15)
 
tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)

  

 

number(数字)

int(整型)

python3中整型均为int

int() python内置函数,将对象转为int类型

int(“a”, base=16)将字符串转为16进制 

int不能迭代

num1 = 1
num2 = 3

num3 = num1 + num2
num3 = num1 - num2
num3 = num1 * num2
num3 = num1 / num2

# 乘方、取余、地板除
num3 = num1 ** num2
num3 = num1 % num2
num3 = num1 // num2

布尔值

True  --> 1

False  -->0

bool( )

True

False 例如None  “”  ()  []  {}  0 False

列表

[ , , , ]

用中括号括起来

逗号 , 分隔元素

列表中的元素可以是任意类型的对象

列表是可迭代对象

列表是以链表的形式进行存储,有序

列表元素可以被修改

索引取值:可以嵌套索引

切片:切片结果也是列表

range() 左闭右开

创建数值列表

python2

  range立即创建

  xrange for循环时创建

python3

  range for循环时创建

删除元素

del list_name[index]

修改元素的值

list_name[index] = new_value

列表转为字符串

1.自己写for循环

2.使用字符串join方法,要求列表中的元素只有字符串

列表常用:

.append() 用于在列表末尾添加新的对象。该方法无返回值,但是会修改原来的列表

.clear()  清空列表

.copy()  列表浅拷贝

.count()  计算元素在列表中出现的次数

.extend()  用于在列表末尾一次性追加另一个序列中的多个值。传入参数为可迭代对象

.index()  根据值获取索引

.inset() 在指定索引位置前插入元素

.pop()  删除某个值(默认最后一个,也可以指定值),并获取删除的值

.remove()  根据值删除列表元素

PS删除: pop  remove  del list_name[1]  del list_name[1:4}  clear

.reverse()  将当前列表反转

.sort(reverse=True)  对列表进行排序

元组

( , , )

用小括号()括起来

元组 tuple 就是不可被修改的列表,

其中的一级元素不能被修改、增加或删除

建议在元组最后的元素末尾加入一个逗号

元组是可迭代对象,有序

可以索引取值、切片

常用:

.count()

.index()

字典

{ key : value ,  :  ,  :  , }

字典dict用大括号{ } 括起来

字典由许多键值对组成,键值对直接用逗号 , 分隔

键和值直接用冒号 分割

字典是无序的

字典中key具有唯一性

字典支持del

字典中的值可以是任意对象

列表、字典不能作为字典的key

常用:

.keys()

.values()

.items()

dict.fromkeys() 根据传入的参数创建字典,并可以指定统一的值

.get()  根据key获取值,key不存在时可以指定返回默认值(None

.pop()  根据key删除对应的键值对,若key不存在可以指定返回值

.popitem() 随机删除字典中的键值对

.setdefault() 设置值:若key存在,返回当前key对应的value;若key不存在,则添加键值对

.update() 更新字典

enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中

di = {
	"name" : "LiMing",
	"age" : 20,
}
di.update({"name" : "xiaohong", 'age' : 30})
print(di)

di.update(name='xiaohong', age=40, like='sport')
print(di)

 序列解包

>>> x = 'hello'
>>> a, *_, c =x
>>> a
'h'
>>> c
'o'
>>> _
['e', 'l', 'l']

小结

数字:

int()

字符串:

replace/find/join/strip/startswith/split/upper/lower/format

列表:

append/extend/inset

索引/切片/循环

元组:

索引/切片/循环,一级元素不能修改

字典:

get/update/keys/values/items

循环/索引/切片

运算符

算数运算

比较运算

赋值运算

逻辑运算

成员运算

 

身份运算

位运算

优先级

 更多内容:菜鸟教程

语句

输入输出

input()

print()

条件语句

if基本语句

if 条件:
    内部代码块
    内部代码块
else:
    ...

if嵌套

if 1 == 1:
	if 2 < 3:
		print("2<3")
	else:
		print('2>3')

if-elif

if 20 < 10:
	print("20<10")
elif 20 > 10:
	print("20>10")

循环语句

while

n = 1
while n < 11:
	if n == 7:
    	    pass
	else:
	    print(n)
	n = n + 1					
print('----end----')

for

pass

pass 代指空代码

continue

直接开始下一次循环

break

跳出当前循环

原文地址:https://www.cnblogs.com/dreamer-lin/p/11563514.html