CS177 Python Programming. Recitation 2 - Computing with Numbers

Similar documents
CS177 Python Programming. Recita4on 2 - Compu4ng with Numbers

Chapter 3 : Computer Science. Class XI ( As per CBSE Board) Data Handling. Visit : python.mykvs.in for regular updates

The Math Class. Using various math class methods. Formatting the values.

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

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

Outline. Data and Operations. Data Types. Integral Types

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions.

Expressions in JavaScript. Jerry Cain CS 106AJ October 2, 2017

CS 102 Lab 3 Fall 2012

Introduction to Computer Science and Object-Oriented Programming

The float type and more on variables FEB 6 TH 2012

Downloaded from Chapter 2. Functions

Subroutines I Computers and Programming

Introduction to Computer Programming for Non-Majors

Announcements. Project 2 due next Monday. Next Tuesday is review session; Midterm 1 on Wed., EE 129, 8:00 9:30pm

egrapher Language Reference Manual

C++ Programming: From Problem Analysis to Program Design, Third Edition

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

Python Programming Exercises 1

PYTHON. Values and Variables

CS 141, Lecture 3. Please login to the Math/Programming profile, and look for IDLE (3.4 or the unnumbered. <-- fine <-- fine <-- broken

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

Introduction to Computer Programming for Non-Majors

Fundamentals: Expressions and Assignment

Variable and Data Type I

Chapter 4 : Informatics Practices. Data Handling. Class XI ( As per CBSE Board) Visit : python.mykvs.in for regular updates

CSE 115. Introduction to Computer Science I

Expressions and Variables

Chapter 2 Getting Started with Python

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

Built-in Types of Data

CMSC 201 Computer Science I for Majors

CSSE 120 Introduction to Software Development Practice for Test 1 paper-and-pencil part Page 1 of 6

Introduction to Computer Programming for Non-Majors

These are reserved words of the C language. For example int, float, if, else, for, while etc.

4. Modules and Functions

Variable and Data Type I

Final Exam Practice Questions

CITS 1401 Problem Solving & Programming

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Python Programming. Introduction Part II

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

1. What is the minimum number of bits needed to store a single piece of data representing: a. An integer between 0 and 100?

CS 112: Intro to Comp Prog

Computing with Numbers

ENGR 101 Engineering Design Workshop

PYTHON- AN INNOVATION

Python Programming: An Introduction to Computer Science

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

Module 01: Introduction to Programming in Python

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

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

Introduction to Programming in Turing. Input, Output, and Variables

Introduction to Programming

Lists How lists are like strings

Scripting Languages. Python basics

Introduction to Programming

XQ: An XML Query Language Language Reference Manual

Python Programming: An Introduction to Computer Science

UNIT 1. Variables Binary Math

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

Computer System and programming in C

Introduction to Programming

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

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

Topic 2: Introduction to Programming

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Comp 151. More on Arithmetic and intro to Objects

Variables, Expressions, and Statements

Arbitrary Precision and Symbolic Calculations

Introduction to Python

Sketchpad Graphics Language Reference Manual. Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321

Spring 2017 CS 1110/1111 Exam 1

Computer Science 217

Computer Science 217

Revision of Basics of Python

Pace University. Fundamental Concepts of CS121 1

COMPSCI 105 S Principles of Computer Science. Classes 3

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Systems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties

Differentiate Between Keywords and Identifiers

A Brief Introduction to Python

Floating Point January 24, 2008

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

CSE 111 Bio: Program Design I Lecture 4: Variables, Functions, Strings, Genbank

Introduction to Computer Science Unit 2. Notes

Integer Data Types. Data Type. Data Types. int, short int, long int

CS 61B Data Structures and Programming Methodology. June David Sun

Introduction to Computer Science Unit 2. Notes

Floating point. Today. IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties Next time.

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

DEPARTMENT OF MATHS, MJ COLLEGE

Arithmetic and IO. 25 August 2017

1 Classes. 2 Exceptions. 3 Using Other Code. 4 Problems. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, / 19

Name Feb. 14, Closed Book/Closed Notes No electronic devices of any kind! 3. Do not look at anyone else s exam or let anyone else look at yours!

Lecture 2: Python Arithmetic

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

A. Incorrect! To simplify this expression you need to find the product of 7 and 4, not the sum.

Objectives. In this chapter, you will:

Transcription:

CS177 Python Programming Recitation 2 - Computing with Numbers

Outline Data types. Variables Math library. Range Function

What is data (in the context of programming)? Values that are stored and manipulated by computer programs. In Python, data can be: numbers, strings (character, word, sentence, etc.), or objects (will be discussed in the later chapter)

Numeric Data Type Numeric is the most common data type Numbers can be classified as natural numbers, integers, rational numbers, real numbers, complex numbers, computable numbers

Numbers in Python In Python, whole numbers are referred to integers; fractions are referred to floating point A numeric literal WITHOUT a decimal point produces an int value A literal that WITH a decimal point is represented by a float (even if the fractional part is 0)

String Data Type in Python A sequence of characters e.g., Programming is cool! Strings are encloses with single quotes this is string double quotes This is a string triple double quote This is a string Two useful functions are used with Strings are input() and eval()

String Data Type in Python input() is used to obtain data from user - Example >>>applicant = input("enter the applicant's name: ") >>>interviewer = input("enter the interviewer's name: ") >>>time = input("enter the appointment time: ") >>>print(interviewer, "will interview", applicant, "at", time)

String Data Type in Python eval(expression) The expression argument is parsed and evaluated as a Python expression - Example >>>x = input( Enter the value of x: ) Enter the value of x: 1 print(eval(x)) Result: 1

String Data Type in Python We can perform + operation within eval() - Example x = 10 print(eval(x+ 1 )) Result: 101 Notice that the expression argument should be a string type

Arithmetic Operators Arithmetic operators inherit their definitions on numerical data types (int/floating point). Operations on floats produce floats. Operations on int produce int (except for /). What if one operand is int and the other is floating point? 2+3.0

Variables Are names for objects that have values in main memory. We define variables by assigning values to names: Example: >>>a = 9 >>>b = 13.54 >>>c = Computer Variables can change their values during the program. It s important to choose good names that describes the variable. Example: firstname, courseno, etc.

Assignment Statement Variable Value Example: X = 10 Variable is also called Left Hand Side. Value is also called Right Hand Side.

Assignment Statement Variable Value The value can be: a number, a string of characters, but also a variable, or an expression. Example: X = 10 Y = X Y = X + 5 Z = Y

Example 1 What is the output of the following statements: >>> X = 10 >>> Y= 20 >>> result = X+ Y >>> print (result) >>> Y= 100 >>> print (x + 10) >>> print (x) >>> print (y) 10 X output 30 result 30 20 10 100 20 100 Y

Exercise 1 >>> name = John >>> price = 9.99 >>> total = price / 3.0 >>> name = Jane >>>print (name) >>>print (price) >>>print (total) John Jane name output 3.33 total Jane 9.99 3.33 9.99 price

Exercise 2 >>> var1 = Purdue >>> var2 = University >>>print (var1) >>>print (var2) >>>var2 = var1 >>>print (var1) >>>print (var2) Purdue var1 output University Purdue var2 Purdue University Purdue Purdue

What about >>> X + 2 = 15 >>> Y = Hello + 10 Tips: Left Hand Side should always be a variable You can t perform operations (such as sum) on two values of different types (numeric and string)

Math Library Python has math library that can perform powerful computations time: provides various time- related functions math: provides access to the mathematical functions defined by the C standard random: generates pseudo- random numbers with various common distributions. Python Programming, 2/e 18

Math Library before using a library, we need to import the library into our program: Example import math Then we can do math.pow(x, y) - Return x raised to the power y. math.sqrt(x) - Return the square root of x. math.factorial(x) - Return x factorial. math.ceil(x) - Return the ceiling of x. math.floor(x) - Return the floor of x. Python Programming, 2/e 19

Math Library Examples >>> import math >>> a = math.factorial(6) >>> print(a) 720 >>> b = math.sqrt(123) >>> print(b) 11.0905365064

Math Library Examples >>> c = math.floor(5.9) >>> print(c) 5 >>> x = math.factorial(4) * math.pow(2, 3) >>> print(x) 192.0

Math Library Examples >>> y = 5.5 >>> z = math.floor(y) * math.ceil(y) >>> print(z) >>> y = - 5.5 >>> z = math.floor(y) * math.ceil(y) >>>print(z) The results for both code snippets are 30, but does math.floor(5.5) equal to abs(math.floor(- 5.5))?

Function range Range is a function that returns a sequence If range has only one input parameter: (i.e range(input)) It generates the sequence of all the non- negative integers that are less than the input parameter value the generated sequence starts with 0 increment is 1 the last element of the sequence is the value of input parameter 1 >>> list(range(3)) >>> list(range(1)) >>>list( range(- 1)) [0,1,2] [0] [] >>> list(range(9)) >>> list(range(0)) >>>list(range(- 5)) [0, 1, 2, 3, 4, 5, 6, 7, 8] [] [] 23

Function range If two inputs (i.e range(first_input, second_input)): It generates the sequence of all the integers that are greater than or equal to the first_input value and less than the second_input value the first element of the sequence is the value of first_input increment is 1 the last element of the sequence is the value of second_input 1 >>> list(range(0, 3)) >>>list(range(4, 7)) >>>list(range(- 2, 2)) [0, 1, 2] [4, 5, 6] [- 2, - 1, 0, 1] >>> list(range(0, 10)) >>> list(range(7, 4)) >>>list(range(- 2, - 5)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [] [] 24

Function range If three inputs (i.e. range(first_input, second_input, third_input)): the sequence starts with the first_input value increment is third- input If increment is positive the sequence ends with the largest value less than second_input If increment is negative the sequence ends with the smallest value greater than second_input >>> list(range(0, 3, 1)) >>>list(range(1, 7, 2)) >>>list(range(- 5, 5, 3)) [0, 1, 2] [1, 3, 5] [- 5, - 2, 1, 4] >>>list(range(0, 6, 3)) >>> list(range(- 7, - 1, 2)) >>> list(range(7, 1, - 2)) [0, 3] [- 7, - 5, - 3] [7, 5, 3] 25

Exercises type(9.5) type(2) type(2.0) type(0.0) type(0)

type(2) type(0.0) type(2+3) type(2.0+3) type(2*3) type(2/3) type(2//3) type(2%3) Exercises

Questions from past exams What is the output of the following Python program? x = list(rang(7, 1, - 2)) print(x[- 2]) A. 1 B. 2 C. 3 D. 5 E. 7

Questions from past exams What is the result of evaluating the following expression 2**4+9/3*2-2? A. 2.166666667 B. 14.666666668 C. 20.0 D. 36.0 E. None of the above

Questions from past exams What is the output of the following Python program? def testfun(a, b, c): print(a+b+c) testfun(11, 12, 13 ) A. 36 B. 2313 C. 2313 D. 23+ 13 E. TypeError

Questions from past exams What is the output of the following Python program? def func(x, y): y = y*2 x = x+y return x print(func( 1, 2 )) A.5 B.23 C.14 D.122 E.211