3 Basics © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.

Slides:



Advertisements
Similar presentations
4 If-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Advertisements

CS 100: Roadmap to Computing Fall 2014 Lecture 0.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Factors, Fractions, and Exponents
Booth’s Algorithm.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Primitive Types CSE 115 Spring 2006 April 3 &
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
division algorithm Before we study divisibility, we must remember the division algorithm. r dividend = (divisor ⋅ quotient) + remainder.
Introduction to Python and programming Michael Ernst UW CSE 190p Summer 2012.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Real Numbers and Algebra
Slide 5-1 Copyright © 2005 Pearson Education, Inc. SEVENTH EDITION and EXPANDED SEVENTH EDITION.
Section 1.1 Numbers and Their Properties.
Objectives You should be able to describe: Data Types
Simple Data Type Representation and conversion of numbers
4. Python - Basic Operators
Copyright 2013, 2010, 2007, Pearson, Education, Inc. Section 5.3 The Rational Numbers.
Computer Arithmetic Nizamettin AYDIN
Computer Science 111 Fundamentals of Programming I Number Systems.
Number Theory.  A prime number is a natural number greater than 1 that has exactly two factors (or divisors), itself and 1.  Prime numbers less than.
2440: 211 Interactive Web Programming Expressions & Operators.
Numbers MST101. Number Types 1.Counting Numbers (natural numbers) 2.Whole Numbers 3.Fractions – represented by a pair of whole numbers a/b where b ≠ 0.
Mathematics in OI Prepared by Ivan Li. Mathematics in OI Greatest Common Divisor Finding Primes High Precision Arithmetic Partial Sum and Differencing.
Python Types Python values are of various “types” Ints, Floats, Strings, Characters, and more Two representations of numbers 1 vs 1.0.
Numeric Types, Expressions, and Output ROBERT REAVES.
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
Computer Systems Architecture Copyright © Genetic Computer School 2008 CSA 1- 0 Lesson 1 Number System.
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
Rational and Irrational Numbers. Standards: Use properties of rational and irrational numbers.  MGSE9–12.N.RN.2 Rewrite expressions involving radicals.
CSC 221 Computer Organization and Assembly Language
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
Copyright © 2009 Pearson Education, Inc. Chapter 5 Section 1 - Slide 1 Chapter 1 Number Theory and the Real Number System.
6 Strings and Tuples © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming 1.
Python Conditionals chapter 5
Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for.
Complex Number Systems and Simplifying Algebraic Expressions Critical Thinking Skill: Demonstrate Understanding of Concepts.
5 While-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Slide Copyright © 2009 Pearson Education, Inc. Unit 1 Number Theory MM-150 SURVEY OF MATHEMATICS – Jody Harris.
Doing math In java.
Warm Up Find each quotient / divided by 3/5
Real Number and the number Line. Number System Real numbers: is number that can be positive or negative and have decimal places after the point. Natural.
Slide Copyright © 2009 Pearson Education, Inc. Slide Copyright © 2009 Pearson Education, Inc. Chapter 1 Number Theory and the Real Number System.
5-3(D) Real Numbers.
Logarithms Common Logarithms Integer Logarithms Negative Logarithms Log of a Product Log of a Quotient Log of an Exponential Natural Logarithms.
Number and Numerical Operations. Real Numbers Rational Numbers -Can be written as a fraction. -In decimal form, either terminates or repeats Examples:
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Slide Copyright © 2009 Pearson Education, Inc. Slide Copyright © 2009 Pearson Education, Inc. Chapter 1 Number Theory and the Real Number System.
Section 5.3 The Rational Numbers.
William Stallings Computer Organization and Architecture 7th Edition
PROGRAMME F1 ARITHMETIC.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Arithmetic operations, decisions and looping
Fundamentals of Programming I Number Systems
Section 5.3 The Rational Numbers
Algorithms computer as the tool process – algorithm
The Data Element.
The Data Element.
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.
Natural Numbers The first counting numbers Does NOT include zero
Class code for pythonroom.com cchsp2cs
COMPUTING.
Presentation transcript:

3 Basics © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming

3-2 Data types (1)  Programs input, store, manipulate, and output values or data.  Values are classified according to their types and the operations that can be performed on them. E.g.: –It makes sense to subtract numbers but not booleans or strings. –It makes sense to concatenate strings but not subtract them.

3-3 Data types (2)  Basic data types in Python: –integer numbers –floating-point numbers –booleans –strings … integers … floats False True booleans ‘’‘$’‘Hi’‘apple’ … strings values

3-4 Data types (3)  Composite data types in Python: –tuples (§6) –lists (§7) –dictionaries (§9).

3-5 Integer numbers  The integer numbers are positive and negative whole numbers: …, –3, –2, –1, 0, +1, +2, +3, …

3-6 Integer operators  Integer operators: - y negation of y x + y sum of x and y x - y difference of x and y x * y product of x and y x // y quotient when x is divided by y x % y remainder when x is divided by y x ** y x raised to the power of y

3-7 Example: integer arithmetic  This function uses integer remainders: def gcd (m, n): # Return the greatest common divisor of m and n. p = m q = n r = p % q # remainder on dividing p by q while r != 0: # i.e., p is not a multiple of q p = q q = r r = p % q return q

3-8 Floating-point numbers  The floating-point numbers are positive and negative real numbers.  Floating-point numbers are represented approximately in a computer (unlike integers, booleans, etc.).

3-9 Floating-point operators  Floating-point operators: - y negation of y x + y sum of x and y x - y difference of x and y x * y product of x and y x / y division of x by y x ** y x raised to the power of y

3-10 Example: floating-point arithmetic (1)  This function uses floating-point arithmetic: def square_root (x): # Return the square root of the positive number x. r = 1.0 while abs(x/r**2 – 1) > : r = 0.5 * (r + x/r) return r  This function assumes that its argument is positive. –What will happen if its argument is negative?

3-11 Example: floating-point arithmetic (2)  Tracing the function call square_root(2.0) : Enter the function: Test “ abs(…) > ”: Execute “ r = 1.0 ”: 2.0 x r 1.0 yields True Execute “ r = 0.5*(r+x/r) ”: Test “ abs(…) > ”: yields True Execute “ r = 0.5*(r+x/r) ”: Test “ abs(…) > ”: yields True

3-12 Example: floating-point arithmetic (3)  Tracing the function call (continued): Execute “ r = 0.5*(r+x/r) ”: rx Test “ abs(…) > ”: Execute “ return r ”: yields False returns

3-13 Floating-point approximation  Floating-point numbers are represented in the form: ± m x 2 ±e where the mantissa m is a binary fraction (½ ≤ m < 1) and the exponent e is a small binary integer.  Most real numbers (including all irrational numbers) can only be approximated in a computer.  It follows that floating-point computations are only approximate – beware!

3-14 Example: floating-point approximation  Consider the expression: – 1.0  On my computer, this expression yields !  The problem is that the number 0.2, although it can be written exactly as a decimal fraction, cannot be represented exactly as a binary fraction.

3-15 Boolean values and operators  The boolean values are False and True.  Boolean operators: not y negation of y (i.e., True iff y is False) x and y conjunction of x and y (i.e., True iff both x and y are True) x or y disjunction of x and y (i.e., True iff either x or y is True)

3-16 Comparison operators  Comparison operators: x == y True iff x is equal to y x != y True iff x is unequal to y x < y True iff x is less than y x <= y True iff x is less than or equal to y x > y True iff x is greater than y x >= y True iff x is greater than or equal to y  Comparison chaining: x < y < z True iff x is less than y and y is less than z etc.

3-17 Example: booleans  Using boolean and comparison operations: def in_range (n, p, q): # Return True iff n is in the range p … q. return (p <= n and n <= q)  Alternatively, using comparison chaining: def in_range (n, p, q): # Return True iff n is in the range p … q. return (p <= n <= q)  Function call: d = input('Enter integer in range 0-9: ') if not in_range(d, 0, 9): print 'Invalid integer'

3-18 String values  A string is a sequence of characters.  The length of a string is the number of characters in it.  The empty string has length 0 (i.e., it consists of no characters at all).  The string values are character sequences of any length.

3-19 String operators  String operators: s + t concatenation of strings s and t n * s concatenation of n copies of s s * n ditto

3-20 String comparison operators  String comparison operators: s == t True iff s is equal to t s != t True iff s is unequal to t s < t True iff s is lexicographically less than t s <= t True iff s is lexicographically less than or equal to t s > t True iff s is lexicographically greater than t s >= t True iff s is lexicographically greater than or equal to t  Comparison chaining: as before.

3-21 Example: string operations  Program: place = 'Paris' season = 'spring' title = place + ' in ' + 2*'the ' + season print title  Output: Paris in the the spring

3-22 Example: string comparisons (1)  Program: word1 = raw_input('Enter a word: ') word2 = raw_input('Enter another word: ') if word1 > word2: # Swap word1 and word2 … word1, word2 = word2, word1 print 'Words in lexicographic order:' print word1 print word2

3-23 Example: string comparisons (2)  Program’s output and input: Enter a word: mouse Enter another word: elephant Words in lexicographic order: elephant mouse