Programming Training kiddo Main Points: - Python Statements - Problems with selections.

Slides:



Advertisements
Similar presentations
Python Basics: Statements Expressions Loops Strings Functions.
Advertisements

COMPSCI 105 S Principles of Computer Science
Recursion.
Recursion Definition: A method that calls itself. Recursion can be direct or indirect. Indirect recursion involves a method call that eventually recalls.
CPSC 320: Intermediate Algorithm Design & Analysis Divide & Conquer and Recurrences Steve Wolfman 1.
Program Design and Development
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Chapter 2: Algorithm Discovery and Design
Fundamentals of Python: From First Programs Through Data Structures
Programming Training Main Points: - Working with Functions in Python
Fundamentals of Python: First Programs
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Log on and Download from website:
Programming Training Main Points: - Lists / Arrays in Python. - Fundamental algorithms on Arrays.
Programming Training Main Points: - Python Statements - Problems with selections.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Programming Training kiddo Main Points: - Python Statements - Problems with selections.
Algorithmic Recursion. Recursion Alongside the algorithm, recursion is one of the most important and fundamental concepts in computer science as well.
Introduction to Computing Using Python Recursion and Algorithm Development  Introduction to Recursion  Recursion Examples  Run Time Analysis  Search.
L-Systems and Procedural Plants CSE 3541 Matt Boggus.
Infinities 6 Iteration Number, Algebra and Geometry.
CS 101: “If” and Recursion Abhiram Ranade. This week’ lab assignment: Write a program that takes as input an integer n and a sequence of numbers w1,w2,...,wn.
Python Programming in Context Chapter 9. Objectives To introduce functional programming style To practice writing recursive functions To introduce grammars.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Programming Training Main Points: - Problems with repetitions. - Discuss some important algorithms.
Main Points: - Python Turtle - Fractals
Agent P, I have been hearing some rumours about a Python Turtle.
Chapter 1. Objectives To provide examples of computer science in the real world To provide an overview of common problem- solving strategies To introduce.
Software Life Cycle What Requirements Gathering, Problem definition
ITEC113 Algorithms and Programming Techniques
Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control.
CS 121 Today Fractals and Turtles! The Koch Curve how random…
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 4 Conditionals Turtles.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Getting started with the turtle Find the latest version of this document at
Review Expressions and operators Iteration – while-loop – for-loop.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Turtle Graphics Lesson 2 1. There are 3 homeworks to complete during the six lessons of this unit. Your teacher will let you know when a homework has.
Functions. functions: a collection of lines of code with a name that one can call. Functions can have inputs and outputs.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Functions. functions – learning targets I will be able to understand how to trace function I will be able to learn how to lavage inputs and outputs with.
Main Points: - Python Statements - Problems with selections.
Recall: d is a divisor of n  n % d == 0
Main Points: - Python Turtle - Fractals
Fractals.
Module 3.
GC211Data Structure Lecture2 Sara Alhajjam.
Think What will be the output?
Introduction To Flowcharting
Functions.
Frozen Graphics Lesson 3.
Algorithms and Flow Charts
Exercise (1) What does function chai draw? def chai(size):
Coding Concepts (Basics)
Programming Training Main Points:
Programming Training Main Points: - Working with Functions in Python
Programming Training Main Points: - Problems with repetitions.
Recursion Definition: A method that calls itself. Examples
Main Points: - Python Turtle - Fractals
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Recursion Taken from notes by Dr. Neil Moore
Computer Graphics - Lecture 6
The structure of programming
Thinking procedurally
CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation
Presentation transcript:

Programming Training kiddo Main Points: - Python Statements - Problems with selections.

Python Blocks Several lines which are identically indented form a block. A block always starts after ‘:’ A block is usually required in functions and statements. INDENTATION MUST BE SIMILAR

Python tests - if statement. Choose between two choices if test : # block of if statement1; statement2; … else : # block of else statement 1; statement 2; … # end if Select one choice if test : # block of if statement1; statement2; … # end if Choose between multiple choices if test1 : # block of choice 1 statement1; statement2; … elif test2 : # block of choice 2 statement 1; statement 2; … elif test3 : # block of choice 2 statement 1; statement 2; … # end if

Minimum of two numbers. Example: Find the maximum/minimum value of a,b if a<b : max=b; min=a; else max=a; min=b; # endif

Python Repetitions. for repetitions execute repeatedly a statement while a condition holds or for a number of times. for elem in iterator : line1 line2 … # endfor For loop repeats statements for all elems in iterator. iterator is a set of elements that can be traversed. Block to repeat

Functions in Python A Python program is a sequence of a functions which can be executed. Function = Routine = Procedure = Method = Solution for a sub-problem. A Python function is written once and used/called as many times as needed. def function_name(arg1, arg2,…): statements to find the output; return output; # end function 1. function_name is the name of the function 2. arg1, arg2,…are the function arguments (usually the inputs) 3. the function body must find the output and return it.

Steps to work with functions 1. Define these functions you want to work with - Start from the arguments as inputs. - Give the sequence of statements to construct the output res. - Return the output res. 2. Call a function at any time you need. - The call is function_name(a1,a2,…) and returns a value that can be used. - a1, a2,… are the actual/current arguments 3. Actual arguments are NOT change after the call even if they change in the function

Python Turtle How to work with: 1.Make a pen object pen = Pen() 2.Set the pen features like color, width, etc pen.color(‘red’) pen.width(3) 3.Make you drawing using the move/draw functions pen.forward(100) pen.left(90)

Python Turtle – Draw a Random Circle 1.random a new module to generate random numbers random.randint(a,b)  generate a random integer between a and b random.random()  generate a random float between [0, 1) 2.Drawing a random circle - get a random radius - get a random color - get a random width - draw the circle

Turtle Random Art 1.Develop methods to draw random shapes 2.Drawing few similar random shapes in your screen 3.Draw 30 random circles + 20 random triangles

1. Define Recursively the figure F n. - Termination: give the shape of F 0 (point, line, etc) - Recursion: define F n based on F n Use a Turtle object to draw the figure. - The direction of the Turtle object must be preserved. Turtle Geometry represents the simplest way to construct geometrical fractals. Important Fractals: Trees, Koch’s curves, Sierpinski’s curves etc. Recursive / Turtle Graphics

12 The Binary Tree T(n,l) n is the order or age l is the length The Binary Tree

13 The Binary Tree T(n,l) n is the order or age l is the length T(0,l)= nothing T(n,l) is defined as follows: - construct the trunk - left 45 (PI/4) - construct T(n-1,l/2) - right 90 (PI/2) - construct T(n-1,l/2) - left 45 (PI/4) - go back at the root The Binary Tree

14 The Binary Tree

15 The Fern Tree F(n,l) -n - the order or the age -l – the length of the curve The Fern F(n,l)

16 The Fern F(n,l) -n - the order or the age -l – the length of the curve The Fern F(n,l) is a tree with 3 asymmetric branches. The Fern depends -the braches angles and the branches trunks The Fern F(n,l)

17 The Fern F(n,l) is defined by: -F(0,l) is a dot or nothing. -F(n,l) is recursively defined by: -Forward (0.3*l) -Left 55; Construct F(n-1, l/2);Right 55 -Forward (0.7*l) -Right 40; Construct F(n-1, l/2); Left 40 -Forward l -Left 5; Construct F(n-1, 0.8*l); Right 5 -Backward 2*l The Fern F(n,l)

18 def fern(n,l): if n==0 or l<2: return # endif pen.forward (0.3*l); pen.left(55); fern(n-1, l/2);pen.right(55); pen.forward (0.7*l); pen.right(40); fern(n-1, l/2); pen.left(40); pen.forward(l); pen.left(5); fern(n-1, 0.8*l); pen.right(5); pen.backward(2*l); } The Fern F(n,l)

To do List 1.Read more about if from the e-tutorial. 2.Solve the HW problems.