Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Types and Expressions

Similar presentations


Presentation on theme: "Data Types and Expressions"— Presentation transcript:

1 Data Types and Expressions

2 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

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

4 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)

5 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)

6 Temperature Conversion
Activity: Convert temperature in Fahrenheit to Celsius

7 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)

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

9 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)

10 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 / # 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

11 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...

12 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( ) # produces 4.0 Python distinguishes between integers (no decimal) and real numbers ("floats")

13 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( ) print( )

14 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 = * 5 y = * 5 print(x) print(y)

15 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

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

17 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"""

18 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

19 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)

20 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?

21 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?

22 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.")

23 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

24 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.")

25 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

26 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


Download ppt "Data Types and Expressions"

Similar presentations


Ads by Google