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

Similar documents
The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

ENGR 101 Engineering Design Workshop

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

Variable and Data Type I

Fundamentals: Expressions and Assignment

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Getting Started with Python

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Fundamentals of Programming (Python) Getting Started with Programming

Introduction to Python, Cplex and Gurobi

DaMPL. Language Reference Manual. Henrique Grando

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

Variable and Data Type I

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

Programming with Python

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif

CMSC 201 Computer Science I for Majors

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Full file at

Chapter 2 Getting Started with Python

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

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

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

Jython. secondary. memory

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

PROGRAMMING FUNDAMENTALS

PYTHON- AN INNOVATION

CS1 Lecture 3 Jan. 22, 2018

And Parallelism. Parallelism in Prolog. OR Parallelism

VLC : Language Reference Manual

Built-in Types of Data

Beyond Blocks: Python Session #1

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

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

Programming for Engineers in Python. Recitation 1

>>> * *(25**0.16) *10*(25**0.16)

Chapter 2 Writing Simple Programs

CS1 Lecture 3 Jan. 18, 2019

Chapter 17. Fundamental Concepts Expressed in JavaScript

Lecture 1. Types, Expressions, & Variables

Basic Syntax - First Program 1

Variables, expressions and statements

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

The float type and more on variables FEB 6 TH 2012

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter

Downloaded from Chapter 2. Functions

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

cs1114 REVIEW of details test closed laptop period

Programming for Engineers in Python. Autumn

Ruby: Introduction, Basics

Program Fundamentals

L-System Fractal Generator: Language Reference Manual

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

Ruby: Introduction, Basics

CSCE 110 Programming I

CSCE 120: Learning To Code

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

A First Program - Greeting.cpp

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Objectives. In this chapter, you will:

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Variables, Expressions, and Statements

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

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Here n is a variable name. The value of that variable is 176.

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

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Expressions, Statements, Variables, Assignments, Types

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions.

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Text Input and Conditionals

About Variables in Python F E B 1 1 T H

UNIT- 3 Introduction to C++

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

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

Chapter 2 Input, Processing and Output. Hong Sun COSC 1436 Spring 2017 Jan 30, 2017

Language Reference Manual

Introduction to Python. Dmytro Karpenko Research Infrastructure Services Group, Department for Research Computing, USIT, UiO

Decisions, Decisions. Testing, testing C H A P T E R 7

Creating a C++ Program

COMP 202 Java in one week

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Language Reference Manual

Chapter 2: Overview of C++

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Transcription:

CSE 231, Rich Enbody Office Hours After class By appointment send an email 2 1

Project 1 Python arithmetic Do with pencil, paper and calculator first Idle Handin Help room 3 What is a Computer Program? 2

Program A program is a sequence of instructions. To run a program is to: o create the sequence of instructions according to your design and the language rules o turn that program into the binary commands the processor understands o give the binary code to the OS, so it can give it to the processor o OS tells the processor to run the program o when finished (or it dies :-), OS cleans up. 5 Python is an interpreted language You can simply open the Python interpreter, and enter instructions one-at-a-time. Or You can import a program which causes the instructions in the program to be executed, as if you had typed them in. 6 3

Your First Program Example 1 First Program: number_input.py # input two numbers, add them together, print them out num_str1 = input('please enter an integer:') num_str2 = input('please enter a floating point number:') str1_int = int(num_str1) str2_float = float(num_str2) # this is a comment print('the numbers are: ',str1_int,' and ',str2_float) print('their sum is:',str1_int+str2_float, \ 'and their product is:',str1_int*str2_float) 8 4

input The function: input( Enter a value ) prints Enter a value in the Python shell and waits till the user types something (anything), followed by Enter Warning, it returns a string (sequence of characters), no matter what is given, even a number ( 1 is not the same as 1, different types) 9 What's a string The word "string" is used to indicate a sequence of characters, a compositor's term 10 5

print my_var = 12 print( My var has a value of:,my_var) print takes a list of elements to print, separated by commas o if the element is a string, prints it as is o if the element is a variable, prints the value associated with the variable o after printing, moves on to a new line of output 11 At the core of any language Control the flow of the program Construct and access data elements Operate on data elements Construct functions Construct classes Libraries and built-in classes 12 6

Save as a module Save with a.py suffix, so it becomes a Python module You run the module from the IDLE menu (F5) to see the results of the operation A module is just a file of Python commands 13 Errors 14 7

Common Error If you save the file without a.py suffix, it will stop colorizing and formatting the file. Fix: save with the.py 15 Syntax Lexical components. A Python program is:. o A module (perhaps more than one) o Each module has Python statements o Each statement has expressions 16 8

Modules http://pypi.python.org 17 Statements are commands in Python. They perform some action, often called a side effect, but they do not return any values 18 9

Expressions perform some operation and return a value. Expressions can act as statements, but statements cannot act as expressions (more on this later). Expressions typically do not modify values in the interpreter. 19 Note that MPL sometimes only asks for an expression 20 10

What is the difference between a side effect and a return? 1+2 returns a value (it s an expression). You can catch the return value (x=1+2). However, nothing else changed. print( hello ) doesn t return anything, but something else, the side effect, did happen. Something printed! 21 Python name conventions must begin with a letter or _ o Ab123 is OK, but 123ABC is not. may contain letters, digits, and underscores o this_is_an_identifier_123 may be of any length upper and lower case letters are different o LengthOfRope is not lengthofrope names starting with _ have special meaning. Be careful 22 11

A comment begins with a # From the # to the end of that line: nothing will be interpreted by Python. Comments help the human reader. 23 Code as essay. 24 12

Knuth, Literate Programming ( 84) Let us change our traditional attitude to the construction of programs: Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do. 25 Python types integers (int): 5 floating point (float): 1.2 Booleans (bool): True Strings (str): anything or something lists: [ a,1,1.3] Others 26 13

A type in Python defines two things: o what it contains o the kinds of operations you can perform on it 27 Fundamental Types Integers (int) o 1, -27 Reals (float) o 3.14, 10.,.001, 3.14e-10, 0e0 Booleans o True, False note the capital 28 14

Example 2 Worksheet 1 15

A character 1 is not an integer 1. You need to convert the value returned by the input command (characters) into an integer (int) int( 123 ) yields the integer 123 31 Type conversion int(some_var) converts to an integer float(some_var) converts to a float str(some_var) converts to a string 32 16

import math imports the math module Brings in Python statements to support math Use math prefix: math.xxx math.pi is π math.sqrt(2) finds the square root of 2 33 When = doesn t mean equal my_int = my_int + 7 34 17

= is assignment lhs = rhs What assignment means is: o evaluate all the stuff on the rhs of the = o take the resulting value and associate it with the name on the lhs 35 Variable Objects Python maintains a list of pairs for every variable: o variable s name o variable s value A variable is created when a value is first assigned. It associates a name and a value. Subsequent assignments update the associated value. We say a name references a value. A variable s type depends on what is assigned. X = 7 Name X 7 Value 36 18

Namespace: a table that associates a name with a value. Namespace X 7 37 Assignment Statement Example: x = 2 + 3 * 5 o evaluate expression (2+3*5): 17 o change the value of x to reference 17 Example (y has value 2): y = y + 3 o evaluate expression (y+3): 5 o change the value of y to reference 5 38 19

x = 2 + 3 * 5 y = 2 y = y + 3 Namespace x 17 y 2 5 39 More on types Python does not require you to pre-define the type of a variable What type a variable holds can change Nonetheless, knowing the type can be important for using the correct operation on a variable. 40 20

x = 2 x = 7.5 x = abc Namespace x 2 7.5 abc 41 What can go on the lhs? The lhs must indicate a name with which a value can be associated. Must follow the naming rules my_int = 5, Yes my_int + 5 = 7, No 42 21

Example 3 More on whitespace For the most part, you can place white space (spaces) anywhere in your program use it to make a program more readable 1 + 2-4 44 22

continuation Python is sensitive to the end of line. To make a line continue, use: \ print( this is a test, \ of continuation ) prints this is a test of continuation 45 Python is sensitive to tabs. (indentation) 46 23

Syntax: tokens 47 Python Keywords: cannot use as variable names and del from not while as elif global or with assert else if pass yield break except import print class exec in raise continue finally is return def for lambda try 48 24

Python Operators + - * ** / // % << >> & ^ ~ < > <= >= ==!= += -= *= /= //= %= &= = ^= >>= <<= **= 49 Python Punctuators and Delimiters ($ and? not allowed) # \ ( ) [ ] { } @, :. ` = ; 50 25

Operators Integer (int) o addition and subtraction: +, - o multiplication: * o division: 3 kinds! division: / quotient: // remainder: % o exponent: ** Floating point (float) o add, subtract, multiply, divide, exp: +, -, *, /, ** 51 Types and division int 5/3 is division, yields float: 1.66666 5//3 is the quotient, yields integer: 1 5%3 is the remainder, yields integer: 2 float 5.0/3.0 is division, yields float: 1.66666 52 26

Mixed Types 4//3 is 1 (integer quotient) 4.0/3.0 is 1.3333333 (float division) What is 4/3.0? o No mixed-type operations. Must convert. o Python automatically converts to float. Thus 4 4.0 so the result is 1.3333333 53 Try 5//2 5//2.0 3+4.0 1234//100 1234%100 1234//100%10 int(1.234) float(2) 54 27

Collections of data types (later in semester) list o sequence of any data elements dictionary o a collection of name:value pairs. Very powerful! class o a user-defined data type 55 Collections and building blocks (examples of what s to come later) Point (x,y,z are real numbers) Plane (3 points in a plane) Line segment (2 end points) Face (N points on a plane) Solid (N faces) Solid (N planes) 56 28

Note The interpreter translates Python into machine (computer) language. The first stage of that process is determining if it is valid Python code. If the program is not a valid Python program, the interpreter cannot translate it. If the interpreter cannot translate your code, it is not a Python program so it is not worth any points. 57 29