CS 100: Roadmap to Computing

Slides:



Advertisements
Similar presentations
CS 100: Roadmap to Computing Fall 2014 Lecture 0.
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
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
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
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.
String Escape Sequences
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Recitation 1 Programming for Engineers in Python.
Objectives You should be able to describe: Data Types
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.
2440: 211 Interactive Web Programming Expressions & Operators.
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.
Introduction to Programming with RAPTOR
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
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 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.
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
Fundamentals of Programming I Overview of Programming
Chapter 2 Variables.
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
ITEC113 Algorithms and Programming Techniques
Multiple variables can be created in one declaration
Variables, Expressions, and IO
Java Programming: From Problem Analysis to Program Design, 4e
Python Data Types Expressions, Variables, and Assignments Strings
Introduction to Python and programming
Introduction to C++ Programming
Variables ICS2O.
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
Introduction to Programming
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
Unit 3: Variables in Java
Chapter 2 Variables.
CS 100: Roadmap to Computing
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

CS 100: Roadmap to Computing Winter 2017-2018 Lecture 0 Python Data Types (I)

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

What this course is about 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, using exactly defined data types and actions Writing programs A program is written in a computer language (e.g., Python) and implements an algorithm

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

Computer language v. natural language Introduction to Computing Using Python Natural language (e.g., English, Arabic, Chinese) Very many words Evolved, not designed. Meaning is derived by association and usage. Has grammatical structure, but very permissive of mistakes, variation and ambiguity Meant for a human being Programming (computer) language Very few words (a few dozen in Python) Meaning is exact and is assigned by the language designer Very persnickety about accuracy. A single mistake destroys the correctness of a program. Can be executed by a special type of machine (a computer)

Arithmetic data types Counting type (int) Introduction to Computing Using Python Counting type (int) A type of thing that is discrete and can be counted Similar to the integer in mathematics The value is made up of the digits 0-9 and (optionally) a sign >>> numberOfLittlePigs = 3 >>> numberOfLittlePigs 3 >>> type(numberOfLittlePigs) class <'int'> Floating-point arithmetic type Can have a non-unit value Similar to (but not exactly the same as) a decimal number in mathematics The value is made up of the digits 0-9, a decimal point and (optionally) a sign >>> pi = 3.14159 >>> type(pi) class <'float'>

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

Boolean (logical, T/F) data type Introduction to Computing Using Python 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 <, >, ==, !=, <=, and >= >>> 2 < 3 True >>> 2 > 3 False >>> 2 == 3 >>> 2 != 3 >>> 2 <= 3 >>> 2 >= 3 >>> 2+4 == 2*(9/3) In an expression containing arithmetic and comparison operators: Arithmetic operators are evaluated first Comparison operators are evaluated next

Boolean operators Python can evaluate more complex Boolean expressions Introduction to Computing Using Python >>> 2<3 and 3<4 True >>> 4==5 and 3<4 False >>> False and True >>> True and True >>> 4==5 or 3<4 >>> False or True >>> False or False >>> not(3<4) >>> not(True) >>> not(False) >>> 4+1==5 or 4-1<4 Python can evaluate more complex Boolean expressions Every Boolean expression evaluates to True or False Boolean expressions may be combined using Boolean operators and, or, and not 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 >>> 25 - 21 4 >>> 14.99 + 27.95 + 19.83 62.769999999999996 >>> 20*15 300 >>> 2**10 1024 >>> min(3,1,8,-2,5,-3,0) -3 >>> 3 == 4-2 False >>> 17//5 == 3 True >>> 17%5 == 3 >>> 284%2 == 0 >>> 284%2 == 0 and 284%3 == 0 >>> 284%2 == 0 or 284%3 == 0 Translate the following into Python algebraic or Boolean expressions and then evaluate them: The difference between Annie’s age (25) and Ellie’s (21) The total of $14.99, $27.95, and $19.83 The area of a 20 x 15 rectangle 2 to the 10th power The minimum of 3, 1, 8, -2, 5, -3, and 0 3 equals 4-2 The value of 17//5 is 3 The value of 17%5 is 3 284 is even 284 is even and 284 is divisible by 3 284 is even or 284 is divisible by 3

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

Naming rules (Variable) names can contain these characters: Introduction to Computing Using Python Naming rules >>> my_x2 = 21 >>> my_x2 21 >>> 2x = 22 SyntaxError: invalid syntax >>> new_temp = 23 >>> newTemp = 23 >>> counter = 0 >>> temp = 1 >>> price = 2 >>> age = 3 (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. Names can start with a capital letter or underscore, but don't do it. For a multiple-word name, use either the underscore as the delimiter or camelCase capitalization Meaningful names are important. They make your program more readable by a human being (such as you).

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

String operators To view all operators, use the help() tool Usage Introduction to Computing Using Python >>> s = 'rock' >>> t = 'climbing' >>> s == 'rock' True >>> s != t >>> s < t #explain this answer! False >>> s > t >>> s + t 'rockclimbing' >>> s + ' ' + t 'rock climbing' >>> 5 * s 'rockrockrockrockrock' >>> 20 * '_' '____________________' >>> 'o' in s >>> 'o' in t >>> 'bi' in t >>> len(t) 8 Usage Explanation 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 To view all operators, use the help() tool >> help(str) Help on class str in module builtins: class str(object) | str(string[, encoding[, errors]]) -> str ...

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

Index and indexing operator Introduction to Computing Using Python 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 index i and returns a substring consisting of the single character at index i 'Apple' s = 1 2 3 4 s[0] 'A' >>> s = 'Apple' >>> s[0] 'A' >>> s[1] 'p' >>> s[4] 'e' s[1] 'p' s[2] 'p' s[3] 'l' s[4] 'e'

Negative index 'A p p l e' 'e' 'l' 'A' Introduction to Computing Using Python 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 … -5 -4 -3 -2 -1 >>> s = 'Apple' >>> s[-1] 'e' >>> s[-2] 'l' >>> s[-5] 'A' 'A p p l e' s = 1 2 3 4 s[-1] 'e' s[-2] 'l' s[-5] 'A'

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