CS 102 Lab 3 Fall 2012

Similar documents
Downloaded from Chapter 2. Functions

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

CS 1110, LAB 2: FUNCTIONS AND ASSIGNMENTS

CS 1110, LAB 2: ASSIGNMENTS AND STRINGS

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

Fundamentals: Expressions and Assignment

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

Python Programming Exercises 1

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

Expressions and Variables

Computing with Numbers

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

Python Class-Lesson1 Instructor: Yao

Strings in Python 1 Midterm#1 Exam Review CS 8: Introduction to Computer Science Lecture #6

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

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide

Lecture 3. Input, Output and Data Types

ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python

CS177 Python Programming. Recitation 2 - Computing with Numbers

The Practice of Computing Using PYTHON. Chapter 4. Working with Strings. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

MICROPROCESSOR SYSTEMS INTRODUCTION TO PYTHON

Python Day 3 11/28/16

Introduction to Python - Part I CNV Lab

Notes on Chapter 1 Variables and String

An Introduction to Python for KS4!

Arithmetic and IO. 25 August 2017

Com S 127 Lab 2. For the first two parts of the lab, start up Wing 101 and use the Python shell window to try everything out.

Text Input and Conditionals

1. The programming language C is more than 30 years old. True or False? (Circle your choice.)

CSE 115. Introduction to Computer Science I

Level 3 Computing Year 2 Lecturer: Phil Smith

Introduction to TURING

Tic Tac Toe Game! Day 8

Variable and Data Type I

ECS Baruch Lab 5 Spring 2019 Name NetID (login, like , not your SUID)

Lesson 9: Decimal Expansions of Fractions, Part 1

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

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

Week 3: Objects, Input and Processing

CMPS 10 Introduction to Computer Science Lecture Notes

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

A tutorial for the Sliderule Activity part 2. The Custom Scales

COMP1730/COMP6730 Programming for Scientists. Strings

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

Lecture 2: Python Arithmetic

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

Getting started with Java

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

Hexadecimal Numbers. Journal: If you were to extend our numbering system to more digits, what digits would you use? Why those?

Python Input, output and variables

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

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

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

Digital Fundamentals. CHAPTER 2 Number Systems, Operations, and Codes

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

The float type and more on variables FEB 6 TH 2012

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

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

The C++ Language. Arizona State University 1

Python Intro GIS Week 1. Jake K. Carr

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

CSCE 110 Programming I

ROUNDING ERRORS LAB 1. OBJECTIVE 2. INTRODUCTION

MATH Ms. Becker

Lab - Create User Accounts in Windows 8

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

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

Iteration. Chapter 7. Prof. Mauro Gaspari: Mauro Gaspari - University of Bologna -

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

CS 1110, LAB 1: EXPRESSIONS AND ASSIGNMENTS First Name: Last Name: NetID:

Operators. Lecture 3 COP 3014 Spring January 16, 2018

QUIZ: Generations of computer technology. Hardware:

Getting Started Values, Expressions, and Statements CS GMU

CSCA20 Worksheet Strings

CS 106 Introduction to Computer Science I

ENGR 101 Engineering Design Workshop

Exercise: Inventing Language

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

12. Numbers. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Part III Appendices 165

Lists How lists are like strings

WHOLE NUMBER AND DECIMAL OPERATIONS

Lecture 1. Types, Expressions, & Variables

Full file at

Algorithms and Programming I. Lecture#12 Spring 2015

Python: common syntax

MODULE 02: BASIC COMPUTATION IN JAVA

CS102: Variables and Expressions

COM110: Lab 2 Numeric expressions, strings, and some file processing (chapters 3 and 5)

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Variables, expressions and statements

7. (2 pts) str( str( b ) ) str '4' will not compile (single, double, or triple quotes

Lecture 3. Functions & Modules

CS 1110, LAB 10: ASSERTIONS AND WHILE-LOOPS 1. Preliminaries

Lab 03 - x86-64: atoi

APCS Semester #1 Final Exam Practice Problems

MIT AITI Python Software Development

Scripting Languages. Python basics

Variable and Data Type I

Transcription:

Name: The symbol marks programming exercises. Upon completion, always capture a screenshot and include it in your lab report. Email lab report to instructor at the end of the lab. Review of built-in functions covered last time: int() and float() help(" ") Review of math functions covered last time: import math math.sqrt() math.pi math.sin(), math.cos(), math.radians() math.floor() and math.ceil() ------------------------------------------------------------------------------------------------------------------------- Use the Python shell to calculate how many bits are needed to represent each set of things: o The 3143 counties in the USA o The 312,931,000 people in the USA 1 o The 196 countries of the world Hint: use the log() function from the math module. If you forgot how to use it, use the help command: 1 2010 census

Binary fractions Convert these binary fractions to decimal using the negative powers of 2. Show your work! 0.1 = 0.01 = 0.11 0.101 = Convert these decimal fractions to binary using repeated multiplication. If the fraction is infinite, stop at 8 bits. Show your work! 0.75 = 0. 0.625 = 0. 0.4 = 0.

Variables in Python What if the value assigned or obtained at one point in our session is needed later for further processing? With the tools we have so far, there s no choice but to re-enter it manually. (Remember how we implemented the repeated division algorithm in the previous lab!) Python, like any other programming language, allows the user to define variables, which are just places in the computer memory where data is stored. In Python, variables are declared by simply giving them values, like so: >>> count = 25 The value is from now on available by calling the variable name: >>> count 25 If we try to use a variable that has not been initialized, an error occurs try it! Values stored in variables can be modified at any time: >>> count = 42 It is possible to modify the variable, then reassign the new value to the same variable, all in one Python command. A heavily-used technique is the incrementation of a variable: >>> count = count + 1 It is possible (but rarely used) to assign several variables in the same Python line: >>> a, b = 42, 43 Enter in the shell the commands on p.8. In batch mode, write a program that uses two variables, q (quotient) and r (remainder) to automate the repeated division algorithm from Lab 2. Test your solution with the numeric example from Ch.2:

Hint: We may use a third variable R to store the base. But we don t need a separate variable for the number to convert (179), since it can be stored directly in q. Strings of characters in Python Strings are one of Python s collection classes, more precisely a sequential collection class. Read p.9 of the lab manual. Strings can be entered with single, double or triple quotes. Individual characters in a string are numbered from 0 to n-1 and they can be accessed using square brackets. Individual characters cannot be changed! Enter in interactive mode the commands on p.10 of the Python Manual (no screenshot) Then do the same thing using your own name, and print the 5 th character in it using brackets. We need not stop at individual characters! Python allows to extract any sub-string using the slicing operator. Just don t forget that in a string of N characters, the first one has index 0, and the last one has index N-1. Referring to index N is illegal! Read the commands at the bottom of p.11 of the Python Manual. Then place in a string variable your own full name (including the middle initial 2 ) and use slicing to extract and print the last name, first name, and middle initial. Sub-strings can be put together with the concatenation operator +, which we encountered in the first lab. This is important, because, as you remember, individual parts of a string cannot be changed in Python. The solution is to create a new string, whose parts come from the old one! 2 Or use a made-up letter.

Use the string with your own full name (declared before) to create a new string, with the format 'Last_name, middle_name middle_initial.' E.g. 'Jane A. Doe' becomes 'Doe, Jane A.' Also available for strings (any any other sequential data type in Python), are the operators: Repetition * Length len Membership in Finally, it it possible to iterate (repeat a certain action) over each character in the string, using the for command. In interactive mode, enter the commands at the top of p.11, and the ones on p.12 (no screenshot) In a batch file, enter commands to do the following: Assign 'tic-tac-' to the variable str1 Use the repetition operator * to assign ten 'tic-tac-'s to the variable str2 Print str1 and str2 Print the length of str2 Test if the sub-string 'ic' is in str2 Print the characters in str1 one at a time, using for For an overview, consult Table 1 on p.10. Copy Table 1 in your cheat-sheet. Today we ve covered pp.7-12 of the Python manual.