美文网首页大数据 爬虫Python AI Sql
菜鸟的Python循环语句学习资料总结

菜鸟的Python循环语句学习资料总结

作者: 梦想编程家小枫 | 来源:发表于2019-03-12 13:37 被阅读0次

1 #基本的for循环语句

2 test_list = [2,"Jone",3,6,7,'hongten','hanyuan','good',"Tom"]

3 #打印列表的长度

4 print(len(test_list))

注:想学习Python的小伙伴们

可以

进群:984632579

领取从0到1完整学习资料 视频 源码 精品书籍 一个月经典笔记和99道练习题及答案

5

6 #遍历列表

7 for i in test_list:

8 print(i)

9

10 test_str = "hello,i'm hongten"

11 print('打印字符串:' + test_str)

12 #遍历一个字符串

13 print('遍历一个字符串')

14 for i in test_str:

15 print(i)

16

17 test_tuple = [("a",1),("b",2),("c",3),("d",4)]

18 print(test_tuple)

19 #遍历一个元组

20 print('遍历一个元组')

21 for (i,j) in test_tuple:

22 print(i,j)

23

24 test_dict = {'name':'hongten','age':'20','gender':'M','sports':'足球,乒乓球,游泳'}

25 #字典迭代器

26 for key in test_dict:

27 print(key + ':' + test_dict[key])

28

29 L1 = [1,3,5,7]

30 L2 = [2,4,6,8]

31 #使用zip将两个列表合并

32 print(zip(L1,L2))

33

34 for (i,j) in zip(L1,L2):

35 print(i,j)

36 print('#######################################################')

37 L3 = L2[:]

38 L3.remove(8)

39 print('L1,L3列表为:')

40 print(L1)

41 print(L3)

42 for (i,j) in zip(L1,L3):

43 print(i,j)

44

45 #可以看出来当长度不一的时候,多余的被忽略

46

47

48 test_keys = ['name','age','gender','weight','hight']

49 test_values = ['Hongten','20','M','55','170']

50 #使用zip来构造一个字典

51 print('字典中的keys:')

52 print(test_keys)

53 print('字典中的key对应的value:')

54 print(test_values)

55 print('构造字典后')

56 test_dic = dict(zip(test_keys,test_values))

57 for key in test_dic:

58 print( key + ':' + test_dic[key])

python爬虫群文件免费分享学习教程下载,每天免费python课程学习,需要的联系群主。pythonweb、python爬虫、数据分析、人工智能、机器学习、深度学习、flask、python自动化测试、python运维、django

运行效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32

Type "copyright", "credits" or "license()" for more information.

>>> ================================ RESTART ================================

>>>

9

2

Jone

3

6

7

hongten

hanyuan

good

Tom

打印字符串:hello,i'm hongten

遍历一个字符串

h

e

l

l

o

,

i

'

m

h

o

n

g

t

e

n

[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

遍历一个元组

a 1

b 2

c 3

d 4

sports:足球,乒乓球,游泳

gender:M

name:hongten

age:20

1 2

3 4

5 6

7 8

#######################################################

L1,L3列表为:

[1, 3, 5, 7]

[2, 4, 6]

1 2

3 4

5 6

字典中的keys:

['name', 'age', 'gender', 'weight', 'hight']

字典中的key对应的value:

['Hongten', '20', 'M', '55', '170']

构造字典后

weight:55

hight:170

gender:M

name:Hongten

age:20

>>>

相关文章

  • 菜鸟的Python循环语句学习资料总结

    1 #基本的for循环语句 2 test_list = [2,"Jone",3,6,7,'hongten','ha...

  • 012.Python循环语句

    Python 循环语句 1. 概述 Python中的循环语句有 for 和 while。 Python循环语句的控...

  • python 基础 - 循环语句

    python 循环语句 Python中的循环语句有 for 和 while。Python循环语句的控制结构图如下所...

  • 我的python学习笔记-第十天

    循环语句 Python中的循环语句有 for 和 while。 while 循环 Python中while语句的一...

  • Lesson 021 —— python 循环语句

    Lesson 021 —— python 循环语句 Python中的循环语句有 for 和 while。 循环可以...

  • day4

      今天主要学习了Python语言的if语句、for循环语句及while语句。   一、if语句   说明: if...

  • 第二篇学习总结

    第二篇学习总结 一、循环语句 和选择语句不同,循环语句满足一定条件循环执行循环体内的语句。循环语句在Java中有三...

  • python 循环语句

    本次将为大家介绍Python循环语句的使用。Python中的循环语句有 for 和 while。Python循环语...

  • continue

    Python continue 语句Python continue 语句跳出本次循环,而break跳出整个循环。 ...

  • python循环语句详细讲解

    想必大家都知道python循环语句吧,可以python循环语句有多种,比如for循环、while循环、if、els...

网友评论

    本文标题:菜鸟的Python循环语句学习资料总结

    本文链接:https://www.haomeiwen.com/subject/edggpqtx.html