3. 1.0 Python numeric types
Basic operation ( + , - , * , / )
To get the accurate
answer, write in a
decimal point.
Because number
without decimal
point is consider as
integer, a whole
number.
Two same function, but
different approach. Print
function will round-off
the nearest number.
Izyan Yasmin 2017
4. 1.1 Assignment statement
Name can be attach to a value and the value will stay around for the rest of
conversational Python session.
Python names must start with a letter or the underbar(_)character.
Examples:
How many seconds in 1 days?
Izyan Yasmin 2017
5. General form of an assignments
𝑛𝑎𝑚𝑒1 = 𝑛𝑎𝑚𝑒2 = ⋯ = 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛
It gives the same
output, since it
carries the same
expression.
Izyan Yasmin 2017
6. 2.0 Character String Basics
In python you can choose to enclose strings either using single-quotes (‘…’)
or double-quotes (“……”)
Izyan Yasmin 2017
Print function
will output
the string
without the
quotes.
8. Python has several escape sequences.
Izyan Yasmin 2017
If a line is not
finished,
python will
displays a
“….” prompt. Using print
function, will
gives the
exact output
in 3 lines.
It will read
directly the line
and output using
the special
escape character
n
9. 2.1 indexing strings
To extract one or more characters from a string value, you need to know the
position. The diagram shows the position between characters, which its
starts before the first to the end of strings.
Izyan Yasmin 2017
10. 2.2 String methods
Method General expression Examples
center()
To center text
s.center(n)
.ljust() and .rjust()
Pad to length on left or
right
s.ljust(n)
s.rjust(n)
.strip(), .lstrip() and
.rstrip()
Remove leading and/or
trailing whitespace
s.strip()
s.lstrip()
s.rstrip()
count()
Search string s to see
how many times t occur
in string s
s.count(t)
Izyan Yasmin 2017
Many expression on strings are expressed as method. To call a method, we can use this syntax:
• expr.method(𝑎𝑟𝑔1, 𝑎𝑟𝑔2, … )
If length isn’t enough, it
will return the original
values.
11. Izyan Yasmin 2017
Method General expression Examples
.find() and r.find()
Locate string within a
longer string
s.find(t)
.startswith() and
.endswith()
To check if a string s
starts with a string t
s.startswith(t)
s.endswith(t)
.lower() and .upper()
Used to convert
uppercased characters to
lower and vice-versa
s.lower()
s.uppper()
.split()
Break field out of a
strings
s.split()
12. 2.3 String format method
To combine string operations is to combine fixed text and variable values
into single string.
General string of format operations:
s.format(𝑝0, 𝑝1, … , 𝑘0 = 𝑒 𝑜, 𝑘1 = 𝑒1)
Formatting of an item can be control using a format code of the form “{N:type}”, N is
number of arguments to the . 𝑓𝑜𝑟𝑚𝑎𝑡() method, and type specifies details of the
formatting.
Izyan Yasmin 2017
13. 3.0 Sequence types
A sequence in Python represents an ordered set.
str and unicode used to hold text, that is strings characters.
list and tuple used for sequence of zero or more values of any type.
Use list if the content of the sequence may change. Enclosed with square bracket “[…]”.
[𝑒𝑥𝑝𝑟1, 𝑒𝑥𝑝𝑟2, … . ]
Use tuple if the contents may not change. Enclosed in parentheses “(….)”
(𝑒𝑥𝑝𝑟1, 𝑒𝑥𝑝𝑟2, … . )
Izyan Yasmin 2017
14. 3.1 Function and operators
Description Operators Example
Return numbers of elements
in a sequence s
len(s)
Return largest value in a
sequence s
max(s)
Return smallest value in
sequence s
min(s)
To test set of membership in
To concatenate two sequence
of the same type
+
To get a new sequence
containing n repetition of
elements of s
*
Izyan Yasmin 2017
15. 3.2 Indexing the position in a sequence
Positions in a sequence refer to locations between the values, using an
expression s[i].
Izyan Yasmin 2017
Last line is an error because there is
nothing in position 12 in string fruits.
16. 3.3 Slicing sequences
Izyan Yasmin 2017
Types of slicing Descriptions Examples
S[B:E] Produces new sequences
S[B:] Get list on left hand-side
S[:E] Get list on right hand-side
S[B:E]=[]
del S[B:E]
Delete a slice from
sequences
17. 3.4 Sequence methods
To find the positions of a value V, in a sequence S.
S.index(V)
Returns the number of elements S that are equal to V.
S.count(V)
Izyan Yasmin 2017
18. 3.5 List Method
For any instances L, the.append(V)method appends a new value to the
list
Insert new value, V at an arbitrary position, P using this expression
L.insert(P,V)
Izyan Yasmin 2017
19. Method L.remove(V) removes the first element of L that equals V, if
there is one. If no elements equal to V, method raises a ValueError
exception.
Izyan Yasmin 2017
20. L.sort(), method sorts the elements of a list into ascending order.
L.reverse(), which reverses the elements in place.
Izyan Yasmin 2017
21. 3.6 the range() function: creating arithmetic
progressions
range(n), return a list containing an arithmetic progression.
To generate a sequence [i, i+1, i+2, ..., n-1], use the form range(i, n)
To generate an arithmetic progression with a difference d between successive
values, use the three-argument form range(i, n, d). The resulting sequence will be [i,
i+d, i+2*d, ...], and will stop before it reaches a value equal to n.
Izyan Yasmin 2017
22. 3.7 One value can have multiple names
It is necessary to be careful when modifying mutable values such as list
because there may be more than one name bound to the value.
𝑉1, 𝑉2 = … . . = 𝑒𝑥𝑝𝑟
We can make a new list using a slice that selects all the elements of menu1.
Izyan Yasmin 2017