170 likes | 326 Views
Beginning Python Programming for Engineers. Introducing general concepts of prorgramming. Evaluation Criteria for Beginning C. Basic computer science Algorithm design Python Programming Debugging Professional conduct. What is Programming?. Given a problem:
E N D
Beginning Python Programming for Engineers Introducing general concepts of prorgramming
Evaluation Criteria for Beginning C • Basic computer science • Algorithm design • PythonProgramming • Debugging • Professional conduct
What is Programming? • Given a problem: • Make sure you understand the problem, i.e. It must be well-defined • Find or develop an algorithm to solve the problem • Express that algorithm in a way that the computer can execute it.
Algorithms • In simple terms, an algorithm is a sequence of instructions to solve a problem, such that: • Each instruction is unambiguous, and is something the computer can do. • After an instruction is finished, there is no ambiguity about which instruction is to be executed next. • Execution finishes in a finite number of steps. Think of the computer as a meticulous moron.
Development Cycle • Design • Edit • Compile and Link • Execute Log Entry describing first ever computer Bug
History of Processing • From the beginning, Processing was designed as a first programming language. It was inspired by earlier languages like BASIC and Logo, as well as our experiences as students and teaching visual arts foundation curricula. The same elements taught in a beginning high school or university computer science class are taught through Processing, but with a different emphasis. Processing is geared toward creating visual, interactive media, so the first programs start with drawing. Students new to programming find it incredibly satisfying to make something appear on their screen within moments of using the software. This motivating curriculum has proved successful for leading design, art, and architecture students into programming and for engaging the wider student body in general computer science classes.
Processing and Computer Engineering • The Processing approach has also been applied to electronics through the Arduino and Wiring projects. Arduino uses a syntax inspired by that used with Processing, and continues to use a modified version of the Processing programming environment to make it easier for students to learn how to program robots and countless other electronics projects.
Processing Programs on the Computer • Processing Code is stored in files on the computer. It must have the suffix .pyde. • Use the processing editor to edit or create this file. (Could open it in notepad) • Execute or run the code
Building Blocks in Python • Python gives us several building blocks for constructing programs. • Variables and arithmetic expressions • Conditional execution of statements • Controlled repetition of statements • Functions (sub programs) • Printing, getting mouse input • Much more
Our first program • We want a program to print the message ”Hello World”.
Displaying Words in The Gui Window • The text() function is used for writing words to the screen. The letters can be place anywhere on the screen using coordinates. (0,0) top left corner. size(600, 600); background(101); # Set background to gray font = createFont("Sans Serif",12); textFont(font); # the text font will be Serif, size 24 fill(0); #change the color of text to black text("HELLO WORLD",100,100); # Draw text more accurately and efficiently.
Drawing Two Lines # Set the size of the graphical output screen to a width and a height of 200 pixels. size(200, 200); background(102); # Set the background color to greyvalue 102 # Draw two lines. line(0, 0, 200, 200); # Top-left to bottom-right line(0, 200, 200, 0); # Bottom-left to top-right
VAriables • Variables are used for storing values • When creating a variable • Be descriptive • Use single words • Be case sensitive • Be manageable – long enough to be readable, not so long that you can no longer type them.
Assignment Statements • a = 5 • The above is a assignment statement, do not confuse with an equation. • Assignment statements can only have one item on the left of the equal sign. • Example1 a + 5 = 10 is not a legal computer line of code. • Example 2 a = 5 + 10 is a legal computer line of code, a has the value 15.
Variables Example • size(200, 200); • background(0); • stroke(153); • a = 20; • b = 50; • c = a*8; • d = a*9; • e = b-a; • f = b*2; • g = f+e; • line(a, f, b, g); • line(b, e, b, g); • line(b, e, d, c); • line(a, e, d-e, c);