ENGR 101 Engineering Design Workshop

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

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

Variable and Data Type I

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

Getting Started with Python

Variable and Data Type I

Getting Started Values, Expressions, and Statements CS GMU

PROGRAMMING FUNDAMENTALS

Lecture 3. Input, Output and Data Types

Algorithms and Programming I. Lecture#12 Spring 2015

Fundamentals of Programming (Python) Getting Started with Programming

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

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

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

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

Chapter 2 Working with Data Types and Operators

Variables, expressions and statements

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

Lecture 1. Types, Expressions, & Variables

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

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

Variables, Expressions, and Statements

Basic Syntax - First Program 1

CSCE 110 Programming I

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

Python Unit

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

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

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Coral Programming Language Reference Manual

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

CSI33 Data Structures

Python I. Some material adapted from Upenn cmpe391 slides and other sources

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

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

Datatypes, Variables, and Operations

Expressions and Variables

CSc Introduction to Computing

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

An overview about DroidBasic For Android

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

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

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

Topic 2: Introduction to Programming

About Variables in Python F E B 1 1 T H

Text Input and Conditionals

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Jython. secondary. memory

CMSC 201 Computer Science I for Majors

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

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

Programming with Python

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

Java for Python Programmers. Comparison of Python and Java Constructs Reading: L&C, App B

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

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

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

Babu Madhav Institute of Information Technology, UTU 2015

DaMPL. Language Reference Manual. Henrique Grando

cs1114 REVIEW of details test closed laptop period

Level 3 Computing Year 2 Lecturer: Phil Smith

Hello, World and Variables

Ruby: Introduction, Basics

Introduction to Python (All the Basic Stuff)

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Full file at C How to Program, 6/e Multiple Choice Test Bank

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Object-Oriented Programming

CPSC 217 Midterm (Python 3 version)

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

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

Visual C# Instructor s Manual Table of Contents

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

egrapher Language Reference Manual

PLT Fall Shoo. Language Reference Manual

Python. Objects. Geog 271 Geographic Data Analysis Fall 2010

Programming in Python 3

Basics of Java Programming

Algorithms and Data Structures

Typescript on LLVM Language Reference Manual

Python Day 3 11/28/16

Fundamentals: Expressions and Assignment

L-System Fractal Generator: Language Reference Manual

The float type and more on variables FEB 6 TH 2012

Introduction to TURING

Introduction to Python

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Ex: If you use a program to record sales, you will want to remember data:

FRAC: Language Reference Manual

Introduction to Python! Lecture 2

Variables and Values

Built-in Types of Data

COMP Primitive and Class Types. Yi Hong May 14, 2015

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

SOFT 161. Class Meeting 1.6

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

Language Reference Manual

Python Input, output and variables

Ruby: Introduction, Basics

Transcription:

ENGR 101 Engineering Design Workshop Lecture 2: Variables, Statements/Expressions, if-else Edgardo Molina City College of New York

Literals, Variables, Data Types, Statements and Expressions

Python as Calculator 2 + 2 # add 4 2 * 3 # multiply 6 Integers 3**2 # powers (whole numbers) 9 12 % 11 # modulo (remainder) 1

Integer Division 6//2 # division 3 5//2 # integer division, no decimals 2 4*(1//4) Careful 0

A Float Division 6/2 # division 3.0 5.0/2.0 2.5 4.0*(1.0/4.0) 1.0 single / will always perform floating point division in Python 3.

Literals (are fixed values) Numbers Integers are natural numbers:..., -2, -1, 0, 1, 2,... (32 bits) Floats contain decimals: 4.5, 67.444443335, 7E2... Booleans: True, False Long int s that exceed 32 bit capacity Complex numbers: 4.5, 1j * 1j = -1 + 0j Strings Strings are used to represent words, text, and characters examples (can use single, double or triple quotes): "I am learning python." 'hello.'

Variables (NAME = VALUE) are names we assign to chunks of memory Computer Memory we retrieve the values by calling its name 42 student_id = 42 print( student_id ) = is the assignment operator, assigns a data value to a variable

Variables Syntax Rules Variable names must begin with a letter (uppercase or lowercase) or underscore (_) * good programming convention: variables should not start with uppercase letters, commonly used for something else remainder of name can contain letters, (_), and numbers names may not contain spaces or special characters names are case sensitive and must not be a reserved python keyword myvariable and myvariable refer to different data

x y Statements and Expressions Statements perform a task; do not return a value = 2 = 3 print(y) Expressions return a value x + y 6

Expressions (evaluate to values) Math expressions 10 * 2 + 3 23 10 * (2.0 + 3) 50.0 Operator Precedence (PEMDAS) Parentheses Exponentiation Multiplication and Division Addition and Subtraction

Expressions (evaluate to values) Boolean expressions 10 < 2 False 10 >= 10 True Combined with Logical Operators (10 < 2) and (10 == 10) False

(a*c Expressions (evaluate to values) Can combine + d) > (d*a - c) String expressions "Hel" + "lo" 'Hello' "Hi"*3 'HiHiHi' Later lectures will reveal more about this

Operator Precedence (top-to-bottom) Operator Description ( ) Parenthesis (grouping) ** Exponentiation +x, -x Positive, Negative *, /, % Multiplication, Division, Remainder +, - Addition, Subtraction <, <=, >, >=,!=, == Comparisons not x Boolean NOT and Boolean AND or Boolean OR

<type <type Data Types Finding out a data type x = 2.0 print(type(x)) 'float'> print(type( hi )) 'str'>

<type <type TypeError: Data Types What if data types don t match? x = 2 y = x + 3.0 # add an int and float print(type(x)) 'int'> print(type(y)) 'float'> STRONG TYPES no automatic conversion (for non number types) x = "str" + 2 cannot concatenate 'str' and 'int' objects

Data Types Explicit conversion x = "5" y = 5 x + str(y) # str() converts to str '55' int(x) + y # int() converts to int 10 float(x) + y # converts to float 10.0

Python Keywords, User Input

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

Give User Input Create interactive programs by requesting user input x = input( Give me a number: ) me a number: 34.5 print(x) User Input '34.5' float(x) String 34.5

Control Structures

Branching / Conditional Statements Decision Making is front of robot clear? False True Stop() Move()

if if DO if - statement if syntax front_is_clear(): move() TEST: THIS

if if print(x, print(x, if DO DO if - else front_is_clear(): move() else: turn_off() if - else syntax x % 2 == 0: else: "is even") TEST: THIS else: SOMETHING ELSE "is odd")

choice if elif elif print("invalid if - elif - else If one test fails, perform next test = input( make a choice (a, b, or c): ) choice == 'a': function_a() choice == 'b': function_b() choice == 'c': function_c() else: choice.")

if print(x, if print(x, print(x, Nested if statements x == y: "and", y, "are equal") else: else: x < y: "is less than", y) "is greater than", y)