cast int( x float( x str( x hex( int string int oct( int string int bin( int string int chr( int int ord( ch

Similar documents
Operators. Java operators are classified into three categories:

Variables and Values

The type of all data used in a C++ program must be specified

Beginning C Programming for Engineers

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

More Programming Constructs -- Introduction

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Applied Computer Programming

The type of all data used in a C (or C++) program must be specified

Chapter 3: Operators, Expressions and Type Conversion

JAVA OPERATORS GENERAL

Chapter 2. Data Representation in Computer Systems

COMP Primitive and Class Types. Yi Hong May 14, 2015

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

CS313D: ADVANCED PROGRAMMING LANGUAGE

Logical and Bitwise Expressions

CT 229. Java Syntax 26/09/2006 CT229

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Expressions & Assignment Statements

DIGITAL SYSTEM FUNDAMENTALS (ECE 421) DIGITAL ELECTRONICS FUNDAMENTAL (ECE 422) COURSE / CODE NUMBER SYSTEM

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

9 Using Equation Networks

Operators in java Operator operands.

Prof. Navrati Saxena TA: Rochak Sachan

Bits, Words, and Integers

Declaration and Memory

Chapter 7. Expressions and Assignment Statements ISBN

Topic Notes: Bits and Bytes and Numbers

ME 461 C review Session Fall 2009 S. Keres

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Topic Notes: Bits and Bytes and Numbers

Information Science 1

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Logical and Bitwise Expressions

CHAPTER 2 (b) : AND CODES

BITWISE OPERATORS. There are a number of ways to manipulate binary values. Just as you can with

Ex: If you use a program to record sales, you will want to remember data:

Unit 3. Operators. School of Science and Technology INTRODUCTION

UNIT- 3 Introduction to C++

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

Fundamentals of Programming Session 2

LECTURE 3 C++ Basics Part 2

CHAPTER 2 Number Systems

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. Number Systems and Number Representation

Under the Hood: Data Representation. Computer Science 104 Lecture 2

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

SECTION II: LANGUAGE BASICS

Java Notes. 10th ICSE. Saravanan Ganesh

Reserved Words and Identifiers

Number Systems and Number Representation

Visual C# Instructor s Manual Table of Contents

Operators in C. Staff Incharge: S.Sasirekha

CS1 Lecture 3 Jan. 18, 2019

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Variables, Constants, and Data Types

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Beyond Base 10: Non-decimal Based Number Systems

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Learning the Language - V

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

CS1 Lecture 3 Jan. 22, 2018

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Advanced Algorithms and Computational Models (module A)

Chapter 7. Expressions and Assignment Statements

Midterms Save the Dates!

What Is It? Instruction Register Address Register Data Register

Chapter 6 Primitive types

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Operators and Expressions

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Numeral Systems. -Numeral System -Positional systems -Decimal -Binary -Octal. Subjects:

Types and Expressions. Chapter 3

COMP2121: Microprocessors and Interfacing. Number Systems

CS2630: Computer Organization Homework 1 Bits, bytes, and memory organization Due January 25, 2017, 11:59pm

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

CSE 142 Su 04 Computer Programming 1 - Java. Objects

ECE 122 Engineering Problem Solving with Java

Chapter 2 Exercises and Answers

Quick Reference Guide

Basics of Java Programming

Variables and literals

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Chapter 12 Variables and Operators

Data Representation 1

9/23/15. Agenda. Goals of this Lecture. For Your Amusement. Number Systems and Number Representation. The Binary Number System

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Lecture 1. Types, Expressions, & Variables

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Goals for this Week. CSC 2400: Computer Systems. Bits, Bytes and Data Types. Binary number system. Finite representations of binary integers

THE LOGIC OF COMPOUND STATEMENTS

Computers Programming Course 6. Iulian Năstac

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Transcription:

More About Values

Casts To cast is to take a value of one type and return the corresponding value of some other type (or an error, if the cast is impossible) int(x) casts a string, float, or boolean x to an integer float(x) casts a string, integer, or boolean x to a float str(x) returns the value of x as a string hex(int) returns a string representing the hexadecimal value of the integer int oct(int) returns a string representing the octal value of the integer int bin(int) returns a string representing the binary value of the integer int chr(int) returns the character represented by the Unicode value int ord(ch) returns the integer value of the Unicode character ch unichr(x) is in the textbook, but no longer exists, because in Python 3, all strings are in Unicode 2

ASCII and Unicode On consumer laptops, memory is organized into bytes A byte is eight bits A bit is a single on/off (or 0/1, or true/false) value For many years, ASCII (American Standard Code for Information Exchange) has been the world standard ASCII uses seven bits to represent each character This is enough to represent all the characters on a standard (American) keyboard, with one bit left over By also using the leftover eighth bit, every company could add more characters (like smart quotes )--and every company did This is why smart quotes (and em-dashes, and many other characters) typed in Windows turn into other, weird characters on a Macintosh...and vice versa Unicode is a much newer standard (~1988) that solves this problem Unicode uses single bytes to represent ASCII characters, and multiple bytes for any additional characters 3

Numbers In mathematics, integers (whole numbers) are exact and may be arbitrarily large In programming, integers are exact and in Python (but not most languages) they may be arbitrarily large In mathematics, real numbers have infinite precision π has been calculated to 2.7 trillion digits In programming, floating point numbers have limited precision >>> 10 / 3 3.3333333333333335 4

Equality Rule: Never compare floating point numbers for exact equality (==) or exact inequality (!=) This comparison usually works, but you can t trust it It s safe to compare integers Except: >>> i = 12345678901234567890 >>> i == int(float(i)) False Rule: If exact results are required, use only integers. It s probably to use floats to keep track of your own dollars and cents Don t do this for banks or other financial institutions! 5

Approximate equality What if you want to determine whether two floating point numbers are approximately equal? You can test if the absolute value of their difference is small abs(x - y) < epsilon What is epsilon? For ordinary numbers, maybe 0.00001 is a reasonable value For distances between atoms, maybe 1.0e-9 is a better value For distances between stars, maybe 1.0e9 is a better value You can test if the absolute value of their quotient is near 1 abs(x / y - 1.0) < epsilon With this approach, epsilon is more like a percentage difference, and doesn t have to be adjusted for the expected size of x and y Unfortunately, if y is zero, this will cause your program to crash 6

Short-circuit logic x y x and y x or y TRUE TRUE TRUE TRUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE Consider x and y: If x is False, Python doesn t have to evaluate y Consider x or y: If x is True, Python doesn t have to evaluate y Consider: (y == 0) or (abs(x / y - 1.0)) < 0.00001 What will this do if y == 0? Do you think this is a good way to determine if x and y are approximately equal? 7

The ternary operator Python has an if-else operator as well as an if-else statement Syntax: true_expr if condition else false_expr Semantics: Evaluate the condition; if True, evaluate the true_expr, otherwise evaluate the false_expr. Whichever gets evaluated becomes the value of the expression Example: abs_x = x if x >=0 else -x Example: if (x == 0 if y == 0 else abs(x / y - 1.0) < eps): # do something when x and y are almost equal The outer parentheses are not necessary in the above, but make it somewhat less difficult to read I think the ternary operator is usually confusing and should be avoided What is the result of this expression if x == 1.3e-50 and y == 0? What is the result of this expression if x == 0 and y == 1.3e-50? 8

Number systems The binary (base 2) number system uses two binary digits, (abbreviation: bits) -- 0 and 1 The octal (base 8) number system uses eight digits: 0, 1, 2, 3, 4, 5, 6, 7 The decimal (base 10) number system uses ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 The hexadecimal, or hex (base 16) number system uses sixteen digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F In Python, write binary, octal, or hexadecimal numbers by prefixing them with 0b, 0o, or 0x, respectively Regardless of how they are written, numbers are stored in binary and displayed in decimal 9

Using octal and hex Computers use binary, but the numbers are too long and confusing for people--it s easy to lose your place Octal or hex is better for people Translation between binary and octal or hex is easy One octal digit equals three binary digits: 101101011100101000001011 5 5 3 4 5 0 1 3 One hexadecimal digit equals four binary digits: 101101011100101000001011 B 5 C A 0 B 10

Bitwise operators x y ~x and y x & y x y x ^ y ~ is bitwise not (or invert or toggle ) & is bitwise and is bitwise or 0 0 1 0 0 0 0 1 1 0 1 1 1 0 0 0 1 1 1 1 0 1 1 0 ^ is bitwise exclusive or (or xor ) x >> i shifts the bits in x to the right i places x << i shifts the bits in x to the left i places 11

2 + 3 * 4 is 14, not 20 Precedence This is because multiplication has higher precedence than addition Here s what you should remember about precedence: Exponentiation (**) has highest precedence Unary operators have higher precedence than the related binary operators in the same family By family I mean arithmetic, logic, or bitwise operators, so not x or y means the same as (not x) or y Multiplication, division, and and operators have higher precedence than additions, subtraction, and or operators x or y and z means the same as x or (y and z) For everything else, use parentheses to clarify your meaning, even if they aren t needed 12

Comparison chaining In Python, you can chain comparisons Example: x < y < z is legal and meaningful In every other language that I know, x < y would result in a boolean value, which would then be compared with z Comparison chaining is a nice feature, and you should feel free to use it, but... It s not allowed in Java In C or C++ or C# it is allowed, but x < y will result in 0 or 1, which is then compared with z 13

Shorthand assignments x += y is shorthand for x = x + y Example: x += 1 adds 1 to x This same shorthand works for all the other binary operators 14

The End Why do programmers confuse Halloween with Christmas? Because Oct 31 == Dec 25. 15