python笔记基础--变量、数据类型、列表(1)

目录

一.Python

二.变量和简单数据类型

1.变量

2.字符串

2.1在字符串中使用变量

2.2添加空白:制表符/换行符

2.3字符串拼接和占位符

2.4数字精度

3.数

4.转换数据类型

5.注释

6.标识符

三.列表

0.数据容器

1.列表

2.组织列表

3.操作列表

3.1遍历整个列表

3.2 for循环

4.创建数值列表

4.1函数range( )

4.2数字列表

4.3列表解析

5.使用列表一部分

5.1切片

5.2复制列表

6.元祖



一.Python

Python解释器的作用:

将Python代码翻译成计算机认识的0和1并提交计算机执行

在解释器环境内可以一行行的执行我们输入的代码

也可以使用解释器程序,去执行”.py”代码文件

二.变量和简单数据类型

类型

描述

说明

数字(Number)

整数(int)

10、-10

浮点数(float)

13.14、-13.14

复数(complex)

4+3j,以j结尾表示复数

布尔(bool)

真和假,True表示真,False表示假。

True:1,False:0

字符串(String)

描述文本的一种数据类型

由中文、英文、各类符号、数字等组成。

‘abc’  “abc”

列表(List)

有序的可变序列

使用最频繁的数据类型,可有序记录一堆数据

元组(Tuple)

有序的不可变序列

有序记录一堆不可变的Python数据集合

集合(Set)

无序不重复集合

无序记录一堆不重复的Python数据集合

字典(Dictionary)

无序Key-Value集合

无序记录一堆Key-Value型的Python数据集合

类型

程序中的写法

输出

整数

print(10)

10

浮点数(小数)

print(13.14)

13.14

字符串(文本)

print("黑马程序员")

黑马程序员

1.变量

变量名称 = 变量的值 【变量没有类型,存储的数据有类型

变量名

可以用

只能包含字母、数字、下划线。

开头:字母/下划线

hand = "abcdefg"
print(hand)

不能用

不能以数字打头

不能包含空格,可以使用下划线分割单词

不能用关键字和函数名

不建议

不建议用小写字母1和大写字母0

查看数据类型type:type(变量)

2.字符串

①由中文、英文、各类符号、数字等组成。

②"和"""单、双、三引号,可以跨行、可以在其中自由的使用单双引号

单引号定义法

双引号定义法

三引号定义法

name = '我是公主'

name = "我是公主"

name ="""
我是公主
是公主
"""

可以内含双引号

可以内含单引号

可以使用转义字符(\)将引号解除效用,变成普通字符串name = "\"我是公主"

③r前缀:在字符串前面加上r或者R前缀,表示该字符串不做特殊的处理

f前缀:3.6版本开始,新增f前缀,格式化字符串

转义符

输出:c:\nt

输出:c:

\\

\t

\r

\n

\’

\”

a = r"c:\nt"print(a)

a = print("c:\nt")

空格

换行

a = "c:\\nt"print(a)

续行:在行尾使用\ ;如果使用各种括号,认为括号内是一个整体,内部跨行不用\

hand = "aba python"

大写

print(hand.upper())

ABA PYTHON

小写

print(hand.lower())

aba python

首字母大写

print(hand.title())

Aba Python

2.1在字符串中使用变量

合并

两个变量

first = "abc"
last = "python"
full_name = f"{first} {last}"
print(full_name)

abc python

print(f"Hello,{full_name}!")

Hello,abc python!

full_name = "{} {}".format(first,last)
print(full_name)

abc python

2.2添加空白:制表符/换行符

空白

\t

print("Python")
print("\tPython")

Python

Python

换行

\n

print("Languages:\nPython\nJava")

Languages:

Python

Java

删除

空白

age = '  python   '

A.rstrip( ) 删除右边空白

print(age.rstrip())

  python

A.lstrip( ) 删除左边空白

print(age.lstrip())

python

A.strip( ) 删除所有空白

print(age.strip())

python

2.3字符串拼接和占位符

字符串不能和非字符串类型的进行拼接

正确

print("我是" + "公主")

name, tel = "公主", "129498280"
print("我是" + name + ",我的电话:" + tel)

错误

name, tel = "公主", 129498280
print("我是" + name + ",我的电话:" + tel)

原因:129498280是数字类型,字符串不能和非字符串类型的进行拼接

占位符

字符串:%s

整数:%d

浮点数:%f

name, tel = "公主", 129498280
full = "我是蘑菇、%s,我的电话是:%s" % (name,tel)

结果:我是蘑菇、公主,我的电话是:129498280

快速格式化

print(f"我是蘑菇、{name},我的电话是:{tel}")

full = f"我是蘑菇、{name},我的电话是:{tel}"

表达式格式化

print("1*1结果:%d" % (1*1))

1*1结果:1

print(f"1*1结果:{1*1}")

print("字符串类型是:%s" % type('字符串'))

字符串类型是:<class 'str'>

2.4数字精度

m.n

含义

例子【会四舍五入】

%5d

整数宽度为5,

56 → [空格][空格][空格]56

%5.2f

宽度为5,小数点精度为2

56.34534 → [空格][空格][空格]56.35  

%.2f

小数点精度为2

56.34534 → 56.35

3.数

①整数&浮点数

结果

结果

任意两数相除

结果都是浮点数

4/2

2.0

整数与浮点数运算

1+2.0

3.0

②数中的下划线:增加数据可读性14_000_000_000 = 14000000000

③给多个变量赋值x, y, z = 1,40,5

④常量:使用全部大写将特定变量视为常量MAX_AGE = 90

4.转换数据类型

int,float都可以转换为str

str不是都可以转换成int,float, str必须都是数字才可以转换。

数字类型转换成字符串

float_str = str(11.32)

num_str = str(11)

字符串转换为数字

num = int("111")
num = float("12.34")

错误的

num = int("文字")

5.注释

行注释

#这是行注释

段注释

'''
这是段注释
这是段注释
'''

6.标识符

标识符:只允许出现 英文、中文、数字、下划线。

              数字不可以开头,不可以使用关键字。

              大小写敏感

  • 三.列表

0.数据容器

数据容器:可以存储多个元素的Python数据类型

分为:列表(list)、元祖(tuple)、字符串(str)、集合(set)、字典(dict)

1.列表

colours = ['red','yellow','blue']

print(colours)

['red', 'yellow', 'blue']

提取列表第一个

print(colours[0])

red

提取列表最后一个

print(colours[-1])

blue

提取列表第一个且首字母大写

print(colours[0].title())

Red

提取列表中内容

message=f"I like {colours[0]}."

I like red.

修改列表元素

colours[0] = 'black'

['black', 'yellow', 'blue']

添加

列表元素

末尾

colours.append('black')

['red', 'yellow', 'blue', 'black']

表中

colours.insert(0,'black')

['black', 'red', 'yellow', 'blue']

删除

列表元素

Del语句

del colours[0]

['yellow', 'blue']

Pop( )

末尾

colours_pop = colours.pop()

Colours = ['red', 'yellow']

colours_pop = blue

任意

位置

colours_pop = colours.pop(0)

Colours = ['yellow', 'blue']

colours_pop = red

Remove()

删值

colours.remove('blue')

['red', 'yellow']

2.组织列表

调整排列顺序。

colours = ['red','yellow','blue']

永久排列

字母顺

序排列

顺序sort( )

colours.sort()

['blue', 'red', 'yellow']

逆序

colours.sort(reverse=True)

['yellow', 'red', 'blue']

临时排列

sorted( )

不影响原始列表排列顺序

print(sorted(colours))

倒着

打印列表

reverse( )

colours.reverse()

['blue', 'yellow', 'red']

列表长度

len( )

print(len(colours))

3

3.操作列表

3.1遍历整个列表

遍历整个列表

结果

colours = ['red','yellow','blue']
for colour in colours:
    print(colour)

red

yellow

blue

3.2 for循环

设置

抬头

消息

colours = ['red','yellow','blue']
for colour in colours:
    print(f"{colour.title()},this is a beautiful color!")

Red,this is a beautiful color!

Yellow,this is a beautiful color!

Blue,this is a beautiful color!

colours = ['red','yellow','blue']
for colour in colours:
    print(f"{colour.title()},this is a beautiful color!\n")

Red,this is a beautiful color!

Yellow,this is a beautiful color!

Blue,this is a beautiful color!

4.创建数值列表

4.1函数range( )

代码

结果

打印数1-3

for value in range(1,4):
    print(value)

1

2

3

打印数0-3

for value in range(4):
    print(value)

0

1

2

3

创建数字列表

numbers = list(range(1,4))
print(numbers)

[1, 2, 3]

打印1-10偶数

numbers = list(range(2,11,2))

[2, 4, 6, 8, 10]

打印前10的平方数

squares = []
for value in range(1,11):
    square = value**2
    squares.append(square)
print(squares)

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

squares = []
for value in range(1,11):
    squares.append(value**2)
print(squares)

4.2数字列表

digits = [1,2,3,4,5,6,7]

列表最小值

min(digits)

1

列表最大值

max(digits)

7

列表总和

sum(digits)

28

4.3列表解析

将for循环和创建新元素的代码合并成一行,并自动附加新元素。

平方

squares = []
for value in range(1,11):
    squares.append(value**2)
print(squares)

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

列表解析

squares = [value**2 for value in range (1,11)]

5.使用列表一部分

5.1切片

切片

players=['1Ann','2Bob','3Daylan','4Seiri','5Job']

players[0:3]

['1Ann', '2Bob', '3Daylan']

players[1:3]

['2Bob', '3Daylan']

print(players[:3])

['1Ann', '2Bob', '3Daylan']

print(players[3:])

['4Seiri', '5Job']

print(players[-3:])

['3Daylan', '4Seiri', '5Job']

遍历切片

for player in players[:3]:
    print(player.title())

1Ann

2Bob

3Daylan

5.2复制列表

foods=['apple','cake','banana']

复制列表

print(foods)

['apple', 'cake', 'banana']

my_food=foods[:]

print(my_food)

['apple', 'cake', 'banana']

给每个列表

分别加元素

foods.append('egg tart')
my_food.append('orange')

foods:['apple', 'cake', 'banana', 'egg tart']

my_food:['apple', 'cake', 'banana', 'orange']

错误情况:

my_food=foods
foods.append('egg tart')
my_food.append('orange')

foods:['apple', 'cake', 'banana', 'egg tart', 'orange']

my_food:['apple', 'cake', 'banana', 'egg tart', 'orange']

6.元祖

不可变的列表。

代码

结果

定义

元祖

无法修改

numbers = (10,22)

print(numbers[0])
print(numbers[1])

10

22

如果只有一个元祖

numbers = (10,)

遍历元祖中所有值

for number in numbers:

    print(number)

10

22

修改元祖变量

不能修改元祖的元素,但可以给存储元祖的变量赋值。

numbers = (10,22)
numbers = (3,80)
for number in numbers:
    print(number)

3

80

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值