Introduction to Python and programming

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.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
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.
Introduction to Python and programming Michael Ernst UW CSE 190p Summer 2012.
Recitation 1 Programming for Engineers in Python.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
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
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
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.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Control flow Ruth Anderson UW CSE 160 Spring
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
Control flow Ruth Anderson UW CSE 160 Winter
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Control Structures I Chapter 3
Topics Designing a Program Input, Processing, and Output
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Introduction to Computer Science / Procedural – 67130
2.5 Another Java Application: Adding Integers
Introduction to Python and programming
Object Oriented 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.
Assignment and Arithmetic expressions
The Selection Structure
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 . . .
Conditions and Ifs BIS1523 – Lecture 8.
Introduction to Python and programming
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Bools and simple if statements
Number and String Operations
Building Java Programs
Arithmetic Expressions & Data Conversions
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Ruth Anderson UW CSE 140 Winter 2014
Michael Ernst UW CSE 140 Winter 2013
Introduction to Python and programming
Rocky K. C. Chang September 18, 2018 (Based on Zelle and Dierbach)
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.
Building Java Programs
SSEA Computer Science: Track A
Building Java Programs
Topics Designing a Program Input, Processing, and Output
Building Java Programs
Topics Designing a Program Input, Processing, and Output
Life is Full of Alternatives
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Unit 3: Variables in Java
COMS 261 Computer Science I
Introduction to C Programming
Michael Ernst UW CSE 190p Summer 2012
CS 100: Roadmap to Computing
Class code for pythonroom.com cchsp2cs
Arithmetic Expressions & Data Conversions
Control flow: Loops UW CSE 160.
Data Types and Expressions
Presentation transcript:

Introduction to Python and programming UW CSE 160

1. Python is a calculator 2. A variable is a container 3. Different types cannot be compared 4. A program is a recipe

0. Don’t panic! CSE 160 is for beginners to programming (If you know how to program, you don’t belong) You can learn to program in 10 weeks You will work hard We will work hard to help you Ask questions! This is the best way to learn “Ask questions”: also, we will be grading on participation. Introduce myself here. Also Dun-Yu.

1. Python is a calculator

You type expressions. Python computes their values. 5 3 + 4 44 / 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? an expression values (72 – 32) / 9.0 * 5 (72 – 32) / 9.0 * 5 (40) / 9.0 * 5 40 / 9.0 * 5 Note: using 9.0 instead of 9 as on previous slide. 4.44 * 5 22.2

Another evaluation example (72 – 32) / (9.0 * 5) (40) / (9.0 * 5) 40 / (9.0 * 5) 40 / (45.0) Note: Still using 9.0 instead of 9 as on previous slide. 40 / 45.0 .888

2. A variable is a container

No output from an assignment statement Variables hold values Recall variables from algebra: Let x = 2 … Let y = x … In Python assign a variable: “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 Draw out the current values

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

How an assignment is executed Evaluate the right-hand side to a value Store that value in the variable x = 2 print (x) y = x print (y) z = x + 1 print (z) x = 5 State of the computer: Printed output: To visualize a program’s execution: http://pythontutor.com Link to this code here

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

More expressions: Conditionals (value is True or False) 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 See in python tutor Numeric operators: +, *, ** Mixed operators: <, >=, == Boolean operators: not, and, or

More expressions: strings A string represents text 'Python' this_class = "CSE 160" "" Empty string is not the same as an unbound variable Operations on strings: Length: len(this_class) Concatenation: "Ruth" + 'Anderson' Containment/searching: '0' in this_class "O" in this_class Empty string is like the 0 value in arithmetic – Arabic numerals are a great advance over Roman numerals

3. Different types cannot be compared

Types of values Integers (int): -22, 0, 44 Arithmetic is exact Some funny representations: 12345678901L Real numbers (float, for “floating point”): 2.718, 3.1415 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 Photo is of George Boole, an English mathematician and logician George Boole

Operations behave differently on different types 3.0 + 4.0 3 + 4 3 + 4.0 "3" + "4" 3 + "4" # Error 3 + True # Insanity! (Don’t do this.) Moral: Python sometimes tells you when you do something that does not make sense. See in python tutor

Operations behave differently on different types 15.0 / 4.0 15 / 4 # Would have been truncated in Python 2. 15.0 / 4 15 / 4.0 Type conversion: float(15) int(15.0) int(15.5) int("15") str(15.5) See in python tutor

4. A program is a recipe

What is a program? A program is a sequence of instructions See in python tutor A program is a sequence of instructions The computer executes one after the other, as if they had been typed to the interpreter Saving your work 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)

Interlude: The print statement See in python tutor The print statement always prints one line The next print statement prints below that one For Python 3 , print is followed by parentheses Write 0 or more expressions after print, separated by commas In the output, the values are separated by spaces Examples: print (3.1415) print (2.718, 1.618) print () print (20 + 2, 7 * 3, 4 * 5) 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: -40 -40.0 0 -17.7778 32 0.0 68 20.0 98.6 37.0 212 100.0 293 145.0 451 232.778 You have created a Python program! (It doesn’t have to be this tedious, and it won’t be.) See lecture 3 for what the final program should look like

Expressions, statements, and programs An expression evaluates to a value 3 + 4 pi * r**2 A statement causes an effect pi = 3.14159 print (pi) Expressions appear within other expressions and within statements (fahr – 32) * (5.0 / 9) print (pi * r**2) A statement may not appear within an expression 3 + print (pi) # Error! A program is made up of statements A program should do something or communicate information Just evaluating an expression does not accomplish either goal

1. Python is a calculator 2. A variable is a container 3. Different types cannot be compared 4. A program is a recipe