python学习日记【03-基本数据类型(含字符串常用操作)】

03-基本数据类型

  • 1.几个概念
    • 1.1 表达式
    • 1.2 语句
    • 1.3 程序(program)
    • 1.4 函数(function)
  • 2 标识符
    • 2.1 关键字
    • 2.2 标识符概念
  • 3 基本数据类型
    • 3.1 整数和小数
    • 3.2 布尔值和空值
  • 4 变量
    • 4.1 变量的概念?
    • 4.2 变量的运算
  • 5 字符串 ★
    • 5.1什么是字符串
    • 5.4 格式化字符串 ★
    • 5.5 字符串的其他操作 ★

1.几个概念

1.1 表达式

  • 表达式,是有数字、算符、数字分组符号(括号)、自由变量和约束变量等以能求得数值的有意义排列方法所得的组合
  • 表达式特点
    • 表达式一遍仅仅用于计算一些结果,不会对程序产生实质性的影响
    • 如果在交互模式中输入一个表达式,解释器会自动将表达式的结果输出

1.2 语句

  • 一个语法上自称体系的单位,它由一个词或句法上有关联的一组词构成
  • 语句的执行一般会对程序产生一定的影响,在交互模式中不一定会输出语句的执行结果

1.3 程序(program)

  • 程序就是由一条一条的语句和一条一条的表达式构成的。

1.4 函数(function)

  • 函数就是一种语句,函数专门用来完成特定的功能
  • 形如:xxx() 的就是函数
  • 函数的分类:
    • 内置函数:或者内建函数,就是由语法规定存在的函数,这些函数,包含在编译器的运行时库中,程序员不必单独书写代码实现它,只需要调用即可
    • 自定义函数: 有程序员自主的创建的函数,当我们需要完成某个功能时,就可以去调用内置函数,或者自定义函数
  • 函数的2个要素
    • 参数
    • 返回值

2 标识符

2.1 关键字

  • python 一些具有特殊功能的标识符,这就是所谓的关键字,是python已经使用的了,所以不允许开发者自己定义和关键字相同的名字的标识符,如下:
    python学习日记【03-基本数据类型(含字符串常用操作)】_第1张图片

2.2 标识符概念

  • 开发人员在程序中自定义的一些符号和名称。标识符是自己定义的,如变量名、函数名等
  • 组成:由26个英文字母大小写、数字0-9、符号_$
  • 标识符的规则:
    • 1.标识符中可以包含字母、数字、_,但是不能使用数字开头,例如:name1、name_1、而1name不行
  • 命名方式
  • 驼峰命名法
    • 小驼峰式命名法:第一个单词以小写字母开始;第二个单词的首字母大写,例如:myName
    • 大驼峰式命名法:每一个单词的首字母都采用大写字母,例如:FirstNameLastName
  • 下划线命名法:
    • 不过在程序员中还有一种命名法比较流行,就是用下划线“_”来链接所有的单词,比如get_urlbuffer_size

3 基本数据类型

  • 数据类型指的就是变量的值的类型,也就是可以为变量赋哪些值

3.1 整数和小数

  • 整数 : 所有整数 例如 : a = 1 b = 100 c =999 都是int类型
  • 小数常量 : 所有小数 例如 a = 1.2 b=6.66 用float类型表示

3.2 布尔值和空值

  • 布尔:只有2个值,TrueFalse
  • None敞亮:只有一个值,就是None也就是空值

4 变量

4.1 变量的概念?

  • 变量是计算机内存中的一块区域,存储规定范围内的值,值是可以改变的,通俗的说变量就是给数据起个名字
  • 变量的命名规则
    • 变量名由字母、数字、下划线组成,要符合标识符的命名规范
    • 不能以数字开头
    • 不能使用关键字
  • 注意:两个对象相等和两个对象是同一个对象是两个概念python学习日记【03-基本数据类型(含字符串常用操作)】_第2张图片

4.2 变量的运算

  • 变量的运算就是正常的四则运算,需要注意的是在运算过程中含有浮点数,那么它返回的就是一个浮点数类型

5 字符串 ★

5.1什么是字符串

  • 长字符串又叫做文档字符串,我们使用三重引号来表示一个长字符串’’’ ‘’’
  • 三重引号可以换行,并且会保留字符串中的格式

5.4 格式化字符串 ★

  • 第一个格式化字符串----拼串
'my' + 'blog'
  • 第二种格式化字符串----参数传递
print('my', 'blog')
  • 第三种格式化字符串----占位符
    • %s 字符串占位
    • %f 浮点数占位
    • %d 整数占位
print('%s' % 'abcdefg')
# abcdefg
print("%f" % 32133.5465451)
# 32133.546545
print("%.8f" % 32133.5465451)
# 32133.54654510
print("%d" % 32133.5465451)
# 32133
  • 第四种格式化字符串f’{变量}’/ str.format
"{} {}".format("hello", "world")  # 不设置指定位置,按默认顺序
# 'hello world'

"{0} {1}".format("hello", "world")  # 设置指定位置
# 'hello world'

"{1} {0} {1}".format("hello", "world")  # 设置指定位置
# 'world hello world'

5.5 字符串的其他操作 ★

>>> len('abcd') # 获取字符串长度
4
>>> max('abcd') # 最大值
'd'
>>> min('abcd') # 最小值
'a'
>>> 'abcdefg'.split('d') #分割字符串
['abc', 'efg']
>>> 'abc'.join('123') # 拼接字符串
'1abc2abc3'
  • 去空格
>>> ' abc '.strip() # 去除左右两边的空格
'abc'
>>> ' abc '.lstrip() # 去除左边空格
'abc '
>>> ' abc '.rstrip() # 去除右边空格
' abc'
  • 字符串大小写
>>> 'acbd'.upper() # 全部大写
'ACBD'
>>> 'acbd'.lower() # 全部小写
'acbd'
>>> 'acbd'.isupper() # 判断是不是大写
False
>>> 'acbd'.islower() # 判断是不是小写
True
  • 字符串操作练习:
  • find
# mystr.find(str, start=0, end=len(mystr))
# 检测 str 是否包含在 mystr中,如果是返回开始的索引值,否则返回-1
>>> mystr = 'hello world itcast and itcastcpp'
>>> mystr.find('world')
6
>>> mystr.find('world', 0, 10)
-1
  • index
# mystr.index(str, start=0, end=len(mystr))
# 跟find()用法一样,只不过如果str不在 mystr中会报一个异常.
>>> mystr = 'hello world itcast and itcastcpp'
>>> mystr.index('world')
6
>>> mystr.index('world', 0, 10)
Traceback (most recent call last):
  File "", line 1, in <module>
ValueError: substring not found
  • count
# mystr.count(str, start=0, end=len(mystr))
# 返回 str在start和end之间 在 mystr里面出现的次数
>>> mystr = 'hello world itcast and itcastcpp'
>>> mystr.count('itcast')
2
>>> mystr.count('itcast',0 , 10)
0
  • replace
# mystr.replace(str1, str2, mystr.count(str1))
# 把 mystr 中的 str1 替换成 str2,
# 如果 count 指定,则替换不超过 count 次
>>> mystr = 'hello world itcast and itcastcpp'
>>> mystr.replace("itcast",'66666')
'hello world 66666 and 66666cpp'
>>> mystr.replace("itcast",'66666', 1)
'hello world 66666 and itcastcpp'
  • split
# mystr.split(str=" ", 2)
# 以 str 为分隔符切⽚ mystr,
# 如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串
>>> mystr = 'hello world itcast and itcastcpp'
>>> mystr.split(" ")
['hello', 'world', 'itcast', 'and', 'itcastcpp']
>>> mystr.split(" ", 2)
['hello', 'world', 'itcast and itcastcpp']
  • capitalize
# mystr.capitalize()
# 把字符串的第一个字符大写
>>> mystr = 'hello world itcast and itcastcpp'
>>> mystr.capitalize()
'Hello world itcast and itcastcpp'
  • title
# mystr.capitalize()
# 把字符串的每个单词首字母大写
>>> mystr = 'hello world itcast and itcastcpp'
>>> mystr.title()
'Hello World Itcast And Itcastcpp'
  • startswith
# mystr.startswith(hello)
# 检查字符串是否是以 hello 开头, 是则返回 True,否则返回 False
>>> mystr = 'hello world itcast and itcastcpp'
>>> mystr.startswith('hello')
True
>>> mystr.startswith('world')
False
  • endswith
# mystr.endswith(obj)
# 检查字符串是否以obj结束,如果是返回True,否则返回 False.
>>> mystr = 'hello world itcast and itcastcpp'
>>> mystr.endswith('cpp')
True
>>> mystr.endswith('hellp')
False
  • lower
# mystr.lower()
# 转换 mystr 中所有大写字符为小写
>>> mystr = 'HELLO world itcast and itcastcpp'
>>> mystr.lower()
'hello world itcast and itcastcpp'
  • upper
# mystr.upper()
# 转换 mystr 中的小写字母为大写
>>> mystr = 'HELLO world itcast and itcastcpp'
>>> mystr.upper()
'HELLO WORLD ITCAST AND ITCASTCPP'
  • rjust
# mystr.rjust(width)
# 返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串
>>> mystr = 'hello'
>>> mystr.rjust(10)
'     hello'
  • center
# mystr.center(width)
# 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串
>>> mystr = 'hello'
>>> mystr.center(50)
'                      hello                       '
  • rfind
# mystr.rfind(str, start=0,end=len(mystr) )
# 类似于 find()函数,不过是从右边开始查找.
>>> mystr = 'hello world itcast and itcastcpp'
>>> mystr.rfind('world')
6
  • rindex
# mystr.rindex( str, start=0,end=len(mystr))
# 类似于 index(),不过是从右边开始.
>>> mystr = 'hello world itcast and itcastcpp'
>>> mystr.rindex('world')
6
  • partition
# mystr.partition(str)
# 把mystr以str分割成三部分,str前,str和str后
>>> mystr = 'hello world itcast and itcastcpp'
>>> mystr.partition('itcast')
('hello world ', 'itcast', ' and itcastcpp')
  • rpartition
# mystr.rpartition(str)
# 类似于 partition()函数,不过是从右边开始
>>> mystr = 'hello world itcast and itcastcpp'
>>> mystr.rpartition('itcast')
('hello world itcast and ', 'itcast', 'cpp')
  • splitlines
# mystr.splitlines()
# 按照行分隔,返回一个包含各行作为元素的列表
>>> mystr = 'hello \n world'
>>> mystr.splitlines()
['hello ', ' world']
  • isalpha
# mystr.splitlines()
# 如果 mystr 所有字符都是字母 则返回 True,否则返回 False
>>> '123'.isalpha()
False
>>> 'abc'.isalpha()
True
>>> 'abc 123'.isalpha()
False
  • isdigit
# mystr.isdigit()
# 如果 mystr 只包含数字则返回 True 否则返回 False.
>>> '123'.isdigit()
True
>>> 'abc'.isdigit()
False
>>> 'abc123'.isdigit()
False
  • isalnum
# mystr.isalnum()
# 如果mystr 所有字符都是字符或数字则返回 True,否则返回 False
>>> '123'.isalnum()
True
>>> 'abc'.isalnum()
True
>>> 'abc123'.isalnum()
True
>>> 'abc 123'.isalnum()
False

  • isspace
# mystr.isspace()
# 如果 mystr 中只包含空格,则返回 True,否则返回 False
>>> 'abc123'.isspace()
False
>>> ''.isspace()
False
>>> ' '.isspace()
True
>>> '\t'.isspace()
True

你可能感兴趣的:(python学习记录)