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,

Slides:



Advertisements
Similar presentations
Getting Input in Python Assumes assignment statement, typecasts.
Advertisements

Types and Arithmetic Operators
Lesson 4: Formatting Input Data for Arithmetic
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 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 Python
Introduction to Python and programming Michael Ernst UW CSE 190p Summer 2012.
Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)
Chapter 2: Variables, Operations, and Strings CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
Munster Programming Training
Fundamentals of Python: First Programs
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 3.  Traditionally, programming languages have assigned different types of data for different types of numbers.  In many languages, there may.
Python Types Python values are of various “types” Ints, Floats, Strings, Characters, and more Two representations of numbers 1 vs 1.0.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Variables and Expressions CMSC 201 Chang (rev )
Type Conversions Implicit Conversion Explicit Conversion.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Java Data Types Assignment and Simple Arithmetic.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Variables, Expressions and Statements
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
3. COMPUTING WITH NUMBERS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
Introduction to Programming
GCSE Computing: Programming GCSE Programming Remembering Python.
Course A201: Introduction to Programming 09/09/2010.
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.
Input, Output and Variables GCSE Computer Science – Python.
Topics Designing a Program Input, Processing, and Output
Topic: Python’s building blocks -> Statements
Data Types and Conversions, Input from the Keyboard
Chapter 2 - Introduction to C Programming
Variables, Expressions, and IO
Introduction to Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Examples of Primitive Values
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Rocky K. C. Chang September 18, 2018 (Based on Zelle and Dierbach)
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Python Basics with Jupyter Notebook
Topics Designing a Program Input, Processing, and Output
Chapter 2 - Introduction to C Programming
Terminal-Based Programs
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 C Programming
Introduction to Python
Presentation transcript:

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, it will get you. -- Peter Farrar

What is a Data Type? The type of value stored by a variable Two general categories: – String: represents text For now, only use for input/output More later, if time allows – Numerical Breaks down into many other types Types have different internal representations

Numerical Data Types int: whole numbers – Computations are exact float: decimal point numbers – Large range, but fixed precision – Not exact Example: =

Data Types: Differences Most arithmetic operations behave as you would expect for all data types Except integer division – result is an integer – Example: result = 5//2 float division behaves as you expect: – Example: result = 5.0/2.0

Data Types: Automatic Conversion int to float: – int op float = float – Examples: 5.0/2 16-(3//2) 16-(3.0/2) What about: /4?

Data Types: Examples of Automatic Conversion /2 3.0/2 5**31 5.0**31 5.0/ //5 – 2 4.0/5 – 2 4/5 – 2.0

Data Types: Explicit Conversion Python provides functions to do this: float ( ) int ( ) #truncates! If you would rather round to the nearest whole number, use round() – Also takes number as the argument

Data Types: Examples of Explicit Conversion What is the output? float(3) int(3.9) int( ) IDLE: Average of 3 test scores

Question: Data Types What is the output of the following code? result = //2 print result A.10.5 B. 8 C D. 8.0

Keyboard Input Read data from the user during program execution input() function How it works: – wait for the user to enter a value – read what has been typed when user enters Enter or Return key

Keyboard Input: input() input( ) Prompts the user to enter a number Assigns entered string to a variable Example: greeting = input("Please enter your message: ")

Keyboard Input If you want to read an integer, use int() Example: score = int(input("Please enter exam score: ")) IDLE: Write a program that asks the user for three integer exam scores, and prints the average.