Python function

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

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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中的函数(function)是一种数据类型,它可以赋值给一个变量,也可以作为另一个函数的返回值或参数使用。函数类型在Python中被称为function,它具有许多特性,包括可以接受参数和返回值,可以通过关键字lambda定义匿名函数,还可以使用过滤函数filter和映射函数map等功能。在Python中,函数类型可以像其他数据类型一样进行操作,比如赋值、传递给其他函数等。如果想了解更多关于Python函数的基础知识,可以参考Python基础系列大纲中的相关文章。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Python脚本,函数数据类型function,过滤函数filter,映射函数map,匿名函数lambda](https://blog.csdn.net/ganfanren00001/article/details/122909886)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Python 基础系列 8 - 函数 Function 基础知识](https://blog.csdn.net/wumingxiaoyao/article/details/108942536)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值