Introduction to Computation for the Humanities and Social Sciences. CS 3 Chris Tanner

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

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

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

CSE 115. Introduction to Computer Science I

Lecture 3. Input, Output and Data Types

Introduction to Computation for the Humanities and Social Sciences. CS 3 Chris Tanner

Text Input and Conditionals

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

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

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Lecture 2: Variables & Assignments

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

CSCI 121: Anatomy of a Python Script

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

CIS 110: Introduction to Computer Programming

CS1 Lecture 3 Jan. 18, 2019

CS1 Lecture 3 Jan. 22, 2018

Lecture 1. Types, Expressions, & Variables

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

MEIN 50010: Python Introduction

CMSC 201 Computer Science I for Majors

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

Fundamentals: Expressions and Assignment

CS 106 Introduction to Computer Science I

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

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

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

ENGR 101 Engineering Design Workshop

Introduction to Python (All the Basic Stuff)

Midterms Save the Dates!

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Data types Expressions Variables Assignment. COMP1400 Week 2

These are notes for the third lecture; if statements and loops.

ENGR 102 Engineering Lab I - Computation

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

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

Expressions and Variables

Lecture 2. Variables & Assignment

Programming for Engineers in Python. Recitation 1

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

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

C++ Reference NYU Digital Electronics Lab Fall 2016

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

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

Programming for Engineers in Python. Autumn

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

Math Day 2 Programming: How to make computers do math for you

CS112 Lecture: Primitive Types, Operators, Strings

Getting started with Java

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Algorithms and Programming I. Lecture#12 Spring 2015

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

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

Assignment 4: Dodo gets smarter

Part 1 - Your First algorithm

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

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

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

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

The Big Python Guide

CS1110 Lab 1 (Jan 27-28, 2015)

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

6.096 Introduction to C++ January (IAP) 2009

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

Fall 2017 CISC124 9/16/2017

Programming: Part I. In this section of notes you will learn about how to write simple programs using JES. Translators

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

CSC108: Introduction to Computer Programming. Lecture 1

EXPRESSIONS AND ASSIGNMENT CITS1001

CSCE 110 Programming I

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

cs1114 REVIEW of details test closed laptop period

Python for Non-programmers

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

MITOCW watch?v=0jljzrnhwoi

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

LECTURE 3 C++ Basics Part 2

cast int( x float( x str( x hex( int string int oct( int string int bin( int string int chr( int int ord( ch

CSCI 136 Data Structures & Advanced Programming. Spring 2018 Instructors Bill Jannen & Jon Park

CSCI 1103: Input with TextIO

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

Data Types and the main Function

CS 251 Intermediate Programming Java Basics

Values, Variables, Types & Arithmetic Expressions. Agenda

Topic 4 Expressions and variables

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

The Dynamic Typing Interlude

Python Programming Exercises 1

Lesson 3: Accepting User Input and Using Different Methods for Output

Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage:

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

Program Fundamentals

Chapter 2: Using Data

Getting Started Values, Expressions, and Statements CS GMU

AP Computer Science A

Lecture 2: Python Arithmetic

Transcription:

Introduction to Computation for the Humanities and Social Sciences CS 3 Chris Tanner

Lecture 4 Python: Variables, Operators, and Casting

Lecture 4 [People] need to learn code, man I m sick with the Python. Childish Gambino HOT 97 freestyle, Sept 8, 2014

A REFRESHER IDE (Atom, Sublime, etc) Atom, Sublime, etc are not Python-specific They are programs just like any other on our computer They are text editors (like Microsoft Word, Text Edit, Notepad), which allow us to display and edit text However, they re catered for the task of programming, so they have nifty features like displaying certain words w/ colors to make it easy to read Related, we could write Python code in Microsoft Word if we wanted, just that would make our life more difficult

Opening a NY Times article Text Edit Sublime Atom

Opening a file of Python code Atom Text Edit

A REFRESHER Python We choose to teach programming via the Python language Python is just the language of the words we choose to type We chose Python because: it s incredibly powerful (arguably the most robust language) easy to read and write code extensive set of libraries to help w/ doing technical stuff The skills you learn in this course (including writing Python code) are completely transferrable to other programming languages; after this course, it would be easy to write code in Java or R (just as Caroline, the TA. She took CS3). The core principles are the same!

A REFRESHER Anaconda In order to run Python code (Python programs) that we write, we need to install software on our computer which knows how to understand and run Python code. Anaconda does this for us. Anaconda installs all necessary things so that we can write and run Python code. We didn t have to use it; there are other ways to install Python, but it s generally a very easy way to install Python

A REFRESHER The Terminal (aka Console) The terminal/console isn t Python-specific! Inherently, has nothing to do with Python It merely provides an alternative way to function w/ our computer, instead of the normal, graphical way with our mouse and clicking on folders and double-clicking programs to open them Instead of using your computer via a mouse and clicking on pretty things, one could do most things while just using the Terminal/ Console Our Python programs we ll create in this course don t have graphical components that display stuff on the screen (e.g., Spotify), so it makes most sense to execute them from the Terminal

Data Types Data Types Variables Operators Casting!10

Data Types all computer programs operate on data just like calculators (limited computers) do, but calculators operate only on numerical data our computer programs can operate on numerical data, text, and more.!11

Data Types Primitive Data Types Boolean Values: only True or False Numeric Values: 0-4 783910 33.3333333-2.59 Strings (text): Hello Today, we heard from the Senate Welcome to CS3!12

Data Types Basic Data Types Boolean Python s type for booleans is bool Boolean Values: only True or False Under the hood, your computer uses only Numeric Values: 1 bit 0 to -4 store 783910 a boolean 33.3333333 value -2.59 0 1 Strings (text): Hello Today, we heard from the Senate Welcome 1 bit to CS3 1 bit!13

Data Types Integers Python s type for integers is int Boolean Values: only True or False Whole numbers only Numeric Values: 0-4 783910 33.3333333-2.59 Calculations with integers are exact, except division Calculations with integers are crazy fast Strings (text): Hello If your Today, integer value we is heard between from the Senate ±9,223,372,036,854,775,807 Welcome to CS3 Under the hood, your computer uses 64 bits (8 bytes) to store an int!14

Data Types Basic Data Types Floating Point (numbers with decimals) Boolean Values: only True or False Python s type for these is called float Numeric Values: 0-4 783910 33.3333333-2.59 Used for representing numbers with decimals Strings (text): Hello Values are between a huge range: -10 Today, we heard from the 307 to 10 Senate 308 Under the hood, your computer uses 64 bits (8 bytes) Welcome to store a float to CS3!15

Data Types Basic Data Types Floating Point (numbers with decimals) Boolean Values: Every calculation only True with or floats False is always approximate. Very close to correct, but unreliable after about 15 digits after the decimal. So, never check if two floats Numeric Values: 0-4 783910 33.3333333-2.59 are equal. Strings (text): e.g., Hello Today, we heard from the Senate Welcome to CS3 Probably off!16

Data Types Basic Data Types Strings (text) Boolean Values: Strings are only used True to represent or False words A string is just a bunch of characters (a-z, 0-9, etc) Numeric Values: 0-4 783910 33.3333333-2.59 Under the hood, the number of bytes your computer Strings (text): Hello uses for a string depends on how long it is. Today, we heard from the Senate (Uses 1 byte per character.) Welcome to CS3!17

Data Types Data Types Variables Operators Casting!18

Data Types Data Types Variables Operators Casting!19

Variables for computers programs to access and use any data, they must store the data somewhere, even if it s a single, tiny piece of data (e.g., 1 number) and for a very brief time. Hence, why we have variables! Variable something that represents a specific, stored piece of data. Each variable has a name (defined by the programmer), and that name is used to later reference/access/use the data.!20

Variables for computers programs to access and use any data, they must store the data somewhere, even if it s a single, tiny piece of data (e.g., 1 number) and for a very brief time. Hence, why we have variables! Variable something that represents a specific, stored piece of data. Each variable has a name (defined by the programmer), and that name is used to later reference/access/use the data. variable value my_age = 19!21

Variables for computers programs to access and use any data, they must store the data somewhere, even if it s a single, tiny piece of data (e.g., 1 number) and for a very brief time. Hence, why we have variables! Variable something that represents a specific, stored piece of data. Each variable has a name (defined by the programmer), and that name is used to later reference/access/use the data. variable value my_age = 19 my_school = Brown University!22

Variables Variable Assignment variable value result = 2 + 2!23

Variables Variable Assignment variable value result = 2 + 2 expression a series of computations, and it always results in a value (the output)!24

Variables Variable Assignment variable value result = 2 + 2 expression a series of computations, and it always results in a value (the output) assignment assigns the value on the right to a memory address in the computer, and you can access it via the variable name result!25

Variables Variable Assignment result = 18 * 3 + 2!26

Variables Variable Assignment result = 18 * 3 + 2 18!27

Variables Variable Assignment result = 18 * 3 + 2 54!28

Variables Variable Assignment result = 18 * 3 + 2 56!29

Variables Variable Assignment result = 18 * 3 + 2 stores 56 (in binary) in the computer s memory somewhere, and you can always access this value via the variable n a m e d result. y o u could have named this variable whatever you want. 56 result 0000000000000000000000000000000000000000000000000000000000111000!30

Variables Variable Assignment result = 18 * 3 + 2 another_result = 1 + result!31

Variables Variable Assignment result = 18 * 3 + 2 another_result = 1 + result 1!32

Variables Variable Assignment result = 18 * 3 + 2 another_result = 1 + result 1+ 56!33

Variables Variable Assignment result = 18 * 3 + 2 another_result = 1 + result 57!34

Variables Variable Assignment result = 18 * 3 + 2 another_result = 1 + result stores 57 (in binary) in the computer s memory somewhere, and you can always access this value via the variable named another_result. you could have named this variable whatever you want. 57 another_result result 0000000000000000000000000000000000000000000000000000000000111001 0000000000000000000000000000000000000000000000000000000000111000!35

Variables Variable Assignment result = 18 * 3 + 2 another_result = 1 + result name = jim!36

Variables Variable Assignment result = 18 * 3 + 2 another_result = 1 + result name = jim stores jim (in binary) in the computer s memory somewhere, and you can always access this value via the variable named name. you could have named this variable whatever you want. jim jim 01101010 01101001 01101101!37

Variables Execution a computer executes one line of a code at a time optionally assigns the computation s output to a variable (if there s an = sign). 1 18 * 3 + 2 calculates 56 but doesn t do anything with it each line of code should do 1 thing, e.g. 1 result = 18 * 3 + 2 2 another_result = 1 + result not 1 result = 18 * 3 + 2 another_result = 1 + result!38

Variables Execution Initializing a variable the first time you assign something to a variable; this is the creation of the variable If you try to use a variable that you haven t yet initialize (aka created/defined), your program will crash with an error. 1 2 result = 18 * 3 + 2 another_result = 1 + results!39

Variables Execution Initializing a variable the first time you assign something to a variable; this is the creation of the variable If you try to use a variable that you haven t yet initialize (aka created/defined), your program will crash with an error. 1 2 result = 18 * 3 + 2 another_result = 1 + results!40

Data Types Data Types Variables Operators Casting!41

Data Types Data Types Variables Operators Casting!42

Operators Mathematical Operators result = a + b operator operators can be: Addition: + a and b can be: numerical values (ints or floats) or text (strings) but if you mix-and-match them, then you gotta be careful. Subtraction: - Multiplication: * Division: / Exponentiation: ** Whole number result of division: // Remainder of division (modulo): %!43

Operators Mathematical Operators result = 5 + 3 calculates 8 result = 5 / 3 calculates 1.6666666666666667 result = 5 ** 3 calculates 5 3 which is 125 result = 5 // 3 calculates 1 result = 5 % 3 calculates 2!44

Operators Mathematical Operators If an expression contains a float anywhere, the result will be a float result = 5.2 + 3 calculates 8.2 If the result of division of two integers is not an integer, the result will be a float result = 5 / 3 calculates 1.6666666666666667!45

Operators Mathematical Operators You can operate on any number or variable, including updating an existing variable s value: adds 1 to the current result = result + 1 value of result. standard way to represent a counter of something.!46

Operators Mathematical Operators You can operate on any number or variable, including updating an existing variable s value: adds 1 to the current result = result + 1 value of result. standard way to represent a counter of something. For succinctness, you could alternatively type: result += 1 same as above!47

Operators Mathematical Operators This nifty alternate version of writing can be used for all operations, e.g.,: is equivalent to result = result * 2 result *= 2

Operators String Operators String concatenation (aka combining words together): result = Red + Sox calculates RedSox Mainly useful to concatenate when you re combining text with numerical data (e.g., answers you care about) and you want to display it to the user!49

Operators For example: 1 result = 6 / 3 2 String Operators calculates 2.0. Remember division always yields a float message = The answer is + result!50

Operators For example: 1 2 result = 6 / 3 String Operators calculates 2.0. Remember division always yields a float message = The answer is + result Although, UH-OH. We get an error.!51

Operators String Operators For example: 1 2 result = 6 / 3 calculates 2.0 TypeError: The computation message = The answer is + result expects a value of a specific type, but received a different one instead Although, UH-OH. We get an error.!52

Operators What value does result contain? 1 2 3 4 result = 6 / 3 age = 20 result = result + age result /= 2!53

Operators What value does result contain? 1 2 3 4 result = 6 / 3 age = 20 result = result + age result /= 2 11.0!54

Data Types Data Types Variables Operators Casting!55

Data Types Data Types Variables Operators Casting!56

Casting Remember, mixing-and-matching numeric data (ints or floats) with text (strings) requires us to be careful The computer doesn t know what to do with these different data types, so it tells us that we must fix it. When trying to use different types of data together, which by default are incompatible, we must cast (convert) them (when possible).!57

Casting Casting to change a particular data s type of value(s) 1 2 result = 6 / 3 message = The answer is + str(result) converts the float to a string!!58

Casting Casting to change a particular data s type of value(s) 1 2 result = 6 / 3 message = The answer is + str(result) converts the float to a string! btw, print() allows us to display to the screen the value of whatever is in the ()!59

Casting Casting Examples We need to use int(), float(), str(), and bool() result = int( 5 ) 5 result = float( 5 ) 5.0 result = str(5) 5 result = str(5.0) 5.0 result = int( 5.2 ) ValueError result = int(float( 5.2 )) 5 result = float( h ) ValueError!60

Casting Casting Examples We need to use int(), float(), str(), and bool() result = int( 5 ) 5 ValueError: The computation result = float( 5 ) 5.0 expected a value with specific result = str(5) 5 properties, but the value it received as input differed result = str(5.0) 5.0 result = int( 5.2 ) ValueError result = int(float( 5.2 )) 5 result = float( h ) ValueError!61

Data Types Data Types Variables Operators Casting!62

Data Types Data Types Variables Operators Casting!63

Lab Time