Python基础

2019-06-02  本文已影响0人  阿_贵

# coding=utf-8

"""

多行注释

"""

from collectionsimport defaultdict

# 基本数据类型**************************************************

counter =100  # 整型

miles =1000.0  # 浮点型

integer00 =int(0)

integer11 =2/3    # int:0(整除) (python2) float:0.666666 (python3)

float00 =2/3.0    # float:0.666666

integer22 =9//2    # int:4(整除,向下取整,4.5 -> 4)

integer33 = -9//2  # int:-5(整除,向下取整,-4.5 -> -5)

integer44 =9 %7  # int:2(余数)

integer55 =2 **4  # int:16(2的4次方)

integer66 =33      # 33 原来是integer (变量可以改变类型)

integer66 =33.3    # 33.3 变成float

isBool1 =True and False  # False

isBool2 =True or False  # True

isBool3 =not True        # False

isBool4 =not 10  # bool False( 非0 相当于 True)

isBool5 =not 0    # bool True ( 0 相当于 False)

isBool55 =not 1  # bool False

isBool56 =not -1  # bool False

isBool57 =not 23  # bool False

isBool58 =not -15 # bool False

x, y =10, 20

iValue4 = xand y# int 20  如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值。

iValue5 =0 and y# int 0

iValue6 = xor y# int 10  如果 x 是非 0,它返回 x 的值,否则它返回 y 的计算值。

iValue7 =0 or y# int 20

isBool66 =not "yyy"  # bool False

isBool67 =not " "    # bool False

isBool68 =not "\n"  # bool False

isBool69 =not "\t"  # bool False

isBool70 =not ""    # bool True

str11 ="xxx" and "yyy"  # 'yyy'  # 串1 and 串2 :串1=空,返回空,否则返回串2

str22 ="" and "yyy"    # ''

str33 ="xxx" or "yyy"  # 'xxx'  # 串1 or 串2 :串1=空,返回串2,否则返回串1

str44 ="" or "yyy"      # 'yyy'

isBool88 =type(counter) ==int  # bool True, 判断变量类型

integer5 =16

isBool99 =10 < integer5 <20    # bool True

print(int(1.0 +2))

print(float(1 +2))

# 字符串**************************************************

name11 ="Jason\""

name12 ='Jason\"'  # '' === "" 单引号双引号无区别

name22 ='Jason' +"Xu"

name33 ='zh' *5

iVar =int("99")# string -> int

strVar =str(88)# int -> string

isBool9 = strVar.isdigit()# True

len11 =len('a')# 1

len12 =len('aa')# 2

len13 =len("aaa")# 3

str1 ='012345'

h11 = str1[1]# h11 = 1

h21 = str1[-1]# h21 = 5    #-1代表字符串末尾:3=-3 4=-2 5=-1

h31 = str1[1:4]# h31 = 123  #不包括 4

h41 = str1[-3:-1]# h41 = 34    #不包括 -1

h42 = str1[-3:]# h42 = 345

h51 = str1[:-1]# h51 = 01234 #不包括 -1

h61 = str1[:5]# h61 = 01234 #不包括 5

h71 = str1[4:]# h71 = 45

h81 = str1[-3:]# h81 = 345

# [beg:end:step]

h11 = str1[1:5:2]# h11 = 13      #不包括 5

h22 = str1[1::2]# h22 = 135

h33 = str1[::1]# h33 = 012345

h44 = str1[::-1]# h44 = 543210  #顺序颠倒

h55 = str1[::2]# h55 = 024

h66 = str1[::-2]# h66 = 531    #顺序颠倒 间隔为2

str222 ="str"

str333 ="str1"

str444 ="str"

isEqual1 = str222 == str333# False

isEqual2 = str222 == str444# True

text ="Game #Over #Game Over "  # 默认以空字符分割,包括空格、换行(\n)、制表符(\t)等

wordList1 = text.split()# wordList1 = : ['Game', '#Over', '#Game', 'Over']

wordList2 = text.split("#")# wordList2 = : ['Game ', 'Over ', 'Game Over ']

oneOne =11

twoTwo =22

pi =3.141592653

str111 =f"{oneOne} one, {twoTwo} two"          # '11 one, 22 two'

str222 =f"{oneOne:.1f} one, {twoTwo:.1f} two"  # '11.0 one, 22.0 two'

str333 =f"pi = {pi:.6f} != {22/7:.5f}"        # 'pi = 3.141593 != 3.14286'  4舍5入

str777 ="{}".format("swedish"[-2:])# sh

str888 ="{}{}".format("swedish"[-2:], "beet"[1:3])# shee

str999 ="{}{}{}".format("swedish"[-2:], "beet"[1:3], "swedish"[-2:])# sheesh

# 元组 一旦创建不可以修改其中元素,也不能单独删除一个元素 **************************************************

tuple1 = ()

tuple1 = (50,)# 元组中只包含一个元素时,需要在元素后面添加逗号

tuple1 = ('runOob', 786, 2.23, True)# 可以改变整个 tuple1 的值

tuple1 = tuple1 + (60,)# 可以增加一个元素

# tuple1[0] = 456 错误 # 不能改变元素的值

intTuple0 = tuple1[1]# 786, 访问元组元素

tuple1 = (123, ('runOob', "786"), 456)

strTuple = tuple1[1][1]# "786" 访问元组中,元素为元组中的元素

# 列表,可以有重复值,每个元素类型可以不一致, 是一种可变序列 **************************************************

list00 =list()

list00.append('item0')

list11 =list()

list11.append((4, 'item0'))# (4, 'item0') 是一个元组类型的元素

list11.append('item1')

list11.append('item2')

del list11[1]# 删除第 1 个元素

valueList = list11.pop()# item1 删除最后一个并返回给 valueList

list11.clear()# 删除所有

list1 = ['runOob', 786, 2.23, 'john', 70.2]

tinyList = [123, 'john']

print(list1)# 输出完整列表

print(list1[0])# 输出列表的第 0 个元素

print(list1[1:3])# 输出第 1 个至第 2 个元素,不包括第 3 个元素

print(list1[2:])# 输出从第 2 个开始至列表末尾的所有元素

print(tinyList *2)# 输出列表两次

print(list1 + tinyList)# 打印组合的列表

list44 = [786, 2.2, 70.3]

list55 =sorted(list44)# list55 = [2.2, 70.3, 786] 排序

list66 = ((786, 2), (2.2, 1), (70.3, 3))

list77 =sorted(list66)# list77 = [(2.2, 1), (70.3, 3), (786, 2)] 按照元组第 0 个元素排序

list88 =sorted(list66, reverse=True)# list88 = [(786, 2), (70.3, 3), (2.2, 1)] 倒序

上一篇 下一篇

猜你喜欢

热点阅读