Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Introduction to C Programming
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Python November 14, Unit 7. Python Hello world, in class.
Introduction to Python
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏
Introduction to C Programming
Introduction to Python
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
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
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Mathematical Calculations in Java Mrs. G. Chapman.
Java Data Types Assignment and Simple Arithmetic.
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
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏ Sec 9-7 Web Design.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
Mathematical Calculations in Java Mrs. C. Furman.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
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.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Debugging, Escape Command, More Math. It’s your birthday!  Write a program that asks the user for their name and their age.  Figure out what their birth.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Variables 1. What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example:
Formatting Output. Line Endings By now, you’ve noticed that the print( ) function will automatically print out a new line after passing all of the arguments.
Python Let’s get started!.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Thinking about programming Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Input, Output and Variables GCSE Computer Science – Python.
Thinking about programming
Lesson 03: Variables and Types
Topics Designing a Program Input, Processing, and Output
Chapter 2 Basic Computation
Python Let’s get started!.
Data Types and Conversions, Input from the Keyboard
Formatting Output.
Introduction to Computer Science / Procedural – 67130
2.5 Another Java Application: Adding Integers
Multiple variables can be created in one declaration
Presented By S.Yamuna AP/IT
Variables, Expressions, and IO
Thinking about programming
Formatting Output.
Functions CIS 40 – Introduction to Programming in Python
Chapter 2 Basic Computation
Number and String Operations
Learning Outcomes –Lesson 4
Topics Designing a Program Input, Processing, and Output
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
THE COMPUTE STATEMENT Purpose: performs mathematical calculations
Variables, Data Types & Math
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Data Types and Maths Programming Guides.
Data Types and Expressions
Presentation transcript:

Math, Data Types

Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //

Expressions  We use operators along with numeric data to create “math expressions” that Python can evaluate on our behalf  However, unless you ask python to output the value of the expression, it will not do so  Example: -writing “5 + 2” in python will not output anything, although the calculation will be done -instead, you must write something like: print ( ) >> 19

Storing results of an expression  You can also store the result of your expression into a variable  Example: answer = print ( ‘the answer to is‘, answer ) >> the answer to is 7

Using variables in math expressions  Math expressions don’t need to be done on only numeric literals  Example: price = sales_tax = 0.07 total = price + price * sales_tax

Data Types  Python needs to know how to set aside memory in your computer based on what kind of information you want to store  There are three basic types of data types that we will be working with:  Strings (character-based data)  Number (floats/integers)  Boolean variables (True/False)

Numeric Data Types  Integers:  Whole numbers that do not contain a decimal point  Abbreviated as “int” in Python  Example: 5, - 5, 100,  Floating Point Numbers:  Numbers that contain a decimal point  Abbreviated as “float” in Python  Example: 5.0, - 5.0, ,

Strictly Typed Languages  Python is not a strictly typed language, which means that you don’t need to pre-declare what kind of data your variables will be holding, it will automatically assign it for you Loosely TypedStrictly Typed PythonC PHPC++ JavaScriptJava PerlActionScript

Strictly Typed Languages ActionScriptJava var name:String = “Donald”;string name = “Donald” ; var top_speed:Number = 50;int top_speed = 50 ; var gravity:Number = 9.5;float gravity = 9.5 ;

Numeric Data Types  You can store numeric data inside variables and Python will automatically assign it a type according to how it is created  num_1 = 5 # Python recognizes this as an int  num_2 = 4.99 # Python recognizes this a float  Keep in mind that you can not use separators or symbols when storing numeric data  num_3 = $ 5, # This will cause an error!

Practice  5  5.5  “Hello”  “5.5”   2.0  “$2.99”

Practice  5 # int  5.5 # float  “Hello” # string  “5.5” # string  # float  2.0 #float  “$2.99” # string

Input and Math expressions  Recall that the input function always returns a string average = input (“what was the average?”) what was the average? 53 # this will be inputted into the variable as a string, “53”

Input and Math expressions average = input (“what was the average?”) new_average = average + 2 # this will cause an error because you are trying to add a string to some numeric data  So, how can we convert our inputted data into a numeric data type that Python supports? (an integer, or a float)

Concatenation  We are able to use math operators on more than numeric data types. We can “add” and also “multiply” strings.  When we add two strings together, we call this concatenation.  Example: print(“53” + “2”) >> 532

Concatenation  This will help us with a problem we ran into previously.  Remember that our print function by nature will add a space between arguments, whenever it recognizes a comma. print (“Average Scores for \“”, class, “\“”) >> Average Scores for “ Intro to Programming ”

Concatenation  Now, with the “+” operator, we can literally attach two strings together without any additional spaces. print (“Average Scores for \“” + class + “\“”) >> Average Scores for “Intro to Programming”

String Repetition  A similar function is applied when we “multiply” strings with a numeric data type.  The * operator will print out a string the number of times it is being “multiplied by” but in a literal sense. print (“Hello” * 3) >> HelloHelloHello

If we do not convert data types … # ask user for monthly salary monthly_salary = input (‘how much do you make a month?’) # calculate annual salary annual_salary = monthly_salary * 12 # print result print ( ‘that means you make’, annual_salary, ‘in a year’) >> how much do you make a month? 100 that means you make in a year

The float( ) and int ( ) functions  The float ( ) and int ( ) functions are data type conversion functions.  They take the passed argument and convert that into a specific data type

The float( ) and int ( ) functions # ask user for monthly salary monthly_salary = input (‘how much do you make a month?’) # convert salary into float monthly_salary_float = float(monthly_salary) # calculate annual salary annual_salary = monthly_salary_float * 12 # print result print ( ‘that means you make’, annual_salary, ‘in a year’)

Nesting data type conversions  It took us two steps to convert our data, but we can do it in one!  We use a technique called “nesting”  Example: my_num = float ( input ( ‘give me a number!’ ) )

Nesting data type conversions

Practice  Ask the user for two numbers. You can assume they will input floating point numbers.  Compute the following and print it out to the user:  The sum of the numbers  The difference between the numbers  The product of the numbers  The quotient of the first number divided by the second