python基础语法和函数学习
2021/9/9 22:35:04
本文主要是介绍python基础语法和函数学习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
循环的写法
>>> names = ['michael','bob','tracy'] >>> for name in names: ... print(name) ... michael bob tracy >>> sum =0 >>> for x in [1,2,3,4,5,6,7,8,9,10]; File "<stdin>", line 1 for x in [1,2,3,4,5,6,7,8,9,10]; ^ SyntaxError: invalid syntax >>> for x in [1,2,3,4,5,6,7,8,9,10]: ... sum = sum + x ... print(sum) File "<stdin>", line 3 print(sum) ^ SyntaxError: invalid syntax >>> print(sum) 0 >>> for x in [1,2,3,4,5,6,7,8,9,10]: ... sum = sum + x ... >>> print(sum) 55 >>> list(range(5)) [0, 1, 2, 3, 4] >>> sum = 0 >>> for x in range(101): ... sum = sum +x ... >>> print(sum) 5050 >>> sum = 0 >>> n =99 >>> while n>0: ... sum+=n ... n-=2 ... >>> print(sum) 2500 >>> n=1 >>> while n<=100 File "<stdin>", line 1 while n<=100 ^ SyntaxError: invalid syntax >>> while n<=100: ... if n>10: ... break ... print(n) ... n+=1 ... 1 2 3 4 5 6 7 8 9 10 >>> n=0 >>> while n<10: ... n+=1 ... if n%2==0: ... continue ... print(n) ... 1 3 5 7 9
dict和set
>>> names = ['micheal','bob','tracy'] >>> score = [95,75,85] >>> >>> d={'micheal':95,'bob':75,'tracy':85} >>> d['micheal'} File "<stdin>", line 1 d['micheal'} ^ SyntaxError: closing parenthesis '}' does not match opening parenthesis '[' >>> d{'micheal'} File "<stdin>", line 1 d{'micheal'} ^ SyntaxError: invalid syntax >>> d['micheal'] 95 >>> d['adam']=67 >>> d['adam'] 67 >>> 'tom' in d False >>> d.get('tom') >>> d.get('tom',-0) 0 >>> d.pop('micheal') 95 >>> d {'bob': 75, 'tracy': 85, 'adam': 67} >>> s=set([1,2,3]) >>> s {1, 2, 3} >>> s = set([1,1,2,3,2,3]) >>> s {1, 2, 3} >>> s.add(5) >>> s {1, 2, 3, 5} >>> s,add(1) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'add' is not defined >>> s.add(1) >>> s {1, 2, 3, 5} >>> s.remove(2) >>> s {1, 3, 5}
可变对象和不可变对象
>>> a = ['a','b','d','c'] >>> a.sort <built-in method sort of list object at 0x000002AABFFCF500> >>> a.sort() >>> a ['a', 'b', 'c', 'd'] >>> a='abc' >>> a,replace('a','A') Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'replace' is not defined >>> a.replace('a','A') 'Abc' >>> a 'abc'
调用函数
>>> help(abs) Help on built-in function abs in module builtins: abs(x, /) Return the absolute value of the argument. >>> abs(-90) 90 >>> abs (1,2) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: abs() takes exactly one argument (2 given) >>> max(1,2,3,4,90.111) 90.111 >>> abs('abc') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: bad operand type for abs(): 'str' >>> int(11.34) 11 >>> int('123') 123 >>> bool(1) True >>> bool() False >>> a= abs >>> a(-1) 1 >>>
定义函数
>>> def my_abs()x: File "<stdin>", line 1 def my_abs()x: ^ SyntaxError: invalid syntax >>> def my_abs(x): ... if x >=0: ... return x ... else: ... return -x ... >>> print(my_abs(-99)) 99 >>> def nop(): ... pass ... >>> nop() >>> my_abs(-1,-2) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: my_abs() takes 1 positional argument but 2 were given >>> abs(-1,-2) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: abs() takes exactly one argument (2 given) >>> my_abs('a') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in my_abs TypeError: '>=' not supported between instances of 'str' and 'int' >>> abs('a') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: bad operand type for abs(): 'str' >>> def my_abs(x): ... if not isinstance(x,(int,float)): ... raise TypeError('bad operand type') ... if x>=0: ... return x ... else: ... return -x ... >>> my_abs('a') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in my_abs TypeError: bad operand type >>> import math >>> def move(x,y,step,angle=0): ... nx = x+step*math.cos(angle) ... ny = y-step*math.sin(angle) ... return nx ny File "<stdin>", line 4 return nx ny ^ SyntaxError: invalid syntax >>> def move(x,y,step,angle=0): ... nx = x+step*math.cos(angle) ... ny = y-step*math.sin(angle) ... return nx,ny ... >>> x,y=move(100,100,60,math.pi/6) >>> print(x,y) 151.96152422706632 70.0 >>> r=move(100,100,60,math.pi/6) >>> print(r) (151.96152422706632, 70.0)
这篇关于python基础语法和函数学习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门
- 2024-11-14Python编程入门指南
- 2024-11-13Python基础教程
- 2024-11-12Python编程基础指南
- 2024-11-12Python基础编程教程
- 2024-11-08Python编程基础与实践示例
- 2024-11-07Python编程基础指南