Python November 14, Unit 7. Python Hello world, in class.

Slides:



Advertisements
Similar presentations
IS means = sign Writing Equations: “2 more than twice a number is 5”
Advertisements

Types and Arithmetic Operators
Study Guide. a. 1 of 2 numbers multiplied together to get a product b. The answer to a division problem c. The answer to a multiplication problem d. None.
Study Guide. a) PEMDAS b) Please Excuse My Dear Aunt Sally c) Parenthesis, Exponents, Multiplication and Division, Addition and Subtraction d) Both A.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Variables and I/O. Types Strings –Enclosed in quotation marks –“Hello, World!” Integers –4, 3, 5, 65 Floats –4.5, 0.7 What about “56”?
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
Variables and I/O. Types Strings –Enclosed in quotation marks –“Hello, World!” Integers –4, 3, 5, 65 Floats –4.5, 0.7 What about “56”?
Python Programming Chapter 2: Variables, expressions, and statements Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Introduction to Python
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Math is a language, learn the words!
Guide to Programming with Python Chapter Two Basic data types, Variables, and Simple I/O: The Useless Trivia Program.
Order of Operations with Exponents and Integers
Introduction to Python
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Programming in Python Part I Dr. Fatma Cemile Serçe Atılım University
2440: 211 Interactive Web Programming Expressions & Operators.
Properties of Algebraic Notation
Algebra Basics.
How do you simplify using order of operations and exponents? Section 1-2.
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
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
Variables, Expressions and Statements
Properties of Numbers; Operations with Integers Presented by Mr. Laws 8 th Grade Math, JCMS.
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏ Sec 9-7 Web Design.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
CONFIDENTIAL1 Good Afternoon! Today we will be learning about Review of Expressions, Variables, equations & Functions Let’s warm up : 1) Simplify: 4 x.
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.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
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.
PYTHON VARIABLES : CHAPTER 2 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Data Manipulation Variables, Data Types & Math. Aug Variables A variable is a name (identifier) that points to a value. They are useful to store.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Variables and Expressions Order of Operations Real Numbers and the Number Line Objective: To solve problems by using the order of operations.
THE ORDER OF OPERATIONS 1-2. VOCABULARY… 1.Order of Operations – a. Work inside grouping symbols ( ) or [ ] b. Simplify any terms with exponents 3 2 c.
Introduction to Python Lesson 2a Print and Types.
Input, Output and Variables GCSE Computer Science – Python.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Topics Designing a Program Input, Processing, and Output
Introduction to Python
Topic: Python’s building blocks -> Statements
2.5 Another Java Application: Adding Integers
Order of Operations.
Design & Technology Grade 7 Python
Presented By S.Yamuna AP/IT
Variables, Expressions, and IO
Formatting Output.
43 Order of Operations  ( ) + - X.
Number and String Operations
Introduction to C++ Programming
Variables, Data Types & Math
Variables, Data Types & Math
Topics Designing a Program Input, Processing, and Output
43 Order of Operations  ( ) + - X.
Topics Designing a Program Input, Processing, and Output
Variables, Data Types & Math
Symbols and Expressions
Data Types and Expressions
Presentation transcript:

Python November 14, Unit 7

Python Hello world, in class

Strings A string is a bunch of characters In >>>print “Hello, world!”, the string is: Hello, world! We can have numbers inside strings >>>print “Today is my friend’s 35 th birthday!” >>>print “4+4” Strings are one type of value that python can manipulate

Values and Type Values are some of the basic items that your program can manipulate “Hello, world!” is a value of the type string >>>print 1+1, the value of this is 2 and is of the type integer So what is a type? –Values are categorized according to their type

Types It’s easy to see that “Hello, world!” and the value of 4+4 are of different types –“Hello, world!” is a string –4+4 produces 8, which is a number They have different types 1.2 and 4 also have different types –4 is an integer (whole number can be positive or negative) –1.2 is called a float (or floating-point number) Can also be positive or negative If it has a decimal point it’s usually a float Like real numbers “4+4” is of the type string –If it’s in quotes, it is a string

What’s so Important about Types? The type of value dictates what you can do with it >>>print 8-4 –Has a value of 4 >>>print “Hello, world!”-H –We may think this means take away the “H” in “Hello, world!” –But the interpreter has no idea what we mean –We can’t subtract a letter from a string in this way One of the reasons for using types is that different types of information are stored differently –Characters are not stored the same way as integers –Integers and floating point numbers aren’t stored the same way

Mathematical Expressions Should all be familiar with a mathematical expression –4+4 –8 * 2 –24 / * 3 An expression is any kind of calculation that returns a result

Expressions, in General We can have things other than mathematical expressions A value by itself is considered an expression –>>>4 –>>>”Hello, world!” Expressions are any combination of values, variables, and operators –We’ll get more into this later

Mathematical Expressions, cont. Mathematical expressions have two parts: –Operators –Operands Operators are the symbols we use for math –+ addition –- subtraction –/ division –* multiplication –** power (exponent) –() Operands are the values we use these operators on –4 + 8 / 6 * 3 Operands are: 4, 8, 6, 3 Operators are: +, /, * –Operandscan also be variables

Evaluating Mathematical Expressions In python (like in most programming languages), mathematical expressions are evaluated like we learned in grammar school PEMDAS –Parenthesis, Exponents, Multiplication, Division, Addition, and Subtraction –Please Explain My Dear Aunt Sally –Multiplication and Division have the same precedence As do Subtraction and Addition Evaluated left to right Doesn’t really matter –7-2 = 5 –3 + 2 = 5

Mathematical Expressions In class example Integer division: –When you divide one integer by another –Produces another integer –Always rounds down –2/3 = 0 –4/3 = 1 Floating point division: –If we want the decimal information at least one of the two operands must be a float 2.0 /3 2 / / 3.0

In-Class Examples Playing with mathematical expressions

Variables Variables are a way to store data so that we can use it over and over >>>x = >>>print x 12 –x is the variable We can store data of any type in a variable >>>name=“Sarah Brown” >>>print name

Variables, cont. Let’s say we have the expression “8 *2 +4” and we want to store it in a variable called “x” >>>x = 8 *2 + 4 The value of 8 *2 + 4 is calculated, resulting in 20 This is then assigned to the variable x using the “=“ operator This is called an assignment statement

Variables, cont. We can assign the value of one variable to another variable >>> y = x We can assign an expression using a variable to another variable >>>y = x*2 + 7 Variables do not store the expression –They store the value of the expression –If x = 4, y has the value of 15 –y does not have the value “x *2 + 7”

User Input Now that we have a way to store data, we can get input from users and manipulate it To prompt the user for information we are going to use raw_input(“Whatever you want to ask for”) and assign that to a variable Example: >>>name = raw_input(“What is your name”) >>>print name, “is such a pretty name”

raw_input, cont. Another example: >>>age = raw_input(“How old are you? “) >>>print “you are “, age,”years old! That is SO old!” So when printing out the above statement we want to put the value for age in between two strings –One way to do this is to use a “,” –Items in python separated by a comma are considered items in a list –Just prints them in order

raw_input, cont. One of the issues with using raw_input is that all input from the user is considered to be a string Take the following: >>>base = raw_input(“Enter a number”) >>>print “ 2 times base is: “, base The out put would be : “2 times base is: basebase” If the user entered the number 4 “2 times base is 44” The user could have entered a name if they had wanted and it would print it twice –If base = “pizza” –“2 times base is pizzapizza”

Another Example with raw_input Let’s say instead of multiplying base by 2 we want to output base+1 >>>base = raw_input(“Enter a number”) >>>print base+1 This would cause an error –We can’t add the number 1 to a string We need to convert the value in base from a string to a number –We are going to use that concept of types

Type Function We can use the function type to determine the type of a variable >>>base = raw_input (“enter a number”) >>>type(base) Will output >>>type(4) will output >>>type(4.0) will output

Type Conversion We can use different functions to convert from one type to another –int() converts whatever is inside the () to an integer –float() converts the contents of the () to a floating point number –str() converts contents of () to a string We can use these function to convert the input from the user to the type that we need

Type Conversion, example >>>base = int(raw_input(“enter a number”)) >>>print base +1 If the user enters the number 5, 6 will be output

In Class Example Getting input from the user –Storing input in a variable Using multiple variables Converting from one type to another

Concatenation So far when we want to print multiple things we’ve been using a comma to separate the items Better way is to concatenate the items Concatenation is like string addition >>>print “Hello,” + “world!” –Outputs “Hello, world!” We can only concatenate strings >>>x = 4+8 >>>print “4 + 8 = “ + x Causes an error

Using str() Like we can change the type of input from the user from a string to a number, we can go back in the other direction >>>x = >>>print “4 + 8 = “ + str(x) Would output: “4 + 8 = 12”

In Class Examples Putting it all together Converting input to number then back to a string for printing

Questions The main points from today’s lecture: –Types (int, float, string) –Mathematical Expressions PEMDAS –Variables Assignment statements –User Input (raw_input()) Always produces a string –Type conversion