PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Similar documents
PROGRAMMING FUNDAMENTALS

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Algorithms and Programming I. Lecture#12 Spring 2015

ENGR 101 Engineering Design Workshop

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Variables, expressions and statements

Conditional Expressions and Decision Statements

Flow Control. So Far: Writing simple statements that get executed one after another.

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Unit 3. Operators. School of Science and Technology INTRODUCTION

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

CSI33 Data Structures

Full file at

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Programming Lecture 3

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

Topic 2: Introduction to Programming

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Chapter 2, Part III Arithmetic Operators and Decision Making

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Variable and Data Type I

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Information Science 1

Learning the Language - V

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Sketchpad Graphics Language Reference Manual. Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Language Reference Manual simplicity

Full file at C How to Program, 6/e Multiple Choice Test Bank

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

CS1 Lecture 3 Jan. 18, 2019

Boolean Algebra A B A AND B = A*B A B A OR B = A+B

If Control Construct

1 Lexical Considerations

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

corgi Language Reference Manual COMS W4115

CS1 Lecture 3 Jan. 22, 2018

Python Unit

CS 1301 CS1 with Robots Summer 2007 Exam 1

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

DaMPL. Language Reference Manual. Henrique Grando

1/11/2010 Topic 2: Introduction to Programming 1 1

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

BASIC ELEMENTS OF A COMPUTER PROGRAM

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Variable and Data Type I

SMURF Language Reference Manual Serial MUsic Represented as Functions

The Warhol Language Reference Manual

Basics of Java Programming

Chapter 17. Fundamental Concepts Expressed in JavaScript

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Selection Statements. Pseudocode

Text Input and Conditionals

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Getting Started with Python

Course Outline. Introduction to java

REVIEW. The C++ Programming Language. CS 151 Review #2

PROGRAMMING FUNDAMENTALS

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Chapter 4: Control Structures I (Selection)

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

How to Think Like a Computer Scientist: Learning with Python 3 Documentation

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Information Science 1

COMP 110 Introduction to Programming. What did we discuss?

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Lexical Considerations

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Chapter 2. Lexical Elements & Operators

4. Inputting data or messages to a function is called passing data to the function.

The Practice of Computing Using PYTHON. Chapter 2. Control. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Lexical Considerations

Creating a C++ Program

Chapter 3: Operators, Expressions and Type Conversion

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

C++ Programming: From Problem Analysis to Program Design, Third Edition

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Pace University. Fundamental Concepts of CS121 1

Operators. Java operators are classified into three categories:

cs1114 REVIEW of details test closed laptop period

PROGRAMMING FUNDAMENTALS

The C++ Language. Arizona State University 1

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Lecture 3 Tao Wang 1

Transcription:

PRG PROGRAMMING ESSENTIALS 1 Lecture 2 Program flow, Conditionals, Loops https://cw.fel.cvut.cz/wiki/courses/be5b33prg/start Michal Reinštein Czech Technical University in Prague, Faculty of Electrical Engineering, Dept. of Cybernetics, Center for Machine Perception http://cmp.felk.cvut.cz/~reinsmic/ reinstein.michal@fel.cvut.cz

PROBLEM SOLVING! 2 Problem formulation (input / output) Formalism (math?) Algorithm (the idea!) Implementation (engineering) Testing (are we good?)

DATA TYPES 3 Integers (int) 1, 10, 124 Strings (str) Hello, World! Float (float) 1.0, 9.999 Strings in Python can be enclosed in either single quotes (') or double quotes ("), or three of each (''' or """) source http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html

VARIABLES 4 We use variables to remember things! The assignment statement gives a value to a variable Do not confuse = and ==! = is assignment token such that name_of_variable = value == is operator to test equality Key property of a variable that we can change its value Naming convention: with freedom comes responsibility! source http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html

VARIABLES cannot begin with a number 5 this $ is illegal character class is reserved keyword The longer life the longer name: very_long_name_of_my_var The more important the longer name Meaningful name does not add the meaning just by itself, the code must do this! Illegal name causes a syntax error Capitals: Variable vs variable source http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html

KEYWORDS 6 Python keywords have special purpose Always choose names meaningful to human readers Use comments (#) and blank lines to improve readability source http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html

BUILT-IN FUNCTIONS 7 Built-in functions have special purpose Study https://docs.python.org/3.4/library/functions.html source http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html

OPERATORS & OPERANDS 8 OPERAND OPERATOR OPERAND Operators are special tokens that represent computations like addition, subtraction, multiplication, division etc The values the operator uses are called operands When a variable name appears in the place of an operand, it is replaced with its value before the operation is performed Division / vs floor division // source http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html

TYPE CONVERSION 9 Functions, int(), float() and str() convert their arguments into types int, float and str respectively. The type converter float() can turn an integer, a float, or a syntactically legal string into a float The type converter str() turns its argument into a string One symbol can have different meaning depending on the data type(s) - try & explore & understand source http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html

ORDER OF OPERATIONS PEMDAS 10 Evaluation depends on the rules of precedence: 1. Parentheses (for order, readability) 2. Exponentiation 3. Multiplication and Division 4. Addition and Subtraction Order left-to-right evaluation on the same level, with the exception of exponentiation (**) source http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html

OPERATIONS ON STRINGS 11 You cannot perform mathematical operations on strings, even if the strings look like numbers The + operator represents concatenation, not addition The * operator also works on strings; it performs repetition (one of the operands has to be a string; the other has to be an integer) source http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html

INPUT 12 Built-in function to get input from a user: User input is stored as string Combine with type conversion input( Message to the user! ) source http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html

COMPOSITION 13 Combination of the elements of a program: variables, expressions, statements, and function calls One of the most useful features of programming languages Take small building blocks and compose them into larger chunks source http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html

MODULUS OPERATOR 14 The modulus operator works on integers (integer expressions) Definition: modulus is the remainder when the first number is divided by the second Modulus operator is a percent sign % Syntax is the same as for other operators The same precedence as the multiplication operator source http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html

THE FOR LOOP 15 The variable friend at line 1 is the loop variable Lines 2 and 3 are the loop body The loop body is always indented The indentation determines exactly what statements are in the body of the loop At the end of each execution of the body of the loop, Python returns to the for statement, to see if there are more items to be handled, and to assign the next one to the loop variable source http://openbookproject.net/thinkcs/python/english3e/hello_little_turtles.html

THE FOR LOOP 16 On each iteration or pass of the loop: Check to see if there are still more items to be processed If there are none left (the terminating condition of the loop) the loop has finished If there are items still to be processed, the loop variable is updated to refer to the next item in the list Program execution continues at the next statement after the loop body To explore: early break, or for else loop source http://openbookproject.net/thinkcs/python/english3e/hello_little_turtles.html

THE FOR LOOP CONTROL FLOW 17 Control flow (control of the flow of execution of the program) As program executes, the interpreter always keeps track of which statement is about to be executed Control flow until now has been strictly top to bottom, one statement at a time, the for loop changes this! source http://openbookproject.net/thinkcs/python/english3e/hello_little_turtles.html

BOOLEAN VALUES & EXPRESSIONS 18 Test conditions and change the program behavior depending on the outcome of the tests Boolean value is either True or False Named after the British mathematician, George Boole, who first formulated Boolean algebra source http://openbookproject.net/thinkcs/python/english3e/conditionals.html

BOOLEAN VALUES & EXPRESSIONS 19 Boolean expression is an expression that evaluates to produce a result which is a Boolean value Six common comparison operators which all produce a bool result (different from the mathematical symbols) source http://openbookproject.net/thinkcs/python/english3e/conditionals.html

LOGICAL OPERATORS 20 three logical operators, and, or, and not, that allow to build more complex expressions from simple Boolean expressions semantics (meaning) of these operators is similar to natural language equivalent source http://openbookproject.net/thinkcs/python/english3e/conditionals.html

TRUTH TABLES 21 Short-circuit evaluation: OR if the expression on the left of the operator yields True, Python does not evaluate the expression on the right AND if the expression on the left yields False, Python does not evaluate the expression on the right. Truth table list of all the possible inputs to give the results for the logical operators source http://openbookproject.net/thinkcs/python/english3e/conditionals.html

BOOLEAN ALGEBRA 22 source http://openbookproject.net/thinkcs/python/english3e/conditionals.html

CONDITIONAL EXECUTION 23 Condition IF ELSE Conditional statement the ability to check conditions and change the behavior of the program accordingly source http://openbookproject.net/thinkcs/python/english3e/conditionals.html

CONDITIONAL EXECUTION 24 Condition IF only No ELSE statement To control flow only for specific condition source http://openbookproject.net/thinkcs/python/english3e/conditionals.html

CONDITIONAL EXECUTION 25 Condition chaining IF ELIF ELSE Recommendation: handle all distinctive options by separate condition, use else to handle all other source http://openbookproject.net/thinkcs/python/english3e/conditionals.html

CONDITIONAL EXECUTION 26 Nesting conditions builds hierarchy of decisions (decision trees) Nesting may reduce readability and clarity source http://openbookproject.net/thinkcs/python/english3e/conditionals.html

CONDITIONAL EXECUTION 27 Early return / early break Can be used to speed-up code execution Special condition: FOR ELSE source http://book.pythontips.com/en/latest/for_-_else.html

BOOLEAN ALGEBRA LOGIC OPPOSITES 28 Each of the six relational operators has a logical opposite Recommendation: not operators may reduce readability, use logical opposites instead source http://openbookproject.net/thinkcs/python/english3e/conditionals.html

DE MORGAN S LAWS 29 De Morgan s laws rules allow the expression of conjunctions and disjunctions in terms of each other via negation Example: suppose we can slay the dragon only if our magic sword is charged to 90% or higher and we have 100 or more energy units in our protective shield source http://openbookproject.net/thinkcs/python/english3e/conditionals.html

DE MORGAN S LAWS 30 Example: suppose we can slay the dragon only if our magic sword is charged to 90% or higher and we have 100 or more energy units in our protective shield source http://openbookproject.net/thinkcs/python/english3e/conditionals.html

EXAMPLE 31 Example: complete the table.. source http://openbookproject.net/thinkcs/python/english3e/conditionals.html

REFERENCES 32 This lecture re-uses selected parts of the OPEN BOOK PROJECT Learning with Python 3 (RLE) http://openbookproject.net/thinkcs/python/english3e/index.html available under GNU Free Documentation License Version 1.3) Version date: October 2012 by Peter Wentworth, Jeffrey Elkner, Allen B. Downey, and Chris Meyers (based on 2nd edition by Jeffrey Elkner, Allen B. Downey, and Chris Meyers) Source repository is at https://code.launchpad.net/~thinkcspy-rleteam/thinkcspy/thinkcspy3-rle For offline use, download a zip file of the html or a pdf version from http://www.ict.ru.ac.za/resources/cspw/thinkcspy3/