Python function

本文详细介绍了Python中的函数用法,包括函数定义、参数、返回值,以及默认参数和无返回值函数的概念。此外,还讨论了变量的作用域,区分了局部和全局变量。字符串方面,讲解了索引、长度、循环、切片以及内置的字符串操作方法。最后,提到了文件处理的基本操作,如打开、读取、写入等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值