Introduction to Python and programming Michael Ernst UW CSE 190p Summer 2012.

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
The If/Else Statement, Boolean Flags, and Menus Page 180
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Recitation 1 Programming for Engineers in Python.
Chapter 2: Variables, Operations, and Strings CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Programming in Python Part I Dr. Fatma Cemile Serçe Atılım University
CPS120: Introduction to Computer Science
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Variables and Expressions CMSC 201 Chang (rev )
CPS120: Introduction to Computer Science Operations Lecture 9.
Introduction to Python and programming Ruth Anderson UW CSE 140 Winter
ECS 15 Variables. Outline  Using IDLE  Building blocks of programs: Text Numbers Variables!  Writing a program  Running the program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
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.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
What are Operators? Some useful operators to get you started.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
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.
Input, Output and Variables GCSE Computer Science – Python.
Control Structures I Chapter 3
Topics Designing a Program Input, Processing, and Output
Data Types and Conversions, Input from the Keyboard
Introduction to Python and programming
Data Types, Identifiers, and Expressions
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Design & Technology Grade 7 Python
Variables, Expressions, and IO
Introduction to C++ October 2, 2017.
Type Conversion, Constants, and the String Object
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Ruth Anderson UW CSE 160 Spring 2018
Ruth Anderson UW CSE 160 Winter 2017
Introduction to Python and programming
And now for something completely different . . .
Introduction to Python and programming
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Bools and simple if statements
Programming Funamental slides
Michael Ernst UW CSE 140 Winter 2013
Introduction to Python and programming
Rocky K. C. Chang September 18, 2018 (Based on Zelle and Dierbach)
COM-152: Computer Programming Types, Variables, Operators Part 1 of 2
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Chapter 2 Programming Basics.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Life is Full of Alternatives
Unit 3: Variables in Java
COMS 261 Computer Science I
General Computer Science for Engineers CISC 106 Lecture 03
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Michael Ernst UW CSE 190p Summer 2012
Introduction to Python and programming
Class code for pythonroom.com cchsp2cs
Control flow: Loops UW CSE 160.
Data Types and Expressions
Getting Started in Python
Presentation transcript:

Introduction to Python and programming Michael Ernst UW CSE 190p Summer 2012

1. Python is a calculator2. A variable is a container 4. A program is a recipe3. Different types cannot be compared

0. Don’t panic! CSE 190p is for people who have never programmed – (If you have programmed, you don’t belong here.) You can learn to program in 9 weeks – You will work hard – We will work hard to help you Ask questions! – This is the best way to learn

1. Python is a calculator

You type expressions. Python computes their values /2 2**3 3*4+5*6 – If precedence is unclear, use parentheses (72 – 32) / 9 * 5

An expression is evaluated from the inside out How many expressions are in this Python code? (72 – 32) / 9.0 * 5 an expression values (72 – 32) / 9.0 * 5 (40) / 9.0 * 5 40 / 9.0 * *

Another evaluation example (72 – 32) / (9.0 * 5) (40) / (9.0 * 5) 40 / (9.0 * 5) 40 / (1.8) 40 /

2. A variable is a container

Variables hold values Recall variables from algebra: – Let x = 2 … – Let y = x … To assign a variable, use “varname = expression” pi = 3.14 pi avogadro = 6*10**23 avogadro 22 = x # Error! Not all variable names are permitted No output from an assignment statement

Changing existing variables (“re-binding” or “re-assigning”) x = 2 x y = 2 y x = 5 x y

Changing existing variables (“re-binding” or “re-assigning”) x = 2 x y = x y x = 5 x y “=” in an assignment is not a statement or promise of eternal equality Evaluating an expression gives a new (copy of a) number, rather than changing an existing one

How an assignment is executed 1.Evaluate the right-hand side to a value 2.Store that value in the variable x = 2 print x y = x print y z = x + 1 print z x = 5 print x print y print z State of the computer:Printed output: x: 2 y: 2 z: 3 x: 5 To visualize a program’s execution:

More expressions: Conditionals 22 > 4 22 < 4 22 == 4 x = 100 # Assignment, not conditional! 22 = 4 # Error! x >= 5 x >= 100 x >= 200 not True not (x >= 200) 3<4 and 5<6 4<3 or 5<6 temp = 72 water_is_liquid = temp > 32 and temp < 212 Numeric operators: +, *, ** Boolean operators: not, and, or Mixed operators: =, ==

More expressions: strings A string represents text ' Python ' myclass = "CSE 190p" "" Empty string is not the same as an unbound variable Operations: Length: len(myclass) Concatenation: "Michael" + 'Ernst' Containment/searching: '0' in myclass "O" in myclass

3. Different types cannot be compared

Types of values Integers ( int ): -22, 0, 44 – Arithmetic is exact – Some funny representations: L Real numbers ( float, for “floating point”): 2.718, – Arithmetic is approximate, e.g., 6.022*10**23 – Some funny representations: 6.022e+23 Strings ( str ): "I love Python", "" Truth values ( bool, for “Boolean”): True, False George Boole

Operations behave differently on different types "3" + "4" 3 + "4" # Error 3 + True # Insanity! Moral: Python sometimes tells you when you do something that does not make sense.

Operations behave differently on different types 15.0 / / 4 # Insanity! 15.0 / 4 15 / 4.0 Type conversion: float(15) int(15.0) int(15.5) int(“15”) str(15.5) float(15) / 4

4. A program is a recipe

What is a program? A program is a sequence of instructions The computer executes one after the other, as if they had been typed to the interpreter Saving as a program is better than re-typing from scratch x = 1 y = 2 x + y print x + y print "The sum of", x, "and", y, "is", x+y

Exercise: Convert temperatures Make a temperature conversion chart: Fahrenheit to Centrigrade, for -40, 0, 32, 68, 98.6, 212, 293, 451 Output: You have created a Python program! (It doesn’t have to be this tedious, and it won’t be.)

1. Python is a calculator2. A variable is a container 4. A program is a recipe3. Different types cannot be compared