Transcript ppt

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 35th 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 / 2 + 6 * 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 3+4-2
– 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
• 2.0 / 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 = 4 + 8
>>>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 ‘str’>
>>>type(4) will output <type ‘int’>
>>>type(4.0) will output <type ‘float’>
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 = 4 + 8
>>>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