Python Types Python values are of various “types” Ints, Floats, Strings, Characters, and more Two representations of numbers 1 vs 1.0.

Slides:



Advertisements
Similar presentations
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Advertisements

Division of Decimal Fractions. STEPS 1. Set up the divisor and dividend 2. Make the divisor a whole number my moving the decimal completely to the end.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Python Programming: An Introduction to Computer Science
Python November 14, Unit 7. Python Hello world, in class.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
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.
String Escape Sequences
Number Systems Computer Science 210 Computer Organization.
Chapter 3 Computing with Numbers
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers.
Python Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers Python Programming, 2/e1.
Announcements Class / Recitation slides available on the syllabus page Check morning of for latest version We will start grading clickers next lecture.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
CSC 110 Numeric data types [Reading: chapter 3] CSC 110 D 1.
Vahé Karamian Python Programming CS-110 CHAPTER 3 Computing with Numbers.
Computer Science 111 Fundamentals of Programming I Number Systems.
CS 102 Computers In Context (Multimedia)‏ 01 / 23 / 2009 Instructor: Michael Eckmann.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Number Systems Spring Semester 2013Programming and Data Structure1.
Chapter 3.  Traditionally, programming languages have assigned different types of data for different types of numbers.  In many languages, there may.
Numeric Types, Expressions, and Output ROBERT REAVES.
Floating Point. Agenda  History  Basic Terms  General representation of floating point  Constructing a simple floating point representation  Floating.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
CS 102 Computers In Context (Multimedia)‏ 01 / 26 / 2009 Instructor: Michael Eckmann.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Java Data Types Assignment and Simple Arithmetic.
CSC 107 – Programming For Science. The Week’s Goal.
1 Data Representation Characters, Integers and Real Numbers Binary Number System Octal Number System Hexadecimal Number System Powered by DeSiaMore.
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.
Announcements Survey link (can be found on piazza): OCJ6
The Real Number System. Whole numbers Whole numbers Rational numbers Whole numbers Natural numbers Integers / ¾ 18% π √2√2 − ….
1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Mathematical Calculations in Java Mrs. C. Furman.
Computers and Numbers. Types of Numbers Computers store two different types of numbers: Whole Numbers AKA Integers (mathematics) AKA Fixed Point Numbers.
3. COMPUTING WITH NUMBERS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Number Representation Lecture Topics How are numeric data items actually stored in computer memory? How much space (memory locations) is.
Doing math In java.
3 Basics © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Numbers1 Working with Numbers There are times that we have to work with numerical values. When we count things, we need Integers or whole numbers. When.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
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 Programming: An Introduction to Computer Science
Programming and Data Structure
Data Types and Conversions, Input from the Keyboard
ITEC113 Algorithms and Programming Techniques
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Variables, Expressions, and IO
Computer Programming Methodology Introduction to Java
Computational Thinking
Computational Thinking
Fundamentals of Programming I Number Systems
Numerical Functions & Tricks
“If you can’t write it down in English, you can’t code it.”
Building Java Programs Chapter 2
Building Java Programs
Introduction to Primitives
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
Unit 3: Variables in Java
Building Java Programs Chapter 2
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
Understanding Variables
Data Types and Maths Programming Guides.
Primitive Data Types and Operators
Building Java Programs
Presentation transcript:

Python Types Python values are of various “types” Ints, Floats, Strings, Characters, and more Two representations of numbers 1 vs 1.0

Numeric Data Types Inside the computer, whole numbers and decimal fractions are represented (encoded) quite differently! We say that decimal fractions and whole numbers are two different data types. The data type of an object determines what values it can have and what operations can be performed on it.

Numeric Data Types Whole numbers are represented using the integer (int for short) data type. These values can be positive or negative whole numbers. Encoded in binary (last week lecture slides)

Numeric Data Types Numbers that can have fractional parts are represented as floating point (or float) values. How might we encode these? What about two numbers? How can we tell which is which? A numeric literal without a decimal point produces an int value A literal that has a decimal point is represented by a float (even if the fractional part is 0)

Numeric Data Types Python has a special function to tell us the data type of any value. >>> type(3) >>> type(3.1) >>> type(3.0) >>> myInt = 32 >>> type(myInt) >>>

Numeric Data Types Why do we need two number types? Values that represent counts can’t be fractional (you can’t have 3 ½ iPods) Most mathematical algorithms are very efficient with integers The float type stores only an approximation to the real number being represented! Since floats aren’t exact, use an int whenever possible!

Numeric Data Types Operations on ints produce ints, operations on floats produce floats (except for /). >>> 10.0/ >>> 10/ >>> 10 // 3 3 >>> 10.0 // >>> >>> >>> 3.0* >>> 3*4 12

Numeric Data Types Integer division produces a whole number. That’s why 10//3 = 3! 10//3 = 3 since 3 goes into 10 3 times (with a remainder of 1) 10%3 = 1 is the remainder of the integer division of 10 by 3. a = (a//b)*(b) + (a%b)

String Arithmetic? In the first week we saw print(“Hello” + “World”) The + operator “adds” two strings together print(“HelloWorld”) What if we wanted a space? print(“Hello “ + ”World”) print(“Hello” + “ World”) print(“Hello” + “ “ + “World”)

String Arithmetic? We have an intuition of what + does, can we reason about * (multiplication)? print(“Hello” * “World”) ??? But what about? print(3 * “Hello”) We know that 3 * 5 is short hand for: … Logically 3 * “Hello” would be short hand for: “Hello” + “Hello” + “Hello”

String Arithmetic? We know what “Hello” + “Hello” + “Hello” does! It gives us: “HelloHelloHello”