-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
3.2 Python 基础语法之数据类型和变量 #14
Comments
数据类型和变量一、数据类型1. 整数和浮点数1.1 整数
1.2 浮点数
数字类型支持的操作:
2. 字符串用
3. 布尔值
4. 空值Python 中只有一个表示空的值—— 5. 自定义对象二、变量在计算机内部,任何数据都可以看作是一个“对象”,而变量就是在程序中用来指向这些数据“对象”的,对变量赋值就是把变量和数据关联起来。 变量名可以包含大小写字母、数字和下划线 在 Python 中,一个变量可以被重复赋值,而且可以是不同类型的值。
三、常量常量是指值是固定不变的变量。比如数学常数 π。 不同于 C 语言中的 const 常量,Python 中并没有一种机制来确保常量的值不被改变。 按照惯例,我们通常会将常量名用大写表示。例如: PI = 3.1415926 四、保留字(Reserved Words)以下单词作为保留字,不能作为变量/常量名: False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield 参考 |
字符串和编码一、字符编码1. 为什么需要对字符进行编码?有哪些常用的编码方式?
2. 如何设置Python 文件的编码、解码方式在文件开头加入
例如: #!/usr/bin/python
#coding=utf-8
print ('你好,世界') 二、Python 中的字符串Python 中字符串通常就是 Python 的字符串有以下几种写法:
1. 访问字符串中的值2. 字符串更新3. 转义字符4. 字符串运算符5. 字符串格式化6. 三引号(triple quotes)7. Unicode 字符串8. 字符串内建函数
参考
问题
使用下面这段代码可以去掉开头和结尾的任何空白、制表符和换行等字符:
|
有序类型Python 中有 3 种常用的有序类型:list,tuple 和 range,其中 list 是可变的,tuple 和 range 不可变。 有序类型都支持的一些操作(摘自官方文档):
可变类型的有序类型支持的一些操作(摘自官方文档):
一、listlist 是一个有序类型,支持增删改查元素,类似于 C 语言中的数组,但更强大。 创建列表的几种方式:
除了上面列出的有序类型和可变有序类型共有的一些操作之外,list 还有一个 示例代码: classmates = ['Michael', 'Bob', 'Tracy']
# 数组长度
print(classmates)
print(len(classmates))
# 获取第一个元素和最后一个元素
print(classmates[0])
print(classmates[-1])
# 在末尾添加一个新元素
classmates.append('Adam')
print(classmates)
# 在制定位置插入一个新元素
classmates.insert(1, 'Jack')
print(classmates)
# 删除末尾的元素
classmates.pop()
print(classmates)
# 删除指定位置的元素
classmates.pop(1)
print(classmates)
# 更新指定位置的元素
classmates[1] = 'Sarah'
print(classmates)
# 排序
classmates.sort() 二、tupletuple 跟数组类似,也是一个有序类型,但是是不可变的。 我们可以通过以下几种方式来创建 tuple:
值得注意的是,当表示一个只有一个元素的 tuple 时,不能直接写成 Tuple 支持上面列出的所有有序集合的操作。 示例代码: t1 = (1, 2)
t2 = (1, ) 三、rangerange 是一个不可变的有序集合范围,range 通常用来在 for 循环中循环指定次数。 示例代码: >>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
>>> list(range(0, 10, 3))
[0, 3, 6, 9]
>>> list(range(0, -10, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> list(range(0))
[]
>>> list(range(1, 0))
[]
>>> r = range(0, 20, 2)
>>> r
range(0, 20, 2)
>>> 11 in r
False
>>> 10 in r
True
>>> r.index(10)
5
>>> r[5]
10
>>> r[:5]
range(0, 10, 2)
>>> r[-1]
18 参考 |
dictPython 中的 dict 跟 Objective-C 中的 NSDictionary 类似,都是存储 key-value 键值对的集合。 >>> d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}
>>> d['Michael']
95
>>> d['Adam'] = 67
>>> d['Adam']
67 常用操作: len(d)
d[key]
d[key] = value
del d[key]
key in d
key not in d
iter(d)
clear()
copy()
classmethod fromkeys(seq[, value])
get(key[, default])
items()
keys()
pop(key[, default])
popitem()
setdefault(key[, default])
update([other])
values() One more thing使用字面量语法读取 dict 的 key-value 键值对时,如果该 key 不存在,会抛出异常: >>> d = {'name': 'James'}
>>> d['age']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'age' 有两种方法可以避免。 >>> 'age' in d
False 另一种方法是使用 value = d.get(key)
value = d.get(key, "empty") 参考: |
set和 dict 类似,set 也是一组 key 的集合,但不存储 value。由于 key 不能重复,所以,在 set 中不能有重复的 key。 set 支持的操作: len(s)
x in s
x not in s
isdisjoint(other)
issubset(other) 或者 set <= other
set < other
issuperset(other) 或者 set >= other
set > other
union(*others) 或者 set | other | ...
intersection(*others) 或者 set & other & ...
difference(*others) 或者 set - other - ...
symmetric_difference(other) 或者 set ^ other
copy() 示例代码: >>> s = set([1, 2, 3])
>>> s
{1, 2, 3}
>>> s = set([1, 1, 2, 2, 3, 3])
>>> s
{1, 2, 3}
>>> s.add(4)
>>> s
{1, 2, 3, 4}
>>> s.add(4)
>>> s
{1, 2, 3, 4}
>>> s.remove(4)
>>> s
{1, 2, 3}
>>> s1 = set([1, 2, 3])
>>> s2 = set([2, 3, 4])
>>> s1 & s2
{2, 3}
>>> s1 | s2
{1, 2, 3, 4} 参考 |
关联 issue
The text was updated successfully, but these errors were encountered: