python 知识点整理(三)
本文只是对python部分知识点进行学习和整理
本篇主要是针对python的function的总结
in python there are two kinds of funtion
- build-in function eg:print()/int()
- define by users
function definition
def function
- creat with ‘def’
- optional parameters in parenthesis 可选变量在括号中
argument/parameters/return values
- argument 实参 输入的参数
- parameters 形参 def函数时候输入的参数
- return 返回值
multiple parameters/arguments/return
multiple
- default argument
1.Arguments can be set as default argument values
2.Default argument values will work when function is invoked without respective arguments
void functions
Void function is a function that does not return any value
scope of variables
local/global
a variable created in function is referred to as local variable
x=1
def f():
x=2
return x
print(f())
x
2
1
could use “global” make it became global variable
x=1
def f():
global x
x=x+1
return x
print(f())
x
2
2
the value of x has changed
string type
index
start with 0 to (largest number-1)
len()
get the string length
Python error if you attempt to index beyond the end of a string
looping/counting
details in part(2)
slicing-str[:]/str[::]
- start with first num
- The second number is one beyond the end of the slice, which is not included
“in” in logical expression
returns True or False
string library
find()/upper()/lower()/replace()/strip()/startwith()
fruit='banana'
print(fruit.find('na'))
print(fruit.find('z'))
print(fruit.upper())
upp='UPPER STRING'
print(upp.lower())
print(fruit.replace('na','nnee'))
greet=' hello bob '
print(greet.lstrip())
print(greet.rstrip())
print(greet.strip())
2
-1
BANANA
upper string
banneennee
hello bob
hello bob
hello bob
break/continue/indefinite/definite
details in part(2)
file processing
open
handle = open(filename, mode)
mode:r-read;w-write
\n
换行符
We represent it as ‘\n’ in strings; Newline is still one character, not two
read lines
#框架
file=open()
for line in file:
print(line)
#whole
all=flie.read()
filehandle.write(str)
#大致框架
file=open('xxxxxx','w')
file.write('the first line\n')
file.write('the second line\n')
file.close()