Data Types and Conversions, Input from the Keyboard

Slides:



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

Types and Arithmetic Operators
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 Python and programming Michael Ernst UW CSE 190p Summer 2012.
Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
Munster Programming Training
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
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.
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.
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.
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.
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.
Course A201: Introduction to Programming 09/09/2010.
Input, Output and Variables GCSE Computer Science – Python.
Introduction to Programming
Topics Designing a Program Input, Processing, and Output
Chapter 2 More on Math More on Input
Topic: Python’s building blocks -> Statements
Chapter 2 - Introduction to C Programming
Design & Technology Grade 7 Python
Variables, Expressions, and IO
Introduction to Programming
Chapter 2 - Introduction to C Programming
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.
Chapter 2 - Introduction to C Programming
Use proper case (ie Caps for the beginnings of words)
Chapter 2 - Introduction to C Programming
Variables and Expressions
Chapter 2 - Introduction to C Programming
Rocky K. C. Chang September 18, 2018 (Based on Zelle and Dierbach)
Introduction to Programming
Topics Designing a Program Input, Processing, and Output
CS190/295 Programming in Python for Life Sciences: Lecture 3
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
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.
CSCE 206 Lab Structured Programming in C
Introduction to C Programming
Introduction to Python
Python Creating a calculator.
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 float: decimal point numbers Computations are exact float: decimal point numbers Large range, but fixed precision Not exact Example: .1 + .2 = .300000000000004

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: 3.5 + 6/4?

Data Types: Examples of Automatic Conversion 6.0+2 7-0.25 3/2 3.0/2 5**31 5.0**31 5.0/2 + 3 4//5 – 2 4.0/5 – 2 4/5 – 2.0

Data Types: Explicit Conversion Python provides functions to do this: float (<put number here>) int (<put number here>) #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(101.566) IDLE: Average of 3 test scores

Question: Data Types What is the output of the following code? result = 5.0 + 11//2 print result 10.5 B. 8 C. 10.0 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(<put prompt string here>) 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.