Module 3.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Lecture 12 Recursion part 1
Python Programming Chapter 5: Fruitful Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Chapter 4: Conditionals and Recursion
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Python Control of Flow.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Programming for Linguists An Introduction to Python 24/11/2011.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 9 Iteration: Recursion 5/02/09 Python Mini-Course: Day 3 - Lesson 9 1.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
PhD, Senior Lecturer, Baimuratov Olimzhon A LGORITHMS & P ROGRAMMING (P YTHON ) Lecture 3 From SDU:
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
Algorithmic Recursion. Recursion Alongside the algorithm, recursion is one of the most important and fundamental concepts in computer science as well.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Programming in Python Part III Dr. Fatma Cemile Serçe Atılım University
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
CIT 590 Intro to Programming Lecture 3. Pair programming logistics You and your partner submit 1 assignment Figure out who the submitter will be PLEASE.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 10 Iteration: Loops 05/02/09 Python Mini-Course: Day 3 - Lesson 10 1.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.
Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Sections 4.1 & 4.2 Recursive Definitions,
Python Basics.
Introduction to Programming
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
Introduction to C++ Programming Language
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Methods Chapter 6.
Java 4/4/2017 Recursion.
Chapter 9 Recursion.
If, else, elif.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Computers & Programming Languages
Introduction to Programming
Repetition Control Structure
Unit 3 Test: Friday.
© 2016 Pearson Education, Ltd. All rights reserved.
Conditional and iterative statements
Recursion Taken from notes by Dr. Neil Moore
Recursion.
statement. Another decision statement, , creates branches for multi-
Control 9 / 25 / 19.
Lecture 6 - Recursion.
Presentation transcript:

Module 3

Conditional execution to check conditions and change the behaviour of the program accordingly. The simplest form is the if statement: if x > 0: print "x is positive" The boolean expression after the if statement is called the condition. If it is true, then the indented statement gets executed. If not, nothing happens. Synatx : HEADER: FIRST STATEMENT ... LAST STATEMENT

The header begins on a new line and ends with a colon (:) The header begins on a new line and ends with a colon (:). The indented statements that follow are called a block. The rest unindented statement marks the end of the block. A statement block inside a compound statement is called the body of the statement. There is no limit on the number of statements that can appear in the body of an if statement, but there has to be at least one.

Alternative execution A second form of the if statement is alternative execution, in which there are two possibilities and the condition determines which one gets executed. The syntax looks like this: if x%2 == 0: print x, "is even" else: print x, "is odd"

The return statement The return statement allows you to terminate the execution of a function before you reach the end. One reason to use it is if detecting an error condition import math def printLogarithm(x): if x <= 0: print "Positive numbers only, please." return result = math.log(x) print "The log of x is", result

Recursion a function to call itself. def countdown(n): if n == 0: print "Blastoff!" else: print n countdown(n-1)

def nLines(n): if n > 0: print nLines(n-1)

Recursion Recursion is a way of programming or coding a problem, in which a function calls itself one or more times in its body. Usually, it is returning the return value of this function call.

Termination condition: A recursive function terminates, if with every recursive call the solution of the problem is downsized and moves towards a base case. A base case is a case, where the problem can be solved without further recursion.

Example:  4! = 4 * 3! 3! = 3 * 2! 2! = 2 * 1 Replacing the calculated values gives us the following expression 4! = 4 * 3 * 2 * 1

Factorial def factorial(n): if n == 1: return 1 else: return n * factorial(n-1)

def factorial(n): print("factorial has been called with n = " +n) if n == 1: return 1 else: res = n * factorial(n-1) print("intermediate result for ", n, " * factorial(" ,n-1, "): ",res) return res print(factorial(5))

output

Fibonacci def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-1) + fib(n-2)

Power of a number def power(a,n) if n==0: return 1 else return a*power(a,n-1) power(2,3)