ENGR (Socolofsky) Week 02 Python scripts

Similar documents
Python Programming Exercises 1

ENGR (Socolofsky) Week 07 Python scripts

ENGR 102 Engineering Lab I - Computation

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

Lecture 2: Python Arithmetic

User-Defined Function

ENGR 101 Engineering Design Workshop

CSE 115. Introduction to Computer Science I

Lecture 3. Input, Output and Data Types

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

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

Lecture 3: Functions & Modules (Sections ) CS 1110 Introduction to Computing Using Python

Lessons on Python Numbers

Python 2 Conditionals and loops Matthew Egbert CS111

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

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

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

ENGR 102 Engineering Lab I - Computation

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

Variable and Data Type I

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

Algorithms and Programming I. Lecture#12 Spring 2015

Lecture 3: Functions & Modules

CSI Lab 02. Tuesday, January 21st

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Lecture 3. Functions & Modules

Functions with Parameters and Return Values

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

CS 112: Intro to Comp Prog

CMPT 120 Basics of Python. Summer 2012 Instructor: Hassan Khosravi

CME 193: Introduction to Scientific Python Lecture 1: Introduction

PROGRAMMING FUNDAMENTALS

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

COMP519 Web Programming Lecture 11: JavaScript (Part 2) Handouts

Variable and Data Type I

Lecture 3. Functions & Modules

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

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

OUTLINE. Number system. Creating MATLAB variables Overwriting variable Error messages Making corrections Entering multiple statements per line

This tutorial will teach you about operators. Operators are symbols that are used to represent an actions used in programming.

CS 1803 Individual Homework 1 Python Practice Due: Wednesday, January 26th, before 6 PM Out of 100 points

Python lab session 1

MIT AITI Python Software Development

Introduction to Mathematical and Scientific Programming TUTORIAL WEEK 1 (MATH 1MP3) Winter 2019

Introduction to Python

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

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

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

Lecture 3 Tao Wang 1

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

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

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

ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python

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

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

CS 102 Lab 3 Fall 2012

Modular Arithmetic. Marizza Bailey. December 14, 2015

Introduction to Python: Data types. HORT Lecture 8 Instructor: Kranthi Varala

CIS192 Python Programming. Robert Rand. August 27, 2015

Lecture 1. Introduction to course, Welcome to Engineering, What is Programming and Why is this the first thing being covered in Engineering?

Not-So-Mini-Lecture 6. Modules & Scripts

Topic 2: Introduction to Programming

Data Handing in Python

Introduction to Python and programming. Ruth Anderson UW CSE 160 Winter 2017

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

Introduction to Computation for the Humanities and Social Sciences. CS 3 Chris Tanner

Computing with Numbers Zelle - Chapter 3

Datatypes, Variables, and Operations

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

COMP1730/COMP6730 Programming for Scientists. Sequence types, part 2

CSI33 Data Structures

A simple interpreted language

Mathematical Data Operators

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

At full speed with Python

Built-in Types of Data

Fundamentals: Expressions and Assignment

Chapter Two PROGRAMMING WITH NUMBERS AND STRINGS

Intro to Python & Programming. C-START Python PD Workshop

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

CSC Web Programming. Introduction to JavaScript

Expressions and Variables

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

CSCI 121: Anatomy of a Python Script

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

C++ Programming Applied to Robotics, Mark Aull Lesson 2: Intro to Visual Studio, debugger Intro to C++, variables, conditionals, loops, strings

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

ENGR 102 Engineering Lab I - Computation

Invent Your Own Computer Games with Python

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

Physics 514 Basic Python Intro

CS313D: ADVANCED PROGRAMMING LANGUAGE

Level 3 Computing Year 2 Lecturer: Phil Smith

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

COMP 110 Introduction to Programming. What did we discuss?

Ch.2: Loops and lists

Math 1 Variable Manipulation Part 2 Exponents & Roots

Week 2. expressions, variables, for loops

Visual Basic for Applications

Transcription:

ENGR 102-213 (Socolofsky) Week 02 Python scripts Listing for script.py 1 # data_types.py 2 # 3 # Lecture examples of using various Python data types and string formatting 4 # 5 # ENGR 102-213 6 # Scott Socolofsky 7 # August 2018 8 9 import math 10 11 # In this first block, we will perform various mathematical operations on 12 # different data types and let the print () function print the raw, 13 # unformatted output so that we can see what data types result. 14 15 # Addition 16 i = 1 17 i = i + 1 18 print (' Addition of two integers :') 19 print (i) 20 21 i += 1 22 print (' Addition of two integers using +=: ') 23 print (i) 24 25 x = 7. 15 26 x = x + 2. 54 27 print (' Addition of two floats :') 28 print (x) 29 30 print (' Addition of a float and integer :') 31 x = x + i 32 print (x) 33 34 x += 1 35 print (' Addition of a float and integer using +=: ') 36 print (x) 37 print (' ') 38 39 # Subtraction 40 i = 10 41 i = i - 1

42 print (' Subtraction of two integers :') 43 print (i) 44 45 i -= 1 46 print (' Subtraction of two integers using -=: ') 47 print (i) 48 49 x = 2. 54 50 x = x - 7. 15 51 print (' Subtraction of two floats :') 52 print (x) 53 54 print (' Subtraction of a float and integer :') 55 x = 7. 15 56 i = 3 57 x = x - i 58 print (x) 59 60 print (' Subtraction of a float and integer using -=: ') 61 x = 7. 15 62 x -= 1 63 print (x) 64 print (' ') 65 66 # Multiplication 67 68 x = 7. 15 69 y = 2. 54 70 z = x * y 71 print (' Multiplication of two floats :') 72 print (z) 73 74 n = 10 75 w = z * n 76 print (' Multiplication of a float and integer :') 77 print (w) 78 79 s = 'ENGR 102 -- ' 80 heading = 5 * s 81 print (' Multiplication of an integer and string :') 82 print ( heading ) 83 print (' ') 84 85 # Division 86 i = 5 87 j = 10 88 k = 7 89 n1 = j / i 90 n2 = k / i 91 print ('Division of two integers (10 / 5): ') 92 print ( n1) 93 print (' Division of two integers ( 7 / 5)') 94 print ( n2) 2

95 96 x = 7. 15 97 y = 2. 54 98 z = x / y 99 print (' Division of two floats :') 100 print ( z) 101 102 w = z / n 103 print (' Division of float by integer :') 104 print ( w) 105 106 w = n / z 107 print (' Division of an integer by a float :') 108 print ( w) 109 print (' ') 110 111 # Powers 112 u = i ** j 113 print (' Integer to an integer power :') 114 print ( u) 115 116 v = x ** j 117 print (' Float to an integer power :') 118 print ( v) 119 120 w = x ** y 121 print (' Float to a float power :') 122 print ( w) 123 124 print (' NOTE : When possible, use integers in powers since ') 125 print (' integer powers execute faster on a computer.') 126 print (' ') 127 128 # Integer division 129 x = 7. 15 130 y = 2. 54 131 z1 = x // y 132 print (' Division of two floats ( 7. 15 // 2. 54): ') 133 print ( z1) 134 135 # Modulus operator 136 z2 = x % y 137 print (' Division of two floats ( 7. 15 % 2. 54): ') 138 print ( z2) 139 print (' ') 140 141 # In this section, we format many of the variables computed above within 142 # various print statements. 143 print (' Format an integer as an integer, part 1: % d' % i) 144 print (' Format an integer as an integer, part 2: % 5. 5d' % i) 145 print (' Format an integer as a float : % 5. 5g' % i) 146 print (' Format a float as a float ( default ): % g' % w) 147 print (' Format a float as a float ( formatted ): % 7. 7g' % w) 3

148 print (' ') 149 print (' Include an apostrophe to symbolize feet in a string :') 150 print (" The wall is %4.4g' long " % w) 151 152 # We can also include multiple strings in a print statement. 153 x = 7. 15 154 y = 3. 45 155 z = x // y 156 print (' Printing a result using multiple strings separated by comma ') 157 print ('The division of ', x, '// ', y, '=', z) 158 print (' Printing a result using string concatenation ') 159 print ('The division of ' + str (x) + '// ' + str (y) + ' = ' + str (z)) 160 print (' ') 161 162 # Finally, we demonstrate the input function 163 print (' Enter the diameter of a sphere :') 164 d = float ( input ()) 165 vol = math.pi / 6. * d**3 166 print (' The volume of your sphere is % 8. 8g units ' % vol ) 167 print (' ') 168 169 d = float ( input (' Enter the diameter of a sphere : ')) 170 vol = math.pi / 6. * d**3 171 print (' The volume of your sphere is % 8. 8g units ' % vol ) 172 print (' ') 4

Code execution for script.py yields Addition of two integers: 2 Addition of two integers using +=: 3 Addition of two floats: 9.690000000000001 Addition of a float and integer: 12.690000000000001 Addition of a float and integer using +=: 13.690000000000001 Subtraction of two integers: 9 Subtraction of two integers using -=: 8 Subtraction of two floats: -4.61 Subtraction of a float and integer: 4.15 Subtraction of a float and integer using -=: 6.15 Multiplication of two floats: 18.161 Multiplication of a float and integer: 181.61 Multiplication of an integer and string: ENGR 102 -- ENGR 102 -- ENGR 102 -- ENGR 102 -- ENGR 102 -- Division of two integers (10 / 5): 2.0 Division of two integers (7 / 5) 1.4 Division of two floats: 2.8149606299212597 Division of float by integer: 0.281496062992126 Division of an integer by a float: 3.5524475524475525 5

Integer to an integer power: 9765625 Float to an integer power: 349188890.423927 Float to a float power: 147.88950188577866 NOTE: When possible, use integers in powers since integer powers execute faster on a computer. Division of two floats (7.15 // 2.54): 2.0 Division of two floats (7.15 % 2.54): 2.0700000000000003 Format an integer as an integer, part 1: 5 Format an integer as an integer, part 2: 00005 Format an integer as a float: 5 Format a float as a float (default): 147.89 Format a float as a float (formatted): 147.8895 Include an apostrophe to symbolize feet in a string: The wall is 147.9 long Printing a result using multiple strings separated by comma The division of 7.15 // 3.45 = 2.0 Printing a result using string concatenation The division of 7.15//3.45 = 2.0 Enter the diameter of a sphere: 3.5 The volume of your sphere is 22.449298 units Enter the diameter of a sphere: 4.5 The volume of your sphere is 47.712938 units Process finished with exit code 0 6