COMP10001 Lecture Notes - Lecture 2: Substring, Type Conversion

50 views2 pages
Value is fixed
Invariant semantics
Literals
-
Possible to store values in variables via assignment
Used to name cells so we don't need to know their addresses
Variables
-
Literals and variables:
-
Value is assigned to the LHS
-
Assignment can only be to a single object on the LHS
-
Possible to assign the same evaluated result to multiple variables
-
Values of variables are changed only through re
-
assignment
-
Assignment:
Must start with a character or underscore
-
Must consist of only alphanumeric characters and underscores
-
Casing is significant
-
Reserved words cannot be used for variable names
-
Variable naming:
int()
-
float()
-
str()
-
complex()
-
Type conversion:
A string is a "chunk" of text
-
To include quotation marks and slashes, prefix them with
\
(escape them)
-
\
t = tab
\
n = new line
Special characters for formatting
-
Use triple quotes to avoid escaping
-
+ = concatenation
* = repeat string N times
in = subset
String operators
-
len() = calculate the length of the string
str() = convert an object to a string
Functions applicable to strings
-
Relative to its position from the left of the string (zero
-
offset)
Relative to its position from the right of the string ([minus] one
-
offset)
eg. [5], [
-
3]
Indexing = return the single character at a particular location
Specify a left and non
-
inclusive right int value
Substring length = R
-
L
Possible to specify slice direction with a third
-
1
eg. [1:10], [:
-
7], [:
-
3:
-
1]
Slicing = extract a substring of arbitrary length
Splitting = break up a string into components based on particular substrings
String manipulation
-
Strings:
Week 2
Thursday, 15 March 2018 11:21 AM
Foundations of Computing Page 1
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Possi(cid:271)le to sto(cid:396)e (cid:448)alues i(cid:374) (cid:448)a(cid:396)ia(cid:271)les (cid:448)ia assig(cid:374)(cid:373)e(cid:374)t. Used to (cid:374)a(cid:373)e (cid:272)ells so (cid:449)e do(cid:374)"t (cid:374)eed to k(cid:374)o(cid:449) thei(cid:396) add(cid:396)esses. Assig(cid:374)(cid:373)e(cid:374)t (cid:272)a(cid:374) o(cid:374)l(cid:455) (cid:271)e to a si(cid:374)gle o(cid:271)je(cid:272)t o(cid:374) the lhs. Possi(cid:271)le to assig(cid:374) the sa(cid:373)e e(cid:448)aluated (cid:396)esult to (cid:373)ultiple (cid:448)a(cid:396)ia(cid:271)les. Values of (cid:448)a(cid:396)ia(cid:271)les a(cid:396)e (cid:272)ha(cid:374)ged o(cid:374)l(cid:455) th(cid:396)ough (cid:396)e-assig(cid:374)(cid:373)e(cid:374)t. Must (cid:272)o(cid:374)sist of o(cid:374)l(cid:455) alpha(cid:374)u(cid:373)e(cid:396)i(cid:272) (cid:272)ha(cid:396)a(cid:272)te(cid:396)s a(cid:374)d u(cid:374)de(cid:396)s(cid:272)o(cid:396)es. Rese(cid:396)(cid:448)ed (cid:449)o(cid:396)ds (cid:272)a(cid:374)(cid:374)ot (cid:271)e used fo(cid:396) (cid:448)a(cid:396)ia(cid:271)le (cid:374)a(cid:373)es. To i(cid:374)(cid:272)lude (cid:395)uotatio(cid:374) (cid:373)a(cid:396)ks a(cid:374)d slashes, p(cid:396)efi(cid:454) the(cid:373) (cid:449)ith \ (cid:894)es(cid:272)ape the(cid:373)(cid:895) * = (cid:396)epeat st(cid:396)i(cid:374)g n ti(cid:373)es i(cid:374) = su(cid:271)set. Fu(cid:374)(cid:272)tio(cid:374)s appli(cid:272)a(cid:271)le to st(cid:396)i(cid:374)gs le(cid:374)(cid:894)(cid:895) = (cid:272)al(cid:272)ulate the le(cid:374)gth of the st(cid:396)i(cid:374)g st(cid:396)(cid:894)(cid:895) = (cid:272)o(cid:374)(cid:448)e(cid:396)t a(cid:374) o(cid:271)je(cid:272)t to a st(cid:396)i(cid:374)g. I(cid:374)de(cid:454)i(cid:374)g = (cid:396)etu(cid:396)(cid:374) the si(cid:374)gle (cid:272)ha(cid:396)a(cid:272)te(cid:396) at a pa(cid:396)ti(cid:272)ula(cid:396) lo(cid:272)atio(cid:374) Relati(cid:448)e to its positio(cid:374) f(cid:396)o(cid:373) the left of the st(cid:396)i(cid:374)g (cid:894)ze(cid:396)o-offset(cid:895) Relati(cid:448)e to its positio(cid:374) f(cid:396)o(cid:373) the (cid:396)ight of the st(cid:396)i(cid:374)g (cid:894)[(cid:373)i(cid:374)us] o(cid:374)e-offset(cid:895) eg. [5], [-(cid:1007)] Sli(cid:272)i(cid:374)g = e(cid:454)t(cid:396)a(cid:272)t a su(cid:271)st(cid:396)i(cid:374)g of a(cid:396)(cid:271)it(cid:396)a(cid:396)(cid:455) le(cid:374)gth. Spe(cid:272)if(cid:455) a left a(cid:374)d (cid:374)o(cid:374)-i(cid:374)(cid:272)lusi(cid:448)e (cid:396)ight i(cid:374)t (cid:448)alue.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents