Computer Science 111 Fundamentals of Programming I Basic Program Elements.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
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
Chapter 8 Improving the User Interface
Fundamentals of Python: From First Programs Through Data Structures Chapter 2 Software Development, Data Types, and Expressions.
Lecture 2 Introduction to C Programming
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.
Introduction to C Programming
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 –
Introduction to C Programming
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 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
CSC 110 Numeric data types [Reading: chapter 3] CSC 110 D 1.
Python Programming Fundamentals
 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
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 2 Elementary Programming.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Computer Science 101 Introduction to Programming.
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 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
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.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
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
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Variables, Types, Expressions Intro2CS – week 1b 1.
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.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Sudeshna Sarkar, IIT Kharagpur 1 I/O in C + Misc Lecture –
CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
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.
Fundamentals of Programming I Overview of Programming
Topics Designing a Program Input, Processing, and Output
Chapter 6 - Functions modular programming general function format
Chapter 2 Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Basic Elements of C++.
I/O in C + Misc Lecture Sudeshna Sarkar, IIT Kharagpur.
Introduction to Programming
Chapter 2 - Introduction to C Programming
Basic Elements of C++ Chapter 2.
Fundamentals of Python: First Programs Second Edition
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Topics Designing a Program Input, Processing, and Output
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
CHAPTER 3: String And Numeric Data In Python
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Terminal-Based Programs
Presentation transcript:

Computer Science 111 Fundamentals of Programming I Basic Program Elements

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)

Behavior of Terminal-Based Programs ComputationsOutputsInputs Prompt the user for some information Use that information to perform some computations Output the results Examples: circle and tax programs (Lab 1 & Chapter 2)

Structure of Terminal-Based Programs ComputationsOutputsInputs docstring import statements input statements computation statements output statements

The docstring """ Author: Ken Lambert This program does nothing yet, but just you wait! """ Not evaluated, but used to document Python programs for other programmers Should appear at the beginning of each file Just as important as the evaluated code!!!

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

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 an 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.

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

More on Variables 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) Expressions are evaluated left to right, unless operator precedence overrides this order. Use parentheses to override standard precedence when necessary. print((totalincome - deduction) * rate)

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

Type Conversion Functions str(3.72) # Returns'3.72' 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. int(3.72) # Returns 3 float(3) # Returns 3.0 float('3.72') # Returns 3.72

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

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

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('Hello there\nKen!') # displays two lines print(str(3) + str(4)) # displays 34 print always ends output with a newline, unless its last argument is end='' print('Hello there ', end='') # displays one line print('Ken')

For Friday Finish Chapter 2