python基础学习笔记一

注释

单行注释

#

多行注释

通过 单引号'''或 双引号"""

'''
这是一个注释
这是一个注释
'''

数据类型和变量

浮点数

科学计数法

3.14x109 3.14e9

0.0000012 1.2e-6

字符串

' ' 或 " " 都可以

注意

>>> print(' I\'am \"ok\"! ')  # 转义字符
 I'am "ok"!
>>> print(" I'am ok! ")  # 嵌套使用
 I'am ok!
 
 >>> print(r'I\'am \"ok\"!') # r' ' 不转义
I\'am \"ok\"!
# 字符串的多行输出保持格式 print(''' ''')
>>> print('''i
... am
... ok''')

i
am
ok

布尔值

>>> True and False
False
>>> True or False
True
>>> not 1>2
True

空值 None

变量赋值

变量的类型自动变化,还可以不断改变

a = 123 # a是整数
print(a)
a = 'ABC' # a变为字符串
print(a)

没有常量写法,通常用PI大写表示,但是仍可以更改

除法/ 和 整除//

>>> 10/3
3.3333333333333335
>>> 9/3
3.0
>>> 10//3
3

没有大小限制,浮点数整数等无限大

编码

127个ASCII编码不够

–>中文GB2312

–> 各国语言集合Unicode通常固定2个字节

–>为了节省空间变长编码 utf-8

字符ASCIIUnicodeutf-8
A0100000100000000 0100000101000001
x01001110 0010110111100100 10111000 10101101

在计算机内存中使用编辑时用Unicode

在硬件存储、传输时使用utf-8

ord()转为编码整数表示,chr()转为字符

>>> ord('A')
65
>>> ord('中')
20013
>>> chr(66)
'B'
>>> chr(25991)
'文'

如果要在网络上传输,或者保存到磁盘上,就需要把str变为以字节为单位的bytes

str类型'ABC'用 bytes类型表示b'ABC'

>>> 'ABC'.encode('ascii')
b'ABC'
>>> '中文'.encode('utf-8')
b'\xe4\xb8\xad\xe6\x96\x87'

如果我们从网络或磁盘上读取了字节流,那么读到的数据就是bytes。要把bytes变为str,就需要用decode()方法:

>>> b'ABC'.decode('ascii')
'ABC'
>>> b'\xe4\xb8\xad\xe6\x96\x87'.decode('utf-8')
'中文'

len()函数

对于str计算字符数

对于bytes计算字节数

>>> len('中文')
2
>>> len('中文'.encode('utf-8'))
6

如果.py文件本身使用UTF-8编码,并且也申明了# -*- coding: utf-8 -*-,打开命令提示符测试就可以正常显示中文

格式控制符

跟c相似

>>> 'Hello, %s' % 'world'
'Hello, world'
>>> 'Hi, %s, you have $%d.' % ('Michael', 1000000)
'Hi, Michael, you have $1000000.'
>>> print('%4d--%04d' %(4,4))  # %4d 为占4个位,%04d 为0充满
   4--0004
>>> print('%.2f' % 3.141592)  # %.2f 为取小数点后两位
3.14

List列表,tuple元祖

List (类似c++中vector)

>>> classmates = ['Michael','Bob',"Tracy"]
>>> classmates
['Michael', 'Bob', 'Tracy']

>>> len(classmates)
3

>>> classmates[0]   # 正向
'Michael'

>>> classmates[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

>>> classmates[-1]  # 逆向 = classmate[ len()+ (-1) ] = classmates[2]
'Tracy'

>>> classmates[-4]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

尾增 classmates.append('haha')

插入定点位置 classs.insert(位置,变量) classs[位置]插入变量

>>> classs = ['ahh','99',"中文",12]  # 列表可以混合变量
>>> classs
['ahh', '99', '中文', 12]

>>> classs.insert(1,'jack')   # 插入指定位置classs[1]
>>> classs
['ahh', 'jack', '99', '中文', 12]

>>> classs.insert(9,3)
>>> classs
['ahh', 'jack', '99', '中文', 12, 3]  # 超过范围默认放入尾部

>>> classs.insert(-3,33333)  # 负的则为classs[ -3 + len() ]
>>> classs
['ahh', 'jack', '99', '中文', 12, 33333, 34, 44, 3]

classs.pop(10) 弹出定点位置,默认为尾部

删某段(根据下标) del L[2:10:2]

删某元素(根据某内容) L.remove('haha') 多个重复删第一个

清空 str.clear()

classs[1] = 'tihuan' 将原来位置元素替换

列表嵌套 多维数组

>>> classs.insert(3,['hah','33',12])
>>> classs
['jjjj', '222', '99', ['hah', '33', 12], '中文', 12, 33333, 34, 44]

>>> classs[3][1] 
'33'

元祖 tuple

元祖跟列表相似

不同之处

  1. 表示: [ ]列表, ( )元祖
  2. 元祖中数据不可 增 删 改

注意

>>> t = (1)  # 非元祖,仅为变量 t = 1
>>> t
1

>>> t = (1,)  # 元祖,用一个逗号 , 区分一个变量特殊情况
>>> t
(1,)

>>> a = ()  # 空元祖
>>> a
()

条件判断

age = 3
if age >= 18:    # 记得加冒号 :
    print('adult')
elif age >= 6:
    print('teenager')
else:
    print('kid')

输入input

>>> s = input('input your birth: ')
input your birth: 24

>>>birth = int(s)   # 通过强制转换判断输入是否为 整数
>>> birth
'24'

循环

for x in 列表: for循环

>>> list(range(5))  列表生成
[0, 1, 2, 3, 4]
names = ['ha','zp','pe']

for name in names:
    print(name)

sum = 0
for x in range(10):
    sum += x;
print(sum)

while n > 0: while循环

sum = 0
n = 100
while n > 0:
    sum += n;
    n -= 1;
print(sum)

break continue

sum = 0
n = 0
while n < 100:
    n += 1;
    if n % 2:       # 当为奇数时结束本轮循环
        continue
    print(n)
    if n > 10:
        break     # 结束循环体

字典 and 集合

字典的key为不变对象 key --> value

>>> d = {'m':90, 'b':70, 't':100}  # 初始化
>>> d['m']   # 查
90

>>> d
{'m': 90, 'b': 70, 't': 0, 's': 10}  # 查

>>> 'ttt' in d  # 查
False
>>> 't' in d
True

>>> d['s'] = 10   #  增加
>>> d['s']
10

>>> d['t'] = 0   #  改
>>> d['t']
0

>>> d.pop('b')  #  删
80

set { }

set 中没有相同的key

>>> s = set([5,1,3,2,1,7])  # 无序且重复元素  自动变为  有序不重复元素
>>> s
{1, 2, 3, 5, 7}

>>> s.add('hah')  # 增加  元素可以为不同类型
>>> s
{1, 2, 3, 4, 5, 7, 'hah'}

>>> s.remove(4)   # 删除  
>>> s
{1, 2, 3, 5, 7, 'hah'}

>>> s1 = set([1,2,3])
>>> s2 = set([2,3,4])
>>> s1 & s2            #  两个集合的交集
{2, 3}
>>> s1 | s2         #  两个集合的并集
{1, 2, 3, 4}

不可变对象

str不可变对象, list可变对象

>>> a = ['c','a','b']
>>> a.sort()        # 列表内排序 
>>> a
['a', 'b', 'c']

>>> a = 'abc'
>>> b = a.replace('a','A')   # 可以对 a 调用,但是不能改变
>>> b
'Abc'
>>> a
'abc'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值