Python Session # 5 By: Saeed Haratian Spring 2016 Outlines - - PowerPoint PPT Presentation

python
SMART_READER_LITE
LIVE PREVIEW

Python Session # 5 By: Saeed Haratian Spring 2016 Outlines - - PowerPoint PPT Presentation

Fundamentals of Programming Python Session # 5 By: Saeed Haratian Spring 2016 Outlines Boolean Algebra Conditional Statement Chained conditionals Nested conditionals


slide-1
SLIDE 1

ميـــحرلا نحنحنرلا للوللوا مــسب

Fundamentals of Programming

Python

Session # 5

By: Saeed Haratian Spring 2016

slide-2
SLIDE 2

Outlines

 Boolean Algebra  Conditional Statement  Chained conditionals  Nested conditionals  Example : Bar Chart  Return Values  Example : Distance

slide-3
SLIDE 3

Boolean Algebra

 Boolean Value  Boolean Expression  Logical Operators  Opposite Operators  Boolean Operators  Truth tables

slide-4
SLIDE 4

Simplifying Boolean Expressions

x and False == False False and x == False y and x == x and y x and True == x True and x == x x and x == x

slide-5
SLIDE 5

Simplifying Boolean Expressions …

x or False == x False or x == x y or x == x or y x or True == True True or x == True x or x == x

slide-6
SLIDE 6

Simplifying Boolean Expressions …

De Morgan Law's not (x and y) == (not x) or (not y) not (x or y) == (not x) and (not y)

slide-7
SLIDE 7

Conditional Statement

slide-8
SLIDE 8

Conditional Statement …

if BOOLEAN EXPRESSION: STATEMENTS_1 else: STATEMENTS_2

slide-9
SLIDE 9

Omitting the else clause

slide-10
SLIDE 10

Chained conditionals

if x < y: STATEMENTS_A elif x > y: STATEMENTS_B else: STATEMENTS_C

slide-11
SLIDE 11

Chained conditionals …

slide-12
SLIDE 12

Nested conditionals

slide-13
SLIDE 13

Nested conditionals …

if x < y: STATEMENTS_A else: if x > y: STATEMENTS_B else: STATEMENTS_C

slide-14
SLIDE 14

The return statement

def print_square_root(x): if x <= 0: print("Positive numbers only, please.") return result = x**0.5 print("The square root of", x, "is", result)

slide-15
SLIDE 15

A Turtle Bar Chart

slide-16
SLIDE 16

Return Values

def area(radius): b = 3.14159 * radius**2 return b def area(radius): return 3.14159 * radius * radius

slide-17
SLIDE 17

Return Values …

def absolute_value(x): if x < 0: return -x else: return x def absolute_value(x): if x < 0: return -x return x

slide-18
SLIDE 18

Return Values …

def absolute_value(x): if x < 0: return -x elif x > 0: return x

slide-19
SLIDE 19

Return Values …

def find_first_2_letter_word(xs): for wd in xs: if len(wd) == 2: return wd return "" >>> find_first_2_letter_word(["This","is","a","parrot"]) ’is’ >>> find_first_2_letter_word(["I","like","cheese"]) ’’

slide-20
SLIDE 20

Example : Distance

def distance(x1, y1, x2, y2): dx = x2 - x1 dy = y2 - y1 dsquared = dx*dx + dy*dy result = dsquared**0.5 return result

slide-21
SLIDE 21

Example : Distance …

import math def distance(x1, y1, x2, y2): return math.sqrt( (x2-x1)**2 + (y2-y1)**2 )