Python基础10- 函数之内部函数与强制转换

#coding=utf-8


import math
import os
import socket


Python函数包括以下三类:内部函数、第三方函数和自定义函数
内部函数包括:字符函数库、数学函数库、系统函数库、网络函数库

字符函数库包括replace(old,new)、islower()、isspace()等,具体可用help(str)查看、
str1 = 'Hello'
str2 = 'hello'
str3 = 'H e llo'

print str1.islower()      #判断是否全为小写,是则返回True,否则返回False
print str2.islower()
print str3.isspace()       #判断是否全为空格
print str3.replace(' ','AA')   #将空格替换成AA

数学函数库math,为防止程序报相关函数未定义错误,最好在开始的时候导入相关模块
print abs(-11)
print pow(3,3)
用函数的时候,如果传入的参数数量不对,会报TypeError的错误
若传入的参数数量是对的,但参数类型不能被函数所接受,也会报TypeError的错误

系统函数库os
pwd = os.getcwd()      #获取当前路径
print pwd
ldirs = os.listdir(pwd)      #获取当前路径下所有文件或文件夹
print ldirs

os.name 获取当前使用的操作系统,其中 'nt' 是 windows,'posix' 是 linux 或者 unix
name = os.name
if name == 'posix':
  print 'this is linux or unix'
elif name == "nt":
  print 'this is windows'
else:
  print 'this is other system'

os.remove()         #删除指定文件
os.remove('f:/123.txt')    #若文件(包括后缀名)不存在则报错,注意绝对路径时要转义
os.remove('hanshu1')

os.mkdir()           #在指定目录下创建文件夹
os.removedirs()      #删除指定目录下的文件夹
oschdir()            #改变当前路径到指定路径()内只需输入指定路径即可
需查看其他函数import os 后,输入help(os) 即可


网络函数库
baiduip = socket.gethostbyname('www.baidu.com')    #获取百度的ip地址
print baiduip
Hname = socket.gethostname()             #获取本机计算机名


print Hname

#强制类型转换
print int('123')
print str(11)
print int(1.23)

原文地址:https://www.cnblogs.com/dtest/p/4083925.html