Terminal-Based Programs

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

15. Python - Modules A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand.
Setting the PYTHONPATH PYTHONPATH is where Python looks for modules it is told to import List of paths Add new path to the end with: setenv PYTHONPATH.
Perkovic, Chapter 7 Functions revisited Python Namespaces
Computer Programming w/ Eng. Applications
Fundamentals of Python: From First Programs Through Data Structures Chapter 2 Software Development, Data Types, and Expressions.
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Tcl and Otcl Tutorial Part I Internet Computing KUT Youn-Hee Han.
Concepts when Python retrieves a variable’s value Namespaces – Namespaces store information about identifiers and the values to which they are bound –
1 Python Chapter 2 © Samuel Marateck, After you install the compiler, an icon labeled IDLE (Python GUI) will appear on the screen. If you click.
CSC 110 Numeric data types [Reading: chapter 3] CSC 110 D 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Munster Programming Training
Fundamentals of Python: First Programs
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Modules and Decomposition UW CSE 190p Summer 2012 download examples from the calendar.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Variables, Expressions and Statements
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
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,
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Sudeshna Sarkar, IIT Kharagpur 1 I/O in C + Misc Lecture –
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Fundamentals of Programming I Overview of Programming
Arithmetic Expressions
Topics Designing a Program Input, Processing, and Output
Chapter 6 - Functions modular programming general function format
G. Pullaiah College of Engineering and Technology
Data Types and Conversions, Input from the Keyboard
Chapter 2 - Introduction to C Programming
Revision Lecture
Variables, Expressions, and IO
I/O in C + Misc Lecture Sudeshna Sarkar, IIT Kharagpur.
Introduction to Programming
Chapter 2 - Introduction to C Programming
Fundamentals of Python: First Programs Second Edition
Chapter 2 - Introduction to C Programming
Topics Introduction to File Input and Output
And now for something completely different . . .
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Reading Input from the Keyboard
Chapter 2 - Introduction to C Programming
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Introduction to Matlab
Variables, Data Types & Math
Elementary Programming (C++)
Introduction to Value-Returning Functions: Generating Random Numbers
CHAPTER 3: String And Numeric Data In Python
Topics Designing a Program Input, Processing, and Output
Experiment No. (1) - an introduction to MATLAB
Topics Designing a Program Input, Processing, and Output
Chapter 2 - Introduction to C Programming
Another Example Problem
Topics Introduction to File Input and Output
Introduction to C Programming
Presentation transcript:

Terminal-Based Programs A terminal allows a user to run a program view output as text on a screen or in a window enter input as text from the keyboard Early computer systems were entirely terminal-based, modern systems have added a GUI (graphical user interface) Retrieved from: http://home.wlu.edu/~lambertk/classes/111/

Behavior of Terminal-Based Programs Inputs Computations Outputs Prompt the user for some information Use that information to perform some computations Output the results

Structure of Terminal-Based Programs Inputs Computations Outputs docstring import statements input statements computation statements output statements Program code goes in a file with a .py extension.

import Statements import math print(math.pi) print(math.sqrt(2)) Imports usually occur before the beginning of the executable program code They make available resources from other Python modules A module is just a file of Python code https://repl.it/site/blog/python-import

import Statements from math import pi print(pi) Alternatively, one can import particular resources from a given module and then omit the module qualifier from the reference

import Statements from math import * print(pi) print(sqrt(2)) Or, one can import all the particular resources from a given module and then omit the module qualifier from all the references

Input of Text input('Enter your name: ') The input function prints its string argument and waits for user input. The function then returns the string of characters entered at the keyboard.

Input of Numbers int(input('Enter your age: ')) When an integer is expected, you must convert the input string to an int. float(input('Enter your hourly wage: ')) When a real number (with a decimal point) is expected, you must convert the input string to a float.

Simple Assignment Statements name = input('Enter your name: ') income = float(input('Enter your income: ')) The = operator evaluates the expression to its right and sets the variable to its left to the resulting value. We use variables to retain data for further use. Note: = does not mean equals in Python!

Syntax Template for Simple Assignment <variable> = <expression> A syntax template expresses a grammar rule in a language. The angle brackets enclose the names of phrases or terms that are defined by other rules. area = math.pi * radius ** 2 century = 100 squarerootof2 = math.sqrt(2)

More on Variables Any variable can name any thing. firstname = input('Enter your first name: ') Any variable can name any thing. Variables must begin with a letter or the _ character. They can contain any number of letters, digits, or _. Variables cannot contain spaces. Python is case-sensitive. Use lowercase letters for now.

Variable References x = 10 # x begins as 10 x = x + 1 # x is reset to 11 y = y + x # Error! Can't find value of y When Python sees a variable in an expression, it must be able to look up its value. If a variable has no established value, the program halts with an error message. Variables are given values by assignment statements

End of Line Comments x = 10 # x begins as 10 x = x + 1 # x is reset to 11 y = y + x # Error! Can't find value of y # begins an end of line comment - Python ignores text from # to the end of line

Evaluating Expressions print(totalincome - deduction * rate) print((totalincome - deduction) * rate) print(10 + x * y ** 2) Expressions are evaluated left to right, unless operator precedence overrides this order. Use parentheses to override standard precedence when necessary.

Mixed-Mode Arithmetic print(5 * 100) print(5 * 100.0) The value of an expression depends on the types of its operands. In general, two ints produce an int, whereas at least one float produces a float. Exception: x / y always produces a float.

Type Conversion Functions str(3.72) # Returns'3.72' float('3.72') # Returns 3.72 int(3.72) # Returns 3 float(3) # Returns 3.0 Each data type has a function to convert values of some other types to values of that type. int truncates a float by removing the fractional part.

Rounding and Precision round(3.72) # Returns 4 round(3.72, 1) # Returns 3.7 round(3.729, 2) # Returns 3.73 round’s optional second argument specifies the number of digits of precision in the fractional part

Using Functions round(3.72) # returns 4 abs(-5) # returns 5 math.sqrt(2) # returns 1.4142135623730951 <function name>(<any arguments>) A function can have one or more required arguments and/or some optional arguments Arguments must be of the appropriate types

Composing Expressions squareofa = a ** 2 squareofb = b ** 2 sumofsquares = squareofa + squareofb c = math.sqrt(sumofsquares) print('The hypotenuse is', c) Use assignment to name the results of computations

Composing Expressions squareofa = a ** 2 squareofb = b ** 2 sumofsquares = squareofa + squareofb c = math.sqrt(sumofsquares) print('The hypotenuse is', c) Use assignment to name the results of computations c = math.sqrt(a ** 2 + b ** 2) print('The hypotenuse is', c) Or just compose the expression and pass it as an argument to the function

Getting the Directory of a Module >>> import math >>> dir(math) ['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'] >>> The dir function returns a list of all of the named components in a module

Getting Help on a Function >>> import math >>> dir(math) ['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'] >>> help(math.sqrt) sqrt(x) Return the square root of x.

Output print(3, 4) # displays 3 4 print(str(3) + str(4)) # displays 34 print('Hello there\nKen!') # displays two lines print('Hello there ', end='') # displays one line print('Ken') print always ends output with a newline, unless its last argument is end=''

For Today Import math and print out it’s directory. Write a python script that asks the user for his/her name and age. Output Hello, name Output The square root of your age is: ans