Main Points: - Python Statements - Problems with selections.

Slides:



Advertisements
Similar presentations
Chapter 4 Computation Bjarne Stroustrup
Advertisements

Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
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.
COMPSCI 101 Principles of Programming
Python.
Python  By: Ben Blake, Andrew Dzambo, Paul Flanagan.
Programming Training Main Points: - Working with Functions in Python
Programming Training Main Points: - Lists / Arrays in Python. - Fundamental algorithms on Arrays.
Munster Programming Training
Programming Training Main Points: - Python Statements - Problems with selections.
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.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
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.
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
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.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Python Conditionals chapter 5
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.
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.
Doing math In java.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
PYTHON VARIABLES : CHAPTER 2 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
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.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Arithmetic Expressions
Introduction to Programming
G. Pullaiah College of Engineering and Technology
Fundamentals of Programming I Overview of Programming
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Topics Designing a Program Input, Processing, and Output
Introduction to Python
1-1 Logic and Syntax A computer program is a solution to a problem.
Data Types and Conversions, Input from the Keyboard
Introduction to Programming
Presented By S.Yamuna AP/IT
Engineering Problem Solving with C++, Etter/Ingber
Functions CIS 40 – Introduction to Programming in Python
Introduction to Programming
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Arithmetic operations, decisions and looping
Introduction to Programming
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
Introduction to Programming Using Python PART 2
Summary Two basic concepts: variables and assignments Basic types:
Core Objects, Variables, Input, and Output
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Terminal-Based Programs
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
COMPUTER PROGRAMMING SKILLS
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Class code for pythonroom.com cchsp2cs
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:

Main Points: - Python Statements - Problems with selections. 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 = ‘)) import math def printFunction(): print(‘\n\n**************\n\n’) a = int(input(‘a = ‘)) b = int(input(‘b = ‘)) c = a+b print(‘c=‘, c) 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: Relational operators: +, -, *, /, ** % 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 Find the inputs and outputs of the problem. Ask yourself if you have done something similar. Identify the elements of the solution. Take a numerical example and see the changes of the variables. 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 Ask yourself if you have done something similar. Solve the problem mathematically. Identify the elements of the solution: Break the problem into small blocks that you know how to. Write functions / find snippets of code for each block. Find the calculations / selections / repetitions 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)) # 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. 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 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 : # 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 Read more about if from the e-tutorial. Solve the HW problems.