Lecture 4
Lecture 4
Arithmetic operators
Operators precedence
Data Types Conversions
Strings
Math Library
2
Basic Arithmetic Operators
Addition + (5 + 2 = 7)
Subtraction - (5 - 2 = 3)
Multiplication * (5 * 2 = 10)
Float Division / (5 / 2 = 2.5)
Integer Division // (5 // 2 = 2) (floor division)
Power ** (5 ** 2 = 25)
Reminder % (5 % 2 = 1)
3
Basic Arithmetic Operators
4
Shortcut Operators
Operator Equivalent to
X += Y X= X+Y
X-=Y X= X-Y
X*= Y X= X*Y
X/= Y X= X/Y
X //= Y X = X // Y
X **= Y X = X ** Y
X%= Y X= X%Y
5
6
Python operators are listed in groups of decreasing
precedence in table (next slide).
Operators with higher precedence bind more strongly
7
Operator Description
( ) Parenthesis
** Exponentiation
+, - Sign positive, sign negative
* Multiplication
/ Float Division
// Integer (floor) Division
% Floor remainder
+ Addition
- Subtraction
= Assignment
8
1 2
9
10
11
Sometimes it is necessary to convert a numerical value to
a string.
For example, suppose you need to append a number to
value to a string
12
Similarity, use int ( ) to convert string to an integer
value
Use float ( ) to convert string to a float value
13
Using type function: to know variable type
14
Many programs process text. Text consists of characters:
letters, numbers, punctuation, spaces, and so on.
A string is a sequence of characters. For example, the
15
A string literal denotes a particular string (such as
"Hello“). In Python,
String literals are specified by enclosing a sequence of
characters within a matching pair of either single or
double quotes.
16
Length, concatenation and repetition:
Number of characters in a string is called the length of the
string. For example, the length of "Harry" is 5
17
Length, concatenation and repetition:
What if you’d like the first and last name separated by a space?
name = firstName + " " + lastName
This statement concatenates three strings: firstName, the string
literal " ", and lastName.
The result is Harry Morgan
A string of any length can be repeated using the *
operator. For example, the statements
message = "Echo..."
print(message * 5)
The result is
Echo...Echo...Echo...Echo...Echo...
18
Access Characters within Strings
Strings are sequences of Unicode characters. You can
access the individual characters of a string based on their
position within the string. This position is called the
index of the character.
For example:
19
Strings Operations
20
Strings Methods
1. Object is a software entity that represents a value with certain
behavior. The value can be simple, such as a string, or complex, like
a graphical window or data file.
2. Behavior of an object is given through its methods where dot (.)
separates the object and method name.
3. A method, like a function, is a collection of programming
instructions that carry out a particular task. But unlike a function,
which is a standalone operation, a method can only be applied to an
object of the type for which it was defined.
21
Strings Methods
The value can be simple, such as a string, or complex, like a graphical
window or data file.
22
Character values and escape sequences
Python provides two functions related to
character encodings:
The ord function returns the number used to represent
a given character.
The chr function returns the character associated with a
given code.
For example,
23
Character values and escape sequences
When you need to include both single and double quotes in
a literal string.
For example, to include double quotes around the word
welcome in string “You’re Welcome”, precede quotation
marks with a backslash (\)
24
Python contains a standard library that can be used to
create powerful programs.
A library is a collection of code that has been written and
translated by someone else, ready for you to use in your
program.
A standard library is a library that is considered part of
the language and must be included with any Python
system.
Python’s standard library is organized into modules.
Related functions and data types are grouped into the
same module.
Functions defined in a module must be explicitly loaded
into your program before they can be used.
25
Python’s math module includes a number of
mathematical functions. To use any function
from this module, you must first import the
function.
To include all functions defined in math, write
26
27
28
29
Call function with exact number of argument
30
31