前言:
此时咱们对“pythonforif一行”大致比较注重,各位老铁们都需要分析一些“pythonforif一行”的相关文章。那么小编在网络上网罗了一些有关“pythonforif一行””的相关内容,希望各位老铁们能喜欢,各位老铁们一起来学习一下吧!if语句 语句如果
>>> x = int(input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
... x = 0
... print('Negative changed to zero')
... elif x == 0:
... print('Zero')
... elif x == 1:
... print('Single')
... else:
... print('More')
...
More
for语句
>>> # Measure some strings: 测量一些字符串
>>> words = ['cat', 'window', 'defenestrate']
>>> for w in words:
... print(w, len(w))
...
cat 3
window 6
defenestrate 12
range() 功能
>>> for i in range(5):
... print(i)
...
0
1
2
3
4
>>> list(range(5, 10))
[5, 6, 7, 8, 9]
>>> list(range(0, 10, 3))
[0, 3, 6, 9]
>>> list(range(-10, -100, -30))
[-10, -40, -70]
range() 和 len() 组合
>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
... print(i, a[i])
...
0 Mary
1 had
2 a
3 little
4 lamb
>>> range(10)
range(0, 10)
sum()
>>> sum(range(4)) # 0 + 1 + 2 + 3
6
break 中断语句
>>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print(n, 'equals', x, '*', n//x)
... break
... else:
... # loop fell through without finding a factor
... print(n, 'is a prime number')
...
2 is a prime number # 2是质数
3 is a prime number
4 equals 2 * 2 # 4等于2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
continue 继续声明
>>> for num in range(2, 10):
... if num % 2 == 0:
... print("Found an even number", num)
... continue
... print("Found an odd number", num)
...
Found an even number 2
Found an odd number 3
Found an even number 4
Found an odd number 5
Found an even number 6
Found an odd number 7
Found an even number 8
Found an odd number 9
pass
>>> while True:
... pass # Busy-wait for keyboard interrupt (Ctrl+C)
...
>>> class MyEmptyClass:
... pass
...
>>> def initlog(*args):
... pass # Remember to implement this!
...
定义函数
>>> def fib(n): # write Fibonacci series up to n
... """Print a Fibonacci series up to n"""
... a, b = 0, 1
... while a < n:
... print(a, end=' ')
... a, b = b, a+b
... print()
...
>>> # Now call the function we just defined: 现在调用刚刚定义的函数
>>> fib(2000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
>>> fib
<function fib at 0x000001DB1C30FEE0>
>>> f = fib
>>> f(100)
0 1 1 2 3 5 8 13 21 34 55 89
>>> def fib2(n):
... result = []
... a, b = 0, 1
... while a < n:
... result.append(a)
... a, b = b, a+b
... return result
...
>>> f100 = fib2(100)
>>> f100
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
默认参数值
>>> def ask_ok(prompt, retries=4, reminder='Please try again!'):
... while True:
... ok = input(prompt)
... if ok in ('y', 'ye', 'yes'):
... return True
... if ok in ('n', 'no', 'nop', 'nope'):
... return False
... retries = retries - 1
... if retries < 0:
... raise ValueError('invalid user response')
... print(reminder)
此函数可以通过以下几种方式调用
只给出强制性参数:ask_ok('Do you really want to quit?')
>>> ask_ok('Do you really want to quit?')
Do you really want to quit?yyyyyyyy
Please try again!
Do you really want to quit?ye
True
>>> ask_ok('Do you really want to quit?')
Do you really want to quit?no
False
给出一个可选的参数:ask_ok('OK to overwrite the file?', 2)>>>
>>> ask_ok('OK to overwrite the file?', 3)
OK to overwrite the file?
Please try again!
OK to overwrite the file?
Please try again!
OK to overwrite the file?
Please try again!
OK to overwrite the file?
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 10, in ask_ok
ValueError: invalid user response
甚至给出所有参数:ask_ok('OK to overwrite the file?', 2, 'Come on, only yes or no!')
>>> ask_ok('OK to overwrite the file?', 2, 'Come on, only yes or no!')
OK to overwrite the file?ttt
Come on, only yes or no!
OK to overwrite the file?ye
True
>>> i = 5
>>> def f(arg=i):
... print(arg)
...
>>> i = 6
>>> f()
5 # 将打印5
>>> def f(a, L=[]):
... L.append(a)
... return L
...
>>> print(f(1))
[1]
>>> print(f(2))
[1, 2]
>>> print(f(3))
[1, 2, 3]
>>> def f(a, L=None):
... if L is None:
... L = []
... L.append(a)
... return L
标签: #pythonforif一行