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

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

Outline. 1 If Statement. 2 While Statement. 3 For Statement. 4 Nesting. 5 Applications. 6 Other Conditional and Loop Constructs 2 / 19

CS 102 Lab 3 Fall 2012

Chapter 5 Conditional and Iterative Statements. Statement are the instructions given to the computer to perform any kind of action.

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

Downloaded from Chapter 2. Functions

CSC 120 Computer Science for the Sciences. Week 1 Lecture 2. UofT St. George January 11, 2016

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

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

Python as a First Programming Language Justin Stevens Giselle Serate Davidson Academy of Nevada. March 6th, 2016

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

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

Variable and Data Type I

Chapter 2 Input, Processing and Output. Hong Sun COSC 1436 Spring 2017 Jan 30, 2017

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

CS177 Python Programming. Recitation 2 - Computing with Numbers

Variable Manipulator Driver. Installation and Usage Guide. Revision: 1.0 Date: Monday, July 10, 2017 Authors: Alan Chow

Python Input, output and variables

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

Functions with Parameters and Return Values

Here n is a variable name. The value of that variable is 176.

ENGR 101 Engineering Design Workshop

Text Input and Conditionals

Lecture 2: Python Arithmetic

Programming Training. Main Points: - More Fundamental algorithms on Arrays. - Reading / Writing from files - Problem Solving

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Python: Functions. Thomas Schwarz, SJ Marquette University

Iteration. Chapter 7. Prof. Mauro Gaspari: Mauro Gaspari - University of Bologna -

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

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

An Introduction to Python for KS4!

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

Built-in Types of Data

Computer System and programming in C

Preview from Notesale.co.uk Page 2 of 79

Lessons on Python Numbers

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide

Variable and Data Type I

Add Subtract Multiply Divide

Java for Python Programmers. Comparison of Python and Java Constructs Reading: L&C, App B

Lesson 3: Basic Programming Concepts

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

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

Flow Control: Branches and loops

CS1 Lecture 3 Jan. 18, 2019

CPSC 217-T03/T08. Functions Ruting Zhou

Computing with Numbers

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

DEPARTMENT OF MATHS, MJ COLLEGE

Introduction to Python, Cplex and Gurobi

Introduction to Python

Computing with Numbers Zelle - Chapter 3

CS1 Lecture 3 Jan. 22, 2018

How to Do Word Problems. Study of Integers

Programming to Python

These are reserved words of the C language. For example int, float, if, else, for, while etc.

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

Scripting Languages. Python basics

Introduction to Python

Module 01: Introduction to Programming in Python

cs1114 REVIEW of details test closed laptop period

Chapter 3 : Computer Science. Class XI ( As per CBSE Board) Data Handling. Visit : python.mykvs.in for regular updates

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

Introduction to Python

Revision of Basics of Python

Beyond Blocks: Python Session #1

Introduction to Programming II W4260. Lecture 2

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Chapter 4: Basic C Operators

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Fundamentals of Programming CS-110. Lecture 3

Assoc. Prof. Dr. Tansu FİLİK

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems

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

Statements 2. a operator= b a = a operator b

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Python Unit

CS177 Recitation. Functions, Booleans, Decision Structures, and Loop Structures

Formatting & Style Examples

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter

CSCE 110: Programming I

Chapter Two MIPS Arithmetic

12. Logical Maneuvers. Topics: Loop-Body Returns Exceptions Assertions Type Checking Try-Except

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

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

1. What is the minimum number of bits needed to store a single piece of data representing: a. An integer between 0 and 100?

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

CIS192 Python Programming. Robert Rand. August 27, 2015

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

DM536 Introduction to Programming. Peter Schneider-Kamp.

Spring 2017 CS 1110/1111 Exam 1

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

SECTION II: LANGUAGE BASICS

COMP1730/COMP6730 Programming for Scientists. Functions

Transcription:

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 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 ); 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 )

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

Solving a triangle Pseudo-code

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)) # 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) # translate the angles in degree degreea = A*180/math.pi degreeb = B*180/math.pi degreec = C*180/math.pi

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. Select one choice if test : # block of if statement1; statement2; # end if Choose between two choices if test : # block of if statement1; statement2; else : # block of else statement 1; statement 2; # 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.