在 以前的 在本文中,我们了解了Python的基础知识。现在,我们继续介绍更多python概念。
null
Python中的字符串 字符串是一个字符序列。可以使用双引号在python中声明它。字符串是不可变的,即不能更改。
python
# Assigning string to a variable a = "This is a string" print (a) |
Python中的列表 列表是python中最强大的工具之一。它们就像其他语言中声明的数组一样。但最有力的一点是,列表不一定总是同质的。单个列表可以包含字符串、整数以及对象。列表还可用于实现堆栈和队列。列表是可变的,也就是说,一旦声明,它们就可以被更改。
python
# Declaring a list L = [ 1 , "a" , "string" , 1 + 2 ] print L L.append( 6 ) print L L.pop() print L print L[ 1 ] |
输出为:
[1, 'a', 'string', 3][1, 'a', 'string', 3, 6][1, 'a', 'string', 3]a
Python中的元组 元组是一系列不变的Python对象。元组就像列表一样,只是元组一旦声明就不能更改。元组通常比列表快。
python
tup = ( 1 , "a" , "string" , 1 + 2 ) print (tup) print (tup[ 1 ]) |
输出为:
(1, 'a', 'string', 3)a
Python中的迭代 在python中,可以通过“for”和“while”循环执行迭代或循环。除了迭代特定条件外,我们还可以迭代字符串、列表和元组。 示例1:条件的while循环迭代
python
i = 1 while (i < 10 ): print (i) i + = 1 |
输出为:
123456789
示例2:字符串上for循环的迭代
python
s = "Hello World" for i in s : print (i) |
输出为:
Hello World
示例3:列表上for循环的迭代
python
L = [ 1 , 4 , 5 , 7 , 8 , 9 ] for i in L: print (i) |
输出为:
145789
示例4:范围的for循环迭代
python
for i in range ( 0 , 10 ): print (i) |
输出为:
0123456789
https://www.youtube.com/watch?v=pCoB45
- 下一篇文章—— Python:字典和关键字
- 测验 Python中的数据类型
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END