Functions
Functions
Flags
Flag Meannq
Data itcm is lefi justificd wilhin ftc fi€ld (blank spaccs rcquired to Iill lhc minimum field-
widlh will be sdd€d a/r?r the data ilem ralicr lhan ,y'ore thc dala ilcm.)
A sign (cilhcr + or .) will prcccde.ach siSncd numcrical dala ilcm withoul lhis nag. only
ncgarivc dal, ilcms arc preceded by I sign
0 Causcs leading zcros lo appea, inslcad ofleading bl6nks. Applics only to data hcms that are
ri8hr justificd wilhin a ficld whosc mrnimum size is lar8.r lhrn l.h. data itcm.
(ffote Somc compilers consider thc zcro fla8 to bc a Parl of lhe l].ld'widlh specificalion
rathar thsn an actual oag. This assurcs lhsl lh€ o h proc.sscd last. if muhiplc flags arc
prescni.)
A blmk space will prccadc.ach posilivc siSncd num.rical da(a ilcn. Tlis fla8 rs ovcr-
Alamt tpoce) ridd.n by the + flag ifbolh at prcscnt.
i Causcs oclal nnd hcxadecimal dala ilcms to bc prcc€dcd by 0 alld 0x. respecliv€ly.
Causcs a decimal poinl lo b. pr.senl in all floalinS'point numbc.s. cv.n rflhe data ilem (*r/,
a-,1- and g- is a whole numb.r. Also prclcnls lhe lruncslion oftrailing ?.ros in o-lyp€ convelslon
int a;
short b;
Iong c;
unsigned d;
dooble x;
prlntf ('t+5d t+5hd t+121d t-12Iu t*15,7Ie\n', a, D, c, d, r)i
Appendix H
Library Functions
include Frlc
c!11(d double R.tum a valuc roundcd up lo thc ocxt highcr inlcgcr. oath.h
)
487
488 I,IBRARY FIINCTIONS [APP, H
tuncon lipe
fread(s,i1,i2,t) inl Enlel i2datailems.eachofsize-Ll hyles, from stdlo.h
Itle f lo slring s
lree(p) void Free ablock olallocaled memory whose beginning is malloc,h,or
indicared by p stdlib.h
fscanf(t,...) inl Entar data itcms from file f (remaining argumcnts arc atdio.h
complicrtcd - scc App.ndix C)
fseok(f,l,i) inl Move thc pointcr for fi la f a distancc I byles from stdio.h
localaon i (i ollhc filc,
may rcprcscnt lhc bc8innins
' tha cuncnt poinl€r posilion, or !h€ cod ofthc filc).
ftell(f) long int Rctum lhc currcnl poinlcr posilion within t. fil. stdio.h
lwrite(s,it,i2,f) int Scnd 12 dala alcms. cachofsizc il byles, from stdio h
strin8 s 1o filc 1
labs( 1) inl
lonS Retum lheabsolule valueofL
losld) double Rrrum Ihe naluml lo8arrlhm ofd matn, h
APP, H] LIDRARY FTINCTIONS 489
Noler: TWe rcfe.s@ tJrc daE rypc oftlc quanliry $st is retum.d by thc functioo An aslcrisl (.) denotes a pornte,
c dcnotcs a charactcr.typ. argumcnt
d denotcs a doublc.prectsion argumcnl
Python has a set of
Burt in Functions
Functlon Description
abs0 Returns the absolute value of a number
aul) Returns True if all itemslnan terable object aretrue
?-0J0
Returns True if any item n an ilerable object istrue
ascio Returns a readable vers on oF an oblect. Rep aces none_ascil characters wrth escape
!naBl) Returns the specified terator with the specified function appled to each rtem
max() Returns the largest item in an terable
mel]lgrf{icrv-0 RetLrrns a memory view object
min{) Returns the smallest iterr in an iterable
0er Returns the next item n an iterab e
obiect{) Returns a new object
oc Converts a nurnber into an octal
ope!0 Opens a file and returns a fle obiect
ordl) Convert an nleger representlng the unicode of the spec Fied character
Returns the value of x !o the power of y
ry" onnic r^ rhp <t:n.lar.l ortort d.vr.c
properiy0 Gets, sets, deletes a property
range0 Returns a sequence of numbers, starling from 0 and increments by 1 (by default)
rcp() Returns a readable version of an object
reversed{) Rcturns a reversed terator
round0 Rounds a nlmbers
sel0 tlctlrfs d ncw sct object
setattr0 Seis an attrlbute (propertv/rnethod) ofan obiect
sllceo Reiurns a slce ob]ect
sorted0 Returns a sorted list
stnt cnrethod0 Converts a method into a slat c method
nro Retlrns a str ng object
sumo Sums the items of an iterator
5!per0 Returns an obiecl that represents the parent class
tuple0 Returns a tuple
rype Returns lhe rype ol a'r oblecl
vars() Retlrns the dict . properiy of an object
lipo Returns an iterator, from two or more terators
pylhon has a scl of buill in methods that you can use on strinSs.
Notei All slring nrethods retLrns new values They do not change the oriSinal string.
Method Description
fapita!11C0 Converts the irrst character to upper case
casefold0 Converts string nto lower case
.^1to'l) perJ.'rs a ce_le-ed s ring
Python Fi e Methods
Method DescriPtion
close0 Closes the file
dctach0 Returns the separated raw stream from the buffer
fi enol) Returns a number that represents the stream, from the operating svstem's
perspectlve
flush0 llushes the nternal buffer
isattv0 Returns whether the file stream rs interact ve or not
read0 Returns the frle conient
rcadable0 Returns whether the file stream can be read or not
re.dl nc0 ll.Lurns onc rne from lhe frlc
rerdl nes{) rleilrrnsalsiof nesfromthef le
Method DescriPtion
Aplelldo Adds an element at the end of the list
cleaO Removes a I the elarnents from the I st
coPY() Returns a coPY of the list
counto Returns the number of clcment! wrth thc specrfrcd va ue
extend0 Add the elements of a list (or any iierab e), to lhe end ot the current
lst
index0 Returns the index of the f rst e ement l^'rth the speof ed value
inslftIl Adds an element at the specifled position
pop0 Removes the elemenl at the specifred position
remQveo Removes the first iiem with the spec fied value
reverse0 Reverses the order of the list
s!(11 Sorts the llst
items{} Returns a list containing a tuple for each key value pair
setdefaUlt()ReturnstheVa|ueofthespecfedkey,Ifthekeydoesnolexlst:nsertthekey,vr'thth..
sPecified value
update0 Updates the dictionary wth the spccificd kcy value pairs
Method Description
cmath.acos(x) Returns the arc cos ne value of x
.math a.nshlxl Returns the hvoerbolc arc cos ne ol x
Python Random Modu e
Method DescriPtion
!eed0 Init a ize the random number generator
getstate0 Returns the current nlernal state ofthe random nurnber generator
setstate0 Restores the lnternal state of the random number generator
getrandbits0 Returns a number representrng the random b ts
randrange0 Returns a random numbel" beillieen the g ven range
randint0 Returns a randorn number between the qrven range
choicgl] Returns a random element from the qiven sequence
choices0 Returns a list with a random se ect on from the qiven sequen'e
pos(url, data, ison, args) Sends a POST request lo the speclFied url
wl(url, data, arqs) Sends a PUT r€quest to the spcc fred ur
rcquesllmethod, url, args) Sends a request of the speciiied method to the spec fied url
statrstics.harmqnic rnean0 Ca culates the harmonic mean (central locatron) of the g ven data
statistiCs.ILeqt!-{) Calculates the mean (averaqe) of the given data
Calculates the median (middle value) of the given data
$a!$!!5.!Ediaa{)
the median of grouped continuous data
stat istics. redia nl(quPed.ll Calculates
Calc!lates the high median of the given data
$4!nie9.!oediao-bicl'11
statistics.median-low0 calculates the low median of the qiven data
statisti!s.ngde1) Calculates the mode (central tendency) of the given numeric or nominal data
statistics.PgtdevIl calculates the standard deviation from an entlrc populatlon
statistic!.stdev0 Calculates the standard deviation from a sample of data
s!a.!istiq.Pv.a-!!!qe0 Calculates the variance of an entire population
5tatistics.variance{) Calculates the variance from a sample of data