Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Similar documents
Algorithms and Programming I. Lecture#12 Spring 2015

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

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

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Variables, expressions and statements

Information Science 1

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Computer Components. Software{ User Programs. Operating System. Hardware

Introduction to programming with Python

Welcome to Python 3. Some history

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

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

CS 231 Data Structures and Algorithms, Fall 2016

Python Input, output and variables

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

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

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Intro to Python & Programming. C-START Python PD Workshop

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

Visual C# Instructor s Manual Table of Contents

The current topic: Python. Announcements. Python. Python

Jython. secondary. memory

Topic 2: Introduction to Programming

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Information Science 1

CS101 Lecture 24: Thinking in Python: Input and Output Variables and Arithmetic. Aaron Stevens 28 March Overview/Questions

Getting Started with Python

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

CMPT 120 Basics of Python. Summer 2012 Instructor: Hassan Khosravi

T H E I N T E R A C T I V E S H E L L

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

FIT9133 Module 1: Introduction to Algorithms and Python Programming Basics

CS1110 Lab 1 (Jan 27-28, 2015)

Full file at

CSCE 120: Learning To Code

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

Winter 2019 CISC101 1/17/2019

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Text Input and Conditionals

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

Computer Components. Software{ User Programs. Operating System. Hardware

Course Outline. Introduction to java

Unit E Step-by-Step: Programming with Python

Chapter 2 Getting Started with Python

AP Computer Science A

CSI Lab 02. Tuesday, January 21st

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Getting Started Values, Expressions, and Statements CS GMU

MEIN 50010: Python Introduction

Table of Contents EVALUATION COPY

Fundamentals of Programming (Python) Getting Started with Programming

Expressions, Statements, Variables, Assignments, Types

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

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

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

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

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

Program Fundamentals

Hello, World! An Easy Intro to Python & Programming. Jack Rosenthal

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

3 The Building Blocks: Data Types, Literals, and Variables

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

MODULE 02: BASIC COMPUTATION IN JAVA

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

Chapter 2 C++ Fundamentals

COMP 202 Java in one week

CS1 Lecture 3 Jan. 22, 2018

Chapter Two PROGRAMMING WITH NUMBERS AND STRINGS

CME 193: Introduction to Scientific Python Lecture 1: Introduction

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

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

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

DSC 201: Data Analysis & Visualization

2. Numbers In, Numbers Out

Basics of Java Programming

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

PROGRAMMING FUNDAMENTALS

Lecture 3. Input, Output and Data Types

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

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

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

Introduction to Python Part 1. Brian Gregor Research Computing Services Information Services & Technology

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

Python Programming Exercises 1

CS1 Lecture 3 Jan. 18, 2019

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

Exercise: Inventing Language

Functions and Decomposition

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

CSC 221: Introduction to Programming. Fall 2013

Transcription:

Python for Analytics Python Fundamentals RSI Chapters 1 and 2

Learning Objectives Theory: You should be able to explain... General programming terms like source code, interpreter, compiler, object code, comment, data type, etc. Basic Python syntax and structure, including statements, variables, expressions, operators, and functions The different types of errors that require debugging Skills: You should know how to... Run Python statements in the command line interpreter Use variables to store, retrieve, and update values Evaluate arithmetic and string expressions

Python Language Origins Python is a high-level scripting language, originally intended for short programs that run from the command line. Timeline: 1980 s: early development by Guido Van Rossum 1994: Python 1.0, the first complete release 2000: Python 2.0 added advanced data types and core object-orientation ( everything is an object ) 2008: Python 3.0 broke backwards compatibility to streamline and unify language syntax and libraries

Python 2 vs Python 3 Plenty of Python libraries (reusable programs) in use today were originally written before 2008 While most libraries have updated to Python 3, some remain stuck in Python 2 We will be learning Python 3 in this class However, you may be asked to use Python 2 in some of your future analytics classes and on the job Fortunately, the switch is pretty easy once you know what you are doing

Source Code, Compilers, and Interpreters Python is a high-level language like C, Java, or C# Written and read by humans as source code Has to be converted into low-level object code (not human-readable) that the computer uses natively Two ways to convert from source code (programs) to object code (machine code): Compilers (used by C, Java, C#, etc.) convert it all at once, requiring all source code to be written in advance Interpreters convert the code one line at a time, allowing the source code to be written interactively

Python Interpreter by Example Most Python programs are executed via interpreter Anaconda supports a command prompt and Jupyter Notebooks Fun fact: This example is from Dr. Huntley s first Python class in 2005

Interpreter Notes Executes a program one statement at a time We entered a Python 2.4 statement print I love this course! In Python 3 it would be print( I love this course! ) The interpreter executed the statement If the interpreter cannot execute the statement, then it returns an error message

Try it yourself! 1. Open the Python interpreter from the command line: Mac: Open Terminal.app from Utilities folder Windows: Open the Command Prompt (Google it) 2. Enter the statement print( I love this course ) 3. Watch as the interpreter executes the statement 4. Enter the statement print I love this course 5. Read the error message about missing parentheses

Debugging / Error Types These are listed in increasing difficulty. Logic errors can be especially hard to diagnose and correct. Syntax errors in the source code are caught by the interpreter or compiler before trying to run it Fix: Look for broken Python statements or typos Runtime errors happen when the computer tries to run a line of code Fix: Read the error message, which explains what caused the interpreter to break Programs with Semantic errors (bad logic) run fine but do not produce the expected results Fix: Study the source code for incorrect logic

Code Comments # Compute the W2H ratio for one person Python statements are meant for the computer to execute Sometimes we want to include explanatory comments for programmers to read Comments can be especially helpful with semantic errors Comments always start with the # character Anything after the # is ignored by the interpreter

Notes on Syntax / Notation Python is very strict about what kinds of statements it can execute Every kind of statement has its own syntax, a pattern composed of keywords, expressions, and punctuation characters. The syntax of a print statement is print(<string expression>) Keywords like print, if, or, and for are reserved and cannot be used for variable names

Python Cheat Sheet Suggestion: Download the Python 3 Beginner s Reference Cheat Sheet from sixthresearcher.com

Values and Data Types Every piece of data in Python has a value and a data type: 5 is a value with the data type int (integer) 5.0 is a value of type float (floating point number) Hello is a value of type str (string of characters) [ IS505,2017] is a list (of values)

Try it out Type the following lines in the Python Interpreter type(5) type(5.0) type( 5.0 ) type( Hello ) type([ IS505,2017]) For each you will get something like <class data type > Notice anything about the value 5.0? What happened?

Data Types Matter! The data type of a value determines what kinds of things we can do with it. A + B AB 1+2 3 1+2.0 3.0 1+ A error message The last example is an example of type incompatibility

Type Conversion We can translate data from one data type to another using type conversion functions float(5) 5.0 int(5.0) 5 str(5) 5 int( 5.0 ) syntax error! Why? We can do more complex conversions like this... int(float( 5.0 )) 5

Variables A variable is just a place that stores a value that we can recall later if needed: Can store one data value at a time Has a unique name (within scope) so you can access the data Can change the data value through assignment We can set the value of a variable using the assignment operator (=) <variable name> = <value>

Try it out first_name = Bob last_name = Gibson print( Hi +first_name+ +last_name) Hi Bob Gibson first_name = Dan print( Hi +first_name+ +last_name) Hi Dan Gibson

Variable Names Should be easy to understand and type Make the names describe what is being stored and how it is being used last_name is much better than names like X or l or even ln Follow the Python style guide Variable names are lower case and use underscores to separate words (e.g., last_name) Don t go too crazy on the length (1-15 chars) And do not use keywords for names

Statements and Expressions A statement is a Python instruction (line of code) that asks Python to do something: X=2 is an assignment statement An expression is a combination of values, variables, operators, and functions that can be evaluated to calculate a value Expressions always evaluate to a value 2+2 and type(2+2) are expressions

Functions (more about this next time) A function is a named, reusable sequence of statements Like how a variable is a place to store a value for later Functions can return values, just like expressions str() is a function that returns a string Function calls always include () after the function name Any values listed inside the () are input arguments str(1.0 + 3) calls str() with the expression 1.0+3 as the argument and returns the equivalent string value

Digression: The input() Function The input() function allows us to ask the user a question directly from the Python interpreter. Execution pauses until the user enters a value (and hits the return key) before returning a string value Usually, we will want to capture the return value with a variable age = input( How old are you? ) We won t be needing the input() function much in this class. Jupyter Notebooks are way cooler than the command line.

Composite Function Calls Function arguments can call other functions if needed. Remember this? int(float( 5.0 )) The float( 5.0 ) function call inside the parentheses is evaluated before passing the value into the int() function.

Operators and Operands An operator is a computation ( verb ) that can be used in an expression to calculate a value +, -,*, and / are arithmetic operators ( ) is a grouping operator An operator is like a function that has special built-in shortcut syntax: 2 + 3 is equivalent to function call like add(2,3) The values being operated on (2 and 3 above) are called operands

Order of Operations: PEMDAS Math expressions are evaluated just like in algebra class: 1. Parentheses 2. Exponents 3. Multiplication 4. Division 5. Addition 6. Subtraction Trick: When in doubt use parentheses to force the right order.

Classwork to do before leaving for home Complete chapters 1 and 2 of the RSI How to Think Like a Computer Scientist e-book. Do not play the videos without headphones; they are redundant anyway If time permits, start in on your homework. Ask questions when you need help. Use this time to get help from the professor!

Homework to be completed before the next class The following homework is due before class on Saturday: RSI Chapters 3,4,and 10. Data Camp Python Basics and Python Lists chapters Study for Quiz 1, which will cover chapters 1-4 of the RSI book Please email chuntley@fairfield.edu if you have any problems or questions.

Python for Analytics Python Fundamentals RSI Chapters 1 and 2