CS 100: Roadmap to Computing Fall 2014 Lecture 0.

Slides:



Advertisements
Similar presentations
CS&E 1111 Exfunctions Using Functions in Excel Objectives: Using Excel functions l SUM, MIN, MAX, AVERAGE, COUNT, COUNTA l ROUND l COUNTIF, SUMIF, AVERAGEIF.
Advertisements

Types and Arithmetic Operators
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Data types and variables
Lecture 3 August 31 Chapter 3. Chapter 3 – numbers, string, booleans integer: MATLAB stores numeric data as double-precision floating point (double) by.
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Introduction to a Programming Environment
JavaScript, Third Edition
Introduction to C Programming
1 Lecture 3  Lexical elements  Some operators:  /, %, =, +=, ++, --  precedence and associativity  #define  Readings: Chapter 2 Section 1 to 10.
Data Types. Every program must deal with data The data is usually described as a certain type This type determines what you can do with the data and how.
Recitation 1 Programming for Engineers in Python.
Fundamentals of Python: From First Programs Through Data Structures
Objectives You should be able to describe: Data Types
A First Book of ANSI C Fourth Edition
Python Data Types Expressions, Variables, and Assignments Strings
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
Introduction to Computing Using Python Python  Python is an interactive language.  Java or C++: compile, run  Also, a main function or method  Python:
CIS Computer Programming Logic
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Computational Linguistics Programming I.
Python Lesson Week 01. What we will accomplish today Install Python on your computer Using IDLE interactively to explore String Variables if/else while.
2440: 211 Interactive Web Programming Expressions & Operators.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Python and programming Ruth Anderson UW CSE 140 Winter
Variables, Expressions and Statements
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Python Conditionals chapter 5
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
3 Basics © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Introduction to Programming
Fundamentals of Programming I Overview of Programming
Topics Designing a Program Input, Processing, and Output
CSc 120 Introduction to Computer Programing II Adapted from slides by
Introduction to Python
BASIC ELEMENTS OF A COMPUTER PROGRAM
CS 100: Roadmap to Computing
Multiple variables can be created in one declaration
Variables, Expressions, and IO
Python Data Types Expressions, Variables, and Assignments Strings
Introduction to Python
Introduction to C++ Programming
Fundamentals 2.
CS 100: Roadmap to Computing
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
CS 100: Roadmap to Computing
General Computer Science for Engineers CISC 106 Lecture 03
CS 100: Roadmap to Computing
Introduction to Strings
Presentation transcript:

CS 100: Roadmap to Computing Fall 2014 Lecture 0

Introduction to Computing Using Python Python Data Types (I)  Expressions, Variables, and Assignments  Integers  Floats  Booleans  Strings

Introduction to Computing Using Python What this course is about Thinking algorithmically An algorithm is a precise, step-by-step solution to a problem Designing computational models A computational model is a simplified description of some piece of the world A program is written in a computer language (e.g., Python) and implements an algorithm Writing programs

Introduction to Computing Using Python Our tools A computer language (Python) Instant task: Go to python.org and read about Python language, see how to download Python The Python shell Instant task: start the Python shell and type in a few expressions Instant task: open a new window, write some text and save the file The Python editor

Introduction to Computing Using Python Computer language v. natural language Natural language (e.g., English, Arabic, Chinese) o Very many words o Not designed, meaning derived by association o Has grammatical structure, but very permissive of mistakes, variation and ambiguity o Meant for a human being o Very few words o Meaning is assigned o Very persnickety about accuracy o Can be executed by a machine (computer) Programming (computer) language

Introduction to Computing Using Python Arithmetic data types Counting type (int) o A type of thing that is discrete and can be counted o Similar to the integer in mathematics >>> numberOfLittlePigs = 3 >>> type(numberOfLittlePigs) class o Can have a non-unit value o Similar to the decimal in mathematics >>> pi = >>> type(pi) class Floating-point arithmetic type

Introduction to Computing Using Python Algebraic expressions >>> >>> >>> >>> >>> 2*(3+1) 8 >>> 5/2 2.5 >>> 5//2 2 >>> 14//3 4 >>> 14%3 2 >>> >>> >>> 2*(3+1) 8 >>> 5/2 2.5 >>> 5//2 2 >>> 14//3 4 >>> 14%3 2 >>> >>> >>> 2*(3+1) 8 >>> 5/2 2.5 >>> 5//2 2 >>> 14//3 4 >>> 14%3 2 >>> 2**3 8 >>> abs(-3.2) 3.2 >>> >>> >>> 2*(3+1) 8 >>> 5/2 2.5 >>> 5//2 2 >>> 14//3 4 >>> 14%3 2 >>> 2**3 8 >>> abs(-3.2) 3.2 >>> >>> >>> 2*(3+1) 8 >>> 5/2 2.5 >>> 5//2 2 >>> 14//3 4 >>> 14%3 2 >>> 2**3 8 >>> abs(-3.2) 3.2 >>> min(23,41,15,24) 15 >>> >>> >>> 2*(3+1) 8 >>> 5/2 2.5 >>> 5//2 2 >>> 14//3 4 >>> 14%3 2 >>> 2**3 8 >>> abs(-3.2) 3.2 >>> min(23,41,15,24) 15 >>> >>> >>> 2*(3+1) 8 >>> 5/2 2.5 >>> 5//2 2 >>> 14//3 4 >>> 14%3 2 >>> 2**3 8 >>> abs(-3.2) 3.2 >>> min(23,41,15,24) 15 >>> max(23,41,15,24) 41 >>> >>> >>> 2*(3+1) 8 >>> 5/2 2.5 >>> 5//2 2 >>> 14//3 4 >>> 14%3 2 >>> 2**3 8 >>> abs(-3.2) 3.2 >>> min(23,41,15,24) 15 >>> max(23,41,15,24) 41 >>> >>> >>> >>> >>> >>> >>> 2*(3+1) 8 >>> >>> >>> 2*(3+1) 8 >>> >>> >>> 2*(3+1) 8 >>> 5/2 2.5 >>> >>> >>> 2*(3+1) 8 >>> 5/2 2.5 >>> >>> >>> 2*(3+1) 8 >>> 5/2 2.5 >>> 5//2 2 >>> >>> >>> 2*(3+1) 8 >>> 5/2 2.5 >>> 5//2 2 The Python interactive shell can be used to evaluate algebraic expressions 14//3 is the quotient when 14 is divided by 3 and 14%3 is the remainder 2**3 is 2 to the 3 rd power abs(), min(), and max() are functions abs() takes a number as input and returns its absolute value min() (resp., max() ) take an arbitrary number of inputs and return the “smallest” (resp., “largest”) among them >>> >>> >>> 2*(3+1) 8 >>> 5/2 2.5 >>> 5//2 2 >>> 14//3 4 >>> 14%3 2 >>> 2**3 8 >>> >>> >>> 2*(3+1) 8 >>> 5/2 2.5 >>> 5//2 2 >>> 14//3 4 >>> 14%3 2 >>> 2**3 8

Introduction to Computing Using Python Boolean (T/F) data type In addition to algebraic expressions, Python can evaluate Boolean (T/F) expressions A Boolean expression evaluates to True or False Boolean expressions often involve comparison operators, ==, !=, = >>> 2 < 3 True >>> 2 > 3 False >>> 2 == 3 False >>> 2 != 3 True >>> 2 <= 3 True >>> 2 >= 3 False >>> 2+4 == 2*(9/3) True >>> 2 < 3 True >>> 2 > 3 False >>> 2 == 3 False >>> 2 != 3 True >>> 2 <= 3 True >>> 2 >= 3 False >>> 2+4 == 2*(9/3) True In a an expression containing algebraic and comparison operators: Algebraic operators are evaluated first Comparison operators are evaluated next

Introduction to Computing Using Python Boolean operators Python can evaluate more complex Boolean expressions Boolean expressions evaluate to True or False Boolean expressions may be combined using Boolean operators and, or, and not >>> 2<3 and 3<4 True >>> 4==5 and 3<4 False >>> False and True False >>> True and True True >>> 4==5 or 3<4 True >>> False or True True >>> False or False False >>> not(3<4) False >>> not(True) False >>> not(False) True >>> 4+1==5 or 4-1<4 True >>> 2<3 and 3<4 True >>> 4==5 and 3<4 False >>> False and True False >>> True and True True >>> 4==5 or 3<4 True >>> False or True True >>> False or False False >>> not(3<4) False >>> not(True) False >>> not(False) True >>> 4+1==5 or 4-1<4 True In a an expression containing algebraic, comparison, and Boolean operators: Algebraic operators are evaluated first Comparison operators are evaluated next Boolean operators are evaluated last

Introduction to Computing Using Python Exercise >>> >>> >>> 20* >>> 2** >>> min(3, 1, 8, -2, 5, -3, 0) -3 >>> 3 == 4-2 False >>> 17//5 == 3 True >>> 17%5 == 3 False >>> 284%2 == 0 True >>> 284%2 == 0 and 284%3 == 0 False >>> 284%2 == 0 or 284%3 == 0 True >>> >>> >>> 20* >>> 2** >>> min(3, 1, 8, -2, 5, -3, 0) -3 >>> 3 == 4-2 False >>> 17//5 == 3 True >>> 17%5 == 3 False >>> 284%2 == 0 True >>> 284%2 == 0 and 284%3 == 0 False >>> 284%2 == 0 or 284%3 == 0 True Translate the following into Python algebraic or Boolean expressions and then evaluate them: a)The difference between Annie’s age (25) and Ellie’s (21) b)The total of $14.99, $27.95, and $19.83 c)The area of a rectangle of length 20 and width 15 d)2 to the 10 th power e)The minimum of 3, 1, 8, -2, 5, -3, and 0 f)3 equals 4-2 g)The value of 17//5 is 3 h)The value of 17%5 is 3 i)284 is even j)284 is even and 284 is divisible by 3 k)284 is even or 284 is divisible by 3

Introduction to Computing Using Python Variables and assignments >>> x = 3 >>> = Just as in algebra, a value can be assigned to a variable, such as x >>> x = 3 >>> x 3 >>> 4*x 16 >>> >>> x = 3 >>> x 3 >>> 4*x 16 >>> When variable x appears inside an expression, it evaluates to its assigned value >>> x = 3 >>> x 3 >>> 4*x 16 >>> y Traceback (most recent call last): File " ", line 1, in y NameError: name 'y' is not defined >>> x = 3 >>> x 3 >>> 4*x 16 >>> y Traceback (most recent call last): File " ", line 1, in y NameError: name 'y' is not defined >>> x = 3 >>> x 3 >>> 4*x 16 >>> y Traceback (most recent call last): File " ", line 1, in y NameError: name 'y' is not defined >>> y = 4*x >>> >>> x = 3 >>> x 3 >>> 4*x 16 >>> y Traceback (most recent call last): File " ", line 1, in y NameError: name 'y' is not defined >>> y = 4*x >>> A variable (name) does not exist until it is assigned The assignment statement has the format is evaluated first, and the resulting value is assigned to variable >>> x = 3 >>> x 3 >>> 4*x 16 >>> y Traceback (most recent call last): File " ", line 1, in y NameError: name 'y' is not defined >>> y = 4*x >>> y 16.0 >>> x = 3 >>> x 3 >>> 4*x 16 >>> y Traceback (most recent call last): File " ", line 1, in y NameError: name 'y' is not defined >>> y = 4*x >>> y 16.0

Introduction to Computing Using Python Naming rules (Variable) names can contain these characters: a through z A through Z the underscore character _ digits 0 through 9 Names cannot start with a digit though For a multiple-word name, use either the underscore as the delimiter or camelCase capitalization Short and meaningful names are ideal >>> My_x2 = 21 >>> My_x2 21 >>> My_x2 = 21 >>> My_x2 21 >>> My_x2 = 21 >>> My_x2 21 >>> 2x = 22 SyntaxError: invalid syntax >>> >>> My_x2 = 21 >>> My_x2 21 >>> 2x = 22 SyntaxError: invalid syntax >>> >>> My_x2 = 21 >>> My_x2 21 >>> 2x = 22 SyntaxError: invalid syntax >>> new_temp = 23 >>> newTemp = 23 >>> >>> My_x2 = 21 >>> My_x2 21 >>> 2x = 22 SyntaxError: invalid syntax >>> new_temp = 23 >>> newTemp = 23 >>> >>> My_x2 = 21 >>> My_x2 21 >>> 2x = 22 SyntaxError: invalid syntax >>> new_temp = 23 >>> newTemp = 23 >>> counter = 0 >>> temp = 1 >>> price = 2 >>> age = 3 >>> My_x2 = 21 >>> My_x2 21 >>> 2x = 22 SyntaxError: invalid syntax >>> new_temp = 23 >>> newTemp = 23 >>> counter = 0 >>> temp = 1 >>> price = 2 >>> age = 3

"Hello, World!" Introduction to Computing Using Python Text data type (string) In addition to number and Boolean values, Python support string values A string value is represented as a sequence of characters enclosed within quotes >>> 'Hello, World!' 'Hello, World!' >>> >>> 'Hello, World!' 'Hello, World!' >>> 'Hello, World!' A string value can be assigned to a variable String values can be manipulated using string operators and functions >>> 'Hello, World!' 'Hello, World!' >>> s = 'rock' >>> t = 'climbing' >>> >>> 'Hello, World!' 'Hello, World!' >>> s = 'rock' >>> t = 'climbing' >>>

Introduction to Computing Using Python String operators >>> 'Hello, World!' 'Hello, World!' >>> s = 'rock' >>> t = 'climbing' >>> s == 'rock' True >>> s != t True >>> s < t False >>> s > t True >>> s + t 'rockclimbing' >>> s + ' ' + t 'rock climbing' >>> 5 * s 'rockrockrockrockrock' >>> 30 * '_' '______________________________' >>> 'o' in s True >>> 'o' in t False >>> 'bi' in t True >>> len(t) 8 >>> 'Hello, World!' 'Hello, World!' >>> s = 'rock' >>> t = 'climbing' >>> s == 'rock' True >>> s != t True >>> s < t False >>> s > t True >>> s + t 'rockclimbing' >>> s + ' ' + t 'rock climbing' >>> 5 * s 'rockrockrockrockrock' >>> 30 * '_' '______________________________' >>> 'o' in s True >>> 'o' in t False >>> 'bi' in t True >>> len(t) 8 UsageExplanation x in s x is a substring of s x not in s x is not a substring of s s + t Concatenation of s and t s * n, n * s Concatenation of n copies of s s[i] Character at index i of s len(s) (function) Length of string s >> help(str) Help on class str in module builtins: class str(object) | str(string[, encoding[, errors]]) -> str... >> help(str) Help on class str in module builtins: class str(object) | str(string[, encoding[, errors]]) -> str... To view all operators, use the help() tool

Introduction to Computing Using Python Exercise >>> s1 'good' >>> s2 'bad' >>> s3 'silly' >>> >>> s1 'good' >>> s2 'bad' >>> s3 'silly' >>> Write Python expressions involving strings s1, s2, and s3 that correspond to: a)'ll' appears in s3 b)the blank space does not appear in s1 c)the concatenation of s1, s2, and s3 d)the blank space appears in the concatenation of s1, s2, and s3 e)the concatenation of 10 copies of s3 f)the total number of characters in the concatenation of s1, s2, and s3 >>> s1 'good' >>> s2 'bad' >>> s3 'silly' >>> 'll' in s3 True >>> ' ' not in s1 True >>> s1 + s2 + s3 'goodbadsilly’ >>> ' ' in s1 + s2 + s3 False >>> 10*s3 'sillysillysillysillysillysillysillysillysillysilly' >>> len(s1+s2+s3) 12 >>> >>> s1 'good' >>> s2 'bad' >>> s3 'silly' >>> 'll' in s3 True >>> ' ' not in s1 True >>> s1 + s2 + s3 'goodbadsilly’ >>> ' ' in s1 + s2 + s3 False >>> 10*s3 'sillysillysillysillysillysillysillysillysillysilly' >>> len(s1+s2+s3) 12 >>>

Introduction to Computing Using Python Index and indexing operator 'A' 'p' 'l' 'e' s[0] = s[1] = s[2] = s[3] = s[4] = s = The index of an item in a sequence is its position with respect to the first item The first item has index 0, The index of an item in a sequence is its position with respect to the first item The first item has index 0, The second has index 1, The index of an item in a sequence is its position with respect to the first item The first item has index 0, The second has index 1, The third has index 2, … The indexing operator [] takes a nonnegative index i and returns a string consisting of the single character at index i >>> s = 'Apple' >>> s[0] 'A' >>> s[1] 'p' >>> s[4] 'e' >>> s = 'Apple' >>> s[0] 'A' >>> s[1] 'p' >>> s[4] 'e' 'A p p l e'

Introduction to Computing Using Python Negative index 'A' 'l' 'e' s[-1] = s[-2] = s[-5] = s = 'A p p l e' A negative index is used to specify a position with respect to the “end” The last item has index -1, The second to last item has index -2, The third to last item has index -3, … >>> s = 'Apple' >>> s[-1] 'e' >>> s[-2] 'l' >>> s[-5] 'A' >>> s = 'Apple' >>> s[-1] 'e' >>> s[-2] 'l' >>> s[-5] 'A'

Introduction to Computing Using Python Exercise >>> s = 'abcdefgh' >>> >>> s = 'abcdefgh' >>> String s is defined to be 'abcdefgh' Write expressions using s and the indexing operator [] that return the following strings: a)'a' b)'c' c)'h' d)'f' >>> s = 'abcdefgh' >>> s[0] 'a' >>> s[2] 'c' >>> s[7] 'h' >>> s[-1] 'h' >>> s[-3] 'f' >>> >>> s = 'abcdefgh' >>> s[0] 'a' >>> s[2] 'c' >>> s[7] 'h' >>> s[-1] 'h' >>> s[-3] 'f' >>>