PYTHON VARIABLES : CHAPTER 2 FROM THINK PYTHON

advertisement
PYTHON
VARIABLES : CHAPTER 2
FROM
THINK PYTHON
HOW TO THINK LIKE A COMPUTER SCIENTIST
VALUES AND TYPES
A value is one of the basic data objects that a Python program
works with. Two of the most used values are numbers and
strings of letters.
These values are categorized into types which include
numbers(float, integer, string)
Examples:
23 is an integer
3.14159 is a floating point (float for short)
‘Hello World!’ is a string of characters
>>> print 23, 3.14,’This is a cool class’
23 3.14 This is a cool class
TYPE() FUNCTION
There is a function called type() that will give you (return) the
type of a value.
>>>type(23)
<class ‘int’>
>>>type(3.456)
<class ‘float’>
>>>type(‘Hello’)
<class ‘str’>
>>>type(‘45’)
<class ‘str’)
 Note this one!!!!
VARIABLES
A variable is a name that refers to a value.
A assignment statement is the usual way to create variables and
assign values to them. Here are a few examples
N = 34
PI = 3.1415926535
Name = ‘Richard Simpson’
The type of a variable is the type of the value it is assigned to.
For example
>>>type(PI)
<class ‘float’)
Note: if you change the value to a different type its type will
change also!
VARIABLE NAMES
• Variables names can be arbitrarily long.
• The can contain numbers and letters and underscore.
• Upper and lower case is acceptable.
• A variable name cannot start with a number
• Keywords are not allowed. These include
• And, as, break, if, else, elif, for, from, import, in, not, or,
return, while with, as well as some others.
STATEMENTS
A statement (instruction) is a unit of code that the interpreter
can execute. At this point we have only seen two
statements, the print statement and the assignment
statement. These are executed one at a time in the
interactive mode.
A script, ie as sequence of instructions, will also execute one
at a time but much faster.
Here is a script
print 34
x=4
Print x
There are many other statements that we shall learn
OPERATORS AND OPERANDS
Operators are used to construction expressions
The usual mathematical operators such as +, -, * and / should
be quite familiar to you. Here are some example expressions
2+3
7+4*8
#what is the result here?
3.24 + 5*(4.0-3.2)
8 – 2**3
4.0//3
# ** is the power (exponentiation) operator
#divide and drop the decimal (floor division)
4/3
#also floor division. Result is 1
4.0/3
# result here is 1.3333333333333333
EXPRESSION EVALUATION IN
INTERACTIVE MODE
>>>2+2
4
>>> 4//3
1
>>> 34 % 3
# divide and return remainder (modulus)
1
>>>2*2**3
16
# ** has higher precedence than *
ORDER OF OPERATIONS
1. Parentheses has the highest precedence
2. Exponentiation
3. Multiplication and Division
4. Addition and Subtraction
5. Operators of equal precedence are L to R
PEMDAS
>>> 2+4*3**2-5-10/2
gives what?
STRING OPERATORS
There are two operators that work on strings (+ and *)
The + operators performs concatenation
>>> ‘Soft’+’ware’
Software
The * operators duplicates a string. For example
>>> ‘Soft’*4
SoftSoftSoftSoft
The others don’t work.
COMMENTS
Comments can be anywhere, but must follow #
The interpreter will not read anything past the # symbol
#This is a comment
X=4
# so is this
Start every program that you turn in with the following
comment collection
# Project # : Topic name
# Your name
# Explain what the program does
EXERCISES
Assume that the following statements are executed
Val = 10
Len =15.5
Spacer = ‘_’
What do the following valuate to?
Val/3
Val/2.0
int(Len)*2
Spacer*10
Do exercise 2.4 page 16 (all three) and study glossary 2.11
Download