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

Similar documents
Welcome to Computer Organization and Design Logic

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Welcome to Computer Organization and Design Logic CS 64: Computer Organization and Design Logic Lecture #1 Winter 2018

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

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

CS1 Lecture 3 Jan. 22, 2018

CS1 Lecture 3 Jan. 18, 2019

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

Finding Pi: Applications of Loops, Random Numbers, Booleans CS 8: Introduction to Computer Science, Winter 2018 Lecture #6

Welcome to Solving Problems with Computers I

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

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

Strings in Python 1 Midterm#1 Exam Review CS 8: Introduction to Computer Science Lecture #6

Conditional Expressions and Decision Statements

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

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

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Review for the Final Exam CS 8: Introduction to Computer Science, Winter 2018 Lecture #15

CIS 110: Introduction to Computer Programming

Python, Part 2 CS 8: Introduction to Computer Science Lecture #4

Lecture 3. Input, Output and Data Types

The C++ Language. Arizona State University 1

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

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

CSE 115. Introduction to Computer Science I

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

CIS192 Python Programming. Robert Rand. August 27, 2015

ENGR 101 Engineering Design Workshop

BASIC ELEMENTS OF A COMPUTER PROGRAM

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

File I/O in Python Formats for Outputs CS 8: Introduction to Computer Science, Winter 2018 Lecture #12

Lecture 1. Types, Expressions, & Variables

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

C/C++ Programming for Engineers: Working with Integer Variables

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

Fundamentals of Programming

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Lecture 2: Variables & Assignments

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Values, Variables, Types & Arithmetic Expressions. Agenda

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

CMSC201 Computer Science I for Majors

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8

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

Visual C# Instructor s Manual Table of Contents

Chapter 2: Basic Elements of C++

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science, Winter 2018 Lecture #9

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

Reviewing all Topics this term

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

Variables and Operators 2/20/01 Lecture #

CS/ENGRD 2110 FALL 2018

CS 241 Data Organization. August 21, 2018

Fundamentals of Programming CS-110. Lecture 2

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

1.00/1.001 Tutorial 1

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

REVIEW. The C++ Programming Language. CS 151 Review #2

Python Input, output and variables

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

CS313D: ADVANCED PROGRAMMING LANGUAGE

MIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8

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

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

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Fundamental of Programming (C)

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

Getting started with C++ (Part 2)

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17

Introduction to the C++ Programming Language

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

More Examples Using Lists Tuples and Dictionaries in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #11

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2 Fall 2018

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

C: How to Program. Week /Mar/05

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

Midterms Save the Dates!

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science Lecture #7

CS 1301 Exam 1 Answers Fall 2009

Algorithms and Programming I. Lecture#12 Spring 2015

Lecture 1. Course Overview, Python Basics

W13:Homework:H08. CS40 Foundations of Computer Science W13. From 40wiki

CS102: Variables and Expressions

CSCA08 Winter 2018 Week 2: Variables & Functions. Marzieh Ahmadzadeh, Brian Harrington University of Toronto Scarborough

Programming for Engineers in Python. Recitation 1

CIS220 In Class/Lab 1: Due Sunday night at midnight. Submit all files through Canvas (25 pts)

Objectives. In this chapter, you will:

BLM2031 Structured Programming. Zeyneb KURT

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

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

Full file at

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

AP Computer Science A

C++ Programming: From Problem Analysis to Program Design, Third Edition

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

Transcription:

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2 Ziad Matni, Ph.D. Dept. of Computer Science, UCSB

Your Instructor Your instructor: Ziad Matni, Ph.D(zee-ahd mat-knee) Email: zmatni@cs.ucsb.edu (please put CS8 at the start of the subject header) My office hours: Wednesdays 1:00 PM 2:30 PM, at SMSS 4409 1/9/2019 Matni, CS8, Wi19 2

A Word About Registration for CS8 This class is FULL, & the waitlist is CLOSED. 1/9/2019 Matni, CS8, Wi19 3

Administrative Lab00 success or not? Pairing in Labs When doing paired labs, you may ONLY pair with someone in the SAME lab as you! The TAs and I will assign you your lab partner, based on the ic00 form you gave us. 1/9/2019 Matni, CS8, Wi19 4

Linux Workshop? Basic commands How to log in remotely from your computer to CSIL Copy files back and forth How many are interested in attending this? Would be on a Friday morning (10 am) Would last < 1 hour 1/9/2019 Matni, CS8, Wi19 5

Python IDLE IDLE is what we use to demonstrate Python in class You can also use it at home (download info given last class) If you want to create a Python program, then you will place all the program code inside a text file Text file always ends in.py You can create and also run (execute) the.py program from Python IDLE Make sure the version of IDLE you use is AT LEAST 3.7.x or LATER 1/9/2019 Matni, CS8, Wi19 6

Lecture Outline Numbers and Arithmetic in Python Variables in Python Variable Types in Python Operations in Python Assignment versus Comparison of Values Yellow Band = Class Demonstration! 1/9/2019 Matni, CS8, Wi19 7

We might talk more about what this is later on in the quarter Numbers are Objects to Python Each object type has: data and related operations 2 basic number types Integers (like 5 or -72) add, subtract, multiply, Floating point numbers (like 0.005 or -7.2) operations similar but not exactly the same as integer operations Expect many non-number object types later in the quarter But they also have data and related operations 1/9/2019 Matni, CS8, Wi19 8

Common Data Types Type Example Description float 3.1415 A real number. Can be positive or negative. int 3 An integer number. Can be positive or negative. string bool ILUVCS8!!! Gaucho Goop True False A series (or a string) of characters. Note the use of as delimiters. A Boolean outcome of a logical comparison. 1/9/2019 Matni, CS8, Wi19 9

Common Data Types Type Example Description float 3.1415 A real number. Can be positive or negative. int 3 An integer number. Can be positive or negative. string bool ILUVCS8!!! Gaucho Goop True False A series (or a string) of characters. Note the use of as delimiters. A Boolean outcome of a logical comparison. 1/9/2019 Matni, CS8, Wi19 10

Arithmetic Operators + - * / add, subtract, multiply, (ordinary) divide % modulus operator remainder ( ) means whatever is inside is evaluated first ** raise to the power Special Python division operator for integers: // result is truncated: 7 // 2 3 (not 3.5) Precedence rules: What s easier to remember: 3 * 2 1 Or: (3 * 2) 1??? 1. ( ) 2. ** 3. *, /, %, // 4. +, - 5. = 1/9/2019 Matni, CS8, Wi19 11

Comments in Python Anything placed after the # symbol is considered a comment Is completely ignored by the compiler Typically place commentary next to code for the benefit of others (humans) reading our code 1/9/2019 Matni, CS8, Wi19 12

Variables A variable is a symbolic reference to data The variable's name represents what information it contains a 1.5 b 3.3 They are called variables because --- data can VARY or change --- while operations on the variable remain the same e.g. Variables a and b can take on different values, but I may always want to add them together 1/9/2019 Matni, CS8, Wi19 13

Variables 96 A SCORE GRADE Variables are like buckets that can keep data You can label these buckets with a name When you reference a bucket, you use its name, not the data stored in the bucket You can re-use the buckets If two variables are of the same type, you can perform operations on them 1/9/2019 Matni, CS8, Wi19 14

Variables in Python We assign a value to variables with the assignment operator = Example: >>> a = 3 We can change that value stored Example: >>> a = 5 # not 3 any more!!! 1/9/2019 Matni, CS8, Wi19 15

Assigning Names to Variables Variable names are actually references Like pointers to objects Can have multiple references to the same object x = 5 y = x # x refers to an integer # Now x and y refer to the same object 1/9/2019 Matni, CS8, Wi19 16

Assigning Names to Variables Dynamic typing is a key Python feature Any legal name can point to any data type even different types at different times x = 5 y = x # x refers to an integer # Now x and y refer to the same object # and both are integers x = 1.2 # Now x refers to floating point 1.2 # (y still refers to the integer 5) 1/9/2019 Matni, CS8, Wi19 17

All Data in Python Has a type() But you can change its type Implicitly, like in the last slide Explicitly, by forcing the type Introducing the built-in function type() Let s try these out on IDLE: >>> grade = 3.8 >>> type( grade ) >>> grade = 5 >>> type( grade ) >>> type( Green Eggs ) >>> type(true) >>> type(true) >>> pi = 3.14 >>> type( pi ) >>> p = int( pi ) >>> print( p ) >>> type ( p ) 1/9/2019 Matni, CS8, Wi19 18

More on Type Casting Let s try these out on IDLE and explain them: >>> int(4.2) >>> int (True) >>> int (False) >>> float(false) >>> float ( true ) >>> float (4) / 5 >>> str ( 42 ) >>> int ( 42 ) 1/9/2019 Matni, CS8, Wi19 19

Variable Names in Python 3 simple rules for choosing names: Can ONLY have letters, digits, and _ (underscores) UserName Age1 Age2 _Deviation Must NOT begin with a digit or non-alphabet character (except underscore) Cannot use Python reserved keywords Example: def, int, False, True, print, etc 2Good2BTrue $$MaMoney!! <0_0> #YOLO 1/9/2019 Matni, CS8, Wi19 20

Variable Names in Python: Other Conventions Choose brief, but meaningful names Most programmers prefer lower case use (Example: total vs. TOTAL) Use either camel case or underscore to separate words Camel Case is using capital letters to separate words, like NumOfCats Underscoring is using underscores to separate words, like num_of_cats Be consistent: use one or the other throughout your program All the above applies to function names, module names, etc 1/9/2019 Matni, CS8, Wi19 21

Objects An object in Python is anything that has: an identity a type a value Example: pi = 3.14159 Identity: pi Type: floating point Value: 3.14159 1/9/2019 Matni, CS8, Wi19 22

Demo Let s try this out what do you think it ll do? pi = 3.14159 radian_angle = 0.7853975 degree_angle = radian_angle*180/pi print(degree_angle) # What is print()? 1/9/2019 Let s Matni, try CS8, Wi19 it out! 23

The Equals Operators = ==!= Assign it to Is it equal to? Is it not equal to? 1/9/2019 Matni, CS8, Wi19 24

The Equals Operators bebe = 22 bebe == 22 bebe!= 22 The int variable bebe now has the value 22 (i.e. it is assigned the value 22) Is bebe equal to 22? The answer is yes, in other words, the answer is Boolean True. Note that bebe is not changed it s still 22 Is bebe not equal to 22? The answer is False. Again, bebe is unchanged. Let s try it out! 1/9/2019 Matni, CS8, Wi19 25

Assignment vs. Comparison What happens when I do this? a = 1.1 b = a a = 2.2 a b 1.1 1.1 1.1 2.2 1.1 1/9/2019 Matni, CS8, Wi19 26

Assignment vs. Comparison What happens when I do this? z = 6 y = 8 z = z + y x = (z == 14) z y x 6 6 8 14 8 14 8 True 1/9/2019 Matni, CS8, Wi19 27

Assignment vs. Comparison What happens when I do this? m = 42 n = 2 - m n == 0 m = (m == 40) m n 42 42-40 42-40 False -40 1/9/2019 Matni, CS8, Wi19 28

Input and Output We ll make use of 2 built-in functions in Python: print() to print out to the screen (called standard output) input() to get input from the keyboard (called standard input) 1/9/2019 Matni, CS8, Wi19 29

Input and Output To output data, use print() >>> print( Hello all you happy people! ) To get data and put it in a variable, use input() >>> name = input() OR >>> name = input( Name. Give it. Now: ) 1/9/2019 Let s Matni, try CS8, Wi19 it out! 30

YOUR TO-DOs Read Chapter 2 Finish Homework1 (due Monday!) Prepare for Lab1 next week Hug a tree! But don t get wet 1/9/2019 Matni, CS8, Wi19 31

1/9/2019 Matni, CS8, Wi19 32