Data Types and Expressions

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Python November 14, Unit 7. Python Hello world, in class.
Introduction to Python and programming Michael Ernst UW CSE 190p Summer 2012.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Input, Output, and Processing
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Python and programming Ruth Anderson UW CSE 140 Winter
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Chapter 2 Writing Simple Programs
CS 106A, Lecture 4 Introduction to Java
Topics Designing a Program Input, Processing, and Output
BASIC ELEMENTS OF A COMPUTER PROGRAM
Topic: Python’s building blocks -> Statements
Intro to CS Nov 2, 2015.
Building Java Programs
Chapter 2 - Introduction to C Programming
2.5 Another Java Application: Adding Integers
Introduction to Python and programming
CSC 131: Introduction to Computer Science
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.
Design & Technology Grade 7 Python
Presented By S.Yamuna AP/IT
Variables, Expressions, and IO
Chapter 2 - Introduction to C Programming
Building Java Programs Chapter 2
Introduction to Python and programming
Chapter 2 - Introduction to C Programming
Introduction to Python and programming
Chapter 2 - Introduction to C Programming
Building Java Programs
Building Java Programs
Arithmetic Expressions & Data Conversions
Chapter 2 - Introduction to C Programming
Building Java Programs
Introduction to Python and programming
Rocky K. C. Chang September 18, 2018 (Based on Zelle and Dierbach)
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Building Java Programs Chapter 2
Variables, Data Types & Math
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.
Building Java Programs
Building Java Programs
Topics Designing a Program Input, Processing, and Output
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Topics Designing a Program Input, Processing, and Output
Building Java Programs
Chapter 2 - Introduction to C Programming
Building Java Programs
Unit 3: Variables in Java
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Introduction to C Programming
Introduction to Python and programming
Building Java Programs
Arithmetic Expressions & Data Conversions
Flow Control I Branching and Looping.
Introduction to Python
Getting Started in Python
PYTHON - VARIABLES AND OPERATORS
Building Java Programs
Presentation transcript:

Data Types and Expressions

Python and Math To a CPU, all information is numeric Aside: Can non-numeric data (text/sound/images) be represented using only numbers? Python provides great support for mathematical calculations Interpreter demo: 2 + 2 2 + 2 * 4 (2 + 2) * 4

Math Operators Operator Operation + addition - subtraction * multiplication / division // truncating division ** exponentiation % modulus (remainder)

The print( ) statement You can type calculations into the Python interpreter interactively and see the results: >>> 2 + 2 4 When writing a Python program, use the print( ) statement to see results: print(2 + 2)

Use print( ) to Display messages: Display results of calculations: print("Hello, Frank!") # Note the double quotes Display results of calculations: print(2 + 2) Display messages and calculation results print("2 + 2 = ", 2 + 2)

Temperature Conversion Activity: Convert temperature in Fahrenheit to Celsius

Variables A variable is a name that has a value associated with it Create a variable using an assignment statement: tempFah = 59 An assignment statement Allocates a block of memory for the variable (if variable not previously assigned) Stores the value in the memory You can use a variable in a calculation: print((tempFah - 32) * 5 / 9)

A Temperature Conversion Program # Convert degrees Fahrenheit to Celsius tempFah = 85 tempCel = (tempFah - 32) * 5 / 9 print(tempCel)

Another (Less Clear) Version A variable's value can change during the execution of a program. This is often useful, but reusing the same variable for different purposes is not a good idea # Convert degrees Fahrenheit to Celsius temp = 85 temp = (temp - 32) * 5 / 9 # update temp to hold Celsius value - Uggh print(temp)

Python is Case Sensitive Python treats variables with different capitalization as distinct variables This program will "crash" when executed: # Convert degrees Fahrenheit to Celsius tempFah = 85 tempCel = (tempfah - 32) * 5 / 9 # This ship is going DOWN right here... print(tempCel) It is illegal to use a variable in a calculation before it has been assigned a value

Expressions An expression is a sequence of operators and operands that yields a value The expression must be well formed Good expression: (tempFah - 32) * 5 / 9 Bad expression: * (tempFah - 32 * 5 / 9 Expressions can be used On the right-hand side of assignment statements x = x + 1 Inside parenthesis of print( ) statements print( x + 1 ) Other places...

Numeric Values An expression yields a value when it is evaluated by the interpreter Notice the difference in output: print(2 + 2) # produces 4 print(2.0 + 2.0) # produces 4.0 Python distinguishes between integers (no decimal) and real numbers ("floats")

ints vs floats Python treats integer values and real numbers ("floats") differently Stores them differently in memory Integers are unbounded (practically speaking) Floating point values have a limited range Try the following: print(222222222222222222222222222222222223) print(222222222222222222222222222222222223.0)

Mixing int and float values Expressions involving calculations on int values generally produce an int (what's the exception?) If the expression contains even one float, the result is a float Consider the following: x = 22 + 2 * 5 y = 22 + 2.0 * 5 print(x) print(y)

Variables and Values When you store a value in a variable, the computer records both the value and remembers whether it is an int or a float x = 5 y = 5.0 print(x + 1) # 6 print(y + 1) # 6.0

Types of Data Variables can hold: Numbers Text values ("Strings") x = "Look, ma!" Boolean values x = True

Strings A string is a value that consists of a sequence of symbols String values are surrounded by quotes The quotes are not part of the string value itself, and do not appear when the string value is printed print("""Hi, ma""") Single or Double Quotes Tripled Quotes 'Hi, ma' "Hi, ma" '''Hi, ma''' """Hi, ma"""

Data Types A data type is a category of data values that a program can manipulate Python supports the following data types: Data Type Category of Values Example int Integers -3 float Real numbers -3.5 str Strings of symbols "Look, ma!" bool Boolean values True, False

Data Types are Important A string value can be a sequence of digits x = "12" If you try doing math using a string value, you'll get errors or odd behavior print(x * 2) print(x + 1)

Operators, Operands, and Data Types Operators operate on values denoted by operands x * 5 Each operand has a value of a particular data type The operator's result depends on the values and types of the operands What value and data type is computed when the expression above is evaluated and x is int? x is float? x is string?

Getting Input Use the input( ) function to get input from the user name = input("What's your name?") print("Hello,", name) input( ) always yields a string value But what if you want to do math using the user's input?

Simple Actuarial Calculator (buggy) age = input("How old are you?") yearsLeft = 80 - age # uh oh - CRASH here print("You may have about", yearsLeft, "years of life left.")

Data Type Conversion Sometimes you need to convert a value from one type to another Type conversion functions convert a value to a target type int(x) attempts to convert x to an integer float(x) attempts to convert x to a float str(x) attempts to convert x to a string

Simple Actuarial Calculator # This one works! ageStr = input("How old are you?") age = int(ageStr) yearsLeft = 80 - age print("You may have about", yearsLeft, "years of life left.")

Using Conversion Functions Three ways to handle conversion: ageStr = input("How old are you?") age = int(ageStr) # after input, before calculation yearsLeft = 80 - age or age = int(input("How old are you?")) # at time of input age = input("How old are you?") yearsLeft = 80 - int(age) # on the fly, during calculation

Summary Python programs manipulate values stored in variables A variable holds a value of a particular data type, which can change The behavior of operators depends on the data types of their operands Understanding what a Python program does requires the reader to keep track of what data type a variable holds each time it appears in a calculation