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

Slides:



Advertisements
Similar presentations
Chapter 4 Computation Bjarne Stroustrup
Advertisements

CMSC 104, Version 9/011 Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental.
Types and Arithmetic Operators
Java Planning our Programs Flowcharts Arithmetic Operators.
Lecture 03 – Sequences of data.  At the end of this lecture, students should be able to:  Define and use functions  Import functions from modules 
True or false A variable of type char can hold the value 301. ( F )
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
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.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
COMPSCI 101 Principles of Programming
Python.
Python  By: Ben Blake, Andrew Dzambo, Paul Flanagan.
Programming Training Main Points: - Lists / Arrays in Python. - Fundamental algorithms on Arrays.
Munster Programming Training
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
Computer Science 101 Introduction to Programming.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Computing with Numbers Zelle - Chapter 3 Charles Severance - Textbook: Python Programming: An Introduction to Computer Science, John.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Programming Training kiddo Main Points: - Python Statements - Problems with selections.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Programming Training Main Points: - Problems with repetitions. - Discuss some important algorithms.
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
CPS120: Introduction to Computer Science Operations Lecture 9.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Python – Part 3 Functions 1. Getting help Start the Python interpreter and type help() to start the online help utility. Or you can type help(print) to.
Program Development C# Programming January 30, 2007 Professor J. Sciame.
Python Functions : chapter 3
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Doing math In java.
CS 141 Computer Programming 1 Branching Statements.
INVITATION TO Computer Science 1 11 Chapter 2 The Algorithmic Foundations of Computer Science.
Controlling Program Flow with Decision Structures.
Programming Training kiddo Main Points: - Python Statements - Problems with selections.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 3. Time Complexity Calculations.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
Fundamental Programming Fundamental Programming Data Processing and Expressions.
Introduction to Programming
G. Pullaiah College of Engineering and Technology
Fundamentals of Programming I Overview of Programming
Main Points: - Python Statements - Problems with selections.
Introduction to Python
1-1 Logic and Syntax A computer program is a solution to a problem.
ALGORITHMS AND FLOWCHARTS
Presented By S.Yamuna AP/IT
Engineering Problem Solving with C++, Etter/Ingber
Functions CIS 40 – Introduction to Programming in Python
Arithmetic operations, decisions and looping
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
Programming Training Main Points: - Problems with repetitions.
Introduction to Programming Using Python PART 2
Terminal-Based Programs
COMPUTER PROGRAMMING SKILLS
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
More Basics of Python Common types of data we will work with
Python Basics. Topics Features How does Python work Basic Features I/O in Python Operators Control Statements Function/Scope of variables OOP Concepts.
Presentation transcript:

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

print() print(value1, value2,…) print(“var = ”, var1) # prints the text ‘var=‘ followed by the value of var print(‘Here is a text’) # prints the text ‘Here is a text’

input() input() returns a str value which must be converted var = input() # reads a str value for var var = float(input(‘Input var: ’)) # write the text ‘Input var: ’, then reads a str and converts that to float var1, var2 = map(int, input(‘Input var1, var2’).split()) # reads two values, split them into a list and # then map the list values to var1, var2 as int

Some rules import math def printFunction(): print(‘\n\n**************\n\n’) a = int(input(‘a = ‘)) b = int(input(‘b = ‘)) c = a+b print(‘c=‘, c) print(‘\n\n**************\n\n’) Read an int variable var: var = int(input(‘var = ‘)) Write a variable var: print(‘var = ‘,var); Write a text: print(”My Text \n”); Write a line of start: printf(”\n\n\n******** \n\n\n”);

C Arithmetic Arithmetic operators: -+, -, *, /, ** -++, -- for incrementing and decrementing. -% is the remainder of the integer division. Relational operators: -, =, ==, != Logical: or, and, not There are several math functions in the math module math.sqrt(), math.sin(), math.cos(), math.asin(), math.acos() etc.

How to solve a problem 1.Find the inputs and outputs of the problem. 2.Ask yourself if you have done something similar. 3.Identify the elements of the solution. 4.Take a numerical example and see the changes of the variables. 5.Write the code.

Inputs - Outputs Inputs are the elements / variables to start from. - Inputs must be read. - Input can be arguments within functions. Output(s) is represented by the problem result. - Outputs must be printed. - Outputs can be returned by a function.

The Process of Solving 1.Ask yourself if you have done something similar. 1.Solve the problem mathematically. 2.Identify the elements of the solution: 1.Break the problem into small blocks that you know how to. 2.Write functions / find snippets of code for each block. 3.Find the calculations / selections / repetitions 3.Write a pseudo-code in plain English.

Solving a triangle z

Solving a triangle – Pseudo-code z

Solving a triangle # import modules import math # define functions def solveTriangle(): # read inputs a = float(input('a = ')) b = float(input('b = ')) c = float(input('c = ')) # calculate the angles in radians A = math.acos((b**2+c**2-a**2)/(2*b*c)) B = math.acos((a**2+c**2-b**2)/(2*a*c)) C = math.acos((a**2+b**2-c**2)/(2*a*b)) # translate the angles in degree degreeA = A*180/math.pi degreeB = B*180/math.pi degreeC = C*180/math.pi # calculate the perimeter and area per = a+b+c area = b*c*math.sin(A)/2 # print results print('Angles are: ', degreeA, degreeB, degreeC) print('Perimeter is: ', per) print('Area is: ', area) # end solveTraingle Comments: The results are displayed with too many decimal. round(value, decimals)

Python Statements. Python statements give the flow of computation within a function. 1.Simple statements: -expression; - to evaluate a Python expression. -block - used to collect several statements in one block. -Jump: -break - jumps outside of repetitions. -return - gets outside of functions/routines 2.Selections -simple if - selects an alternative -complete if - selects from two alternatives

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

Testing Operators Relational Operators: >, >=, <, <= == is equal to!= is not equal to Logical Operators: or and not Use ( ) to make complex test expressions

Max of 4 numbers. Given for integer numbers a, b, c, d then calculate the maximum value of them. Inputs: a, b, c, d – int Output: max – int. How to do it? read a, b, c, d; find max1 as the maximum of a and b find max2 as the maximum of c and d find max as the maximum of max1 and max2 write max

Test if three floats form a triangle. Inputs: a, b, c – float Output: answer. How to do it? read a, b, c; test if at least one is negative test if at least one number is too big

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