Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

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.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Python November 14, Unit 7. Python Hello world, in class.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Introduction to Python
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)
Guide to Programming with Python Chapter Two Basic data types, Variables, and Simple I/O: The Useless Trivia Program.
Chapter 2: Variables, Operations, and Strings CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Munster Programming Training
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.
COMP 116: Introduction to Scientific Programming Lecture 28: Data types.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Input, Output, and Processing
Chapter 4 – Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To.
Computer Science 101 Introduction to Programming.
More While Loop Examples CS303E: Elements of Computers and Programming.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
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.
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.
Variables, Expressions and Statements
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
3. COMPUTING WITH NUMBERS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
© 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,
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.
Introduction to Programming
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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.
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.
Input, Output and Variables GCSE Computer Science – Python.
Introduction to Programming
Creating Database Objects
Topics Designing a Program Input, Processing, and Output
Chapter 2 Basic Computation
Introduction to Python
Data Types and Conversions, Input from the Keyboard
Programming Fundamentals
Chapter 2 - Introduction to C Programming
Variables, Expressions, and IO
Computing with C# and the .NET Framework
Chapter 2 - Introduction to C Programming
Chapter 2.
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
Chapter 2 - Introduction to C Programming
Fundamentals of Programming I Number Systems
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
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
Data Types and Maths Programming Guides.
Introduction to C Programming
Creating Database Objects
Python Creating a calculator.
Data Types and Arithmetic in C
Presentation transcript:

Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming

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

Numerical Data Types int : whole numbers int : whole numbers –Stored in 4 bytes (32 bits) –Can represent whole numbers -2^31 though 2^31-1 –Computations are exact float : decimal point numbers float : decimal point numbers –Large range, but fixed precision –Not exact (remember, all data is 0s and 1s!) Example:.1+.2 = Example:.1+.2 = long : whole numbers larger or smaller than int long : whole numbers larger or smaller than int –Specify by adding “L” to the end

Data Types: Differences Integers should be your default Integers should be your default –Integer arithmetic is faster and more precise Most arithmetic operators behave as you would expect for all data types Most arithmetic operators behave as you would expect for all data types –Except integer division (gozinta)---result is an integer Example: result = 5/2 Example: result = 5/2 longs are integers, so they behave this way too longs are integers, so they behave this way too –float division behaves as you expect: Example: result = 5.0/2.0 Example: result = 5.0/2.0

Data Types: Automatic Conversion int to float : int to float : –int op float = float –Examples: 5.0/216-(3/2)16-(3.0/2) But what about: /4? int to long : int to long : –Large integers are converted to long Example: 5^31

Data Types: Examples of Automatic Conversion /23.0/25**315.0**315L+35.0/ /2 4/5 – 2 4.0/5 – 2 4/5 – 2.0

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

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

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

Keyboard Input Read data from the user during program execution Read data from the user during program execution Two ways: Two ways: –input(): reads numbers –raw_input(): reads strings How they work: How they work: –wait for the user to enter a value –read what has been typed when the user hits the Enter or Return key

Keyboard Input: input() input( ) Prompts the user to enter a number Prompts the user to enter a number Assigns entered number to a variable Assigns entered number to a variableExample: score1 = input(“Please enter the first exam score: ”) IDLE: Modify average program to prompt the user

Keboard Input: raw_input() raw_input( ) Prompts the user to enter a string Prompts the user to enter a string Assigns entered string to a variable Assigns entered string to a variableExample: name = raw_input(“What’s your full name?”) print name What happens if you give a number to raw_input() ? What happens if you give a string to input() ?

iClicker Question: Keyboard Input Which data type does input() expect? A. string B. int C. float D. int, float, or long