Core Objects, Variables, Input, and Output

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Types and Arithmetic Operators
Introduction to C Programming
Declaring and Using Variables Visual Basic Data Types.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
JavaScript, Fourth Edition
Introduction to C Programming
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Computer Science 101 Introduction to Programming.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
CMPT 120 Topic: Python’s building blocks -> More Statements
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
Introduction to Programming
Chapter 4 Assignment Statement
ARITHMETIC IN C Operators.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Visual Basic Variables
Lecture 3: Operators, Expressions and Type Conversion
Chapter 2 - Introduction to C Programming
2.5 Another Java Application: Adding Integers
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.
Revision Lecture
Variables, Expressions, and IO
Chapter 2 - Introduction to C Programming
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Java Programming: From Problem Analysis to Program Design, 4e
IDENTIFIERS CSC 111.
Chapter 2 Elementary Programming
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Chapter 2 - Introduction to C Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2: Basic Elements of Java
Chapter 2 - Introduction to C Programming
Numbers.
Chapter 2 Variables.
Introduction to Programming
Chapter 2 - Introduction to C Programming
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, 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.
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2 - Introduction to C Programming
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Unit 3: Variables in Java
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
Variables in C Topics Naming Variables Declaring Variables
DATA TYPES AND OPERATIONS
Presentation transcript:

Core Objects, Variables, Input, and Output Section 1 Chapter 2 © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Numbers Numbers are referred to as numeric literals Two Types of numbers: ints and floats Whole number written without a decimal point called an int Number written with a decimal point called a float © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Numbers Arithmetic Operators Addition, subtraction, multiplication, division, and exponentiation. Result of a division is always a float Result of the other operations is a float if … Either of the numbers is a float Otherwise is an int. © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. The print Function Used to display numbers on the monitor If n is a number, print(n) displays number n. The print function can display the result of evaluated expressions A single print function can display several values Example 1: Program demonstrates operations © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Variables In mathematics problems, quantities are referred to by names Names given to the values are called variables Example 2: Program uses speed, time … calculates distance © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Variables Assignment statements Expression on right evaluated That value assigned to variable © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Variables Variable names in Python Begin with letter or underscore _ Can only consist of letters, numbers, underscores Recommend using descriptive variable names Convention will be Begin with lowercase Use cap for additional “word” in the name Example: rateOfChange © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Variables Names in Python are case-sensitive There are thirty-three words, called reserved words (or keywords) Have special meanings in Python Cannot be used as variable names See Appendix B © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. The abs Function © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. The int Function © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. The round Function © 2016 Pearson Education, Ltd. All rights reserved.

abs, int, and round Example Example 3: Program evaluates functions, prints results © 2016 Pearson Education, Ltd. All rights reserved.

Augmented Assignments Remember: expression on right side of assignment statement evaluated before assignment is made var = var + 1 Python has special operator to accomplish same thing var += 1 © 2016 Pearson Education, Ltd. All rights reserved.

Augmented Assignments Example 4: Program illustrates different augmented assignment operators. © 2016 Pearson Education, Ltd. All rights reserved.

Two Other Integer Operators Integer division operator Written // Modulus operator Written % © 2016 Pearson Education, Ltd. All rights reserved.

Two Other Integer Operators Example 5: converts 41 inches to 3 feet, 5 inches: © 2016 Pearson Education, Ltd. All rights reserved.

Parentheses, Order of Precedence Parentheses used to clarify meaning of an expression Order of precedence Terms inside parentheses (inner to outer) Exponentiation Multiplication, division (ordinary and integer), modulus Addition and subtraction © 2016 Pearson Education, Ltd. All rights reserved.

Syntax Errors Grammatical and punctuation errors are called syntax errors. Table 2.1 Three Syntax Errors © 2016 Pearson Education, Ltd. All rights reserved.

Syntax Errors FIGURE 2.1 Syntax error message boxes. © 2016 Pearson Education, Ltd. All rights reserved.

Runtime Errors Errors discovered while program is running called runtime errors or exceptions Table 2.2 Three Runtime Errors © 2016 Pearson Education, Ltd. All rights reserved.

Runtime Errors When Python encounters exception Terminates execution of program, displays message FIGURE 2.2 An Error Message for an Exception Error © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Logic Error Occurs when a program does not perform the way it was intended Example average = firstNum + secondNum / 2 Syntax correct, logic wrong: should be average = (firstNum + secondNum) / 2 Logic errors are most difficult type of error to locate © 2016 Pearson Education, Ltd. All rights reserved.

Numeric Objects in Memory Consider this code: This is what happens: FIGURE 2.3 Numeric objects in memory. © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. End Section 1 Chapter 2 © 2016 Pearson Education, Ltd. All rights reserved.