LING 388: Computers and Language

Slides:



Advertisements
Similar presentations
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Advertisements

CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
LING 408/508: Programming for Linguists Lecture 19 November 4 th.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Lecture 17: 10/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
05/09/2015SJF L31 F21SF Software Engineering Foundations Formatting Converting numbers to Strings and vice versa Monica Farrow EM G30
Section 8.5 Scientific Notation.
CIS 270—Application Development II Chapter 28—Formatted Output.
Computer Science 111 Fundamentals of Programming I Number Systems.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 計算機程式設計 Introduction to Computer Programming Lecture01: Introduction and Hello World 9/10/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction.
Input, Output, and Processing
Introducing Python CS 4320, SPRING Format: Field widths and Alignment The string representation of a value can be padded out to a specific width.
Formatted Output CSE 1310 – Introduction to Computers and Programming 1.
Type Conversions Implicit Conversion Explicit Conversion.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
5-1 Embedded Systems C Programming Language Review and Dissection III Lecture 5.
CSC 1010 Programming for All Lecture 7 Input, Output & Graphics.
Formatting Output. Line Endings By now, you’ve noticed that the print( ) function will automatically print out a new line after passing all of the arguments.
Introduction to Computer Organization & Systems Topics: Types in C: floating point COMP C Part III.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Formatted Output CSE 1310 – Introduction to Computers and Programming 1.
LING 408/508: Programming for Linguists Lecture 20 November 16 th.
1.2 Primitive Data Types and Variables
Introduction to Programming Python Lab 5: Strings and Output 05 February PythonLab5 lecture slides.ppt Ping Brennan
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
1 float Data Type Data type that can hold numbers with decimal values – e.g. 3.14, 98.6 Floats can be used to represent many values: –Money (but see warning.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
Introduction to Programming
C++ Basic Input and Output (I/O)
Introduction to Programming
Topics Designing a Program Input, Processing, and Output
Data Types and Conversions, Input from the Keyboard
Formatting Output.
Programming Fundamentals
TMF1414 Introduction to Programming
Introduction to Programming
Python’s input and output
OUTPUT STATEMENTS GC 201.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Few More Math Operators
Introduction to Programming
BCD = Binary Coded Decimal
Lecture 13 Input/Output Files.
Scientific Notation.
Introduction to Programming
Introduction to Programming
Introduction to Programming
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Topics Designing a Program Input, Processing, and Output
CMSC201 Computer Science I for Majors Lecture 18 – String Formatting
Conversion Check your class notes and given examples at class.
LING 408/508: Computational Techniques for Linguists
LING 408/508: Computational Techniques for Linguists
LING 388: Computers and Language
Topics Designing a Program Input, Processing, and Output
Data Types and Expressions
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Data Types and Maths Programming Guides.
Introduction to Programming
Formatting Output.
Presentation transcript:

LING 388: Computers and Language Lecture 8

Administrivia Next week I'm out of town The TA Patricia Lee will lead both classes You will do Python programming exercises in class

Python numbers revisited Python invisibly automatically converts to type long int from int (32 bit 2's complement), (page 64–65, section 3.5) explicit type coercion: float() int() long() Python2.7 complex(r,i) complex(string)

Python numbers revisited Note the difference in precision here

Python numbers revisited page 60: see factorial.py Python 3.x Python 2.7 ( )

Python eval() Think of eval(input()) as being the same as the user typing directly at the Python console …

Python numbers revisited factorial2.py

argv[1] and argv[2] ignored Python argv List of arguments from the command line: what's argv[0] then? test.py argv[1] and argv[2] ignored

Python example How about printing to 2 decimal places? Python 2.7 program How about printing to 2 decimal places?

Formatted Output Python 2.7 program

Formatted Output: floating point (f) $100 principal for 10 years at 4% (0.04) compound interest = $148.02

Formatted Output Lots of ways: (Old way) notation comes originally from the C Programming Language: sprintf function http://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm

Now use instead: <template-string>.format(<values>) Formatted Output Now use instead: <template-string>.format(<values>) %<width>.<precision><type> <type> = d, f, s <width> = minimum number of characters; right-justified by default, -width => left-justified 0 = as wide as needed <precision> = number of places after decimal point e.g. 02d two digits wide, pad with 0 if needed

Formatted Output: examples Function str() coerces integer or float above into type 'str'

Formatted Output Newer way: https://docs.python.org/2/tutorial/inputoutput.html#fancier-output- formatting Use {} for each argument (can be numbered, e.g. {0}, {1},… or referenced by keyword {x}) {:width}

Formatted Output {0:..} and {1:…} index into the format arguments 0 in {0:..} can be omitted but not the colon

A note on string encoding Python 2.7 Python 3.x