Python for 循环

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> #Python for 循环
>>> #
>>> #for target in sequences:
>>>     #statements
>>> #
>>> #sequences
>>> #1.list
>>> #2.strings
>>> #3.tuple
>>> #4.files
>>> #
>>> s1 = 'www.jeapedu.com' >>> i = 0 >>> for c in s1: print format(i,'2d'),c i = i + 1 else: print 'for out' 0 w 1 w 2 w 3 . 4 j 5 e 6 a 7 p 8 e 9 d 10 u 11 . 12 c 13 o 14 m for out >>> >>> #2.list >>> >>> #for var in list: >>> # >>> list1 = [1,2,4,'g',12.3] >>> i = 0 >>> for val in list1: print format(i,'2d'),val i = i + 1 0 1 1 2 2 4 3 g 4 12.3 >>> list2 = list(s1) >>> print list2 ['w', 'w', 'w', '.', 'j', 'e', 'a', 'p', 'e', 'd', 'u', '.', 'c', 'o', 'm'] >>> i = 0 >>> for val in list2: print format(i,'2d'),val i = i + 1 else: print 'out list2' 0 w 1 w 2 w 3 . 4 j 5 e 6 a 7 p 8 e 9 d 10 u 11 . 12 c 13 o 14 m out list2 >>> i = 0 >>> for val in 'www.jeapedu.com' SyntaxError: invalid syntax >>> for val in 'www.jeapedu.com': print format(i,'2d'),val i = i + 1 0 w 1 w 2 w 3 . 4 j 5 e 6 a 7 p 8 e 9 d 10 u 11 . 12 c 13 o 14 m >>> i = 0 >>> for val in [1,2,4,'g',12.3]: print format(i,'2d'),val i = i + 1 0 1 1 2 2 4 3 g 4 12.3 >>> i = 0 >>> help(range) Help on built-in function range in module __builtin__: range(...) range([start,] stop[, step]) -> list of integers Return a list containing an arithmetic progression of integers. range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0. When step is given, it specifies the increment (or decrement). For example, range(4) returns [0, 1, 2, 3]. The end point is omitted! These are exactly the valid indices for a list of 4 elements. >>> for val in range(1,10,2): print format(i,'2d'),val i = i + 1 0 1 1 3 2 5 3 7 4 9 >>> for val in range(2,10,2): print format(i,'2d'),val i = i + 1 5 2 6 4 7 6 8 8 >>> >>> #3.for var in tuples >>> >>> #元祖用()括起来,列表用[],元祖不可修改,列表可以 >>> tup = (1,2,3,4,) >>> for t in tup: print t else SyntaxError: invalid syntax >>> tup = (1,2,3,4,) >>> for t in tup: print t else: print 'out tup' 1 2 3 4 out tup >>> #4.for var in files >>> >>> #for n in open('xx.txt','r').read() >>> #print format(n,'2d') >>> >>> help(file.read) Help on method_descriptor: read(...) read([size]) -> read at most size bytes, returned as a string. If the size argument is negative or omitted, read until EOF is reached. Notice that when in non-blocking mode, less data than what was requested may be returned, even if no size parameter was given. >>> help(file.readline) Help on method_descriptor: readline(...) readline([size]) -> next line from the file, as a string. Retain newline. A non-negative size argument limits the maximum number of bytes to return (an incomplete line may be returned then). Return an empty string at EOF. >>> help(file.readlines) Help on method_descriptor: readlines(...) readlines([size]) -> list of strings, each a line from the file. Call readline() repeatedly and return a list of the lines so read. The optional size argument, if given, is an approximate bound on the total number of bytes in the lines returned. >>> >>> for c in open('python for.py','r').readline(): print c Traceback (most recent call last): File "<pyshell#76>", line 1, in <module> for c in open('python for.py','r').readline(): IOError: [Errno 2] No such file or directory: 'python for.py' >>> for c in open('F:\\PYTHON\\Python Developing\\python for.py','r').readline(): print c P y t h o n 2 . 7 . 3 ( d e f a u l t , A p r 1 0 2 0 1 2 , 2 3 : 3 1 : 2 6 ) [ M S C v . 1 5 0 0 3 2 b i t ( I n t e l ) ] o n w i n 3 2 >>> list3 = open('F:\\PYTHON\\Python Developing\\python for.py','r').readlines() >>> len(list3) 212 >>> for c in open('F:\\PYTHON\\Python Developing\\python for.py','r').readlines(): print c Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> s1 = 'www.jeapedu.com' >>> i = 0 >>>......

当for循环正常执行完的情况下,则继续执行else语句;当for循环中执行了跳出循环break语句,则不再执行else语句。

>>> def eg(num:int)->bool:
	n = num // 2
	for i in range(n, 1, -1):
		if num % i == 0:
			print('{}最大约数为{}'.format(num, i))
			break
	else:
		print('{}是质数'.format(num))

		
>>> eg(9)
9最大约数为3
>>> eg(11)
11是质数
>>> eg(20)
20最大约数为10
>>> 

 

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值