小刘花了一段时间,学习了python基础,第一版!

538 阅读1小时+

image.png

#输出数字
print(520)
print(999)

#输出字符串
print('lyyyyyyyyyy')
print("lyyyyyyyyyy")


#含有运算符的表达式 (3和1是键,+是运算符)
print(3+1)

#将数据输出到文件当中(注意:1.所指定的盘符在存在2.使用file=fp)
fp=open('D:/text.txt','a+') #fp是一个变量,a+文件不存在,就创建文件。文件存在的话,就在原有的 文件上去追加。
print('helloworld',file=fp)#file= 文件的对象,数据在text.txt显示
fp.close()#close关闭,

#不进行换行输出(输出内容在一行当中)逗号进行分隔一行输出
print('hello','world','python')

image.png

image.png

#转义字符(反斜杠+想要实现的转义功能首字母)
#原字符,不希望字符串中的转义字符起作用,就使用原字符,就是在字符串之前加上r或者R
print(r'hello\nworld')
print(r'hello\nworld\')#注意最后一个不能是反斜杠,两个行,一个不行

#反斜杠:\
print('http:\\www.baidu.com')
#单引号:'
print('我们说:'世界你好'')
#双引号:"

#换行:\n转义功能首字母 n-->newline的首字母表示换行
print('hello\nworld')
#回车:\r 将l覆盖了
print('l\ry')
#水平制表符:\t  实际上就是4个空格
print('hello\tworld')
#退格:\b  退一格,将0退没了
print('hello\bworld')

image.png

Unicode汉字编码表

Unicode只有一个字符集,中、日、韩的三种文字占用了Unicode中0x3000到0x9FFF的部分。 Unicode目前普遍采用的是UCS-2,它用两个字节来编码一个字符, 比如汉字"经"的编码是0x7ECF,注意字符编码一般用十六进制来 表示,为了与十进制区分,十六进制以0x开头,0x7ECF转换成十进制 就是32463,UCS-2用两个字节来编码字符,两个字节就是16位二进制, 2的16次方等于65536,所以UCS-2最多能编码65536个字符。 编码从0到127的字符与ASCII编码的字符一样,比如字母"a"的Unicode 编码是0x0061,十进制是97,而"a"的ASCII编码是0x61,十进制也是97, 对于汉字的编码,事实上Unicode对汉字支持不怎么好,这也是没办法的, 简体和繁体总共有六七万个汉字,而UCS-2最多能表示65536个,才六万 多个,所以Unicode只能排除一些几乎不用的汉字,好在常用的简体汉字 也不过七千多个,为了能表示所有汉字,Unicode也有UCS-4规范,就是用 4个字节来编码字符,不过现在普遍采用的还是UCS-2,只用两个字节来 编码,看一下Unicode对汉字的编码

汉字编码表
U+  0   1  2  3  4  5  6  7  8  9  A  B  C  D  E  F    
-----------------------------------------------------  
4e00                  
4e10                  
4e20                  
4e30                丿  
4e40                  
4e50                  
4e60                  
4e70                乿  
4e80                  
4e90                  
4ea0                  
4eb0                亿  
4ec0                  
4ed0                  
4ee0                  
4ef0                仿  
4f00                  
4f10                  
4f20                  
4f30                伿  
4f40                  
4f50                  
4f60                  
4f70                使  
4f80                  
4f90                  
4fa0                  
4fb0                便  
4fc0                  
4fd0                  
4fe0                  
4ff0                俿  
5000 倀                 
5010                  
5020                  
5030                  
5040                  
5050                  
5060                  
5070                  
5080                  
5090                  
50a0                  
50b0                  
50c0                  
50d0                  
50e0                  
50f0                  
5100                  
5110                  
5120                  
5130                  
5140                  
5150                  
5160                  
5170                  
5180                  
5190                  
51a0                  
51b0                  
51c0                  
51d0                  
51e0                  
51f0                  
5200  ***                
5210                  
5220                  
5230                  
5240                  
5250                  
5260                  
5270                  
5280                  
5290                  
52a0                  
52b0                  
52c0                  
52d0                  
52e0                  
52f0                  
5300                  
5310                  
5320                  
5330                  
5340                  
5350                  
5360                  
5370                  
5380                  
5390                  
53a0                  
53b0                  
53c0                  
53d0                  
53e0                  
53f0                  
5400                  
5410                  
5420                  
5430                  
5440                  
5450                  
5460                  
5470                  
5480                  
5490                  
54a0                  
54b0                  
54c0                  
54d0                  
54e0                  
54f0                  
5500                  
5510                  
5520                  
5530                  
5540                  
5550                  
5560                  
5570                  
5580                  
5590                  
55a0                  
55b0                  
55c0                  
55d0                  
55e0                  
55f0                  
5600                  
5610                  
5620                  
5630                  
5640                  
5650                  
5660                  
5670                  
5680                  
5690                  
56a0                  
56b0                  
56c0                  
56d0                  
56e0                  
56f0                  
5700                  
5710                  
5720                  
5730                  
5740                  
5750                  
5760                  
5770                  
5780                  
5790                  
57a0                  
57b0                  
57c0                  
57d0                  
57e0                  
57f0                  
5800                  
5810                  
5820                  
5830                  
5840                  
5850                  
5860                  
5870                  
5880                  
5890                  
58a0                  
58b0                  
58c0                  
58d0                  
58e0                  
58f0                  
5900                  
5910                  
5920                  
5930                夿  
5940                  
5950                  
5960                  
5970                奿  
5980                  
5990                  
59a0                  
59b0                妿  
59c0                  
59d0                  
59e0                  
59f0                姿  
5a00                  
5a10                  
5a20                  
5a30                娿  
5a40                  
5a50                  
5a60                  
5a70                婿  
5a80                  
5a90                  
5aa0                  
5ab0                媿  
5ac0                  
5ad0                  
5ae0                  
5af0                嫿  
5b00                  
5b10                  
5b20                  
5b30                嬿  
5b40                  
5b50                  
5b60                  
5b70                孿  
5b80                  
5b90                  
5ba0                  
5bb0                宿  
5bc0                  
5bd0                  
5be0                  
5bf0                寿  
5c00                  
5c10                  
5c20                  
5c30                尿  
5c40                  
5c50                  
5c60                  
5c70                屿  
5c80                  
5c90                  
5ca0                  
5cb0                岿  
5cc0                  
5cd0                  
5ce0                  
5cf0                峿  
5d00                  
5d10                  
5d20                  
5d30                崿  
5d40                  
5d50                  
5d60                  
5d70                嵿  
5d80                  
5d90                  
5da0                  
5db0                嶿  
5dc0                  
5dd0                  
5de0                  
5df0                巿  
5e00                  
5e10                  
5e20                  
5e30                帿  
5e40                  
5e50                  
5e60                  
5e70                广  
5e80                  
5e90                  
5ea0                  
5eb0                庿  
5ec0                  
5ed0                  
5ee0                  
5ef0                廿  
5f00                  
5f10                  
5f20                  
5f30                弿  
5f40                  
5f50                  
5f60                  
5f70                彿  
5f80                  
5f90                  
5fa0                  
5fb0                徿  
5fc0                  
5fd0                  
5fe0                  
5ff0                忿  
6000 怀                 
6010                  
6020                  
6030                  
6040                  
6050                  
6060                  
6070                   
6080                  
6090                  
60a0                  
60b0                  
60c0                  
60d0                  
60e0                  
60f0                  
6100                  
6110                  
6120                  
6130                  
6140                  
6150                  
6160                  
6170                  
6180                  
6190                  
61a0                  
61b0                  
61c0                  
61d0                  
61e0                  
61f0                  
6200                  
6210                  
6220                  
6230                  
6240                  
6250                  
6260                  
6270                  
6280                  
6290                  
62a0                  
62b0                  
62c0                  
62d0                  
62e0                  
62f0                  
6300                  
6310                  
6320                  
6330                  
6340                  
6350                  
6360                  
6370                  
6380                  
6390                  
63a0                  
63b0                  
63c0                  
63d0                  
63e0                  
63f0                  
6400                  
6410                  
6420                  
6430                  
6440                  
6450                  
6460                  
6470                  
6480                  
6490                  
64a0                  
64b0                  
64c0                  
64d0                  
64e0                  
64f0                  
6500                  
6510                  
6520                  
6530                  
6540                  
6550                  
6560                  
6570                  
6580                  
6590                  
65a0                  
65b0                  
65c0                  
65d0                  
65e0                  
65f0                  
6600                  
6610                  
6620                  
6630                  
6640                  
6650                  
6660                  
6670                  
6680                  
6690                  
66a0                  
66b0                  
66c0                  
66d0                  
66e0                  
66f0                  
6700                  
6710                  
6720                  
6730                  
6740                  
6750                  
6760                  
6770                  
6780                  
6790                  
67a0                  
67b0                  
67c0                  
67d0                  
67e0                  
67f0                  
6800                  
6810                  
6820                  
6830                  
6840                  
6850                  
6860                  
6870                  
6880                  
6890                  
68a0                  
68b0                  
68c0                  
68d0                  
68e0                  
68f0                  
6900                  
6910                  
6920                  
6930                椿  
6940                  
6950                  
6960                  
6970                楿  
6980                  
6990                  
69a0                  
69b0                榿  
69c0                  
69d0                  
69e0                  
69f0                槿  
6a00                  
6a10                  
6a20                  
6a30                樿  
6a40                  
6a50                  
6a60                  
6a70                橿  
6a80                  
6a90                  
6aa0                  
6ab0                檿  
6ac0                  
6ad0                  
6ae0                  
6af0                櫿  
6b00                  
6b10                  
6b20                  
6b30                欿  
6b40                  
6b50                  
6b60                  
6b70                歿  
6b80                  
6b90                  
6ba0                  
6bb0                殿  
6bc0                  
6bd0                  
6be0                  
6bf0                毿  
6c00                  
6c10                  
6c20                  
6c30                氿  
6c40                  
6c50                  
6c60                  
6c70                汿  
6c80                  
6c90                  
6ca0                  
6cb0                沿  
6cc0                  
6cd0                  
6ce0                  
6cf0                泿  
6d00                  
6d10                  
6d20                  
6d30                洿  
6d40                  
6d50                  
6d60                  
6d70                浿  
6d80                  
6d90                  
6da0                  
6db0                涿  
6dc0                  
6dd0                  
6de0                  
6df0                淿  
6e00                  
6e10                  
6e20                  
6e30                渿  
6e40                  
6e50                  
6e60                  
6e70                湿  
6e80                  
6e90                  
6ea0                  
6eb0                溿  
6ec0                  
6ed0                  
6ee0                  
6ef0                滿  
6f00                  
6f10                  
6f20                  
6f30                漿  
6f40                  
6f50                  
6f60                  
6f70                潿  
6f80                  
6f90                  
6fa0                  
6fb0                澿  
6fc0                  
6fd0                  
6fe0                  
6ff0                濿  
7000 瀀                 
7010                  
7020                  
7030                  
7040                  
7050                  
7060                  
7070                  
7080                  
7090                  
70a0                  
70b0                  
70c0                  
70d0                  
70e0                  
70f0                  
7100                  
7110                  
7120                  
7130                  
7140                  
7150                  
7160                  
7170                  
7180                  
7190                  
71a0                  
71b0                  
71c0                  
71d0                  
71e0                  
71f0                  
7200                  
7210                  
7220                  
7230                  
7240                  
7250                  
7260                  
7270                  
7280                  
7290                  
72a0                  
72b0                  
72c0                  
72d0                  
72e0                  
72f0                  
7300                  
7310                  
7320                  
7330                  
7340                  
7350                  
7360                  
7370                  
7380                  
7390                  
73a0                  
73b0                  
73c0                  
73d0                  
73e0                  
73f0                  
7400                  
7410                  
7420                  
7430                  
7440                  
7450                  
7460                  
7470                  
7480                  
7490                  
74a0                  
74b0                  
74c0                  
74d0                  
74e0                  
74f0                  
7500                  
7510                  
7520                  
7530                  
7540                  
7550                  
7560                  
7570                  
7580                  
7590                  
75a0                   
75b0                  
75c0                  
75d0                  
75e0                  
75f0                  
7600                  
7610                  
7620                  
7630                  
7640                   
7650                  
7660                  
7670                  
7680                  
7690                  
76a0                  
76b0                  
76c0                  
76d0                  
76e0                  
76f0                  
7700                  
7710                  
7720                  
7730                  
7740                  
7750                  
7760                  
7770                  
7780                  
7790                  
77a0                  
77b0                  
77c0                  
77d0                  
77e0                  
77f0                  
7800                  
7810                  
7820                  
7830                  
7840                  
7850                  
7860                  
7870                  
7880                  
7890                  
78a0                  
78b0                  
78c0                  
78d0                  
78e0                  
78f0                  
7900                  
7910                  
7920                  
7930                礿  
7940                  
7950                  
7960                  
7970                祿  
7980                  
7990                  
79a0                  
79b0                禿  
79c0                  
79d0                  
79e0                  
79f0                秿  
7a00                  
7a10                  
7a20                  
7a30                稿  
7a40                  
7a50                  
7a60                  
7a70                穿  
7a80                  
7a90                  
7aa0                  
7ab0                窿  
7ac0                  
7ad0                  
7ae0                  
7af0                竿  
7b00                  
7b10                  
7b20                  
7b30                笿  
7b40                  
7b50                  
7b60                  
7b70                筿  
7b80                  
7b90                  
7ba0                  
7bb0                箿  
7bc0                  
7bd0                  
7be0                  
7bf0                篿  
7c00                  
7c10                  
7c20                  
7c30                簿  
7c40                  
7c50                  
7c60                  
7c70                籿  
7c80                  
7c90                  
7ca0                  
7cb0                粿  
7cc0                  
7cd0                  
7ce0                  
7cf0                糿  
7d00                  
7d10                  
7d20                  
7d30                紿  
7d40                  
7d50                  
7d60                  
7d70                絿  
7d80                  
7d90                  
7da0                  
7db0                綿  
7dc0                  
7dd0                  
7de0                  
7df0                緿  
7e00                  
7e10                  
7e20                  
7e30                縿  
7e40                  
7e50                  
7e60                  
7e70                繿  
7e80                  
7e90                  
7ea0                  
7eb0                线  
7ec0                  
7ed0                  
7ee0                  
7ef0                绿  
7f00                  
7f10                  
7f20                  
7f30                缿  
7f40                  
7f50                  
7f60                  
7f70                罿  
7f80                  
7f90                  
7fa0                  
7fb0                羿  
7fc0                  
7fd0                  
7fe0                  
7ff0                翿  
8000 耀                 
8010                  
8020                  
8030                  
8040                  
8050                  
8060                  
8070                  
8080                  
8090                  
80a0                  
80b0                  
80c0                  
80d0                  
80e0                  
80f0                  
8100                  
8110                  
8120                  
8130                  
8140                  
8150                  
8160                  
8170                  
8180                  
8190                  
81a0                  
81b0                  
81c0                  
81d0                  
81e0                  
81f0                  
8200                  
8210                  
8220                  
8230                  
8240                  
8250                  
8260                  
8270                  
8280                  
8290                  
82a0                  
82b0                  
82c0                  
82d0                  
82e0                  
82f0                  
8300                  
8310                  
8320                  
8330                  
8340                  
8350                  
8360                  
8370                  
8380                  
8390                  
83a0                  
83b0                  
83c0                  
83d0                  
83e0                  
83f0                  
8400                  
8410                  
8420                  
8430                  
8440                  
8450                  
8460                  
8470                  
8480                  
8490                  
84a0                  
84b0                  
84c0                  
84d0                  
84e0                  
84f0                  
8500                  
8510                  
8520                  
8530                  
8540                  
8550                  
8560                  
8570                  
8580                  
8590                  
85a0                  
85b0                  
85c0                  
85d0                  
85e0                  
85f0                  
8600                  
8610                  
8620                  
8630                  
8640                  
8650                  
8660                  
8670                  
8680                  
8690                  
86a0                  
86b0                  
86c0                  
86d0                  
86e0                  
86f0                  
8700                  
8710                  
8720                  
8730                  
8740                  
8750                  
8760                  
8770                  
8780                  
8790                  
87a0                  
87b0                  
87c0                  
87d0                  
87e0                  
87f0                  
8800                  
8810                  
8820                  
8830                  
8840                  
8850                  
8860                  
8870                  
8880                  
8890                  
88a0                  
88b0                  
88c0                  
88d0                   
88e0                  
88f0                  
8900                  
8910                  
8920                  
8930                褿  
8940                  
8950                  
8960                  
8970                西  
8980                  
8990                  
89a0                  
89b0                覿  
89c0                  
89d0                  
89e0                  
89f0                觿  
8a00                  
8a10                  
8a20                  
8a30                訿  
8a40                  
8a50                  
8a60                  
8a70                詿  
8a80                  
8a90                  
8aa0                  
8ab0                調  
8ac0                  
8ad0                  
8ae0                  
8af0                諿  
8b00                  
8b10                  
8b20                  
8b30                謿  
8b40                  
8b50                  
8b60                  
8b70                譿  
9210                  
9220                  
9230                  
9240                  
9250                  
9260                  
9270                  
9280                  
9290                  
92a0                  
92b0                  
92c0                  
92d0                  
92e0                  
92f0                  
9300                  
9310                  
9320                  
9330                  
9340                  
9350                  
9360                  
9370                  
9380                  
9390                  
93a0                  
93b0                  
93c0                  
93d0                  
93e0                  
93f0                  
9400                  
9410                  
9420                  
9430                  
9440                  
9450                  
9460                  
9470                  
9480                  
9490                  
94a0                  
94b0                  
94c0                  
94d0                  
94e0                  
94f0                  
9500                  
9510                  
9520                
print(chr(0b100111001011000))#0b 表示二进制
print(ord('乘'))

image.png

Python标识符和保留字

变量、函数、类、模块和其他对象的气的名字就叫标识符

import keyword
print(keyword.kwlist)

image.png

规则:

  1. 字母、数字、下划线
  2. 不能以数字开头
  3. 不能是我的保留字
  4. 我是严格区分大小写的

变量的定义和使用

变量由三部分组成:

  • 标识:表示对象所存储的内存地址,使用内置函数id(obj)来获取
  • 类型:表示的是对象的数据类型,使用内置函数type(obj)来获取
  • 值:表示对象所存储的具体数据,使用print(obj)可以将值进行打印输出
#name(变量名) = (赋值运算符) ‘爱因斯坦’(值)
name='爱因斯坦'
print(name)
print('标识',id(name))
print('类型',type(name))
print('值',name)

id 就是标识 类型就是str value就是爱因斯坦

image.png

#name(变量名) = (赋值运算符) ‘爱因斯坦’(值)
name='爱因斯坦'
name='ly'
print(name)
print('标识',id(name))
print('类型',type(name))
print('值',name)

image.png

数据类型

常用的数据类型

  • 整数类型→int→98
  • 浮点数类型→float→3.14159
  • 布尔类型→bool→True,False
  • 字符串类型→str→'人生苦短'
#整数类型
#可以表示整数,负数,0
n1=90
n2=-76
n3=0
print(n1,type(n1))
print(n2,type(n2))
print(n3,type(n3))
#整数可以表示为二进制,十进制,八进制,十六进制
print('十进制',118)
print('二进制',0b1010101111) #二进制以0b开头
print('八进制',0o176)#八进制以0o开头
print('十六进制',0x1eAF)#十六进制以0x开头

image.png

浮点类型

浮点数整数部分和小数部分组成
  • 浮点数存储不精确性
  • 使用浮点数进行计算时,可能会出现小数位数不确定的情况
    • 解决防范
    • 导入模块decimal
from decimal import Decimal
print(Decima|('1.1')+Decima|(2.2)) #3.3
//浮点
a=3.14159
print(a,type(a))
n1=1.1
n2=2.2
print(n1+n2)

from decimal import Decimal
print(Decimal('1.1')+Decimal('2.2'))

image.png

布尔类型

f1=True
f2=False
print(f1,type(f1))
print(f2,type(f2))

#布尔值可以转成整数计算
print(f1+1) #2
print(f2+1) #1

image.png

字符串
str1='青春不常在'
str2="青春不常在"
str3="""青春不常在"""
str4='''青春不,
常在'''
print(str1,type(str1))
print(str2,type(str2))
print(str3,type(str3))
print(str4,type(str4))

image.png

转换函数

image.png

name='张三'
age=20
print(type(name),type(age))
print('我叫'+name+'今年,'+str(age)+'岁') #将int类型通过str()函数转换成str函数

print('-----------str()将其他类型转成str类型---------------')
a=10
b=198.8
c=False
print(type(a),type(b),type(c))
print(str(a),str(b),str(c),type(str(a)),type(str(b)),type(str(c)))


print('-----------int()将其他类型转成int类型---------------')
s1='128'
f1=98.77
s2='77.77'
ff=True
s3='hello'
print(type(s1),type(f1),type(s2),type(ff),type(s3))
print(type(int(s1))) #将str转成int类型,字符传为数字串
print(type(int(f1))) #float转成int类型,只截取整数部分,舍掉小数部分
#print(type(int(s2)))#将str转成int类型,报错,因为字符串为小数串
print(int(ff),type(int(ff)))
#print(int(s3),type(int(s3)))#将str转成int类型时,字符串必须为数字串(整数)



print('-----------float()将其他类型转成float类型---------------')
e1='128.98'
e2='76'
e3=True
e4='hello'
e5=98
print(type(s1),type(s2),type(s3),type(e4),type(e5))
print(float(e1),type(float(e1)))
print(float(e2),type(float(e2)))
print(float(e3),type(float(e3)))
#print(float(e4),type(float(e4)))#字符串中的数据如果是非数字串,则不允许转换
print(float(e5),type(float(e5)))

image.png

image.png

注释

#coding:utf-8

#单行注释

'''hahah,
hahaha
hahhah'''

#中文编码声明注释→在文件开头加上中文声明注释,用以指定源码文件的编码格式
#coding:gbk

image.png

7e066f3c9b17d7a486f9b99117e99e3.png

#从键盘录入两个整数,计算两个整数的和
# a=input('请输入一个加数:')
# a=int(a)
# b=input('请输入另外一个加数')
# b=int(b)
a=int(input('请输入一个加数:'))
b=int(input('请输入另一个加数:'))
print(type(a),type(b))
print(a+b)

image.png

算数运算符

image.png

#标准运算符
print(1+1) #加法运算
print(1-1) #减法运算
print(2*4) #乘法运算
print(1/2) #触发运算
print(11//2) #整除运算

#取余运算符’‘
print(11%2) #取余运算
#冥运算符
print(2**3) #表示的是2的3次方 2*2*2

image.png

print(9//4)
print(-9//-4)
print(9//-4)
print(-9//4) #一正一负的整数公式,向下取整。

print(9%-4) #公式 余数=被除数-除数*商 9-(-4)*(-3)=-3
print(-9%4) #-9+12=3

image.png

赋值运算符

image.png

#赋值运算符,运算顺序从右到左
i=3+4
print(i)
a=b=c=20 #链式赋值
#标识id是变量在内存中的位置
print(a,id(a))
print(b,id(b))
print(c,id(c))

print('-------支持参数赋值---------')
a=20
a+=30 #相当于a=a+30
print(a)
a-=10 #相当于a=a-10
print(a)
a*=2  #相当于a=a*2
print(a)
print(type(a))
a/=3
print(a)
print(type(a))
a//=2
print(a)
print(type(a))


print('-------支持系列解包赋值---------')
a,b,c=20,30,40
print(a,b,c)



print('----------交换两个变量的值------------')
a,b=10,20
print('交换之前:',a,b)
#交换
a,b=b,a
print('交换之后:',a,b)

image.png

image.png

#比较运算符,比较运算符的结果为bool类型
a,b=10,20
print('a>b吗?',a>b)
print('a<b吗?',a<b)
print('a<=b吗?',a<=b)
print('a>=b吗?',a>=b)
print('a==b吗?',a==b)
print('a!=b吗?',a!=b)

'''一个 = 称为赋值运算符,==称为比较运算符
一个变量由三部分组成,标识,类型,值
== 比较的是值还是标识呢?比较的是值
比较对象的标识使用is
'''

a=10
b=10
print(a==b) #说明ab的value 相等
print(a is b) #说明ab的id标识,相等


#
lis1=[11,22,33,44]
lis2=[11,22,33,44]
print(lis1==lis2)
print(lis1 is lis2)
print(id(lis1))
print(id(lis2))
print(a is not b) #a的id与b的id是不相等的
print(lis1 is not lis2)

image.png

image.png

#布尔运算符
#and
a,b=1,2
print('--------------and 并且--------------')
print(a==1 and b==2)
print(a==1 and b<2)
print(a!=1 and b==2)
print(a!=1 and b!=2)
#or
print('--------------or 或者--------------')
print(a==1 or b==2)
print(a==1 or b<2)
print(a!=1 or b==2)
print(a!=1 or b!=2)
#not
print('--------------not 对运算数取反--------------')
f=True
f2=False
print(not f)
print(not f2)

#not in
print('--------------in 与not in --------------')
s='helloworld'
print('w' in s)
print('k' in s)
print('w' not in s)
print('k' not in s)

image.png

image.png

#位运算符将数据转成二进制进行计算
print(4&8)  #按位与&,同为1时结果为1
print(4|8)  #按位或|,同为0时结果为0,
print(4<<1) #向左移动1位(移动个位置)相当于乘以2
print(4<<2) #向左移动2位(移动2个位置)

print(4>>1) #向右移动1位,相当于除以2
print(4>>1) #向右移动2位,相当于除以4

image.png

image.png

image.png

#顺序结构
'''把大象装冰箱'''
print('--------程序开始------')
print('1.把冰箱门打开')
print('1.把大象放冰箱里')
print('1.把冰箱门关上')
print('--------程序结束------')

image.png

image.png

#测试对象的布尔值
print('----以下对象的布尔值为False----------')
print(bool(False))
print(bool(0))
print(bool(0.0))
print(bool(None))
print(bool(''))
print(bool(""))
print(bool([])) #空列表
print(bool(list())) #空列表
print(bool(())) #空元组
print(bool({})) #空字典
print(bool(dict())) #空字典
print(bool(set())) #空集合

print('----其他对象的布尔值为Ture----------')
print(bool(18))
print(bool(True))
print(bool('helloworld'))

image.png

money=1000 #余额
s=int(input('请输入取款金额')) #取款金额
#判断余额是否充足
if money>=s:
    money = money -s
    print('取款成功,余额为:',money)

image.png

image.png

#双分支结构 if...else,二选一执行
'''从键盘录入一个整数,编写程序让计算机判断是奇数还是偶数'''
num=int(input('请输入一个整数'))
#条件判断
if  num%2==0:
    print(num,'是偶数')
else:
    print(num,'是奇数')

image.png

'''多分支结构,多选一执行
从键盘录入一个整数 成绩
90-100 A
80-89 B
70-79 C
60-69 D
0-59 E
大于0或大于100为非法数据(不是成绩的有限范围)
'''

score=int(input('请输入一个成绩:'))

if score>=90 and score<=100:
    print('A级')
elif score>=80 and score<=89:
    print('B级')
elif score>=70 and score<=79:
    print('C级')
elif score >= 60 and score <= 69:
    print('D级')
elif score >= 0 and score <= 59:
    print('E级')
else:
    print('对不起,成绩有误,不在成绩的有效范围')
'''多分支结构,多选一执行
从键盘录入一个整数 成绩
90-100 A
80-89 B
70-79 C
60-69 D
0-59 E
大于0或大于100为非法数据(不是成绩的有限范围)
'''

score=int(input('请输入一个成绩:'))

if 90<=score<=100:
    print('A级')
elif 80<=score<=89:
    print('B级')
elif 70<=score<=79:
    print('C级')
elif 60<=score<=69:
    print('D级')
elif 0<=score<=59:
    print('E级')
else:
    print('对不起,成绩有误,不在成绩的有效范围')

image.png

image.png

'''
会员 >=200 8折
>100 9折
不打折
非会员 >=200 9.5折
不打折'''

answer = input('您是会员吗?y/n')
#外层判断是否是会员
if answer=='y':#会员
    print('会员')
else: #非会员
    print('非会员')

image.png

'''
会员 >=200 8折
>100 9折
不打折
非会员 >=200 9.5折
不打折'''

answer = input('您是会员吗?y/n')
money=float(input("请输入您的购物金额"))
#外层判断是否是会员
if answer=='y':#会员
    if money>=200:
        print('付款金额为:',money*0.8)
    elif money>=100:
        print('打九折,付款金额为:',money*0.9)
    else:
        print('不打折,付款金额为:',money)
else: #非会员
    print('非会员')
    if money>=200:
        print('打9.5折,付款金额为:',money*0.95)
    else:
        print('不打折,付款金额为:',money)

image.png

image.png

'''从键盘录入两个整数,比较两个整数的大小'''
num_a=int(input('请输入第一个整数'))
num_b=int(input('请输入第二个整数'))
#比较大小
'''正常的写法'''
'''if num_a>=num_b:
    print(num_a,'大于等于',num_b)
else:
    print(num_a,'小于',num_b)'''
print('使用条件表达式进行比较')
print((str(num_a)+'大于等于'+str(num_b)) if num_a>=num_b else (str(num_a)+'小于'+str(num_b)))

image.png

image.png

#pass语句,什么都不做,只是一个占位符,用到需要些语句的地方
answer=input('您是会员吗?y/n')
#判断是否是会员
if answer == 'y':
    pass
else:
    pass

image.png

image.png

age=int(input('请输入您的年龄:'))
if age:
    print(age)
else:
    print('年龄为:',age)

image.png

image.png

#range()的三种创建方式
'''第一种创建方式,只有一个参数(小括号中只给了一个数)'''
r=range(10) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 默认从0开始,默认相差1称为步长
print(r) #range(0,10)
print(list(r)) #用于查看range对象中的整数序列  -->list是列表的意思

'''第二种创建方式,给了两个参数(小括号中给了两个数)'''
r=range(1,10) #指定了起始值,从1开始,到0结束(不包含10),默认不长为1
print(list(r)) #[1, 2, 3, 4, 5, 6, 7, 8, 9]

'''第三种创建方式,给了三个参数(小括号中给了三个数)'''
r=range(1,10,2)
print(list(r)) #[1, 3, 5, 7, 9]

'''判断指定的整数 在序列中是否存在 in,not in'''
print(10 in r) #False 10不在当前的r这个整数序列中
print(9 in r)
print(10 not in r)
print(9 not in r)

print(range(1,20,1))
print(range(1,101,1))

image.png

image.png

a=1
#判断条件表达式
if a<10:
    print(a)
a+=1

image.png

a=1
#判断条件表达式
while a<10:
    print(a)
    a+=1

image.png

image.png

#计算0到4之间的累加和
'''
步循环法
1.初始化变量
2.条件判断
3.条件执行本(循环体)
4.改变变量
总结:初始化的变量与条件判断的变量与改变的变量为同一个
'''
sum=0#用存储累加
'''初始化变量为0'''
a=0
'''条件判断'''
while a<5:
    '''条件执行本(循环体)'''
    sum+=a
    '''改变变量'''
    a+=1
    print('和为',sum)

image.png

#计算1-100之间的偶数和
sum=0 #用于存储偶数和
#初始化变量
a=1
#条件判断
while a<=100:
    '''条件执行体(求和)'''
    '''条件判断是否是偶数'''
    if a%2==0:
       sum+=a
    '''改变变量'''
    a+=1
print('1-100之间的偶数和',sum)
#计算1-100之间的偶数和
sum=0 #用于存储偶数和
#初始化变量
a=1
#条件判断
while a<=100:
    '''条件执行体(求和)'''
    '''条件判断是否是偶数'''
    if a%2:
       sum+=a
    '''改变变量'''
    a+=1
print('1-100之间的偶数和',sum)
#计算1-100之间的偶数和
print(bool(0))
sum=0 #用于存储偶数和
#初始化变量
a=1
#条件判断
while a<=100:
    '''条件执行体(求和)'''
    '''条件判断是否是偶数'''
    if not bool(a%2):
       sum+=a
    '''改变变量'''
    a+=1
print('1-100之间的偶数和',sum)

image.png

image.png

for item in 'Python':
    print(item)


#range() 产生一个整数序列,--》也是一个可迭代对象

for i in range(10):
    print(i)

#如果在循环体中不惜要使用到自定义变量,可将自定义变量写为“—”
for _ in range(5):
    print('wwww')

print('使用for循环,计算1到100之间的偶数和')
sum=0 #用于存储偶数和
for item in range(1,101):
    if item %2==0:
        sum+=item
print('1到100之间的偶数和为:',sum)

image.png

'''输出100到999之间的水仙花
举例:
153=3*3*3+5*5*5+1*1*1
'''
for item in range(100,1000):
    ge=item%10 #个位
    shi=item//10%10#十位
    bai=item//100#百位
    # print(ge,shi,bai)
    #判断
    if ge**3+shi**3+bai**3==item:
        print(item)

image.png

image.png

'''从键盘录入密码,最多录入3次,如果正确就结束循环'''


for item in range(3):
    pw=input('请输入密码')
    if pw =='8888':
        print('密码正确')
        break
    else:
        print('密码不正确')

image.png

a=0
while a<3:
    '''条件执行体(循环体)'''
    pwd=input('请输入密码')
    if pwd =='8888':
        print('密码正确')
        break
    else:
        print('密码不正确')

        '''改变变量'''
        a+=1

image.png

image.png

image.png

'''要求输出1到50之间所有5的倍数,5,10,15,20,25....
5的倍数的共同点:和5的余数为0的数都是5的倍数
什么样的数不是5的倍数,1,2,3,4,6,7,8,9....
与5的余数部位0的数都死不是5的倍数
'''

for item in range(1,51):
    if item%5==0:
        print(item)

print('-----continue---------')
for item in range(1,51):
    if item%5!=0:
        continue
        print(item)

image.png

image.png

for item in range(3):
    pw=input('请输入密码')
    if pw =='8888':
        print('密码正确')
        break
    else:
        print('密码不正确')
else:
    print('对不起,三次密码均输入错误')

image.png

a=0
while a<3:
    pwd=input('请输入密码')
    if pwd =='8888':
        print('密码正确')
        break
    else:
        print('密码不正确')
        a+=1
else:
    print('对不起,三次密码均输入错误')

image.png

image.png

'''输出一个三行四列的矩形'''
for i in range(1,4): #行表,执行三次,一次是一行
    for j in range(1,5):
        print('*',end='\t') #不换行输出
    print() #打行

image.png

image.png

'''输出一个直角三角形'''
for i in range(1,10): #行数
    for j in range(1,i+1):
        print('*',end='')
    print()

image.png

'''输出一个99乘法表'''
for i in range(1,10): #行数
    for j in range(1,i+1):
        print(i,'*',j,'=',i*j,end='\t')
    print()

image.png

image.png

'''流程控制语句breack与continue在二重循环中的使用'''
for i in range(5): #代表外层循环要执行5次
    for j in range(1,11):
        if j%2==0:
            #breack
            continue
        print(j,end='\t')
    print()

image.png

image.png

image.png

a=10 #变量存储的是一个对象的引用
lst=['hello','world',98]
print(id(lst))
print(type(lst))
print(lst)

image.png

image.png

image.png

'''创建列表的第一种方式,使用[]'''

lst=['hello','world',98]
print(lst)
print(lst[0],lst[-3])
'''创建列表的第二种方式,使用内置函数list()'''
lst2=list(['hello','world',98])

image.png

image.png

'''创建列表的第一种方式,使用[]'''

lst=['hello','world',98,'hello']
print(lst.index('hello')) #如果列表中相同元素,只返回列表中的第一个索引
# print(lst.index('python'))  #ValueError: 'python' is not in list
print(lst.index('hello',1,4))

image.png

lst=['hello','world',98,'hello','world',234]
#获取索引为2的元素
print(lst[2])
#获取索引为-3的元素
print(lst[-3])

#获取索引为10的元素
# print(lst[10])

image.png

image.png

lst=[10,20,30,40,50,60,70,80]
print(lst[1:6:1])
print('原列表',id(lst))
lst2=lst[1:6:1]
print('切的片段:',id(lst2))
print(lst[1:6]) #默认不长为1
print(lst[1:6:])
#start=1,stop=6,step=2
print(lst[1:6:2])
#stop=6,step=2,start采用默认
print(lst[:6:2])
#start=1,step=2,stop采用默认
print(lst[1::2])

print('----------step步长为负数的情况------------')
print('原列表:',lst)
print(lst[::-1])
#statrt=7,stop 省略 step=-1
print(lst[7::-1])
#statrt=6,stop=0,step=-2
print(lst[6:0:-2])

image.png

image.png

print('p' in 'python')
print('k' in 'python')

lst=[10,20,'python','hello']
print(10 in lst)
print(100 in lst)
print(10 not in lst)
print(100 not in lst)

print('---------------------------')
for item in lst:
    print(item)

image.png

image.png

#向列表的末尾添加一个元素
lst=[10,20,30]
print('添加元素之前',lst,id(lst))
lst.append(100)
print('添加元素之后',lst,id(lst))
lst2=['hello','world']
# lst.append(lst2)  #将lst2作为一个元素添加到列表的末尾
lst.extend(lst2) #向列表的末尾一次性添加多个元素
print(lst)

#在任意位置上添加一个元素
lst.insert(1,90)
print(lst)


lst3=[True,False,'hello']
#在任意的位置上添加n多个元素
lst[1:]=lst3
print(lst)

image.png

image.png

image.png

lst=[10,20,30,40,50,60,30]
lst.remove(30) #从列表中移除一个元素,如果有重复元素只移第一个元素
print(lst)

#pop()根据索引移除
lst.pop(1)
print(lst)
lst.pop()#如果不指定参数(索引),将删除列表中的最后一个元素
print(lst)
print('-----------切片操作-删除至少一歌元素,将产生一个虚拟的列表对象---------------')
new_list=lst[1:3]
print('原列表',lst)
print('切片后的列表',new_list)

image.png

c51aad497f0afa0524a7162e0fd8b16.png

lst=[10,20,30,40]
#一次修改一个值
lst[2]=100
print(lst)
lst[1:3]=[300,400,500,600]
print(lst)

image.png

image.png

lst=[20,40,10,98,54]
print('排序前的列表',lst)
lst.sort()
print('排序后的列表',lst)

#通过指定关键字参数,将列表中的元素进行降序排序
lst.sort(reverse=True) #reverse=True表示降序排序,reverse=False就是升序排序
print(lst)
lst.sort(reverse=False)
print(lst)

#指定关键字参数,实现列表元素的降序排序
desc_list=sorted(lst,reverse=True)
print(desc_list)

image.png

image.png

lst=[i*i for i in range(1,10)]
print(lst)

'''列表中的元素的值为2,4,6,8,10'''
lst2=[i*2 for i in range(1,6)]

image.png

image.png

image.png

image.png

image.png

'''字典的创建方式'''
'''使用{}创建字典'''

scores={'张三':100,'李四':98,'王五':45}
print(scores)
print(type(scores))

'''第二种创建dict()'''
student=dict(name='jack',age=20)
print(student)

'''空字典'''
d={}
print(d)

image.png

image.png

'''获取字典元素'''
scores={'张三':100,'李四':98,'王五':45}
'''第一种方式,使用[]'''
print(scores['张三'])
#print(scores['陈六']) #KeyError: '陈六'


'''第二种创建get()方法'''
print(scores.get('张三'))
print(scores.get('陈六'))
print(scores.get('麻七',99)) #99是在查找’麻七‘所对的value不存在时,提供的一个默认值。

image.png

image.png

image.png

scores={'张三':100,'李四':98,'王五':45}
#获取所有的key
keys=scores.keys()
print(keys)
print(type(keys))
print(list(keys)) #将所有的key组成的视图转成列表

#获取所有的value
values=scores.values()
print(values)
print(type(values))

#获取所有的key-value对
items=scores.items()
print(items)
print(list(items)) #转换之后的列表元素是由元组组成

image.png

image.png

scores={'张三':100,'李四':98,'王五':45}
#字典元素的遍历
for item in scores:
    print(item,scores[item],scores.get(item))

image.png

image.png

d={'name':'张三','name':'李四'}  #key 不允许重复 重复就被后面覆盖
print(d)

d={'name':'张三','nickname':'张三'} #value是可以重复的
print(d)

lst=[10,20,30]
lst.insert(1,100)
print(lst)

image.png

image.png

image.png

items=['Fruits','Books','Others']
prices=[96,78,85]
#upper(): 表示大写
d={item.upper():price for item,price in zip(items,prices)}
print(d)

image.png

image.png

image.png

#元祖就是()定义的
#不可变序列,可变序列
'''可变序列 列表,字典'''

lst=[10,20,45]
print(id(lst))
lst.append(300)
print(id(lst))
'''不可变序列,字符串,元组'''
s='hello'
print(id(s))
s=s+'world'
print(id(s))
print(s)

image.png

image.png

#空元祖
t4=()
t5=tuple()

print('空列表',lst,lst1)
print('空字典',d,d2)
print('空元祖',t4,t5)

image.png

t=(10,[20,30],9)
print(t)
print(type(t))
print(t[0],type(t[0]),id(t[0]))
print(t[1],type(t[1]),id(t[1]))
print(t[2],type(t[2]),id(t[2]))
'''尝试将t[1]修改为100'''
print(id(100))
#t[1]=100   #元组是不允许修改的元素
'''由于【20,30】列表,而列表是可变徐磊,所以可以向列中添加元素,而列表的内存地址不变'''
t[1].append(100) #向列表中添加元素
print(t,id(t[1]))

image.png

image.png

'''元组的遍历'''
t=('Python','world',98)
'''第一种获取元组元组的方式,使用索引'''
print(t[0])
print(t[1])
print(t[2])

'''遍历元组'''
for item in t :
    print(item)

image.png

image.png

image.png

#集合的创建方式
'''第一种创建方式使用{}'''
s={2,3,4,5,6,7,7}#集合中的元素不允许重复
print(s)

'''第二种创建方式使用set()'''
s1=set(range(6))
print(s1,type(s1))
s2=set([1,2,3,4,5,5,5,5,6,6,6])
print(s2,type(s2))
s3=set([1,2,3,4,5,5,5,5,6,6,6])
print(s3,type(s3))
s4=set('python')
print(s4,type(s4))
s5=set([1,2,3,4,5,5,5,5,6,6,6])
print(s5,type(s5))

s6={}
print(type(s6))

s7=()
print(type(s7))

image.png

image.png

#集合的相关操作
s={10,20,30,405,60}
'''集合元素的判断操作'''
print(10 in s)
print(100 in s)
print(10 not in s)
print(100 not in s)

'''集合元素的新增操作'''
s.add(80)
print(s)
s.update({200,400,300})
print(s)
s.update([100,99,8])
s.update([78,64,56])
print(s)

'''集合元素的删除操作'''
s.remove(100)
print(s)
#s.remove(500) #KeyError
s.discard(500)
s.discard(300)
print(s)
s.pop()
s.pop()

print(s)
s.clear()
print(s)

image.png

image.png

'''两个集合是否相等(元素相同,就相等)'''
s={10,20,30,40}
s2={30,40,20,10}
print(s==s2)
print(s!=s2)


'''一个集合是都是另一个集合的子集'''
s1={10.,20,30,40,50,60}
s2={10,20,30,40}
s3={10,20,90}
print(s2.issubset(s1))
print(s3.issubset(s1))


'''一个集合是都是另一个集合的超集'''
print(s1.isdisjoint(s2))
print(s1.isdisjoint(s3))


'''两个集合是否含有交集'''
print(s2.isdisjoint(s3))
s4={100,200,300}

print(s2.isdisjoint(s4))

image.png

image.png

#集合的数学操作
#(1)交集
s1={10,20,30,40}
s2={20,30,40,50,60}
print(s1.intersection(s2))
print(s1 & s2)   #intersection()与&等价,交集操作
#(2)并集操作
print(s1.union(s2))
print(s1 | s2) #union与| 等价,并集操作
print(s1)
print(s2)

#(3)差集操作
print(s1.difference(s2))
print(s1-s2)
print(s1)
print(s2)


#(4)对称差集
print(s1.symmetric_difference(s2))
print(s1 ^ s2)

image.png

#列表生成式
lst=[i*i for i in range(10)]
print(lst)

#集合生成式
s={i*i for i in range(10)}
print(s)

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

#字符传的查询操作
s='hello,hello'
print(s.index('lo'))
print(s.find('lo'))
print(s.rindex('lo'))
print(s.rfind('lo'))


# print(s.index('k'))
print(s.find('k'))
# print(s.rindex('k'))
print(s.rfind('k'))

image.png

image.png

#字符串中的大小写转换的方法
s='hello,python'
a=s.upper()  #转成大写之后,会产生一个新的字符串对象
print(a,id(a))
print(s,id(s))
print(s.lower(),id(s.lower()))
print(s,id(s.lower()))

s2='hello,Python'
print(s2.swapcase())
print(s2.title())

image.png

image.png

s='hello,Python'
print(s.center(20,'*'))#居中对齐
print(s.ljust(20,'*'))#左对齐
print(s.ljust(10))
print(s.ljust(20))

print(s.rjust(20,'*'))#右对齐
print(s.rjust(20))
print(s.rjust(10))
#右对齐,使用0进行填充
print(s.zfill(20))

image.png

image.png

s='hello world Python'
lst=s.split()
print(lst)
s1='hello|world|Pyton'
print(s1.split(sep='|'))  #分割标识符
print(s1.split(sep='|',maxsplit=1))
'''rsplit()从右侧开始劈分'''
print(s.rsplit())
print(s1.rsplit('|'))
print(s1.rsplit(sep='|',maxsplit=1))

image.png

image.png

s='hello,python'
print('1.',s.isidentifier())
print('2.','hello'.isidentifier())
print('3.','张三_'.isidentifier())
print('4.','张三_123'.isidentifier())

print('5.','\t'.isspace())
print('6.','abc'.isalpha())
print('7.','张三_'.isalpha())
print('8.','张三_123'.isalpha())

print('9.','123'.isdecimal())
print('10.','1234'.isdecimal())
print('11.','12dd3'.isdecimal())

print('12.','12dd3'.isnumeric())
print('13.','12dd3思'.isnumeric())
print('14.','}}}}'.isnumeric())

print('15.','abc'.isalnum())
print('16.','张三123'.isalnum())
print('17.','abc!'.isalnum())

image.png

image.png

#字符串的合并
lst=['hello','java','Python']
print('|'.join(lst))
print(''.join(lst))

#元组
t=('hello','java','Python')
print(''.join(t))

print('*'.join('Python'))

image.png

image.png

###字符串替换replace()

print('apple'>'app')
print('apple'>'banana')  #相当于97>98
print(ord('a'),ord('b'))
print(ord('刘'))
print(chr(97),chr(98))
print(chr(21016))


'''==与is的区别
    ==比较的是value
    is比较的是id是否相等'''
a=b='Python'
c='Python'
print(a==b)
print(a==c)

print(a is b)
print(a is c)
print(id(a))
print(id(b))
print(id(c))

image.png

s='hello,Python'
s1=s[:5] #由于没有指定起始位置,所以从0开始切
s2=s[6:] #由于没有指定结束位置,所以切到字符串的最后一个元素
s3='!'
newstr=s1+s3+s2

print(s1)
print(s2)
print(newstr)

print('---------------------')
print(id(s))
print(id(s1))
print(id(s2))
print(id(s3))
print(id(newstr))

print('-----------切片【start:end:step-------------')
print(s[1:5:1]) #从1开始截到5(不包含),步长为1
print(s[::2]) #默认从0开始,没有写结束,默认到字符串是最后一个元素,步长为2,两个元素之间的索引间隔为2
print(s[::-1]) #默认从字符串的最后一个元素开始,到字符串的第一个元素结束,因为步长为负数。
print(s[-6::1]) #从索引为-6开始,到字符串的最后一个元素结束,步长为1

image.png

image.png

image.png

#格式化字符串
#(1)%占位符
name='张三'
age=20
print('我叫%s,今年%d岁' % (name,age))

#(2) {}作占位符
print('我叫{0}今年{1}岁'.format(name,age))

#(3)f-string
print(f'我叫{name},今年{age}岁')

image.png

print('%d' % 99)
print('%10d' % 99)  #10表示的是宽度

print('%.3f' % 3.1415926)  #.3表示是小数点后三位

#同时表示宽度和精度
print('%f' % 3.1415926) #一共总宽度为10,小数点后3位

print('hellohello')

image.png


print('{0:.3}'.format(3.1415926))  #.3表示的是一共是3位数
print('{:.3f}'.format(3.1415926)) #.3f表示是3位小数

print('{:10.3f}'.format(3.1515926)) #同时设置宽度和精度,一共是10位,3位是小数

image.png

image.png

s='天涯共此时'
print(s.encode(encoding='GBK')) #在GBK这种编码格式中 一个中文占两个字节
print(s.encode(encoding='UTF-8')) #在UTF-8这种编码格式中,一个中文占三个字节

#解码
#byte代表就是一个二进制数据(字节类型的数据)
byte=s.encode(encoding='GBK')  #编码
print(byte.decode(encoding='GBK'))  #解码


byte=s.encode(encoding='UTF-8')  #编码
print(byte.decode(encoding='UTF-8'))  #解码

image.png

image.png

image.png

image.png

image.png

image.png

image.png

def calc(a,b):  #a,b称为形式参数,简称形参,形参的位置是在函数的定义处 a:10  b:20
    c=a+b
    return c

result=calc(10,20)  #10,20称为实际参数的值,简称实参,实参的位置是函数的调用处
print(result)


res=calc(b=10,a=20) #   = 左侧的变量的名称称为 关键字参数
print(res)

image.png

image.png

def fun(arg1,arg2):
    print('arg1',arg1)
    print('arg2',arg2)
    arg1=100
    arg2.append(10)
    print('arg1',arg1)
    print('arg2',arg2)

n1=11
n2=[22,33,44]
print('n1', n1)
print('n2', n2)
fun(n1,n2)  #将位置传参arg1,arg2是函数定义处的形参,n1,n2是函数调用的处的实参,总结,实参名称与形参名称可以不一致,也可以一致。
print('n1', n1)
print('n2', n2)

'''在函数调用过程中,进行线束的传递
如果是不可变对象,在函数体的修改不会影响实参的值
如果是可变对象,在函数体的修改会影响到实参的值 arg2的修改,append(10),会影响到n2的值
'''

image.png

image.png

print(bool(0))
print(bool(8))  #非0的布尔值为true

image.png

def fun(num):
    odd=[] #存奇数
    even=[] #存偶数
    for i in num:
        if i%2:
            odd.append(i)
        else:
            even.append(i)
    return odd,even
#函数的调用
lst=[10,29,34,23,44,53,55]
print(fun(lst))
'''
函数的返回值
(1)如果函数没有返回值【执行完毕之后,不需要给调用处提供数据】return可以省略不写
(2)函数的返回值,如果是1个,直接返回类型
(3)函数的返回值,如果是多个,返回的结果为元组
'''


def fun1():
    return 'hello'
print(fun1())



def fun2():
    return 'hello'


res=fun2()
print(res)

def fun3():
    return 'hello','world'
print(fun3())


'''函数在定义式,是否需要返回值,视情况而定'''

image.png

image.png

def fun(a,b=10): #b称为默认值参数
    print(a,b)

#函数的调用
fun(100)
fun(20,30)

print('hello',end='\t')
print('world')

image.png

image.png

def fun(*args):
    print(args)
    print(args[0])

fun(10)
fun(10,30)
fun(30,405,50)



def fun1(**args):
    print(args)

fun1(a=10)
fun1(a=10,b=20,c=40)


'''
def fun2(*args,*a):
pass
以上代码,程序会报错,可变的位置参数,只能是1个
def fun2(**args,**args):
pass以上代码,程序会报错,个数可变的关键字参数,只能是1个
'''

def fun2(*ars1,**args2):
    pass


'''
def fun2(**ars1,*args2):
    pass
在一个函数的定义过程中,既有个数可变的关键字形参,也有个数可变的位置形参,要求,个数可变的位置形参,放在个数可变的关键字形参之前;
'''

image.png

image.png

def fun(a,b,c): #a,b,c在函数的定义处,所以是形式参数
    print('a=',a)
    print('b=',b)
    print('c=',c)

#函数的调用
fun(10,20,30)  #函数调用时的参数传递,称为位置传参
lst=[11,22,33]
fun(*lst)  #在函数调用时,将列表中的每个元素都转换为位置实参传入

fun(a=100,c=300,b=200) #函数的调用,所以是关键字实参
dic={'a':111,'b':2222,'c':3333}
fun(**dic) #在函数调用时,将字典中发键值对都转换为关键字实参传入

image.png

def fun(a,b=10): #b是在函数的定义处,所以b是形参,而且进行了赋值,所以b称为默认值形参
    print('a=',a)
    print('b=',b)

def fun2(*args):  #个数可变的位置参数
    print(args)

def fun3(**args2):#个数可变的关键字形参
    print(args2)


fun2(10,20,30,40)
fun3(a=11,b=22,c=33,d=44,e=55)




def fun4(a,b,c,d):
    print('a=', a)
    print('b=', b)
    print('c=', c)
    print('d=', d)


# 调用fun4函数
fun4(10,20,30,40) #位置实参传递
fun4(a=10,b=20,c=30,d=40) #关键字实参传递
fun4(10,20,c=30,d=40) #钱两个参数,采用的是未知实参传递,而c,d采用的是关键字实参传递


'''需求,cd只能采用关键字实参传递'''
'''函数定义时的形参的顺序问题'''
def fun5(a,b,*,c,d,**args):
    pass

def fun6(*args,**args2):
    pass

def fun7(a,b=10,*args,**arge2):
    pass

image.png

image.png

def fun(a,b): #c,就称为局部变量,因为c在市函数体内进行定义的变量,a,b为函数的形参,作用范围也是函数内部,相当于变量
    c=a+b
    print(c)

name='小刘' #name的作用范围为函数内部和外部都可以使用-->称为全局变量
print(name)
def fun2():
    print(name)

#调用函数
fun2()

def fun3():
    global age   #函数内部定义的变量,局部变量,局部变量使用global声明,这个变量实际上就变成了全局变量
    age=20
    print(age)

fun3()
print(age)

image.png

image.png

image.png

# def fac(n):
#     if n==1:
#         return 1
#     else:
#         return n*fac(n-1)
# print(fac(6))


def fac(n):
    if n==1:
        return 1
    else:
        res=n*fac(n-1)
        return res
print(fac(6))

image.png

def fib(n):
    if n==1:
        return 1
    elif n==2:
        return 1
    else:
        return fib(n-1)+fib(n-2)

#斐波那契数列第6位上的数字
print(fib(6))

print('---------------------------')
#输出这个数列的前6位上的数字
for i in range(1,7):
    print(fib(i))

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

print(type(Student))

image.png

class Student:    #Student为类的名称(类名)由一个或多个单词组成,每个单词的首字母大写,其余小写
    native_pace='吉林'   #直接写在类里的变量,称为类属性
    def __init__(self,name,age):
        self.name=name  #self.name称为实体属性,进行了一个赋值的操作,将局部变量的name的值赋值给实体属性
        self.age=age

    #实例方法
    def eat(selfself):
        print('学生在吃饭..')

    #静态方法
    @staticmethod
    def method():
        print('我使用了staticmethod进行修饰,所以我是静态方法')
    #类方法
    @classmethod
    def cm(cls):
         print('我是类方法,因为我使用了classmethod进行修饰')

#在类之外定义的称为函数,在类之内定义的称为方法
def drink():
    print('喝水')

image.png

image.png

class Student:    #Student为类的名称(类名)由一个或多个单词组成,每个单词的首字母大写,其余小写
    native_pace='吉林'   #直接写在类里的变量,称为类属性
    def __init__(self,name,age):
        self.name=name  #self.name称为实体属性,进行了一个赋值的操作,将局部变量的name的值赋值给实体属性
        self.age=age

    #实例方法
    def eat(selfself):
        print('学生在吃饭..')

    #静态方法
    @staticmethod
    def method():
        print('我使用了staticmethod进行修饰,所以我是静态方法')
    #类方法
    @classmethod
    def cm(cls):
         print('我是类方法,因为我使用了classmethod进行修饰')

#在类之外定义的称为函数,在类之内定义的称为方法
def drink():
    print('喝水')

#创建Student类的对象

stul=Student('张三',20)
print(id(stul))
print(type(stul))
print(stul)
print('---------------------')
print(id(Student))
print(type(Student))
print(Student)

image.png

class Student:    #Student为类的名称(类名)由一个或多个单词组成,每个单词的首字母大写,其余小写
    native_pace='吉林'   #直接写在类里的变量,称为类属性
    def __init__(self,name,age):
        self.name=name  #self.name称为实体属性,进行了一个赋值的操作,将局部变量的name的值赋值给实体属性
        self.age=age

    #实例方法
    def eat(selfself):
        print('学生在吃饭..')

    #静态方法
    @staticmethod
    def method():
        print('我使用了staticmethod进行修饰,所以我是静态方法')
    #类方法
    @classmethod
    def cm(cls):
         print('我是类方法,因为我使用了classmethod进行修饰')

#在类之外定义的称为函数,在类之内定义的称为方法
def drink():
    print('喝水')

#创建Student类的对象
stul=Student('张三',20)
stul.eat()  #对象名,方法名()
print(stul.name)
print(stul.age)

print('-----------------')

Student.eat(stul)  #32行与26行代码功能相同,都是调用Student中的eat方法
                   #类名.方法名(类的对象)-->实际上就是方法定义处的self

image.png

class Student:    #Student为类的名称(类名)由一个或多个单词组成,每个单词的首字母大写,其余小写
    native_pace='吉林'   #直接写在类里的变量,称为类属性
    def __init__(self,name,age):
        self.name=name  #self.name称为实体属性,进行了一个赋值的操作,将局部变量的name的值赋值给实体属性
        self.age=age

    #实例方法
    def eat(selfself):
        print('学生在吃饭..')

    #静态方法
    @staticmethod
    def method():
        print('我使用了staticmethod进行修饰,所以我是静态方法')


    #类方法
    @classmethod
    def cm(cls):
         print('我是类方法,因为我使用了classmethod进行修饰')

#类属性的使用方法
print(Student.native_pace)
stu1=Student('张三',20)
stu2=Student('李四',30)
print(stu1.native_pace)
print(stu2.native_pace)
Student.native_pace='天津'
print(stu1.native_pace)
print(stu2.native_pace)
print('------------类方法的使用方式---------------')
Student.cm()
print('------------静态方法的使用方式--------------')
Student.method()

image.png

image.png

image.png

class Student:    #Student为类的名称(类名)由一个或多个单词组成,每个单词的首字母大写,其余小写
    def __init__(self,name,age):
        self.name=name  #self.name称为实体属性,进行了一个赋值的操作,将局部变量的name的值赋值给实体属性
        self.age=age

    #实例方法
    def eat(self):
        print(self.name+'在吃饭')
stu1=Student('张三',20)
stu2=Student('李四',20)
print(id(stu1))
print(id(stu2))
print('-----------------为stu2动态绑定性别属性----------------------')
stu1.gender='女'
print(stu1.name,stu1.age,stu1.gender)
print(stu2.name,stu2.age)

print('-------------------------')
stu1.eat()
stu2.eat()

def show():
    print('定义在类之外的,称函数')
stu1.show=show
stu1.show()

image.png

image.png

类与对象

类是多个类似事物组成的群体的统称,能够帮我们跨苏理解和判断事物的性质。

image.png

image.png

class Student:    #Student为类的名称(类名)由一个或多个单词组成,每个单词的首字母大写,其余小写
    native_pace='吉林' #直接卸载类里的变量,称为类属性
    def __init__(self,name,age):
        self.name=name #self.name 称为实体属性,进行了一个复制的操作,将局部变量的name的值赋给实体属性
        self.age=age

    # 实例方法
    def eat(self):
        print('学生在吃饭')

    # 静态方法
    @staticmethod
    def method():
        print('我使用了statticmethod进行修饰,所以我是静态方法')

    # 类方法
    @classmethod
    def cm(cls):
        print('我是类方法,因为我使用了classmethod进行修饰')
# 在类之外定义的称为函数,在类之内定义的称为方法
def drink():
    print('喝水')

# 创建Student类的对象
stul=Student('张三',20)
stul.eat()
print(id(stul))
print(type(stul))
print(stul)


print('---------------------')
Student.eat(stul)  #与26行相似
class Student:    #Student为类的名称(类名)由一个或多个单词组成,每个单词的首字母大写,其余小写
    native_pace='吉林' #直接卸载类里的变量,称为类属性
    def __init__(self,name,age):
        self.name=name #self.name 称为实体属性,进行了一个复制的操作,将局部变量的name的值赋给实体属性
        self.age=age

    # 实例方法
    def eat(self):
        print('学生在吃饭')

    # 静态方法
    @staticmethod
    def method():
        print('我使用了statticmethod进行修饰,所以我是静态方法')

    # 类方法
    @classmethod
    def cm(cls):
        print('我是类方法,因为我使用了classmethod进行修饰')



# 类属性使用方法
# print(Student.native_pace)
stul1=Student('张三',20)
stul2=Student('李四',30)
print(stul1.native_pace)
print(stul2.native_pace)
Student.native_pace='天津'
print(stul1.native_pace)
print(stul2.native_pace)
print('-------------类方法的使用方式-----------')
Student.cm()
print('-------------静态方法的使用方法------------')
Student.method()

image.png

image.png

image.png

class Student:
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def eat(self):
        print(self.name+'在吃饭')
stu1=Student('张三',20)
stu2=Student('李四',30)

print(id(stu1))
print(id(stu2))

print('----------为stu2动态绑定性别属性-----------')

stu1.gender='女'
print(stu1.name,stu1.age,stu1.gender)
print(stu2.name,stu2.age)


print('------------------')
stu1.eat()
stu2.eat()


def show():
    print('定义在类之外的,称为函数')
stu1.show=show
stu1.show()

image.png

image.png