跳至主要內容

01-python第一课

黑静美...大约 8 分钟编程python

0.来个传统

print "hello word"

Python中的变量

变量就是在内存中开辟空间。

每个变量用的是同一块内存空间,所以新一次改变变量(如:赋值),变量旧的属性被覆盖(新的值取代旧的值)。

x = 1  # 1赋值给了x,x代表1
x = x + 10 # x+10等价于 1+10,最后得出11,11赋值给x
# 也就是说11 覆盖了 1
print(x)  # 打印x 结果:11
# 用来注释(注意井号后面有空格)

井号 # 用来注释,解释某一行代码的功能和作用

代码的运行逻辑:

从上到下,从右到左,最后才是赋值

也可以通过变量把一个变量的值直接赋给另一个变量

name1 = "lilei"
name2 = name1
print(name1)
print(name2)

1.输出print

print()

1.同时输出变量

python支持同一命令同时输出变量,默认以空格(占位字符)输出变量

a = 1
b = 2
c = 3
print(a, b, c)

2. 变量之外sep&end

  • 使用sep = 更改间隔的内容
a = 1
b = 2
c = 3
print(a,b,c, sep = "间隔")

默认最后一个字符串变量


不同行的print默认输出换行

print(a)
print(b)
print(c)
  • 使用end = 改变换行内容
print(a, end = "结束")
print(b, end = "结束")
print(c, end = "结束")
  • end 可以和sep同时使用
a = 1
b = 2
c = 3
print(a, b, c, sep="~", end="love Python")

输出结果将没有换行,如有需要添加print()换行

此外print还可以按照一定格式输出

2. 变量

1. 赋值方式

  • 变量基本赋值方式如上,将右边的值赋给左边

  • 变量也可以同时赋值

a, b, c = 1, 2, 3
print(a, b, c)
  • 同时赋同一个值
a = b = c = 1
print(a, b, c)
  • python特色 交换值
Austin = "Coke" # 该赋值可以理解为倒果汁的过程
Jaden = "juice" # 该赋值可以理解为倒果汁的过程 
Austin, Jaden = Jaden, Austin  #先右边取值,先认定杯子
 # 直接交换杯子(赋值),这样就不需要一个新杯子换果汁了
print("Austin", Austin)       
print("Jaden", Jaden)

2. 变量的命名规则

  • 大小写英文、数字和_下划线的结合,且不能用数字开头; 【数字开头报错1】
    • 不能包含空格,请用下划线代替 【报错2】
  • Python中变量名区分大小写
  • **系统关键词**不能做变量名使用「获取关键词列表:help('keywords')」【报错类型3】
  • 不要使用Python内置函数名称做变量(如:print) 【4】
Here is a list of the Python keywords.  Enter any keyword to get more help.

False               break               for                 not
None                class               from                or
True                continue            global              pass
__peg_parser__      def                 if                  raise
and                 del                 import              return
as                  elif                in                  try
assert              else                is                  while
async               except              lambda              with
await               finally             nonlocal            yield

:::tab

@tab【报错1】

1n = 1

@tab 【报错2】

n n = 1

@tab 【报错3】

await = 1

@tab 【4】

没报错。但是不能运行

print = 1
print(print)

:::

3. 数据类型

python不需要要求申明数据类型,会根据值自动分配一个默认的数据类型

布尔型 boolean
关键词: bool()
数字型 integer
整型:int()
浮点型:float()
字符串 String
关键词:str()
元组 Tuple
关键词:tuple()
列表 list
关键词:list()
字典 dictionary
关键词:dict()
集合 set
关键词:set()

如果想要知道数据被分配了哪种类型,请使用type()

type

a. 数字型「int、float」「bool」

  1. 整形
int_num = 1
t = type(int_num)
print(int_num)
print("int num type is>>>",t)
print("直接检测数据类型并输出:",type(int_num))
  1. 浮点数
float_num = 1
t = type(int_num)
print(int_num)
print("int num type is>>>",t)
print("直接检测数据类型并输出:",type(int_num))
  1. 布尔型 「bool」
condition= True 
t = type(condition)
print(condition)
print("int num type is>>>",t)
print("直接检测数据类型并输出>>>",type(condition))

b. 字符串「str」

注:python没有“字符”这种单独类型,一个字母也是字符串

  • python“字符串”采用UFT-8编码
string = "Hello llohe"
t = type(string)
print(string)
print("int num type is>>>",string)
print("直接检测数据类型并输出>>>",type(string))
特性
  1. 有序性

    1. 从左到右。下标为0开始,到n
    2. 从右到左。下标从-1开始,到-n
    3. 引号里出现的,都算一个下标
  2. 不可变性(hashable)

    1. 注意⚠️:不可变性,指的的是字符串在创建后,其内容是不能被直接修改、追加或删除的。

      • 不是不能用新的值代替旧的值绑定给该字符串(绑定:用等号赋值)

      • 这里新的值和旧的值在Python中的地址是不一样的

        地址: 变量或者某一(组)值(不同的编程语言储存的内容不同,变量是等号左边的,某一(组)值是等号右边的)在内存中储存的位置

      什么是可哈希性
  3. 任意字符

    1. 键盘⌨️上可以输入的任何字符,都是字符串的元素
    2. 字符放到字符串中,都将成为字符串类型。「也就是:里面的每一个元素都可以称为子字符」

c. 列表「list」

lst = ["Hallo", "lloha",1, 1.1, ("look",11),[12,"汉堡包", True, False]]
t = type(lst)
print(lst)
print("int num type is>>>",t)
print("直接检测数据类型并输出>>>",type(lst))
特性
  1. 有序性

    1. 从左到右。下标为0开始

    2. 从右到左。下标从-1开始

    3. 列表中每一个元素算一个下标

      比如:

      1. Ha1lo整体是下标0,lloha是下标1
      2. ("look","book",11) 整体是下标4
  2. 可变性(unhashable)

    1. 程序在运行过程中,列表可以被「添加、删除、修改」
    2. 不可追溯哈希值
  3. 任意数据类型

    python所有的数据类型

d. 元组「tuple」

tup = ("Hallo", "lloha",1, 1.1, ("look","book",11),[12,"汉堡包"], True, False)
t = type(tup)
print(tup)
print("int num type is>>>",t)
print("直接检测数据类型并输出>>>",type(tup))
image-20240426162514876
image-20240426162514876
特性
  1. 有序性

    1. 从左到右。下标为0开始

    2. 从右到左。下标从-1开始

    3. 列表中每一个元素算一个下标

      比如:

      1. Ha1lo整体是下标0,lloha整体是1
      2. ("look","book",11) 整体是下标4
  2. 不可变性(hashable)

    1. 创建后内容不能被直接修改、追加或删除
    2. 元组中可以插入可变的列表,但是插入后将变成不可哈希
  3. 可插入任意数据类型

e.集合「set」

set1={1,2,"aiyc",1.1,"book",(1,2,3),False}
t = type(set1)
print(set1)
print("set1 type is>>>",t)
print("直接检测数据类型并输出>>>",type(set1))

# output {False, 1, 2, 1.1, 'book', (1, 2, 3), 'aiyc'}
特性
  1. 无序性

    集合是没有顺序的,也就是没有下标

    output的顺序可能和input不同

  2. 可变性

    1. 不可以追溯哈希值

      print(hash({1,2,3}))
      # TypeError: unhashable type: 'set'
      
    2. 可变但是在其中插入可变参数会报错

      set1 ={1,2,[1,2,3]}
      # TypeError: unhashable type: 'list'
      
  3. 去重性

    set1 = {1, 2, 1, 1, 2, 1, 1}
    print("直接检测数据类型并输出>>>",type(set1))
    print(set1)
    

    打印结果:

    直接检测数据类型并输出>>> <class 'set'>
    {1, 2}
    

f. 字典「dict」

d = {"name":"aiyc","age":18,1:"int",1.1:1,"tup":(123)}
t = type(d)
print(d)
print("d types is:>>>",t)
print("直接检测数据类型,并输出:>>>",type(d))
特性
  1. 无序性[python 3.6. 之后有序]

    1. 3.6.之后可以用for循环遍历
    2. 先以无序性理解即可,有些前期基本用不到;
    3. 索引不是列表元组的数值,而且前边的Key
  2. 字典的组成:是由一系列的key和value组成。d = {"key1": “value1“,“key2“:"value2".....}

  3. Key:

    1. 不更改的数据类型,才可以当作字典key; 比如:字符串、数字、布尔、元组;
    2. 不能插入列表
  4. value: 可以插入列表

    • 可以插入Python所有的数据类型
  5. 可变性(unhashable):

    1. 不可以追溯哈希值
    2. 可以添加、修改、删除key对应值;
      1. 删除key:删除该位置,下一位放入该位置
      2. 不是删除value:删除value后保留该位置,value变为空

哈希值相关hash()

d = {"name":"aiyc","age":18,1:"int",1.1:1,"tup":(123)}

print(hash(123))
print(hash("hello"))
print(hash((1,2,3)))
# print(hash([1,2,3])) todo: TypeError: unhashable type: 'list'
# print(hash((1,[2,3]))) todo: TypeError: unhashable type: 'list' 
# print(hash({1,2,3})) todo: TypeError: unhashable type: 'set'
# print(hash(d)) todo: TypeError: unhashable type: 'dict'

打印结果:

123 # 数字型的哈希值是其本身
4121659629528356476 # 是一串生成的数字,有些“随机”
529344067295497451 # 是一串固定的数字

总结

复合类型字符串列表元组集合字典
有序性
可哈希性(不可变性)在插入列表之前可以
可插入全部数据类型
内部类型仅为UFT-8编码的内容!

不能插入可变性
Vuale:✅
Key:❌
上次编辑于:
贡献者: Heijingmei
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v3.1.3