FIT1045 Lecture Notes - Lecture 1: .Int, Substring, Empty String

249 views4 pages
Useful'commands
!"##$%&'()*+,)-+%.(-"*/0%'(&$$(1i/J2
3$(04&%+5&(%0+67(z.real
3$(04&%+5&()*+,)-+%.7(z.imag
8
9$**0-&:(#
;"&#"&:(print()
\n\ 1-0<(6)-02(='#6)&'(6$-,(6)-0'($>(&04&()-&$('0#+%+&0(6)-0'?(@$$A()>(.$"(<+-&(
)-A0-&B#+%+,%+#C)-,?
DDD!#+50'(5$"-&(+'(5C+%+5&0%'7(0'#(<C0-(>)-A)-,(60-,&C($>('&%)-,'?
Mathematical'ops
// EEE( )-&0,0%(A)F)')$-
%EEE( ,)F0'(%0*+)-A0%(+>&0%(A)F)')$-
G,?(HIJ(KK(L
abs(x) EEE(+/'$6"&0(F+6"0(1+M+(*$A"6"'2
** EEE( #$<0%
Built-in'math'functions
>>> import math NO!3(A$(&C)'(>)%'&(/0>$%0("')-,(+-.($>(&C0(/06$<P
>>> math.
sqrt(x), factorial (x)
8
ceil (x), floor (x)
8
cos, sin, tan, exp, log
Calculates in RADIANS!!
8
pi, e
8
**don't use f(x) as a var name!
int() =*+M0'(+('&%)-,(+-()-&0,0%
float() =*+M0'(+('&%)-,(+(-"*/0%(1-$(-00A()-&0,0%2
Q+F0(&$(import random +6'$?(R-A(random.SS
uniform(1,10) -random float 1 <= x < 10
8
randint(1,10) -random integer frm 1-10
8
randrange(0,101,2) -even integer frm 0 to 101
8
choice('afvfd') -chooses a random element
8
shuffle([5,6,7]) -shuffles elements
8
sample([4,5,6,7],2) -chooses 2 elements
8
String'methods
<str>
str.capitalize() =%0&"%-'(+(5$#.($>(&C0('&%)-,(<)&C()&'(>)%'&(5C+%+5&0%(
5+#)&+6)'0A(+-A(&C0(%0'&(6$<0%5+'0A
<str>.lower() =6$<0%=5+'0'(0F0%.(60&&0%
<str>.upper() ="##0%=5+'0'(0F0%.(60&&0%
str.replace(old, new[, countT2 =%0&"%-'(+(5$#.($>(&C0('&%)-,(<B(+66(
$55"%%0-50'($>('"/'&%)-,($6A(%0#6+50A(/.(-0<?(U>($#&)$-+6(+%,"*(5$"-&()'(,)F0-7(
$-6.(>)%'&(5$"-&($55"%%0-50'(+%0(%0#6+50A?
10,?(
>>> text = 'a piece of string'
>>> text.replace('tring','heet')
'a piece of sheet'
DD&C0(5$-&0-&($>(F+%)+/60(text A$0'-V&(5C+-,0P
len() =%0&"%-'(60-,&C($>(+('&%)-,
W$%A('#6)5)-,(X)-A04($>(>)%'&(60&&0%:('&$#(/0>$%0(&C0(60&&0%($>(&C)'()-A04T
>>> word = "sitting"
sitting
word[0]
s
word[1:3]
it
word[3:6]
Tin
word[:2]
si
word[2:]
tting
word [:2] + "nning"
sinning
"splat" + word[4]
splati
word[-1] =)>(=F0(-"*/0%(E(5C+%+5&0%'7($M?(;&C0%<)'0(-$?
g
word[1:100] =)>(-$("##0%(6)*)&7(-$&C)-,?
itting
Y===Y===Y===Y===Y===Y
ZQ(Z0(Z6(Z#(ZR(Z
Y===Y===Y===Y===Y===Y
L((([(( (\(((J(((](((^
=^(( =]((=J(( =\((=[
Others
.count
.int() ='&%)-,(==E()-&0,0%
.float() ='&%)-,(==E(>6$+&
.str() =)-&0,0%B>6$+&(==E('&%)-,
.strip() =
Python
@0-0%+6(#"%#$'0
G+'.(&$(#%$,%+*7(C+'(A0#&C()>(-00A0A
N"6&)=#+%+A),*:(;=;7(>"-5&)$-+67(
+-A()*#0%+&)F0
R''$5)+&0A(#%$,%+**)-,(5$-50#&'(
6)M0($/_05&'7(C),C0%($%A0%7(0&5?
U-#"&B;"&#"&(strings
>>> yourName = input("Enter your name:")
Enter your name: [superman]
>>> print("Hello " +
yourName.capitalize())
Hello Superman
U-#"&B;"&#"&(numbers
>>> userNum = input("Enter a number: ")
Enter a number: [3]
>>> userNum = float(userNum)
>>> userNumSqrd = userNum * userNum
>>> print("The square of your number is
" + str(userNumSqrd())
The square of your number is 9
Indexing
XT(>$%()-A040'B+AA%0'')-,P
=F0(-"*/0%'(+6'$($M?(`"&(5+--$&(04500A(
-"*/0%($>(5C+%+5&0%'?(XL:LT(0*#&.('&%)-,?
if a4a(in a&A,A4a:
print("Yes")
word.count(sub[start[,end]])
9$"-&'(-"*/0%($>(50%&+)-(60&&0%(1sub2()-(
'&%)-,(1word2
8
R6,$%)&C*(K(+(<+.(&$(+5C)0F0(+(&+'MB#%$A"50('$*0&C)-,
K(+(>)-)&0('0b"0-50($>('&0#' <%)&&0-(>$%(+-(+,0-&(10,?(
5$*#"&0%7(5C0>2(&$('$6F0(+(#%$/60* <)&C(&C0(>$66$<)-,(
#%$#0%&)0':
c0>)-)&0-0'' =0+5C('&0#(*"'&(/0(#%05)'0(+-A(
"-+*/),"$"'7('#05)>)5(>$%(&C0(+,0-&
8
U-#"& =$#&)$-+68
;"&#"& =#%$A"5&(must-have8
G>>05&)F0-0'' ='">>)5)0-&6.(/+')5('$(&C0.(5+-(/0(A$-0(
04+5&6.(+-A()-(+(>)-)&0(60-,&C($>(&)*0(/.(+,0-&
R,0-&(-00A'(&$(M-$<(C$<(&$(,0&(&C0%0
8
d)-)&0-0'' =must'terminate +>&0%(+(>)-)&0(-$?($>('&0#'
W)&C$"&(&C)'7(<$"6A(/0(+(#%$50''
e$&(0-$",C('$*0&)*0'(>$%(+,0-&(&$(A05)A0(
#0%'$-+66.(<C0-(&$('&$#?(Q+'(&$(/0(cGdUeGc
8
10,?(%05)#07(-+F),+&)$-7()-'&+66+&)$-(,")A07(#%$,2
Variables
N"'&('&+%&(<)&C(+(60&&0%7(-"*/0%'(5+-(/0(+>&0%<+%A'
e;('#05)+6B>$%0),-(5C+%+5&0%'B'#+50'8
Df($M(+'(6$-,(+'(-$&(>)%'&(5C+%+5&0%8
9+'0='0-')&)F0
3$(A0>)-07(_"'&(&.#0(-+*0(+-A(,)F0()&(+(F+6"0?(e$(-00A(
var?
DD9+-(*"6&)#6.(stringsP(10,?(
EEE(A)>>304&(K(3*text.replace('a
piece of', 'not another')
not another stringnot another
stringnot another string'
'&%)-,(5$-5+&0-+&)$-(<$%M'(C0%0(&$$?(R6'$(
"'0'(b"$&0'(&$(0-56$'0('&%)-,'?
DD'&%)-,'(+%0()**"&+/60(1"-+/60(&$(
5C+-,02
Week$1
!+&"%A+.7(\\(g"6.(\L[h
[]:[h
Unlock document

This preview shows page 1 of the document.
Unlock all 4 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Associated programming concepts like objects, higher order, etc. \n\ (new line) - splits long lines of text into separate lines. ***spaces count as characters, esp when finding length of strings. 9%3 == 0 abs(x) >>> absolute value (aka modulus) >>> import math must do this first before using any of the below! >>> math. sqrt(x), factorial (x) ceil (x), floor (x) cos, sin, tan, exp, log. **don"t use f(x) as a var name! int() - makes a string an integer float() - makes a string a number (no need integer) str. capitalize() - returns a copy of the string with its first character capitalised and the rest lowercased. . upper() - upper-cases every letter str. replace(old, new[, count]) - returns a copy of the string w/ all occurrences of substring old replaced by new. If optional argum count is given, only first count occurrences are replaced. (eg. >>> text = "a piece of string"

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents