Winter 2019 CISC101 4/28/2019 CISC101 Reminders

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

JavaScript, Third Edition
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
Introduction to Python
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Today… Continue Building Expressions: –Literals –Constants –Casting –Array Declaration –Operators and Operator Precedence –Keywords Winter 2016CMPE212.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
CompSci 230 S Programming Techniques
Expressions.
Introduction to Python
Building Java Programs
Overview: Programming Concepts
Data Types, Identifiers, and Expressions
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Debugging and Random Numbers
Variables, Expressions, and IO
CISC124 Labs start this week in JEFF 155: Meet your TA.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Java Programming: From Problem Analysis to Program Design, 4e
Winter 2018 CISC101 11/9/2018 CISC101 Reminders
CISC101 Reminders Quiz 2 this week.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Winter 2018 CISC101 11/15/2018 CISC101 Reminders
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
CISC101 Reminders Your group name in onQ is your grader’s name.
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 11/27/2018 CISC101 Reminders
Winter 2018 CISC101 11/29/2018 CISC101 Reminders
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Building Java Programs
CISC101 Reminders Assn 3 due tomorrow, 7pm.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
CISC101 Reminders Quiz 2 this week.
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
CISC124 TA names and s have been added to the course web site.
CISC124 Labs start this week in JEFF 155.
Winter 2019 CISC101 2/17/2019 CISC101 Reminders
CISC124 Labs start this week in JEFF 155. Fall 2018
CISC/CMPE320 - Prof. McLeod
Core Objects, Variables, Input, and Output
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
CISC/CMPE320 - Prof. McLeod
Boolean Expressions to Make Comparisons
Winter 2019 CISC101 4/8/2019 CISC101 Reminders
SSEA Computer Science: Track A
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Labs start this week. Meet your TA! Get help with:
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
CISC101 Reminders Assignment 2 due today.
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Winter 2019 CISC101 4/14/2019 CISC101 Reminders
Building Java Programs
CISC101 Reminders Quiz 1 marking underway.
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Assignment 3 due today.
Building Java Programs
Introduction to Python
Presentation transcript:

Winter 2019 CISC101 4/28/2019 CISC101 Reminders You have been assigned to two groups in onQ – the “Lab” group and the “Grader” group. You must write the quiz in the lab time as indicated by the Lab group you are in. Quiz 1 next week, starting in the Tuesday lab. More info in yesterday’s lecture. Assignment 1 due next Friday. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

Today Continue Python Syntax by looking at Expressions: Winter 2019 CISC101 4/28/2019 Today Continue Python Syntax by looking at Expressions: Python Types – we did this yesterday. Type BIFs. Variables Operators Operator Precedence Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

From Exercise 1 – Types, Cont. How can you discover the type of a variable? How can you change a literal of one type to another? Winter 2019 CISC101 - Prof. McLeod

Aside - Using the Help Docs Press <F1> (or use a menu option) from the Python Shell window or from IDLE. The most useful sections: The Python Tutorial. The Python Standard Library. Look for the table of Built-In Functions, for example. Winter 2019 CISC101 - Prof. McLeod

Variables What is a variable anyways? CISC101 Variables What is a variable anyways? In Python, variables are created by an assignment statement (or in a function’s parameter list). A variable takes the type of the value being assigned to it when the program runs. A variable’s value can change any time, as can its type. This is a convenient way to address a location in memory. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

Assigning/Creating a Variable In code: myVal = 20 Now myVal refers to some location in RAM that stores the int type value 20. We don’t have to worry about what the actual memory address is. Winter 2019 CISC101 - Prof. McLeod

Variable Naming Syntax Rules Variable names are case sensitive. You can’t use a Python keyword for a variable name. No spaces! Start with a letter (use lower case, by convention), or an underscore_. The rest of the name can contain numbers, letters or the underscore (no spaces – oops I said that already!) Why no spaces, anyways? Winter 2019 CISC101 - Prof. McLeod

Variable Naming Style Rules Use descriptive names. Capitalize successive words in a name using camelCase. No limit to the length of a variable name, but don’t write an essay!! Don’t use single letter variable names, except if you need a loop counter that has no intrinsic meaning, then you can use i, j or k. Winter 2019 CISC101 - Prof. McLeod

Aside – The Worst Variable Name! My favourite bad variable name: l1 Winter 2019 CISC101 - Prof. McLeod

Arithmetic Operators As listed in Exercise 1: + addition Winter 2013 CISC101 Arithmetic Operators As listed in Exercise 1: + addition - subtraction (and unary negation) * multiplication / division // "floor" division % modulo or "remainder" ** exponentiation or "to the power of" The first four make sense, how do the last three work? Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

/ or Division For example, what is the value of 1 / 3 ? 0.3333333333333333 In previous versions of Python the result of int divided by int would be an int. Not any more! Now the result is a float. BTW, good thing there is a limit to the size of a float! Winter 2019 CISC101 - Prof. McLeod

// or Integer or “Floor” Division What is the value of 1 // 3? The result is always truncated, not rounded. So, 99 // 100 is also zero. You still get the truncated value, even if one or the other numbers are floats. So, 1.0 // 3.0 is still 0.0 Winter 2019 CISC101 - Prof. McLeod

% or “Modulo” Always gives the remainder after division. For example, 20 % 3 is 2 Also works with floats. Result is a float. Winter 2019 CISC101 - Prof. McLeod

** or “To the Power Of” Example 5 ** 2 is 25 If both numbers are ints, the result is an int. You can generate some pretty large int values this way! For example 5 ** 100 is: 7888609052210118054117285652827862296732064351090230047702789306640625 If at least one number is a float, the answer is a float. So, 5.0 ** 100 is: 7.888609052210118e+69 Winter 2019 CISC101 - Prof. McLeod

Mixed Numeric Types If you have an expression like: aVal = 4 + 5 – 2.0 the result will always be a float. 7.0 in this case. So, if you get any float values in an expression that has integers the result will always be a float. So something like 4 + 10 / 5 gives 6.0 Winter 2019 CISC101 - Prof. McLeod

5 * ‘abc’ is ‘abcabcabcabcabc’ Strings and Numbers Multiplication: 5 * ‘abc’ is ‘abcabcabcabcabc’ The + operator is the only other numeric operator that works with strings. Winter 2019 CISC101 - Prof. McLeod

Addition and Strings The + sign not only adds numbers but can also concatenate strings (and collections). Shall we try it out? You can concatenate a number to a string, but have to convert it to a string first using the str() BIF. Winter 2019 CISC101 - Prof. McLeod

Operators, Summary + - * / // % ** + - * / // % ** All of these work with numbers on both sides. + and * will accept a string or even a collection on one side or both. If an expression has a float number in it, then the result will always be a float. Winter 2019 CISC101 - Prof. McLeod

Assignment Operator We’ve used this one already: = Used to assign a value to a variable and to create that variable, if necessary. Note that you can combine the other arithmetic operators with = as long as the variable already exists. For example +=. Assuming aVar is 3, then aVar += 5 stores 8 in aVar This is the same as writing: aVar = aVar + 5 Winter 2019 CISC101 - Prof. McLeod

Precedence Rules Suppose I have an expression like: a = 5 * b + 27 / c CISC101 Precedence Rules Suppose I have an expression like: a = 5 * b + 27 / c How do we know the order of operations? These rules are built-in to the interpreter. = is always last (why?) ** first, then - (as unary negation), then *, /, // and %, then + and -, and then = Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

Precedence Rules, Cont. Rules again: ** - (unary) * / // % + - = All operators on the same line have equal precedence. Winter 2019 CISC101 - Prof. McLeod

Precedence Rules, Cont. How can you take over control of the order of operations? Use the round brackets: For example (4 + 5) // 3 is 3, but 4 + 5 // 3 is 5 If you have a series of operators that have equal precedence and no brackets to control things, then the expression is evaluated from left to right. Winter 2019 CISC101 - Prof. McLeod

Example 4 + (5 * (6 - 3)) % 4 4 + (5 * 3) % 4 4 + 15 % 4 4 + 3 7 4 + (5 * (6 - 3)) % 4 4 + (5 * 3) % 4 4 + 15 % 4 4 + 3 7 Winter 2019 CISC101 - Prof. McLeod

Another Example aNum = 3 ((7 - 12) // 2) + 7 / 2 - aNum + 2 * 3 - (7 % 2) ((7 - 12) // 2) + 7 / 2 - 3 + 2 * 3 - (7 % 2) (-5 // 2) + 7 / 2 - 3 + 2 * 3 - (7 % 2) -3 + 7 / 2 - 3 + 2 * 3 - (7 % 2) -3 + 7 / 2 - 3 + 2 * 3 – 1 -3 + 3.5 - 3 + 2 * 3 – 1 -3 + 3.5 - 3 + 6 – 1 0.5 - 3 + 6 – 1 -2.5 + 6 – 1 3.5 – 1 2.5 Winter 2019 CISC101 - Prof. McLeod

CISC101 Boolean Operators Also have a bunch of binary operators (and one unary operator) that yield a Boolean or a bool type result – a True or False. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

Unary Boolean Operator One unary boolean operator: not Sets True to False, and False to True (just like a NOT gate!) Winter 2019 CISC101 - Prof. McLeod

Binary Boolean Operators > < >= <= == != greater than less than greater than or equal to less than or equal to equals not equals All of these result in a True or False bool value. and or a logical AND a logical OR Winter 2019 CISC101 - Prof. McLeod

Binary Boolean Operators, Cont. > < >= <= == != greater than less than greater than or equal to less than or equal to equals not equals All of these must have a number on both sides or a string on both sides. and or a logical AND a logical OR These must have a bool value on both sides. Winter 2019 CISC101 - Prof. McLeod

Binary Boolean Operators, Cont. The interpreter is going to want both sides to be either numeric or both sides to be strings. You will get an error if you try to compare a string to a number. Winter 2019 CISC101 - Prof. McLeod

Example How can you test a variable, aVal, to see if it is between two limits, inclusive? Call them low and high: Check: low <= aVal and aVal <= high Unfortunately, in Python, this also works sometimes: low <= aVal <= high This is bad form and will not work in most other programming languages! Winter 2019 CISC101 - Prof. McLeod

Example, Cont. If aVal is between the limits the expression on the previous slide will evaluate to a True. Suppose you want to see a True if aVal is outside the limits. How? Two ways: not( low <= aVal and aVal <= high ) Or: low > aVal or aVal > high Which way do you like better? Winter 2019 CISC101 - Prof. McLeod