Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

Similar documents
Introduction to Python and programming. Ruth Anderson UW CSE 160 Winter 2017

Monty Python and the Holy Grail (1975) BBM 101. Introduction to Programming I. Lecture #03 Introduction to Python and Programming, Control Flow

Monty Python and the Holy Grail (1975) BBM 101. Introduction to Programming I. Lecture #03 Introduction to Python and Programming, Control Flow

Welcome'to'BBM'101' Course'Introduc-on' Course'Staff' Course'Staff' Lecturers:# TAs#(teaching#assistants):# # # Do#not#hesitate#to#ask#TAs#for#help!

Lecture 3. Input, Output and Data Types

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

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

Fundamentals of Programming (Python) Getting Started with Programming

CSE 115. Introduction to Computer Science I

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Information Science 1

Getting Started Values, Expressions, and Statements CS GMU

Topic 2: Introduction to Programming

CS 115 Lecture 4. More Python; testing software. Neil Moore

1/11/2010 Topic 2: Introduction to Programming 1 1

Information Science 1

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

Algorithms and Programming I. Lecture#12 Spring 2015

CS313D: ADVANCED PROGRAMMING LANGUAGE

WELCOME! (download slides and.py files and follow along!) LECTURE 1

Information Science 1

Fundamentals: Expressions and Assignment

Getting Started with Python

COMP 202 Java in one week

Expressions and Variables

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

cs1114 REVIEW of details test closed laptop period

Operators in C. Staff Incharge: S.Sasirekha

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

Chapter 2: Using Data

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

Topic 4 Expressions and variables

Computer Science 217

Full file at

CSEN 102 Introduction to Computer Science

Text Input and Conditionals

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

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

Building Java Programs

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

CPS122 Lecture: From Python to Java

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Variables, Constants, and Data Types

Expressions and Casting

Introduction to Mathematical and Scientific Programming TUTORIAL WEEK 1 (MATH 1MP3) Winter 2019

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Building Java Programs

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

COMP 102: Computers and Computing

Advanced Algorithms and Computational Models (module A)

Building Java Programs

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Loop structures and booleans

Lecture 3 Operators MIT AITI

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

CMSC201 Computer Science I for Majors

Chapter 3: Operators, Expressions and Type Conversion

Logical Thinking through Computer Programming

Unit 3. Operators. School of Science and Technology INTRODUCTION

ECE 122 Engineering Problem Solving with Java

Real Python: Python 3 Cheat Sheet

CS 115 Lecture 8. Selection: the if statement. Neil Moore

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS242 COMPUTER PROGRAMMING

Introduction in. Programming 1. programming. Course 1

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

PLT Fall Shoo. Language Reference Manual

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

Outline for Today CSE 142. CSE142 Wi03 G-1. withdraw Method for BankAccount. Class Invariants

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

Boolean Expressions. So, for example, here are the results of several simple Boolean expressions:

CS1 Lecture 3 Jan. 18, 2019

Sorting. Sorting. Customizing the Sort Order. Sorting

Sorting. BBM Introduction to Programming I. Hacettepe University Fall Fuat Akal, Aykut Erdem, Erkut Erdem

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

5. Control Statements

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4

ENGR 102 Engineering Lab I - Computation

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

CS1 Lecture 3 Jan. 22, 2018

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Lecture 4. Defining Functions

L-System Fractal Generator: Language Reference Manual

Programming Lecture 3

Python Unit

Hacettepe University Computer Engineering Department. Programming in. BBM103 Introduction to Programming Lab 1 Week 4. Fall 2018

ENGR 101 Engineering Design Workshop

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

CIS 110: Introduction to Computer Programming

Course PJL. Arithmetic Operations

Transcription:

1. Python is a calculator. A variable is a container Introduction to Python and Programming BBM 101 - Introduction to Programming I Hacettepe University Fall 016 Fuat Akal, Aykut Erdem, Erkut Erdem 3. Different types cannot be compared 4. A program is a recipe Slides based on material prepared by Ruth Anderson, Michael Ernst and Bill Howe in the course CSE 140 University of Washington 1 1. Python is Like a Calculator 5 3+4 44/ You Type Expressions. Python Computes Their Values. **3 3*4+5*6 If precedence is unclear, use parentheses (7 3) / 9 * 5 3 4

An Expression is Evaluated From the Inside Out How many expressions are in this Python code? an expression (7 3) / 9.0 * 5 values (7 3) / 9.0 * 5 (40) / 9.0 * 5 40 / 9.0 * 5 4.44 * 5. Another Evaluation Example (7 3) / (9.0 * 5) (40) / (9.0 * 5) 40 / (9.0 * 5) 40 / (45.0) 40 / 45.0.888 5 6. A Variable is a Container Variables Hold Values Recall variables from algebra: Let x = Let y = x To assign a variable, use varname = expression pi = 3.14 pi var = 6*10**3 = x # Error! No output from an assignment statement 7 Not all variable names are permitted 8

Changing Existing Variables ( re-binding or re-assigning ) x = x y = x y x = 5 x Y = 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 9 How an Assignment is Executed 1. Evaluate the right-hand side to a value. Store that value in the variable x = print(x) y = x print(y) z = x + 1 print(z) x = 5 print(x) print(y) print(z) State of the computer: x: 5 y: z: 3 Printed output: 3 5 3 To visualize a program s execution: http://pythontutor.com 10 More Expressions: Conditionals (value is True or False) > 4 # condition, or conditional < 4 # condition, or conditional == 4 x == 100 = 4 # Error! x >= 5 x >= 100 x >= 00 not True not (x >= 00) 3<4 and 5<6 4<3 or 5<6 temp = 7 # Assignment, not conditional! Numeric operators: +, *, ** Boolean operators: not, and, or Mixed operators: <, >=, == water_is_liquid = (temp > 3 and temp < 1) 11 More Expressions: strings A string represents text 'Python mystring = "BBM 101-Introduction to Programming " Empty string is not the same as an unbound variable " and are the same Operations: Length: len(mystring) Concatenation: Hacettepe" + + ' University Containment/searching: a' in mystring a" in mystring 1

3. Different Types cannot be Compared Types of Values anint = astring = Hacettepe anint == astring # Error Integers (int): -, 0, 44 Arithmetic is exact Some funny representations: 1345678901L Real numbers (float, for floating point ):.718, 3.1415 Arithmetic is approximate, e.g., 6.0*10**3 Some funny representations: 6.0e+3 Strings (str): "I love Python", " Truth values (bool, for Boolean ): True, False 13 George Boole 14 Operations Behave differently on Different Types 3.0 + 4.0 3 + 4 3 + 4.0 "3" + "4 # Concatenation 3 + "4" # Error 3 + True # Error Moral: Python only sometimes tells you when you do something that does not make sense. Operations on Different Types 15.0 / 4.0 3.75 3.75 15 / 4 3.75 3 15.0 / 4 3.75 3.75 15 / 4.0 3.75 3.75 15.0 // 4.0 3.0 15 // 4 3 15.0 // 4 3.0 15 // 4.0 3.0 Python 3.5 Python.x Before Python version 3.5, operand used to determine the type of division. / : Division //: Integer Division 15 16

Type Conversion A Program is a Recipe float(15) 15.0 int(15.0) 15 int(15.5) 15 int("15") 15 str(15.5) 15.5 float(15) / 4 3.75 17 18 Design the Algorithm Before Coding We should think (design the algorithm) before coding Algorithmic thinking is the logic. Also, called problem solving Coding is the syntax Make this a habit Some students do not follow this practice and they get challenged in all their courses and careers! 19 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 your work as a program is better than re-typing from scratch x = 1 y = x + y print(x + y) print("the sum of", x, "and", y, "is", x+y) 0

The print() Statement The print statement always prints one line The next print statement prints below that one Write 0 or more expressions after print, separated by commas In the output, the values are separated by spaces Examples: x = 1 y = print(3.1415) print(.718, 1.618) print() print(0 +, 7 * 3, 4 * 5) 3.1415.718 1.618 1 0 The sum of 1 and is 3 print("the sum of", x, "and", y, "is", x+y) 1 Exercise: Convert Temperatures Make a temperature conversion chart as the following Fahrenheit to Centigrade, for Fahrenheit values of: -40, 0, 3, 68, 98.6, 1 C = (F - 3) 5/9 Output: Fahrenheit Centigrade -40-40.0 0-17.7778 3 0.0 68 0.0 98.6 37.0 1 100.0 You have created a Python program! (It doesn t have to be this tedious, and it won t be.) Expressions, Statements, and Programs An expression evaluates to a value 3 + 4 pi * r** print() Function A statement causes an effect pi = 3.14159 print(pi) Expressions appear within other expressions and within statements (fahr 3) * (5.0 / 9) print(pi * r**) 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 3 4

1. Python is a calculator. A variable is a container Programming Languages A programming language is a language to write programs in, such as Python, C, C++, Java The concept of programming languages are quite similar 3. Different types cannot be compared 4. A program is a recipe Python: Java: Python is simpler! That s why we are learning it first J 5 6 Evolution of Programming Languages 7 http://blog.codeeval.com/codeevalblog/015 8