0% found this document useful (0 votes)
20 views

Lecture 4

The document discusses various Python programming concepts including arithmetic operators, data type conversions, strings, and the math library. It covers topics like operator precedence, shortcut operators, converting between data types, accessing and manipulating string values, escape sequences, and using functions from the math module.

Uploaded by

Rehab Hassan
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lecture 4

The document discusses various Python programming concepts including arithmetic operators, data type conversions, strings, and the math library. It covers topics like operator precedence, shortcut operators, converting between data types, accessing and manipulating string values, escape sequences, and using functions from the math module.

Uploaded by

Rehab Hassan
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

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

than those with lower precede.


E.g. x + y * z means x + (y * z) because the *
operator has higher precedence than the + operator.
 The associativity of an operator indicates whether it
groups left to right / right to left. All operators in Python
have left to right associativity except exponentiation,
which has right to left associativity.
E.g., the - operator binds left to right. Therefore, x - y - z
means (x - y) - z. But the ** operator binds right to left,
and x ** y ** z means x ** (y ** z).

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

the end of a string.


name = "Agent " + 1729
# Error: Can only concatenate strings
Because string concatenation can be performed between
two strings, we must first convert the number to a string.
name = "Agent " + str(1729)
 str function can also be used to convert a floating-point

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

string "Hello" is a sequence of five characters.


 Examples:

print(“Hello”) String is “Hello”


greeting = “Hello” String is “Hello”
print(greeting) will print “Hello”
 We use double quotation marks “ ” around strings

because this is a common convention in many other


programming languages. However, interactive Python
interpreter displays strings with single quotation mark ‘ ‘

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

 In Python, use + operator to concatenate two strings. For


example,

 You can produce a string that is the result of repeating a


string multiple times. For example, suppose you need to
print a dashed line. Instead of specifying a literal string
with 10 dashes, you can use the * operator to create a
string that is comprised of the string "-" repeated 10
times. For example

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 (\)

 Another common escape sequence is \n, which denotes a


newline character, causes start of a new line on display.

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

import math at the top of the python code.


 Or, to include certain function inside math (as

sqrt), write from math import sqrt


 To call sqrt function, write y = sqrt(x)

26
27
28
29
Call function with exact number of argument

If calling abs(-10,2) or abs( ) ,it displays error message

30
31

You might also like