Python学习笔记中如何实现循环引用?

2026-06-09 05:542阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计376个文字,预计阅读时间需要2分钟。

Python学习笔记中如何实现循环引用?

循环使用while语句,注意缩进。

循环1:while True: word=input('please enter a word:') if not word: break print('The word is:', word)

循环2:while True: word=input('please enter a word:') if not word: break print('The word is:', word)

循环使用一、while注意缩进。循环1whileTrue:wordinput(‘pleaseenteraword:‘)ifnotword:breakprint(‘Thewor

循环使用

一、while 注意缩进。

循环1

>>> while True:

word = input(‘please enter a word:‘)

if not word:

break

print(‘The word was ‘+word)

结果

please enter a word:ddd

The word was ddd

please enter a word:ddd

The word was ddd

please enter a word:rrr

The word was rrr

循环2

word=‘‘

while not word:

word = input(‘name:‘)

Python学习笔记中如何实现循环引用?

print("hell, %s"%word)

结果

name:hhh

hell, hhh

二、for循环使用

1、列表输出

words = [‘a‘,‘b‘,‘c‘,‘d‘]

for word in words:

print(word, )

输出

a

b

c

d

2、遍历字典元素。

d = {‘a1‘: 33, ‘port6‘: 55, ‘a2‘: ‘ddd‘}

for key in d:

print("key:%s"%key),

print("value: %s"%d[key])

输出结果,注意顺序不是从字典左到右输出。随机的。

key:a2

value: ddd

key:a1

value: 33

key:port6

value: 55

注意和c语言一样跳出循环式break,继续下一次循环式continue。

python学习笔记--循环使用,布布扣,bubuko.com

本文共计376个文字,预计阅读时间需要2分钟。

Python学习笔记中如何实现循环引用?

循环使用while语句,注意缩进。

循环1:while True: word=input('please enter a word:') if not word: break print('The word is:', word)

循环2:while True: word=input('please enter a word:') if not word: break print('The word is:', word)

循环使用一、while注意缩进。循环1whileTrue:wordinput(‘pleaseenteraword:‘)ifnotword:breakprint(‘Thewor

循环使用

一、while 注意缩进。

循环1

>>> while True:

word = input(‘please enter a word:‘)

if not word:

break

print(‘The word was ‘+word)

结果

please enter a word:ddd

The word was ddd

please enter a word:ddd

The word was ddd

please enter a word:rrr

The word was rrr

循环2

word=‘‘

while not word:

word = input(‘name:‘)

Python学习笔记中如何实现循环引用?

print("hell, %s"%word)

结果

name:hhh

hell, hhh

二、for循环使用

1、列表输出

words = [‘a‘,‘b‘,‘c‘,‘d‘]

for word in words:

print(word, )

输出

a

b

c

d

2、遍历字典元素。

d = {‘a1‘: 33, ‘port6‘: 55, ‘a2‘: ‘ddd‘}

for key in d:

print("key:%s"%key),

print("value: %s"%d[key])

输出结果,注意顺序不是从字典左到右输出。随机的。

key:a2

value: ddd

key:a1

value: 33

key:port6

value: 55

注意和c语言一样跳出循环式break,继续下一次循环式continue。

python学习笔记--循环使用,布布扣,bubuko.com