0% found this document useful (0 votes)
12 views24 pages

Expressions

The document discusses expressions in Python including defining expressions, variables, operators, order of evaluation, and type casting. Expressions combine values, variables, operators and functions to evaluate to a single value. The order of operations and operator precedence determine the order of evaluation in expressions.

Uploaded by

Peepeh Ette
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views24 pages

Expressions

The document discusses expressions in Python including defining expressions, variables, operators, order of evaluation, and type casting. Expressions combine values, variables, operators and functions to evaluate to a single value. The order of operations and operator precedence determine the order of evaluation in expressions.

Uploaded by

Peepeh Ette
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Expressions

Datec Learning Centers


EXPRESSIONS
• It is a combination of values, variables, operators, and calls to functions.

• Expressions need to be evaluated

• Interpreter evaluates the expression

Datec Learning Centers


EXPRESSION
• An expression is an instruction that combines values and
operators and always evaluates down to a single value.

• For example, this is an expression:

>>> 2 + 2

>>> 4

• 2s are integer values and the + is the mathematical operator. This


expression evaluates downDatec
toLearning
theCenters
single integer value 4.
EXPRESSIONS
Example:
a=5+2
type(a)
b=3
b *= 2

Datec Learning Centers


EXPRESSION
A variable is a memory location used to
x = 0.6
store a value (0.6).

0.6 0.6
x = 3.9 * x * ( 1 - x )

0.4

Right side is an expression. Once the 0.93


expression is evaluated, then its result is
placed in (assigned to) x.
Datec Learning Centers
EXPRESSION
A variable is a memory location used to
store a value. The value stored in a
variable can be updated by replacing the
old value (0.6) with a new value (0.93).

x = 3.9 * x * ( 1 - x )

Right side is an expression. Once


expression is evaluated, the result is placed 0.93
in (assigned to) the variable on the left side
(i.e. x).
Datec Learning Centers
Numeric Expressions
>>> xx = 2 >>> jj = 23
>>> xx = xx + 2 >>> kk = jj % 5
>>> print xx >>> print kk
4 3
>>> yy = 440 * 12 >>> print 4 ** 3
>>> print yy 64
5280
>>> zz = yy / 1000
>>> print zz
5
Datec Learning Centers
Order of Evaluation
❖ When we string operators together - Python must know which one
to do first, this is called “operator precedence”.

❖ Which operator “takes precedence” over the others.

x = 1 + 2 * 3 - 4 / 5 ** 6

Datec Learning Centers


Operator Precedence Rules
❖ Highest precedence rule to lowest precedence rule.

❖ Parenthesis are always respected.

❖ Exponentiation (raise to a power).

❖ Multiplication, Division, and Remainder. Parenthesis


Power
Multiplication & Division
❖ Addition and Subtraction.
Addition & Subtraction
Left to Right
❖ Left to right.
Datec Learning Centers
Order of Operations
• For mathematical operators, the order is PEMDAS

• Parentheses , Exponentiation, Multiplication and Division, Addition


and Subtraction

• Operators with the same precedence are evaluated from left to right.

Datec Learning Centers


Operator Precedence Example
1 + 2 ** 3 / 4 * 5
>>> x = 1 + 2 ** 3 / 4 * 5
>>> print x
11 1+8/4*5

1+2*5
Parenthesis
Power 1 + 10
Multiplication
Addition 11
Left to Right
Datec Learning Centers
Operator Precedence Example
1 + 2 ** 3 / 4 * 5
>>> x = 1 + 2 ** 3 / 4 * 5
>>> print x
11 1+8/4 *5
Note:
8/4 goes before 4*5 because
of the left-right rule.
1+2*5
Parenthesis
Power
Multiplication
1 + 10
Addition
Left to Right
Datec Learning Centers 11
EXPRESSION Example
>>> x=(a+b)
>>> a=5
>>> print(x)
>>> b=10
15
>>> c=3
>>> d=2
>>> x=(a+b)*(c*d)/e-f
>>> e=1
>>> print(x)
>>> f=9
81.0
Datec Learning Centers
Operator Precedence
❖ Remember the rules top to bottom.

❖ When writing code - use parenthesis.

❖ When writing code - keep mathematical expressions simple


enough that they are easy to understand.

❖ Break long series of mathematical operations up to make


them more clear.
x=1+2*3-4/5
x =Datec
? Learning Centers
TYPE CASTING

Datec Learning Centers


TYPE CASTING / TYPE CONVERSION
❖ Typecasting, or type conversion, is a method of changing an
entity from one data type to another.

Datec Learning Centers


TYPE CASTING / TYPE CONVERSION
>>> print (float(99) / 100)
❖ When you put an integer and 0.99
floating point in an expression >>> i = 42
>>> type(i)
the integer is implicitly
<type 'int'>
converted to a float. >>> f = float(i)
❖ You can control this with the >>> print f
42.0
built in functions int() and
>>> type(f)
float(). <type 'float'>
>>> print (1 + 2 * float(3) / 4 – 5)
Datec Learning Centers
-2.5
TYPE CASTING / TYPE CONVERSION
String Conversions:

❖ You can also use int() and float() to convert between strings and
integers.

❖ You will get an error if the string does not contain numeric
characters.

Datec Learning Centers


TYPE CASTING / TYPE CONVERSION
>>> sval = '123'
>>> type(sval) >>> print ival + 1
<type 'str'> 124
>>> print (sval + 1) >>> nsv = 'hello'
Traceback (most recent call last): >>> niv = int(nsv)
File "<stdin>", line 1, in <module>
Traceback (most recent call last):
TypeError: cannot concatenate 'str'
File "<stdin>", line 1, in <module>
and 'int'
ValueError: invalid literal for int()
>>> ival = int(sval)
>>> type(ival)
Datec Learning Centers
<type 'int’>
TYPE CASTING / TYPE CONVERSION

>>> number = int(input("Enter a number: "))

Enter a number: 3

>>> number

An integer 3
(no single quotes)!
>>>

Datec Learning Centers


TYPE CASTING / TYPE CONVERSION
>>> number = float(input("Enter a number: "))

Enter a number: 3.7

>>> number
Float
(no single quotes)! 3.7

>>>

Datec Learning Centers


TYPE CASTING / TYPE CONVERSION
>>> x = 10 >>> y = "20" >>> z = 30.0
>>> float(x) >>> float(y) >>> int(z)
10.0 20.0 30
>>> str(x) >>> int(y) >>> str(z)
'10' 20 '30.0'
>>> >>> >>>

integer ➔ float string ➔ float float ➔ integer


integer ➔ string string ➔ integer float ➔ string
Datec Learning Centers
TYPE CASTING / TYPE CONVERSION
>>> print(2 + 3.4)
❖The result of an expression that 5.4
involves a float number alongside >>> print( 2 + 3)
5
(an) integer number(s) is a float >>> print(9/5 * 27 + 32)
80.6
number.
>>> print(9//5 * 27 + 32)
59
>>> print(5.9 + 4.2)
10.100000000000001
>>>
Datec Learning Centers
TYPE CASTING / TYPE CONVERSION
>>> print(2 + 3.4)
❖ The result of an expression that involves 5.4
a float number alongside (an) integer >>> print( 2 + 3)
5
number(s) is a float number >>> print(9/5 * 27 + 32)
80.6
>>> print(9//5 * 27 + 32)
❖ The result of an expression that involves 59
values of the same data type will not result >>> print(5.9 + 4.2)
10.100000000000001
in any conversion. >>>

Datec Learning Centers

You might also like