Lessons on Python Numbers

Similar documents
Computing with Numbers Zelle - Chapter 3

Getting Started Values, Expressions, and Statements CS GMU

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter

Values, Variables, Types & Arithmetic Expressions. Agenda

Variables, Expressions, and Statements

ENGR 101 Engineering Design Workshop

ENGR (Socolofsky) Week 02 Python scripts

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Lecture 2: Python Arithmetic

A simple interpreted language

Here n is a variable name. The value of that variable is 176.

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

Python Numbers. Learning Outcomes 9/19/2012. CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 Discussion Sections 02-08, 16, 17

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

Work relative to other classes

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Operators & Expressions

Python Programming: An Introduction to Computer Science

Introduction to Computer Science-103. Midterm

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

CIS 110: Introduction to Computer Programming

CS 115 Lecture 4. More Python; testing software. Neil Moore

Variables and literals

CS 112: Intro to Comp Prog

Variable and Data Type I

Chapter 2 Binary Values and Number Systems

CITS 1401 Problem Solving & Programming

1/11/2010 Topic 2: Introduction to Programming 1 1

Programming in Python 3

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Computer Programming CS F111

CIS133J. Working with Numbers in Java

Python language: Basics

Reserved Words and Identifiers

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

Topic 2: Introduction to Programming

Informatica e Sistemi in Tempo Reale

Mathematical Data Operators

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Fundamentals of Programming

Python Intro GIS Week 1. Jake K. Carr

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

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

Number Systems and Binary Arithmetic. Quantitative Analysis II Professor Bob Orr

Lecture 3. More About C

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Unit 3. Operators. School of Science and Technology INTRODUCTION

'...' "..." escaping \u hhhh hhhh '''...''' """...""" raw string Example: r"abc\txyz\n" in code;

Programming Training. Main Points: - Python Statements - Problems with selections.

Introduction to Python

Fundamentals: Expressions and Assignment

Python Programming Exercises 1

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

Learning the Language - V

Number Representation & Conversion

CS Kangmei Yang. Page 1

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Advanced Algorithms and Computational Models (module A)

CS313D: ADVANCED PROGRAMMING LANGUAGE

Variables, Constants, and Data Types

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Chapter 4: Basic C Operators

Lab 1: Cipher Fundamentals

MIT AITI Python Software Development

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Python Programming: An Introduction to Computer Science

Chapter Two PROGRAMMING WITH NUMBERS AND STRINGS

Octal and Hexadecimal Integers

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

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Fundamentals of Programming CS-110. Lecture 3

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Expressions and Variables

Unit 3: Multiplication and Division Reference Guide pages x 7 = 392 factors: 56, 7 product 392

Visual C# Instructor s Manual Table of Contents

Getting started with Java

Variables, expressions and statements

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Arithmetic. 2.2.l Basic Arithmetic Operations. 2.2 Arithmetic 37

CSC 120 Computer Science for the Sciences. Week 1 Lecture 2. UofT St. George January 11, 2016

CSc 110, Spring 2017 Lecture 3: Expressions, Variables and Loops. Adapted from slides by Marty Stepp and Stuart Reges

Informatics Ingeniería en Electrónica y Automática Industrial

Computer Science 121. Scientific Computing Winter 2016 Chapter 3 Simple Types: Numbers, Text, Booleans

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

python 01 September 16, 2016

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Built-in Types of Data

MICROPROCESSOR SYSTEMS INTRODUCTION TO PYTHON

>>> * *(25**0.16) *10*(25**0.16)

CS 112: Intro to Comp Prog

BEGINNING PROBLEM-SOLVING CONCEPTS FOR THE COMPUTER. Chapter 2

Transcription:

Lessons on Python Numbers Walter Didimo [ 30 minutes ]

Types of numbers There are only three kinds of number in Python: integer any integer number floating-point any real number complex any number having an imaginary part Examples: 10 is an integer number 10.0 is a floating-point number 10 + 4j is a complex number (4 is the imaginary part)

Function type() The function type() returns the type of a number >>> type(10) <class 'int'> >>> type(10.0) <class 'float'> >>> type(10j) <class 'complex'> More in general, function type() can be used to know the type (class) of any data >>> type('hello') <class 'str'>

How to format numbers You can use the %d, %f, %e, and %E parameters to concatenate integers and floats to a string >>> print("int and float --> %d and %f" % (10, 2.5)) int and float --> 10 and 2.500000 >>> print("int and float --> %d and %e" % (10, 2.5)) int and float --> 10 and 2.500000e+00 >>> print("int and float --> %d and %E" % (10, 2.5)) int and float --> 10 and 2.500000E+00 >>> print("int --> %d" % 10.6) What do you expect? Try yourself

How to format numbers You can also control the number of decimal digits that must be displayed >>> print("rounded float --> %.02f" % 12.567) rounded float --> 12.57 >>> print("rounded float --> %.03E" % 234.455) rounded float --> 2.345E+02

Complex numbers How can you format a complex number? use the.real and.imag attributes to extract its real and imaginary part, respectively format each of the two parts (e.g., with %f) >>> (10+4j).real 10.0 >>> (10+4j).imag 4.0 >>> n = (10+4j) >>> print("(%0.2f,%0.2f)" % (n.real, n.imag)) (10.00,4.00)

Basic math You can do basic math with numbers, by applying the classical operators + summation - subtraction * multiplication ** power / division // integer division Parenthesis can be used to force precedence rules (like in a simple calculator)

Basic math Notice that, in the new versions of Python, the division operator / always returns the exact result (even if both the operators are integer) >>> (10+24)*5 170 >>> 10/4 2.5 >>> 10//4 2 >>> 2E340*10 inf this stands for infinity (too large)

More examples >>> 2**3 / (10-4) 1.3333333333333333 >>> print("%f" % (2**3 / (10-4))) 1.333333 few digits shown by default >>> print("%.30f" % (2**3 / (10-4))) 1.333333333333333259318465024990

More about integer division Notice that, the result of an integer division is always rounded down (towards inf) >>> -10//4-3 (obtained by rounding down -2.5) This is different from other languages (e.g. Java), where the decimal part is just truncated

Modulus Like other languages, the operator % returns the remainder (modulus) of an integer division >>> 10%4 2 with negative numbers, the behavior is different from other languages, such as C or Java >>> -10%4 2 (in Java or C it would be -2!!) The rules in Python are: (a//b)*b +(a%b) == a (a%b) has the same sign as b

Function round() You can also round a float to an integer, by using the round() function >>> round(10.3) 10 >>> round(10.7) 11 >>> round(-10.7) -11

Join of strings and numbers Unlike other languages, you cannot use the + operator to join a string and a number >>> print("my " + 4 + " friends") Traceback (most recent call last): File "<pyshell#21>", line 1, in <module> print("my " + 4 + " friends") TypeError: Can't convert 'int' object to str implicitly But you can use the str() "function" to convert a number into a string >>> print("my " + str(4) + " friends") My 4 friends

The str class name More in general, str is the class name for strings; str(x) creates a string representing x The parameter x can also be a string >>> str1 = str("hello") >>> str2 = str("world") >>> print(str1 + " " + str2) Hello world

Join of strings and numbers To insert numbers within a string you can also use the format() method, which we already introduced >>> print('my {} friends'.format(4)) My 4 friends

Octal and hexadecimal formats Use the %o, %x, %X formatting symbols to format a number as octal or hexadecimal >>> print("%o %x %X" % (30,30,30)) 36 1e 1E

Calculator in interactive mode In the interactive mode, the value of the last expression is assigned to the _ variable Such a variable should be used as a "read-only" variable >>> 6+10 16 >>> 10 - _ -6