标签:ignore com not 实现 NPU mat bsp 遇到 highlight
1.输入输出
输出:
print(‘‘),print(‘xixi‘,‘haha‘)
会依次打印每个字符串,遇到逗号“,”会输出一个空格
print(100+200)
输入:input()函数
举例:name=input()
print(name)
>>>name
‘xxx‘
print("请输入您的名字!") name=input() print(‘hello‘,name)
2.数据类型
整型,浮点型,
字符串:
含有 ‘ 用 " "包括字符串
print("I‘m OK")
含有"的字符串,转义字符
print(‘I\‘m \"OK\"‘)
转义字符\
可以转义很多字符,比如\n
表示换行,\t
表示制表符,字符\
本身也要转义,所以\\
表示的字符就是\
print(‘hello boys\n girls‘)
hello boys
girls
print(‘hello\they‘)
hello hey
print(‘\\hey\\‘)
\hey\
>>> print(‘\\\t\\‘) \ >>> print(r‘\\\t\\‘) \\\t\
如果字符串内部有很多换行,用\n
写在一行里不好阅读,为了简化,Python允许用‘‘‘...‘‘‘
的格式表示多行内容
>>> print(‘‘‘line1 ... line2 ... line3‘‘‘) line1 line2 line3
>>> print(r‘‘‘hello,\n ... world‘‘‘) hello,\n world >>> print(r‘‘‘hello,\n world‘‘‘) hello,\n world
布尔值(true,false),and or not
>>> 3>2 True >>> 3<1 False
空值
none,不能用0表示,0有意义
变量
动态语言,变量本身类型不固定
常量
两种除法 //取整
>>> print(10/3) 3.3333333333333335 >>> print(10//3) 3
%取余数
2.字符编码???
由于Python的字符串类型是str
Python对bytes
类型的数据用带b
前缀的单引号或双引号表示:
要注意区分‘ABC‘
和b‘ABC‘
,前者是str
,后者虽然内容显示得和前者一样,但bytes
的每个字符都只占用一个字节。
以Unicode表示的str
通过encode()
方法可以编码为指定的bytes
对于单个字符的编码,Python提供了ord()
函数获取字符的整数表示,chr()
函数把编码转换为对应的字符:
要把bytes
变为str
,就需要用decode()
方法
如果bytes
中只有一小部分无效的字节,可以传入errors=‘ignore‘
忽略错误的字节:
>>> b‘ABC‘.decode(‘ascii‘) ‘ABC‘ >>> ‘ABC‘.encode(‘ascii‘) b‘ABC‘ >>> b‘\xe4\xb8\xad\xff‘.decode(‘utf-8‘, errors=‘ignore‘) ‘中‘
要计算str
包含多少个字符,可以用len()
函数:
>>> len(‘ABC‘) 3
1个中文字符经过UTF-8编码后通常会占用3个字节,而1个英文字符只占用1个字节
避免乱码
#!/usr/bin/env python3 # -*- coding: utf-8 -*-
格式化
内容都是根据变量变化的,所以,需要一种简便的格式化字符串的方式
在Python中,采用的格式化方式和C语言是一致的,用%
实现
>>> ‘hello,%s‘ % ‘world‘ ‘hello,world‘ >>> ‘hi %s,you have$%d‘ % (‘micheal‘,100000) ‘hi micheal,you have$100000‘
print(‘hi %s,you have $%d‘ % (‘tom‘,1000)) print(‘Hello, %s‘ % ‘world‘)
其中,格式化整数和浮点数还可以指定是否补0和整数与小数的位数
print(‘%2d-%02d‘ %(3,1)) print(‘%.2f‘ % 3.1415926)
3-01
3.14
如果你不太确定应该用什么,%s
永远起作用,它会把任何数据类型转换为字符串
字符串里面的%
是一个普通字符怎么办?这个时候就需要转义,用%%
来表示一个%
:
另一种格式化字符串的方法是使用字符串的format()
方法,它会用传入的参数依次替换字符串内的占位符{0}
、{1}
……,不过这种方式写起来比%要麻烦得多:
print(‘hello,{0},成绩提升了{1:.1f}%‘.format(‘小明‘,17.125))
标签:ignore com not 实现 NPU mat bsp 遇到 highlight
原文地址:https://www.cnblogs.com/jieyi/p/9379827.html